-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add new tool call streaming system #1713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 25 commits
8f8d768
1260ec6
4a71943
55d2d77
4864dca
8e55b1e
95694b6
c4f95d4
f2834e9
34421a0
d224d00
d1fb561
1093bd0
844c5a4
49bbff6
f730423
337d05e
410d410
51b200d
d36648c
4a23c9d
444f9b0
afb98d2
4c95b49
5b52aa9
cdeb4e6
3f96a58
c1dd29b
5a9cb4b
d18bbfb
658afef
9b0b3df
d75a202
3c8bdba
1066d30
b0f8818
6574f39
8eb6c3d
949d8a0
20f39a5
ec31a58
ded0bd1
ac91ad0
c0a0bb0
4cad7b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| #!/usr/bin/env -S npm run tsn -T | ||
|
|
||
| import OpenAI from 'openai'; | ||
| import { betaZodTool } from 'openai/helpers/beta/zod'; | ||
| import { z } from 'zod'; | ||
|
|
||
| const client = new OpenAI(); | ||
|
|
||
| async function main() { | ||
| const runner = client.beta.chat.completions.toolRunner({ | ||
| messages: [ | ||
| { | ||
| role: 'user', | ||
| 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.`, | ||
| }, | ||
| ], | ||
| tools: [ | ||
| betaZodTool({ | ||
| name: 'getWeather', | ||
| description: 'Get the weather at a specific location', | ||
| inputSchema: z.object({ | ||
| location: z.string().describe('The city and state, e.g. San Francisco, CA'), | ||
| }), | ||
| run: ({ location }) => { | ||
| return `The weather is sunny with a temperature of 20°C in ${location}.`; | ||
| }, | ||
| }), | ||
| betaZodTool({ | ||
| name: 'getTime', | ||
| description: 'Get the current time in a specific timezone', | ||
| inputSchema: z.object({ | ||
| timezone: z.string().describe('The timezone, e.g. America/Los_Angeles'), | ||
| }), | ||
| run: ({ timezone }) => { | ||
| return `The current time in ${timezone} is 3:00 PM.`; | ||
| }, | ||
| }), | ||
| betaZodTool({ | ||
| name: 'getCurrencyExchangeRate', | ||
| description: 'Get the exchange rate between two currencies', | ||
| inputSchema: z.object({ | ||
| from_currency: z.string().describe('The currency to convert from, e.g. USD'), | ||
| to_currency: z.string().describe('The currency to convert to, e.g. EUR'), | ||
| }), | ||
| run: ({ from_currency, to_currency }) => { | ||
| return `The exchange rate from ${from_currency} to ${to_currency} is 0.85.`; | ||
| }, | ||
| }), | ||
| ], | ||
| model: 'gpt-4o', | ||
| max_tokens: 1024, | ||
| // This limits the conversation to at most 10 back and forth between the API. | ||
| max_iterations: 10, | ||
| }); | ||
|
|
||
| console.log(`\n🚀 Running tools...\n`); | ||
|
|
||
| for await (const message of runner) { | ||
| if (!message) continue; | ||
|
|
||
| console.log(`┌─ Message ${message.id} `.padEnd(process.stdout.columns, '─')); | ||
| console.log(); | ||
|
|
||
| const { choices } = message; | ||
| const firstChoice = choices.at(0)!; | ||
|
|
||
| // When we get a tool call request it's null | ||
| if (firstChoice.message.content !== null) { | ||
| console.log(`${firstChoice.message.content}\n`); | ||
| } else { | ||
| // each tool call (could be many) | ||
| for (const toolCall of firstChoice.message.tool_calls ?? []) { | ||
| if (toolCall.type === 'function') { | ||
| console.log(`${toolCall.function.name}(${JSON.stringify(toolCall.function.arguments, null, 2)})\n`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| console.log(`└─`.padEnd(process.stdout.columns, '─')); | ||
| console.log(); | ||
| console.log(); | ||
|
|
||
| const defaultResponse = await runner.generateToolResponse(); | ||
| if (defaultResponse && Array.isArray(defaultResponse)) { | ||
| console.log(`┌─ Response `.padEnd(process.stdout.columns, '─')); | ||
| console.log(); | ||
|
|
||
| for (const toolResponse of defaultResponse) { | ||
| if (toolResponse.role === 'tool') { | ||
| const toolCall = firstChoice.message.tool_calls?.find((tc) => tc.id === toolResponse.tool_call_id); | ||
| if (toolCall && toolCall.type === 'function') { | ||
| console.log(`${toolCall.function.name}(): ${toolResponse.content}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| console.log(); | ||
| console.log(`└─`.padEnd(process.stdout.columns, '─')); | ||
| console.log(); | ||
| console.log(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| main(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| #!/usr/bin/env -S npm run tsn -T | ||
|
|
||
| import OpenAI from 'openai'; | ||
| import { betaZodTool } from 'openai/helpers/beta/zod'; | ||
| import { z } from 'zod'; | ||
|
|
||
| const client = new OpenAI(); | ||
|
|
||
| async function main() { | ||
| const runner = client.beta.chat.completions.toolRunner({ | ||
| messages: [ | ||
| { | ||
| role: 'user', | ||
| 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`, | ||
| }, | ||
| ], | ||
| tools: [ | ||
| betaZodTool({ | ||
| name: 'getWeather', | ||
| description: 'Get the weather at a specific location', | ||
| inputSchema: z.object({ | ||
| location: z.string().describe('The city and state, e.g. San Francisco, CA'), | ||
| }), | ||
| run: ({ location }) => { | ||
| return `The weather is sunny with a temperature of 20°C in ${location}.`; | ||
| }, | ||
| }), | ||
| betaZodTool({ | ||
| name: 'getTime', | ||
| description: 'Get the current time in a specific timezone', | ||
| inputSchema: z.object({ | ||
| timezone: z.string().describe('The timezone, e.g. America/Los_Angeles'), | ||
| }), | ||
| run: ({ timezone }) => { | ||
| return `The current time in ${timezone} is 3:00 PM.`; | ||
| }, | ||
| }), | ||
| betaZodTool({ | ||
| name: 'getCurrencyExchangeRate', | ||
| description: 'Get the exchange rate between two currencies', | ||
| inputSchema: z.object({ | ||
| from_currency: z.string().describe('The currency to convert from, e.g. USD'), | ||
| to_currency: z.string().describe('The currency to convert to, e.g. EUR'), | ||
| }), | ||
| run: ({ from_currency, to_currency }) => { | ||
| return `The exchange rate from ${from_currency} to ${to_currency} is 0.85.`; | ||
| }, | ||
| }), | ||
| ], | ||
| model: 'gpt-4o', | ||
| max_tokens: 1024, | ||
| // This limits the conversation to at most 10 back and forth between the API. | ||
| max_iterations: 10, | ||
| stream: true, | ||
| }); | ||
|
|
||
| console.log(`\n🚀 Running tools...\n`); | ||
|
|
||
| let prevMessageStarted = ''; | ||
| let prevToolStarted = ''; | ||
| let prevWasToolCall = false; | ||
|
|
||
| for await (const messageStream of runner) { | ||
| for await (const event of messageStream) { | ||
| const hadToolCalls = !!event.choices?.[0]?.delta?.tool_calls; | ||
|
|
||
| if (hadToolCalls) { | ||
| if (!prevMessageStarted) { | ||
| console.log(`┌─ Message ${event.id} `.padEnd(process.stdout.columns, '─')); | ||
| prevMessageStarted = event.id; | ||
| } | ||
|
|
||
| prevWasToolCall = true; | ||
| const toolCalls = event.choices[0]!.delta.tool_calls!; | ||
|
|
||
| for (const toolCall of toolCalls) { | ||
| if (toolCall.function?.name && prevToolStarted !== toolCall.function.name) { | ||
| process.stdout.write(`\n${toolCall.function.name}: `); | ||
| prevToolStarted = toolCall.function.name; | ||
| } else if (toolCall.function?.arguments) { | ||
| process.stdout.write(toolCall.function.arguments); | ||
| } | ||
| } | ||
| } else if (event.choices?.[0]?.delta?.content) { | ||
| if (prevWasToolCall) { | ||
| console.log(); | ||
| console.log(); | ||
| console.log(`└─`.padEnd(process.stdout.columns, '─')); | ||
| console.log(); | ||
| prevWasToolCall = false; | ||
| } | ||
|
|
||
| if (prevMessageStarted !== event.id) { | ||
| console.log(`┌─ Message ${event.id} `.padEnd(process.stdout.columns, '─')); | ||
| console.log(); | ||
| prevMessageStarted = event.id; | ||
| } | ||
|
|
||
| process.stdout.write(event.choices[0].delta.content); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| console.log(); | ||
| console.log(); | ||
| console.log(`└─`.padEnd(process.stdout.columns, '─')); | ||
| } | ||
|
|
||
| main(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #!/usr/bin/env -S npm run tsn -T | ||
|
|
||
| import OpenAI from 'openai'; | ||
| import { betaZodTool } from 'openai/helpers/beta/zod'; | ||
| import { z } from 'zod'; | ||
|
|
||
| const client = new OpenAI(); | ||
|
|
||
| async function main() { | ||
| const message = await client.beta.chat.completions.toolRunner({ | ||
| messages: [ | ||
| { | ||
| role: 'user', | ||
| content: `What is the weather in SF?`, | ||
| }, | ||
| ], | ||
| tools: [ | ||
| betaZodTool({ | ||
|
||
| name: 'getWeather', | ||
| description: 'Get the weather at a specific location', | ||
| inputSchema: z.object({ | ||
| location: z.string().describe('The city and state, e.g. San Francisco, CA'), | ||
| }), | ||
| run: ({ location }) => { | ||
| return `The weather is foggy with a temperature of 20°C in ${location}.`; | ||
| }, | ||
| }), | ||
| ], | ||
| model: 'gpt-4o', | ||
| max_tokens: 1024, | ||
| // the maximum number of iterations to run the tool | ||
| max_iterations: 10, | ||
| }); | ||
|
|
||
| console.log('Final response:', message.content); | ||
| } | ||
|
|
||
| main(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #!/usr/bin/env -S npm run tsn -T | ||
|
|
||
| import OpenAI from 'openai'; | ||
| import { betaTool } from 'openai/helpers/beta/json-schema'; | ||
|
|
||
| const client = new OpenAI(); | ||
|
|
||
| async function main() { | ||
| const message = await client.beta.chat.completions.toolRunner({ | ||
| messages: [ | ||
| { | ||
| role: 'user', | ||
| content: `What is the weather in SF?`, | ||
| }, | ||
| ], | ||
| tools: [ | ||
| betaTool({ | ||
| name: 'getWeather', | ||
| description: 'Get the weather at a specific location', | ||
| inputSchema: { | ||
| type: 'object', | ||
| properties: { | ||
| location: { | ||
| type: 'string', | ||
| description: 'The city and state, e.g. San Francisco, CA', | ||
| }, | ||
| }, | ||
| required: ['location'], | ||
| }, | ||
| run: ({ location }) => { | ||
| return `The weather is foggy with a temperature of 20°C in ${location}.`; | ||
| }, | ||
| }), | ||
| ], | ||
| model: 'gpt-4o', | ||
| max_tokens: 1024, | ||
| // the maximum number of iterations to run the tool | ||
| max_iterations: 10, | ||
| }); | ||
|
|
||
| console.log('Final response:', message.content); | ||
| } | ||
|
|
||
| main(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import type { FromSchema, JSONSchema } from 'json-schema-to-ts'; | ||
| import type { BetaRunnableTool, Promisable } from '../../lib/beta/BetaRunnableTool'; | ||
| import type { FunctionTool } from '../../resources/beta'; | ||
| import { OpenAIError } from '../../error'; | ||
|
|
||
| type NoInfer<T> = T extends infer R ? R : never; | ||
|
|
||
| /** | ||
| * Creates a Tool with a provided JSON schema that can be passed | ||
| * to the `.toolRunner()` method. The schema is used to automatically validate | ||
| * the input arguments for the tool. | ||
| */ | ||
| export function betaTool<const Schema extends Exclude<JSONSchema, boolean> & { type: 'object' }>(options: { | ||
| name: string; | ||
| inputSchema: Schema; | ||
| description: string; | ||
| run: (args: NoInfer<FromSchema<Schema>>) => Promisable<string | Array<FunctionTool>>; | ||
| }): BetaRunnableTool<NoInfer<FromSchema<Schema>>> { | ||
| if (options.inputSchema.type !== 'object') { | ||
| throw new OpenAIError( | ||
| `JSON schema for tool "${options.name}" must be an object, but got ${options.inputSchema.type}`, | ||
| ); | ||
| } | ||
|
|
||
| return { | ||
| type: 'function', | ||
| function: { | ||
| name: options.name, | ||
| parameters: options.inputSchema, | ||
| description: options.description, | ||
| }, | ||
| run: options.run, | ||
| parse: (content: unknown) => content as FromSchema<Schema>, | ||
| } as any; // For some reason this causes infinite inference so we cast to any to not crash lsp | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think
messageStreamis an accurate description of this