-
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
Open
404Wolf
wants to merge
44
commits into
openai:master
Choose a base branch
from
404Wolf:add-new-tool-call-streaming
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 30 commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
8f8d768
add chat to beta
404Wolf 1260ec6
start adding option for new iterator
404Wolf 4a71943
more progress on iterative tool calls
404Wolf 55d2d77
fix type issues
404Wolf 4864dca
finish port
404Wolf 8e55b1e
add support for options
404Wolf 95694b6
update to new async iterable pattern
404Wolf c4f95d4
fix E2E test
404Wolf f2834e9
fix more tests
404Wolf 34421a0
fix more tests
404Wolf d224d00
most tests passing
404Wolf d1fb561
add more examples
404Wolf 1093bd0
throw when n>1
404Wolf 844c5a4
update another test
404Wolf 49bbff6
make tool runner beta only
404Wolf f730423
update snapshot
404Wolf 337d05e
make executable
404Wolf 410d410
tests passing
404Wolf 51b200d
fix type errors
404Wolf d36648c
unformat mistakenly formatted jsons
404Wolf 4a23c9d
make nock dev dep
404Wolf 444f9b0
more minimal package changes
404Wolf afb98d2
don't await the promise
404Wolf 4c95b49
use zod v4
404Wolf 5b52aa9
catch with separate update
404Wolf cdeb4e6
PR feedback
404Wolf 3f96a58
start working on docs
404Wolf c1dd29b
more readme work
404Wolf 5a9cb4b
remove object check
404Wolf d18bbfb
add image tool
404Wolf 658afef
pass directly
404Wolf 9b0b3df
add test for top level array
404Wolf d75a202
rename BetaRunnableTool
404Wolf 3c8bdba
Solve more TODOs
404Wolf 1066d30
add doc
404Wolf b0f8818
improve names
404Wolf 6574f39
Merge remote-tracking branch 'upstream/master' into add-new-tool-call…
404Wolf 8eb6c3d
remove debugging
404Wolf 949d8a0
fix more naming
404Wolf 20f39a5
rename to parameters
404Wolf ec31a58
allow object
404Wolf ded0bd1
make linter happy
404Wolf ac91ad0
clean up
404Wolf c0a0bb0
change anys to unknowns
404Wolf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { betaZodFunctionTool } 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: [ | ||
| betaZodFunctionTool({ | ||
| name: 'getWeather', | ||
| description: 'Get the weather at a specific location', | ||
| parameters: 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}.`; | ||
| }, | ||
| }), | ||
| betaZodFunctionTool({ | ||
| name: 'getTime', | ||
| description: 'Get the current time in a specific timezone', | ||
| parameters: 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.`; | ||
| }, | ||
| }), | ||
| betaZodFunctionTool({ | ||
| name: 'getCurrencyExchangeRate', | ||
| description: 'Get the exchange rate between two currencies', | ||
| parameters: 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { betaZodFunctionTool } 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: [ | ||
| betaZodFunctionTool({ | ||
| name: 'getWeather', | ||
| description: 'Get the weather at a specific location', | ||
| parameters: 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}.`; | ||
| }, | ||
| }), | ||
| betaZodFunctionTool({ | ||
| name: 'getTime', | ||
| description: 'Get the current time in a specific timezone', | ||
| parameters: 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.`; | ||
| }, | ||
| }), | ||
| betaZodFunctionTool({ | ||
| name: 'getCurrencyExchangeRate', | ||
| description: 'Get the exchange rate between two currencies', | ||
| parameters: 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { betaZodFunctionTool } 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: [ | ||
| betaZodFunctionTool({ | ||
| name: 'getWeather', | ||
| description: 'Get the weather at a specific location', | ||
| parameters: 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
FYI looks like the primary examples in those linked docs are for the Responses API, not Chat Completions.
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.
It's where they explain what a function tool call actually is, but yes it focuses on the responses API. Should we re-explain it here instead and not link to it, since it could cause confusion?