|
| 1 | +#!/usr/bin/env -S npm run tsn -T |
| 2 | + |
| 3 | +import OpenAI from 'openai'; |
| 4 | +import { betaZodTool } from 'openai/helpers/beta/zod'; |
| 5 | +import { z } from 'zod'; |
| 6 | + |
| 7 | +const client = new OpenAI(); |
| 8 | + |
| 9 | +async function main() { |
| 10 | + const runner = client.chat.completions.toolRunner({ |
| 11 | + messages: [ |
| 12 | + { |
| 13 | + role: 'user', |
| 14 | + content: `I'm planning a trip to San Francisco and I need some information. Can you help me with the weather, current time, and currency exchange rates (from EUR)? Please use parallel tool use`, |
| 15 | + }, |
| 16 | + ], |
| 17 | + tools: [ |
| 18 | + betaZodTool({ |
| 19 | + name: 'getWeather', |
| 20 | + description: 'Get the weather at a specific location', |
| 21 | + inputSchema: z.object({ |
| 22 | + location: z.string().describe('The city and state, e.g. San Francisco, CA'), |
| 23 | + }), |
| 24 | + run: ({ location }) => { |
| 25 | + return `The weather is sunny with a temperature of 20°C in ${location}.`; |
| 26 | + }, |
| 27 | + }), |
| 28 | + betaZodTool({ |
| 29 | + name: 'getTime', |
| 30 | + description: 'Get the current time in a specific timezone', |
| 31 | + inputSchema: z.object({ |
| 32 | + timezone: z.string().describe('The timezone, e.g. America/Los_Angeles'), |
| 33 | + }), |
| 34 | + run: ({ timezone }) => { |
| 35 | + return `The current time in ${timezone} is 3:00 PM.`; |
| 36 | + }, |
| 37 | + }), |
| 38 | + betaZodTool({ |
| 39 | + name: 'getCurrencyExchangeRate', |
| 40 | + description: 'Get the exchange rate between two currencies', |
| 41 | + inputSchema: z.object({ |
| 42 | + from_currency: z.string().describe('The currency to convert from, e.g. USD'), |
| 43 | + to_currency: z.string().describe('The currency to convert to, e.g. EUR'), |
| 44 | + }), |
| 45 | + run: ({ from_currency, to_currency }) => { |
| 46 | + return `The exchange rate from ${from_currency} to ${to_currency} is 0.85.`; |
| 47 | + }, |
| 48 | + }), |
| 49 | + ], |
| 50 | + model: 'gpt-4o', |
| 51 | + max_tokens: 1024, |
| 52 | + // This limits the conversation to at most 10 back and forth between the API. |
| 53 | + max_iterations: 10, |
| 54 | + stream: true, |
| 55 | + }); |
| 56 | + |
| 57 | + console.log(`\n🚀 Running tools...\n`); |
| 58 | + |
| 59 | + let prevMessageStarted = ''; |
| 60 | + let prevToolStarted = ''; |
| 61 | + let prevWasToolCall = false; |
| 62 | + |
| 63 | + for await (const messageStream of runner) { |
| 64 | + for await (const event of messageStream) { |
| 65 | + const hadToolCalls = !!event.choices?.[0]?.delta?.tool_calls; |
| 66 | + |
| 67 | + if (hadToolCalls) { |
| 68 | + if (!prevMessageStarted) { |
| 69 | + console.log(`┌─ Message ${event.id} `.padEnd(process.stdout.columns, '─')); |
| 70 | + prevMessageStarted = event.id; |
| 71 | + } |
| 72 | + |
| 73 | + prevWasToolCall = true; |
| 74 | + const toolCalls = event.choices[0]!.delta.tool_calls!; |
| 75 | + |
| 76 | + for (const toolCall of toolCalls) { |
| 77 | + if (toolCall.function?.name && prevToolStarted !== toolCall.function.name) { |
| 78 | + process.stdout.write(`\n${toolCall.function.name}: `); |
| 79 | + prevToolStarted = toolCall.function.name; |
| 80 | + } else if (toolCall.function?.arguments) { |
| 81 | + process.stdout.write(toolCall.function.arguments); |
| 82 | + } |
| 83 | + } |
| 84 | + } else if (event.choices?.[0]?.delta?.content) { |
| 85 | + if (prevWasToolCall) { |
| 86 | + console.log(); |
| 87 | + console.log(); |
| 88 | + console.log(`└─`.padEnd(process.stdout.columns, '─')); |
| 89 | + console.log(); |
| 90 | + prevWasToolCall = false; |
| 91 | + } |
| 92 | + |
| 93 | + if (prevMessageStarted !== event.id) { |
| 94 | + console.log(`┌─ Message ${event.id} `.padEnd(process.stdout.columns, '─')); |
| 95 | + console.log(); |
| 96 | + prevMessageStarted = event.id; |
| 97 | + } |
| 98 | + |
| 99 | + process.stdout.write(event.choices[0].delta.content); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + console.log(); |
| 105 | + console.log(); |
| 106 | + console.log(`└─`.padEnd(process.stdout.columns, '─')); |
| 107 | +} |
| 108 | + |
| 109 | +main(); |
0 commit comments