Skip to content

Commit

Permalink
feat(api): run polling helpers (#749)
Browse files Browse the repository at this point in the history
refactor: rename createAndStream to stream
  • Loading branch information
stainless-bot committed Apr 1, 2024
1 parent 5376837 commit 02920ae
Show file tree
Hide file tree
Showing 12 changed files with 389 additions and 20 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,30 @@ Documentation for each method, request param, and response field are available i
> [!IMPORTANT]
> Previous versions of this SDK used a `Configuration` class. See the [v3 to v4 migration guide](https://github.com/openai/openai-node/discussions/217).
### Polling Helpers

When interacting with the API some actions such as starting a Run may take time to complete. The SDK includes
helper functions which will poll the status until it reaches a terminal state and then return the resulting object.
If an API method results in an action which could benefit from polling there will be a corresponding version of the
method ending in 'AndPoll'.

For instance to create a Run and poll until it reaches a terminal state you can run:

```ts
const run = await openai.beta.threads.runs.createAndPoll(thread.id, {
assistant_id: assistantId,
});
```

More information on the lifecycle of a Run can be found in the [Run Lifecycle Documentation](https://platform.openai.com/docs/assistants/how-it-works/run-lifecycle)

### Streaming Helpers

The SDK also includes helpers to process streams and handle the incoming events.

```ts
const run = openai.beta.threads.runs
.createAndStream(thread.id, {
.stream(thread.id, {
assistant_id: assistant.id,
})
.on('textCreated', (text) => process.stdout.write('\nassistant > '))
Expand Down
5 changes: 5 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ Methods:
- <code title="post /threads/{thread_id}">client.beta.threads.<a href="./src/resources/beta/threads/threads.ts">update</a>(threadId, { ...params }) -> Thread</code>
- <code title="delete /threads/{thread_id}">client.beta.threads.<a href="./src/resources/beta/threads/threads.ts">del</a>(threadId) -> ThreadDeleted</code>
- <code title="post /threads/runs">client.beta.threads.<a href="./src/resources/beta/threads/threads.ts">createAndRun</a>({ ...params }) -> Run</code>
- <code>client.beta.threads.<a href="./src/resources/beta/threads/threads.ts">createAndRunPoll</a>(body, options?) -> Promise&lt;Threads.Run&gt;</code>
- <code>client.beta.threads.<a href="./src/resources/beta/threads/threads.ts">createAndRunStream</a>(body, options?) -> AssistantStream</code>

### Runs
Expand All @@ -242,7 +243,11 @@ Methods:
- <code title="get /threads/{thread_id}/runs">client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">list</a>(threadId, { ...params }) -> RunsPage</code>
- <code title="post /threads/{thread_id}/runs/{run_id}/cancel">client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">cancel</a>(threadId, runId) -> Run</code>
- <code title="post /threads/{thread_id}/runs/{run_id}/submit_tool_outputs">client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">submitToolOutputs</a>(threadId, runId, { ...params }) -> Run</code>
- <code>client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">createAndPoll</a>(threadId, body, options?) -> Promise&lt;Run&gt;</code>
- <code>client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">createAndStream</a>(threadId, body, options?) -> AssistantStream</code>
- <code>client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">poll</a>(threadId, runId, options?) -> Promise&lt;Run&gt;</code>
- <code>client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">stream</a>(threadId, body, options?) -> AssistantStream</code>
- <code>client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">submitToolOutputsAndPoll</a>(threadId, runId, body, options?) -> Promise&lt;Run&gt;</code>
- <code>client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">submitToolOutputsStream</a>(threadId, runId, body, options?) -> AssistantStream</code>

#### Steps
Expand Down
Empty file modified examples/assistant-stream-raw.ts
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion examples/assistant-stream.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function main() {
console.log('Created thread with Id: ' + threadId);

const run = openai.beta.threads.runs
.createAndStream(threadId, {
.stream(threadId, {
assistant_id: assistantId,
})
//Subscribe to streaming events and log them
Expand Down
22 changes: 7 additions & 15 deletions examples/assistants.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/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
Expand Down Expand Up @@ -32,24 +31,17 @@ async function main() {
let threadId = thread.id;
console.log('Created thread with Id: ' + threadId);

const run = await openai.beta.threads.runs.create(thread.id, {
const run = await openai.beta.threads.runs.createAndPoll(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);
console.log('Run finished with status: ' + run.status);

if (run.status == 'completed') {
const messages = await openai.beta.threads.messages.list(thread.id);
for (const message of messages.getPaginatedItems()) {
console.log(message);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ More information can be found in the documentation: [Assistant Streaming](https:

```ts
const run = openai.beta.threads.runs
.createAndStream(thread.id, {
.stream(thread.id, {
assistant_id: assistant.id,
})
.on('textCreated', (text) => process.stdout.write('\nassistant > '))
Expand Down Expand Up @@ -41,7 +41,7 @@ const run = openai.beta.threads.runs
There are three helper methods for creating streams:

```ts
openai.beta.threads.runs.createAndStream();
openai.beta.threads.runs.stream();
```

This method can be used to start and stream the response to an existing run with an associated thread
Expand Down
1 change: 1 addition & 0 deletions src/resources/beta/beta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ export namespace Beta {
export import ThreadCreateAndRunParams = ThreadsAPI.ThreadCreateAndRunParams;
export import ThreadCreateAndRunParamsNonStreaming = ThreadsAPI.ThreadCreateAndRunParamsNonStreaming;
export import ThreadCreateAndRunParamsStreaming = ThreadsAPI.ThreadCreateAndRunParamsStreaming;
export import ThreadCreateAndRunPollParams = ThreadsAPI.ThreadCreateAndRunPollParams;
export import ThreadCreateAndRunStreamParams = ThreadsAPI.ThreadCreateAndRunStreamParams;
}
1 change: 1 addition & 0 deletions src/resources/beta/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export {
ThreadCreateAndRunParams,
ThreadCreateAndRunParamsNonStreaming,
ThreadCreateAndRunParamsStreaming,
ThreadCreateAndRunPollParams,
ThreadCreateAndRunStreamParams,
Threads,
} from './threads/index';
4 changes: 4 additions & 0 deletions src/resources/beta/threads/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ export {
RunCreateParamsStreaming,
RunUpdateParams,
RunListParams,
RunCreateAndPollParams,
RunCreateAndStreamParams,
RunStreamParams,
RunSubmitToolOutputsParams,
RunSubmitToolOutputsParamsNonStreaming,
RunSubmitToolOutputsParamsStreaming,
RunSubmitToolOutputsAndPollParams,
RunSubmitToolOutputsStreamParams,
RunsPage,
Runs,
Expand All @@ -52,6 +55,7 @@ export {
ThreadCreateAndRunParams,
ThreadCreateAndRunParamsNonStreaming,
ThreadCreateAndRunParamsStreaming,
ThreadCreateAndRunPollParams,
ThreadCreateAndRunStreamParams,
Threads,
} from './threads';
3 changes: 3 additions & 0 deletions src/resources/beta/threads/runs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ export {
RunCreateParamsStreaming,
RunUpdateParams,
RunListParams,
RunCreateAndPollParams,
RunCreateAndStreamParams,
RunStreamParams,
RunSubmitToolOutputsParams,
RunSubmitToolOutputsParamsNonStreaming,
RunSubmitToolOutputsParamsStreaming,
RunSubmitToolOutputsAndPollParams,
RunSubmitToolOutputsStreamParams,
RunsPage,
Runs,
Expand Down

0 comments on commit 02920ae

Please sign in to comment.