-
Notifications
You must be signed in to change notification settings - Fork 870
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(assistants): add support for streaming (#714)
See the reference docs for more information: https://platform.openai.com/docs/api-reference/assistants-streaming We've also improved some of the names for the types in the assistants beta, non exhaustive list: - `CodeToolCall` -> `CodeInterpreterToolCall` - `MessageContentImageFile` -> `ImageFileContentBlock` - `MessageContentText` -> `TextContentBlock` - `ThreadMessage` -> `Message` - `ThreadMessageDeleted` -> `MessageDeleted`
- Loading branch information
1 parent
beea0c7
commit 7d27d28
Showing
25 changed files
with
3,155 additions
and
279 deletions.
There are no files selected for viewing
This file contains 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 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,39 @@ | ||
import OpenAI from 'openai'; | ||
|
||
const openai = new OpenAI(); | ||
|
||
async function main() { | ||
const assistant = await openai.beta.assistants.create({ | ||
model: 'gpt-4-1106-preview', | ||
name: 'Math Tutor', | ||
instructions: 'You are a personal math tutor. Write and run code to answer math questions.', | ||
}); | ||
|
||
const thread = await openai.beta.threads.create({ | ||
messages: [ | ||
{ | ||
role: 'user', | ||
content: '"I need to solve the equation `3x + 11 = 14`. Can you help me?"', | ||
}, | ||
], | ||
}); | ||
|
||
const stream = await openai.beta.threads.runs.create(thread.id, { | ||
assistant_id: assistant.id, | ||
additional_instructions: 'Please address the user as Jane Doe. The user has a premium account.', | ||
stream: true, | ||
}); | ||
|
||
for await (const event of stream) { | ||
if (event.event === 'thread.message.delta') { | ||
const chunk = event.data.delta.content?.[0]; | ||
if (chunk && 'text' in chunk) { | ||
process.stdout.write(chunk.text.value); | ||
} | ||
} | ||
} | ||
|
||
console.log(); | ||
} | ||
|
||
main(); |
This file contains 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,48 @@ | ||
#!/usr/bin/env -S npm run tsn -T | ||
|
||
import OpenAI from 'openai'; | ||
|
||
/** | ||
* Example of streaming a response from an assistant | ||
*/ | ||
|
||
const openai = new OpenAI(); | ||
|
||
async function main() { | ||
const assistant = await openai.beta.assistants.create({ | ||
model: 'gpt-4-1106-preview', | ||
name: 'Math Tutor', | ||
instructions: 'You are a personal math tutor. Write and run code to answer math questions.', | ||
}); | ||
|
||
let assistantId = assistant.id; | ||
console.log('Created Assistant with Id: ' + assistantId); | ||
|
||
const thread = await openai.beta.threads.create({ | ||
messages: [ | ||
{ | ||
role: 'user', | ||
content: '"I need to solve the equation `3x + 11 = 14`. Can you help me?"', | ||
}, | ||
], | ||
}); | ||
|
||
let threadId = thread.id; | ||
console.log('Created thread with Id: ' + threadId); | ||
|
||
const run = openai.beta.threads.runs | ||
.createAndStream(threadId, { | ||
assistant_id: assistantId, | ||
}) | ||
//Subscribe to streaming events and log them | ||
.on('event', (event) => console.log(event)) | ||
.on('textDelta', (delta, snapshot) => console.log(snapshot)) | ||
.on('messageDelta', (delta, snapshot) => console.log(snapshot)) | ||
.on('run', (run) => console.log(run)) | ||
.on('messageDelta', (delta, snapshot) => console.log(snapshot)) | ||
.on('connect', () => console.log()); | ||
const result = await run.finalRun(); | ||
console.log('Run Result' + result); | ||
} | ||
|
||
main(); |
This file contains 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,57 @@ | ||
#!/usr/bin/env -S npm run tsn -T | ||
|
||
import OpenAI from 'openai'; | ||
import { sleep } from 'openai/core'; | ||
|
||
/** | ||
* Example of polling for a complete response from an assistant | ||
*/ | ||
|
||
const openai = new OpenAI(); | ||
|
||
async function main() { | ||
const assistant = await openai.beta.assistants.create({ | ||
model: 'gpt-4-1106-preview', | ||
name: 'Math Tutor', | ||
instructions: 'You are a personal math tutor. Write and run code to answer math questions.', | ||
// tools = [], | ||
}); | ||
|
||
let assistantId = assistant.id; | ||
console.log('Created Assistant with Id: ' + assistantId); | ||
|
||
const thread = await openai.beta.threads.create({ | ||
messages: [ | ||
{ | ||
role: 'user', | ||
content: '"I need to solve the equation `3x + 11 = 14`. Can you help me?"', | ||
}, | ||
], | ||
}); | ||
|
||
let threadId = thread.id; | ||
console.log('Created thread with Id: ' + threadId); | ||
|
||
const run = await openai.beta.threads.runs.create(thread.id, { | ||
assistant_id: assistantId, | ||
additional_instructions: 'Please address the user as Jane Doe. The user has a premium account.', | ||
}); | ||
|
||
console.log('Created run with Id: ' + run.id); | ||
|
||
while (true) { | ||
const result = await openai.beta.threads.runs.retrieve(thread.id, run.id); | ||
if (result.status == 'completed') { | ||
const messages = await openai.beta.threads.messages.list(thread.id); | ||
for (const message of messages.getPaginatedItems()) { | ||
console.log(message); | ||
} | ||
break; | ||
} else { | ||
console.log('Waiting for completion. Current status: ' + result.status); | ||
await sleep(5000); | ||
} | ||
} | ||
} | ||
|
||
main(); |
This file contains 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
Oops, something went wrong.