-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreaming.js
More file actions
133 lines (107 loc) · 3.63 KB
/
Copy pathstreaming.js
File metadata and controls
133 lines (107 loc) · 3.63 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Example demonstrating streaming support for text generation.
*
* This script shows how to use streaming with the OpenAI-compatible API.
*/
import { Pollinations } from '@gpt4free/g4f.dev';
async function main() {
console.log('='.repeat(70));
console.log('Streaming Examples');
console.log('='.repeat(70));
// Initialize client
const client = new Pollinations();
// ========================================================================
// OpenAI-Compatible Streaming API
// ========================================================================
console.log('\n1. Basic Streaming (OpenAI-compatible):');
console.log('-'.repeat(70));
const stream1 = await client.chat.completions.create({
messages: [
{ role: 'user', content: 'Count to 10' }
],
stream: true
});
for await (const chunk of stream1) {
if (chunk.choices[0]?.delta?.content) {
process.stdout.write(chunk.choices[0].delta.content);
}
}
console.log('\n');
console.log('\n2. Streaming with System Message:');
console.log('-'.repeat(70));
const stream2 = await client.chat.completions.create({
messages: [
{ role: 'system', content: 'You are a creative poet.' },
{ role: 'user', content: 'Write a haiku about coding' }
],
stream: true
});
for await (const chunk of stream2) {
if (chunk.choices[0]?.delta?.content) {
process.stdout.write(chunk.choices[0].delta.content);
}
}
console.log('\n');
console.log('\n3. Streaming with Temperature Control:');
console.log('-'.repeat(70));
const stream3 = await client.chat.completions.create({
messages: [
{ role: 'user', content: 'Tell me a very short story about a robot' }
],
temperature: 0.9,
stream: true
});
let fullResponse = '';
for await (const chunk of stream3) {
const delta = chunk.choices[0]?.delta?.content;
if (delta) {
fullResponse += delta;
process.stdout.write(delta);
}
// Check finish reason
if (chunk.choices[0].finish_reason) {
console.log(`\n\n[Finished: ${chunk.choices[0].finish_reason}]`);
}
}
console.log(`\n[Full response length: ${fullResponse.length} characters]`);
console.log('\n4. Collecting Chunks for Analysis:');
console.log('-'.repeat(70));
const stream4 = await client.chat.completions.create({
messages: [
{ role: 'user', content: 'List 5 programming languages' }
],
stream: true
});
const chunks = [];
for await (const chunk of stream4) {
chunks.push(chunk);
if (chunk.choices[0]?.delta?.content) {
process.stdout.write(chunk.choices[0].delta.content);
}
}
console.log(`\n\n[Received ${chunks.length} chunks]`);
// ========================================================================
// Advanced Use Cases
// ========================================================================
console.log('\n5. Real-time Processing:');
console.log('-'.repeat(70));
console.log('Simulating character-by-character display:');
const stream5 = await client.chat.completions.create({
messages: [{ role: 'user', content: 'Say "Hello World"' }],
stream: true
});
for await (const chunk of stream5) {
if (chunk.choices[0]?.delta?.content) {
const content = chunk.choices[0].delta.content;
process.stdout.write(content);
// Simulate processing delay
await new Promise(resolve => setTimeout(resolve, 50));
}
}
console.log('\n');
console.log('\n' + '='.repeat(70));
console.log('Streaming examples completed!');
console.log('='.repeat(70));
}
// Run the examples
main().catch(console.error);