|
| 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 { BetaToolUseBlock } from 'openai/helpers/beta'; |
| 6 | +import { z } from 'zod'; |
| 7 | + |
| 8 | +const client = new OpenAI(); |
| 9 | + |
| 10 | +async function main() { |
| 11 | + const runner = client.beta.chat.completions.toolRunner({ |
| 12 | + messages: [ |
| 13 | + { |
| 14 | + role: 'user', |
| 15 | + 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`, |
| 16 | + }, |
| 17 | + ], |
| 18 | + tools: [ |
| 19 | + betaZodTool({ |
| 20 | + name: 'getWeather', |
| 21 | + description: 'Get the weather at a specific location', |
| 22 | + inputSchema: z.object({ |
| 23 | + location: z.string().describe('The city and state, e.g. San Francisco, CA'), |
| 24 | + }), |
| 25 | + run: ({ location }) => { |
| 26 | + return `The weather is sunny with a temperature of 20°C in ${location}.`; |
| 27 | + }, |
| 28 | + }), |
| 29 | + betaZodTool({ |
| 30 | + name: 'getTime', |
| 31 | + description: 'Get the current time in a specific timezone', |
| 32 | + inputSchema: z.object({ |
| 33 | + timezone: z.string().describe('The timezone, e.g. America/Los_Angeles'), |
| 34 | + }), |
| 35 | + run: ({ timezone }) => { |
| 36 | + return `The current time in ${timezone} is 3:00 PM.`; |
| 37 | + }, |
| 38 | + }), |
| 39 | + betaZodTool({ |
| 40 | + name: 'getCurrencyExchangeRate', |
| 41 | + description: 'Get the exchange rate between two currencies', |
| 42 | + inputSchema: z.object({ |
| 43 | + from_currency: z.string().describe('The currency to convert from, e.g. USD'), |
| 44 | + to_currency: z.string().describe('The currency to convert to, e.g. EUR'), |
| 45 | + }), |
| 46 | + run: ({ from_currency, to_currency }) => { |
| 47 | + return `The exchange rate from ${from_currency} to ${to_currency} is 0.85.`; |
| 48 | + }, |
| 49 | + }), |
| 50 | + ], |
| 51 | + model: 'gpt-4o', |
| 52 | + max_tokens: 1024, |
| 53 | + // This limits the conversation to at most 10 back and forth between the API. |
| 54 | + max_iterations: 10, |
| 55 | + }); |
| 56 | + |
| 57 | + console.log(`\n🚀 Running tools...\n`); |
| 58 | + |
| 59 | + for await (const message of runner) { |
| 60 | + console.log(`┌─ Message ${message.id} `.padEnd(process.stdout.columns, '─')); |
| 61 | + console.log(); |
| 62 | + |
| 63 | + const { choices } = message; |
| 64 | + const firstChoice = choices.at(0)!; |
| 65 | + |
| 66 | + console.log(`${firstChoice.message.content}\n`); // text |
| 67 | + // each tool call (could be many) |
| 68 | + for (const toolCall of firstChoice.message.tool_calls ?? []) { |
| 69 | + if (toolCall.type === 'function') { |
| 70 | + console.log(`${toolCall.function.name}(${JSON.stringify(toolCall.function.arguments, null, 2)})\n`); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + console.log(`└─`.padEnd(process.stdout.columns, '─')); |
| 75 | + console.log(); |
| 76 | + console.log(); |
| 77 | + |
| 78 | + // const defaultResponse = await runner.generateToolResponse(); |
| 79 | + // if (defaultResponse && typeof defaultResponse.content !== 'string') { |
| 80 | + // console.log(`┌─ Response `.padEnd(process.stdout.columns, '─')); |
| 81 | + // console.log(); |
| 82 | + |
| 83 | + // for (const block of defaultResponse.content) { |
| 84 | + // if (block.type === 'tool_result') { |
| 85 | + // const toolUseBlock = message.content.find((b): b is BetaToolUseBlock => { |
| 86 | + // return b.type === 'tool_use' && b.id === block.tool_use_id; |
| 87 | + // })!; |
| 88 | + // console.log(`${toolUseBlock.name}(): ${block.content}`); |
| 89 | + // } |
| 90 | + // } |
| 91 | + |
| 92 | + // console.log(); |
| 93 | + // console.log(`└─`.padEnd(process.stdout.columns, '─')); |
| 94 | + // console.log(); |
| 95 | + // console.log(); |
| 96 | + // } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +main(); |
0 commit comments