-
Notifications
You must be signed in to change notification settings - Fork 1.5k
docs: clarify abort-on-function-call example #1843
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 all commits
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 | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -590,14 +590,20 @@ The underlying `AbortController` for the runner. | |||||||||||||||||
| #### Abort on a function call | ||||||||||||||||||
|
|
||||||||||||||||||
| If you have a function call flow which you intend to _end_ with a certain function call, then you can use the second | ||||||||||||||||||
| argument `runner` given to the function to either mutate `runner.messages` or call `runner.abort()`. | ||||||||||||||||||
| argument `runner` given to the function to inspect the terminating call, mutate `runner.messages`, or call | ||||||||||||||||||
| `runner.abort()`. | ||||||||||||||||||
|
|
||||||||||||||||||
| Because `abort()` ends the run immediately, the `final*` helpers will reject afterwards. Capture the terminating call | ||||||||||||||||||
| inside the function itself (or via event handlers), then await `runner.done()` and handle the abort error. | ||||||||||||||||||
|
|
||||||||||||||||||
| ```ts | ||||||||||||||||||
| import OpenAI from 'openai'; | ||||||||||||||||||
|
|
||||||||||||||||||
| const client = new OpenAI(); | ||||||||||||||||||
|
|
||||||||||||||||||
| async function main() { | ||||||||||||||||||
| let terminatingCall: unknown; | ||||||||||||||||||
|
|
||||||||||||||||||
| const runner = client.chat.completions | ||||||||||||||||||
| .runTools({ | ||||||||||||||||||
| model: 'gpt-3.5-turbo', | ||||||||||||||||||
|
|
@@ -607,17 +613,19 @@ async function main() { | |||||||||||||||||
| type: 'function', | ||||||||||||||||||
| function: { | ||||||||||||||||||
| function: function updateDatabase(props, runner) { | ||||||||||||||||||
| runner.abort() | ||||||||||||||||||
| terminatingCall = props; | ||||||||||||||||||
| runner.abort(); | ||||||||||||||||||
| }, | ||||||||||||||||||
| … | ||||||||||||||||||
| } | ||||||||||||||||||
| }, | ||||||||||||||||||
| ], | ||||||||||||||||||
| }) | ||||||||||||||||||
| .on('message', (message) => console.log(message)); | ||||||||||||||||||
| .on('message', (message) => console.log(message)) | ||||||||||||||||||
| .on('abort', (error) => console.log('Run aborted:', error.message)); | ||||||||||||||||||
|
|
||||||||||||||||||
| const finalFunctionCall = await runner.finalFunctionCall(); | ||||||||||||||||||
| console.log('Final function call:', finalFunctionCall); | ||||||||||||||||||
| await runner.done().catch(() => {}); | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This line suppresses all failures from Useful? React with 👍 / 👎.
|
||||||||||||||||||
| await runner.done().catch(() => {}); | |
| try { | |
| await runner.done(); | |
| } catch (error) { | |
| if ((error as { name?: string })?.name !== 'APIUserAbortError') { | |
| throw error; | |
| } | |
| } |
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.
In this example
terminatingCallis assignedprops, which are the tool/function arguments, not the full function/tool call (name/id/raw arguments). The variable name and log message (“Function call that ended the run”) are misleading; consider renaming to reflect that it’s arguments, or capture the actual tool call object via the appropriate event and log that instead.