Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -601,13 +601,18 @@ The underlying `AbortController` for the runner.

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()`.
Calling `runner.abort()` cancels the runner, so promise-returning helpers like `runner.done()`,
`runner.finalChatCompletion()`, and `runner.finalFunctionToolCall()` will reject with an `APIUserAbortError`.
Capture any data you need before aborting, or listen for the relevant event.

```ts
import OpenAI from 'openai';

const client = new OpenAI();

async function main() {
let requestedForecast: { location: string } | undefined;

const runner = client.chat.completions
.runTools({
model: 'gpt-3.5-turbo',
Expand All @@ -617,7 +622,8 @@ async function main() {
type: 'function',
function: {
function: function updateDatabase(props, runner) {
runner.abort()
requestedForecast = props;
runner.abort();
},
}
Expand All @@ -626,8 +632,13 @@ async function main() {
})
.on('message', (message) => console.log(message));

const finalFunctionCall = await runner.finalFunctionCall();
console.log('Final function call:', finalFunctionCall);
await runner.done().catch((error) => {
if (error.name !== 'APIUserAbortError') {
throw error;
}
});

console.log('Requested forecast:', requestedForecast);
}

main();
Expand Down