Repository navigation
Expand file tree
/
Copy pathevent-runtime.ts
More file actions
66 lines (62 loc) · 1.52 KB
/
Copy pathevent-runtime.ts
File metadata and controls
66 lines (62 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import {
AxAIOpenAIModel,
AxPushEventSource,
ai,
ax,
eventRoute,
eventRuntime,
eventTarget,
} from '@ax-llm/ax';
const apiKey = process.env.OPENAI_API_KEY ?? process.env.OPENAI_APIKEY;
if (!apiKey) {
throw new Error('Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.');
}
const llm = ai({
name: 'openai',
apiKey,
config: { model: AxAIOpenAIModel.GPT54Mini, temperature: 0 },
});
const summarize = ax('change:string -> summary:string');
const source = new AxPushEventSource('demo');
const runtime = eventRuntime({
sources: [source],
routes: [
eventRoute({
id: 'summarize-change',
match: { types: ['document.changed'] },
action: 'wake',
requireAuthenticated: true,
target: eventTarget({
id: 'change-summary',
ai: llm,
program: summarize,
mapInput: ({ event }) => ({
change: JSON.stringify(event.data),
}),
retrySafety: 'idempotent',
sinks: [
{
id: 'console',
write: (result, { run }) =>
console.log(run.id, JSON.stringify(result, null, 2)),
},
],
}),
}),
],
});
await runtime.start();
await source.publish({
event: {
specversion: '1.0',
id: 'document-change-1',
source: 'app://documents',
type: 'document.changed',
subject: 'document-42',
data: { title: 'AxEventRuntime guide', status: 'ready' },
},
identity: { tenantId: 'demo' },
trust: 'authenticated',
});
await runtime.waitForIdle();
await runtime.close();