Skip to content

Commit f83a733

Browse files
committed
Support streaming background responses
1 parent 1bd29ac commit f83a733

File tree

6 files changed

+7316
-627
lines changed

6 files changed

+7316
-627
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env -S npm run tsn -T
2+
3+
import OpenAI from 'openai';
4+
5+
const openai = new OpenAI();
6+
7+
async function main() {
8+
const runner = openai.responses.stream({
9+
model: 'gpt-4o-2024-08-06',
10+
input: 'solve 8x + 31 = 2',
11+
background: true,
12+
});
13+
14+
var id: string | null = null;
15+
16+
for await (const event of runner) {
17+
if (event.type == 'response.created') {
18+
id = event.response.id;
19+
}
20+
21+
console.log('event', event);
22+
if (event.sequence_number == 10) {
23+
break;
24+
}
25+
}
26+
27+
console.log('Interrupted. Continuing...');
28+
29+
const runner2 = openai.responses.stream({
30+
response_id: id!,
31+
starting_after: 10,
32+
});
33+
34+
for await (const event of runner2) {
35+
console.log('event', event);
36+
}
37+
38+
const result = await runner2.finalResponse();
39+
console.log(result);
40+
}
41+
42+
main();

0 commit comments

Comments
 (0)