Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

tool-calling-example.js

214 lines6.1 KB

1#!/usr/bin/env node
2​
3/**
4 * TanStack AI - Tool Calling Example
5 *
6 * This demonstrates how to use function calling with streaming
7 * Works with both OpenAI and Anthropic using the same code!
8 *
9 * Usage:
10 * # With OpenAI only:
11 * OPENAI_API_KEY=sk-... node examples/tool-calling-example.js
12 *
13 * # With Anthropic only:
14 * ANTHROPIC_API_KEY=sk-ant-... node examples/tool-calling-example.js
15 *
16 * # With both (recommended):
17 * OPENAI_API_KEY=sk-... ANTHROPIC_API_KEY=sk-ant-... node examples/tool-calling-example.js
18 */
19​
20import { AI } from "../packages/ai/dist/index.js";
21import { OpenAIAdapter } from "../packages/ai-openai/dist/index.js";
22import { AnthropicAdapter } from "../packages/ai-anthropic/dist/index.js";
23​
24// Define a simple calculator tool
25const tools = [
26 {
27 type: "function",
28 function: {
29 name: "calculate",
30 description: "Perform mathematical calculations",
31 parameters: {
32 type: "object",
33 properties: {
34 expression: {
35 type: "string",
36 description: "Mathematical expression like '2 + 2' or '10 * 5'",
37 },
38 },
39 required: ["expression"],
40 },
41 },
42 },
43];
44​
45// Simple calculator implementation
46async function calculate(expression) {
47 try {
48 // eslint-disable-next-line no-eval
49 const result = eval(expression.replace(/[^0-9+\-*/().\s]/g, ""));
50 return JSON.stringify({ result, expression });
51 } catch (error) {
52 return JSON.stringify({ error: error.message });
53 }
55​
56async function demoProvider(providerName, adapter, model) {
57 console.log(`\n${"=".repeat(60)}`);
58 console.log(`🛠️ ${providerName} - Tool Calling Demo`);
59 console.log("=".repeat(60));
60​
61 const ai = new AI(adapter);
62​
63 const messages = [
64 { role: "user", content: "What is 847 multiplied by 392?" },
65 ];
66​
67 console.log("\n📝 User: What is 847 multiplied by 392?\n");
68​
69 try {
70 let iteration = 0;
71 const maxIterations = 2;
72​
73 while (iteration < maxIterations) {
74 iteration++;
75​
76 const toolCalls = [];
77 const toolCallsMap = new Map();
78 let content = "";
79​
80 console.log("🔄 Streaming response...\n");
81​
82 for await (const chunk of ai.streamChat({
83 model,
84 messages,
85 tools,
86 toolChoice: "auto",
87 })) {
88 if (chunk.type === "content") {
89 content = chunk.content;
90 process.stdout.write(chunk.delta);
91 } else if (chunk.type === "tool_call") {
92 const existing = toolCallsMap.get(chunk.index) || {
93 id: chunk.toolCall.id,
94 name: "",
95 args: "",
96 };
97​
98 if (chunk.toolCall.function.name) {
99 existing.name = chunk.toolCall.function.name;
100 }
101 existing.args += chunk.toolCall.function.arguments;
102 toolCallsMap.set(chunk.index, existing);
103 } else if (chunk.type === "done") {
104 if (chunk.finishReason === "tool_calls") {
105 console.log("\n");
106 toolCallsMap.forEach((call) => {
107 toolCalls.push({
108 id: call.id,
109 type: "function",
110 function: {
111 name: call.name,
112 arguments: call.args,
113 },
114 });
115 });
116 }
117​
118 if (chunk.usage) {
119 console.log(`\n[Tokens used: ${chunk.usage.totalTokens}]\n`);
120 }
121 }
122 }
123​
124 if (toolCalls.length > 0) {
125 // Add assistant message with tool calls
126 messages.push({
127 role: "assistant",
128 content: content || null,
129 toolCalls,
130 });
131​
132 console.log(`🔧 AI wants to call ${toolCalls.length} tool(s):\n`);
133​
134 // Execute tools
135 for (const call of toolCalls) {
136 console.log(` → ${call.function.name}(${call.function.arguments})`);
137​
138 const result = await calculate(
139 JSON.parse(call.function.arguments).expression
140 );
141 console.log(` ✓ Result: ${result}\n`);
142​
143 // Add tool result to messages
144 messages.push({
145 role: "tool",
146 content: result,
147 toolCallId: call.id,
148 name: call.function.name,
149 });
150 }
151​
152 // Continue to get final response
153 console.log("🔄 Getting final response...\n");
154 } else {
155 console.log("\n✅ Conversation complete!\n");
156 break;
157 }
158 }
159 } catch (error) {
160 console.error(`\n❌ ${providerName} Error:`, error.message);
161 if (error.message.includes("401") || error.message.includes("API key")) {
162 console.log(
163 ` Set ${
164 providerName === "OpenAI" ? "OPENAI_API_KEY" : "ANTHROPIC_API_KEY"
165 } to try this provider`
166 );
167 }
168 }
170​
171async function demo() {
172 console.log("\n🌟 TanStack AI - Universal Tool Calling Demo 🌟");
173 console.log("\nThis shows the SAME code working with multiple providers!\n");
174​
175 // Try OpenAI
176 if (process.env.OPENAI_API_KEY) {
177 await demoProvider(
178 "OpenAI",
179 new OpenAIAdapter({ apiKey: process.env.OPENAI_API_KEY }),
180 "gpt-3.5-turbo-0125"
181 );
182 } else {
183 console.log("\n⏭️ Skipping OpenAI (set OPENAI_API_KEY to try)");
184 }
185​
186 // Try Anthropic
187 if (process.env.ANTHROPIC_API_KEY) {
188 await demoProvider(
189 "Anthropic Claude",
190 new AnthropicAdapter({ apiKey: process.env.ANTHROPIC_API_KEY }),
191 "claude-3-5-sonnet-20241022"
192 );
193 } else {
194 console.log("\n⏭️ Skipping Anthropic (set ANTHROPIC_API_KEY to try)");
195 }
196​
197 console.log("\n" + "=".repeat(60));
198 console.log("\n✨ Key Takeaway:");
199 console.log(" The EXACT SAME code works with both providers!");
200 console.log(" No provider-specific logic needed.\n");
201​
202 console.log("📚 What happened in each demo:");
203 console.log(" 1. User asked a math question");
204 console.log(" 2. AI decided to use the 'calculate' tool");
205 console.log(" 3. Tool was executed with the expression");
206 console.log(" 4. Result was sent back to AI");
207 console.log(" 5. AI provided the final answer to user\n");
208​
209 console.log("🎯 Try the interactive CLI:");
210 console.log(" pnpm cli tools --provider openai");
211 console.log(" pnpm cli tools --provider anthropic\n");
213​
214demo();