-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathai-sdk-integration.test.ts
More file actions
167 lines (157 loc) · 4.39 KB
/
ai-sdk-integration.test.ts
File metadata and controls
167 lines (157 loc) · 4.39 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { describeEval, ToolCallScorer, StructuredOutputScorer } from "./index";
import { generateText, generateObject, tool, stepCountIs } from "ai";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";
const weatherTool = tool({
description: "Get current weather for a location",
inputSchema: z.object({
location: z.string().describe("The city/location to get weather for"),
}),
execute: async ({ location }) => {
// Only works for Seattle - forces correct tool usage
if (location.toLowerCase() !== "seattle") {
throw new Error(
`Weather data only available for Seattle, not ${location}`,
);
}
// Return hardcoded temperature for Seattle
return {
temperature: 72,
};
},
});
const databaseTool = tool({
description: "Look up specific user data from internal database",
inputSchema: z.object({
userId: z.string().describe("The user ID to look up"),
}),
execute: async ({ userId }) => {
// Only works for user123 - forces correct tool usage
if (userId !== "user123") {
throw new Error(`User ${userId} not found in database`);
}
// Return fixed data for user123
return {
email: "[email protected]",
};
},
});
describeEval("@ai/sdk ToolCallScorer", {
data: async () => [
{
input: "What's the weather like in Seattle?",
expectedTools: [
{ name: "getWeather", arguments: { location: "Seattle" } },
],
},
{
input: "What's the email address for user123?",
expectedTools: [
{
name: "lookupUser",
arguments: { userId: "user123" },
},
],
},
],
task: async (input) => {
const { text, steps } = await generateText({
model: openai("gpt-4o"),
system: `You are a helpful assistant. You MUST use the provided tools when users ask for information. Do NOT guess, estimate, or make up any data.`,
prompt: input,
tools: {
getWeather: weatherTool,
lookupUser: databaseTool,
},
stopWhen: stepCountIs(5),
});
return {
result: text,
toolCalls: steps
.flatMap((step) => step.toolCalls)
.map((call) => ({
name: call.toolName,
arguments: call.input as Record<string, any>,
})),
};
},
scorers: [
ToolCallScorer({
params: "fuzzy", // More flexible matching
allowExtras: true, // AI might call tools creatively
}),
],
skipIf: () => !process.env.OPENAI_API_KEY,
});
describeEval("@ai/sdk StructuredOutputScorer", {
data: async () => [
{
input: "Give me the color red, number 42, and set valid to true",
expected: {
color: "red",
number: 42,
valid: true,
},
},
],
task: async (input) => {
const { object } = await generateObject({
model: openai("gpt-4o"),
prompt: input,
schema: z.object({
color: z.enum(["red", "blue", "green"]).describe("A color"),
number: z.number().describe("A number"),
valid: z.boolean().describe("A boolean value"),
}),
});
return {
result: JSON.stringify(object),
toolCalls: [],
};
},
scorers: [
StructuredOutputScorer({
match: "strict", // Exact matching for simple values
}),
],
skipIf: () => !process.env.OPENAI_API_KEY,
});
// Test without stopWhen to verify single-step default behavior
describeEval("@ai/sdk ToolCallScorer (No stopWhen)", {
data: async () => [
{
input: "What's the weather like in Seattle?",
expectedTools: [
{ name: "getWeather", arguments: { location: "Seattle" } },
],
},
],
task: async (input) => {
const { text, steps } = await generateText({
model: openai("gpt-4o"),
system: `You are a helpful assistant. You MUST use the provided tools when users ask for information. Do NOT guess, estimate, or make up any data.`,
prompt: input,
tools: {
getWeather: weatherTool,
lookupUser: databaseTool,
},
// NO stopWhen here — defaults to stepCountIs(1)
});
return {
result: text,
toolCalls: steps
.flatMap((step) => step.toolCalls)
.map((call) => ({
name: call.toolName,
arguments: call.input as Record<string, any>,
})),
};
},
scorers: [
ToolCallScorer({
params: "fuzzy",
allowExtras: true,
}),
],
skipIf: () => !process.env.OPENAI_API_KEY,
});