-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmultiple-tool-variations.ts
More file actions
95 lines (85 loc) · 2.24 KB
/
multiple-tool-variations.ts
File metadata and controls
95 lines (85 loc) · 2.24 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
/**
* Multiple Tool Variations Example
*
* This example demonstrates how to generate datasets with different tools
* using oneOf to randomly select which tool to use for each example.
*/
import {
generateDataset,
generatedUser,
generatedAssistant,
generatedToolCall,
generatedToolCallResult,
tool,
oneOf,
} from "@qforge/torque";
import { createOpenAI } from "@ai-sdk/openai";
import { z } from "zod";
const apiKey = process.env.OPENAI_API_KEY;
if (!apiKey) {
console.error("❌ ERROR: OPENAI_API_KEY not found!");
console.log("\n📝 To add your API key:");
console.log(
"1. Add: OPENAI_API_KEY=your-key-here or change the apiKey variable above."
);
console.log("2. Run this script again\n");
process.exit(1);
}
const openai = createOpenAI({
apiKey,
});
// Define multiple tools
const weatherTool = tool({
name: "get_weather",
description: "Get current weather for a location",
parameters: z.object({
location: z.string().describe("City name"),
units: z.enum(["celsius", "fahrenheit"]).optional(),
}),
output: z.object({
temperature: z.number(),
condition: z.string(),
}),
});
const calculatorTool = tool({
name: "calculator",
description: "Perform basic arithmetic operations",
parameters: z.object({
operation: z.enum(["add", "subtract", "multiply", "divide"]),
a: z.number(),
b: z.number(),
}),
output: z.object({
result: z.number(),
}),
});
const searchTool = tool({
name: "web_search",
description: "Search the web for information",
parameters: z.object({
query: z.string().describe("Search query"),
}),
output: z.object({
results: z.array(z.string()),
}),
});
// Generate dataset with tool variations
const tools = [weatherTool, calculatorTool, searchTool];
await generateDataset(
() => {
const selectedTool = oneOf(tools);
return [
selectedTool.toolFunction(),
generatedUser({ prompt: "Ask question requiring this tool" }),
generatedToolCall(selectedTool, "t1"),
generatedToolCallResult(selectedTool, "t1"),
generatedAssistant({ prompt: "Present the result" }),
];
},
{
count: 5, // 5 examples per tool on average
model: openai("gpt-5-mini"),
output: "data/multi-tool.jsonl",
seed: 12345,
}
);