-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathparsing-stream.ts
57 lines (48 loc) · 1.36 KB
/
parsing-stream.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { zodResponseFormat } from 'openai/helpers/zod';
import OpenAI from 'openai/index';
import { z } from 'zod';
const Step = z.object({
explanation: z.string(),
output: z.string(),
});
const MathResponse = z.object({
steps: z.array(Step),
final_answer: z.string(),
});
async function main() {
const client = new OpenAI();
const stream = client.beta.chat.completions
.stream({
model: 'gpt-4o-2024-08-06',
messages: [
{
role: 'user',
content: `What's the weather like in SF?`,
},
],
response_format: zodResponseFormat(MathResponse, 'math_response'),
})
.on('refusal.delta', ({ delta }) => {
process.stdout.write(delta);
})
.on('refusal.done', () => console.log('\n\nrequest refused 😱'))
.on('content.delta', ({ snapshot, parsed }) => {
console.log('content:', snapshot);
console.log('parsed:', parsed);
console.log();
})
.on('content.done', (props) => {
if (props.parsed) {
console.log('\n\nfinished parsing!');
console.log(`answer: ${props.parsed.final_answer}`);
}
});
await stream.done();
const completion = await stream.finalChatCompletion();
console.dir(completion, { depth: 5 });
const message = completion.choices[0]?.message;
if (message?.parsed) {
console.log(message.parsed.steps);
}
}
main();