-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
65 lines (57 loc) · 2.23 KB
/
index.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
59
60
61
62
63
64
65
import { generateText, jsonSchema, tool } from "ai";
import { openai } from "@ai-sdk/openai";
const apiKey = process.env.BYTECHEF_API_KEY;
const response = await fetch(
"http://localhost:9555/api/embedded/v1/tools?externalUserId=12345",
{
method: "GET",
headers: {
Authorization: `Bearer ${apiKey}`,
},
}
);
const integrationTools = await response.json();
await generateText({
model: openai("gpt-4o"),
tools: Object.fromEntries(
integrationTools.map((integrationTool) => [
integrationTool.function.name,
tool({
description: integrationTool.function.description,
parameters: jsonSchema(integrationTool.function.parameters),
execute: async (params: any, { toolCallId }) => {
try {
const response = await fetch(
`http://localhost:9555/api/embedded/v1/tools?externalUserId=12345`,
{
method: "POST",
body: JSON.stringify({
action: integrationTool.function.name,
parameters: params,
}),
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
}
);
const output = await response.json();
if (!response.ok) {
throw new Error(JSON.stringify(output, null, 2));
}
return output;
} catch (err) {
if (err instanceof Error) {
return { error: { message: err.message } };
}
return err;
}
},
}),
])
),
toolChoice: "auto",
temperature: 0,
system: "You are a helpful assistant. Be as concise as possible.",
prompt: "Help me create a new task in Jira.",
});