-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.ts
58 lines (49 loc) · 1.41 KB
/
ai.ts
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
import { openai } from "@ai-sdk/openai";
import { logger, metadata, schemaTask, toolTask } from "@trigger.dev/sdk/v3";
import { streamText, type TextStreamPart } from "ai";
import { z } from "zod";
export type STREAMS = {
openai: TextStreamPart<{ getWeather: typeof weatherTask.tool }>;
};
export const weatherTask = toolTask({
id: "weather",
description: "Get the weather for a location",
parameters: z.object({
location: z.string(),
}),
run: async ({ location }) => {
// return mock data
return {
location,
temperature: 72 + Math.floor(Math.random() * 21) - 10,
};
},
});
export const aiWeather = schemaTask({
id: "ai-weather",
description: "Send the fullStream from the ai SDK to the metadata system",
schema: z.object({
model: z.string().default("gpt-4o-mini"),
prompt: z.string().default("Hello, how are you?"),
}),
run: async ({ model, prompt }) => {
logger.info("Running OpenAI model", { model, prompt });
const result = streamText({
model: openai(model),
prompt,
tools: {
getWeather: weatherTask.tool,
},
maxSteps: 10,
});
const stream = await metadata.stream("openai", result.fullStream);
let text = "";
for await (const chunk of stream) {
logger.log("Received chunk", { chunk });
if (chunk.type === "text-delta") {
text += chunk.textDelta;
}
}
return { text };
},
});