Skip to content

Commit 6a2c41b

Browse files
docs: assistant improvements (#725)
1 parent 139e205 commit 6a2c41b

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

Diff for: README.md

+31
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,37 @@ Documentation for each method, request param, and response field are available i
100100
> [!IMPORTANT]
101101
> Previous versions of this SDK used a `Configuration` class. See the [v3 to v4 migration guide](https://github.com/openai/openai-node/discussions/217).
102102
103+
### Streaming Helpers
104+
105+
The SDK also includes helpers to process streams and handle the incoming events.
106+
107+
```ts
108+
const run = openai.beta.threads.runs
109+
.createAndStream(thread.id, {
110+
assistant_id: assistant.id,
111+
})
112+
.on('textCreated', (text) => process.stdout.write('\nassistant > '))
113+
.on('textDelta', (textDelta, snapshot) => process.stdout.write(textDelta.value))
114+
.on('toolCallCreated', (toolCall) => process.stdout.write(`\nassistant > ${toolCall.type}\n\n`))
115+
.on('toolCallDelta', (toolCallDelta, snapshot) => {
116+
if (toolCallDelta.type === 'code_interpreter') {
117+
if (toolCallDelta.code_interpreter.input) {
118+
process.stdout.write(toolCallDelta.code_interpreter.input);
119+
}
120+
if (toolCallDelta.code_interpreter.outputs) {
121+
process.stdout.write('\noutput >\n');
122+
toolCallDelta.code_interpreter.outputs.forEach((output) => {
123+
if (output.type === 'logs') {
124+
process.stdout.write(`\n${output.logs}\n`);
125+
}
126+
});
127+
}
128+
}
129+
});
130+
```
131+
132+
More information on streaming helpers can be found in the dedicated documentation: [helpers.md](helpers.md)
133+
103134
### Streaming responses
104135

105136
This library provides several conveniences for streaming chat completions, for example:

Diff for: helpers.md

+30-7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@ const run = openai.beta.threads.runs
3636
});
3737
```
3838

39+
### Starting a stream
40+
41+
There are three helper methods for creating streams:
42+
43+
```ts
44+
openai.beta.threads.runs.createAndStream();
45+
```
46+
47+
This method can be used to start and stream the response to an existing run with an associated thread
48+
that is already populated with messages.
49+
50+
```ts
51+
openai.beta.threads.createAndRunStream();
52+
```
53+
54+
This method can be used to add a message to a thread, start a run and then stream the response.
55+
56+
```ts
57+
openai.beta.threads.runs.submitToolOutputsStream();
58+
```
59+
60+
This method can be used to submit a tool output to a run waiting on the output and start a stream.
61+
3962
### Assistant Events
4063

4164
The assistant API provides events you can subscribe to for the following events.
@@ -108,25 +131,25 @@ The last event send when a stream ends.
108131
The assistant streaming object also provides a few methods for convenience:
109132

110133
```ts
111-
.currentEvent()
134+
.currentEvent(): AssistantStreamEvent | undefined
112135

113-
.currentRun()
136+
.currentRun(): Run | undefined
114137

115-
.currentMessageSnapshot()
138+
.currentMessageSnapshot(): Message
116139

117-
.currentRunStepSnapshot()
140+
.currentRunStepSnapshot(): Runs.RunStep
118141
```
119142

120143
These methods are provided to allow you to access additional context from within event handlers. In many cases
121144
the handlers should include all the information you need for processing, but if additional context is required it
122145
can be accessed.
123146

124-
Note: There is not always a relevant context in certain situations (these will be undefined in those cases).
147+
Note: There is not always a relevant context in certain situations (these will be `undefined` in those cases).
125148

126149
```ts
127-
await.finalMessages();
150+
await .finalMessages() : Promise<Message[]>
128151

129-
await.finalRunSteps();
152+
await .finalRunSteps(): Promise<RunStep[]>
130153
```
131154

132155
These methods are provided for convenience to collect information at the end of a stream. Calling these events

0 commit comments

Comments
 (0)