-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreaming-test.js
More file actions
42 lines (35 loc) · 959 Bytes
/
streaming-test.js
File metadata and controls
42 lines (35 loc) · 959 Bytes
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
require('dotenv').config();
const OpenAI = require('openai');
// Initialize OpenAI client
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
async function testOpenAIStreaming() {
try {
console.log('Sending request to OpenAI...');
const stream = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: [
{
role: 'system',
content: 'You are a helpful assistant...'
},
{
role: 'user',
content: 'Tell me more about streaming responses.'
}
],
max_tokens: 150,
temperature: 0.2,
stream: true,
});
// For handling streaming responses
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || '');
}
console.log('\nStream ended.');
} catch (error) {
console.error('Error in testOpenAIStreaming:', error);
}
}
testOpenAIStreaming();