/** * Advanced streaming example with Ask Client SDK * * Run with: * bun run examples/node/streaming.ts * or * tsx examples/node/streaming.ts */ import { AskClient } from '../../src/client/AskClient'; async function main() { console.log('🌊 Ask Client SDK - Streaming Example\n'); // Initialize client const client = new AskClient({ baseUrl: process.env.WORKER_API_URL || 'http://localhost:59898/v1', askApiKey: process.env.ASK_API_KEY, defaultProvider: 'groq', defaultModel: 'llama-3.1-70b-versatile', debug: false, }); // Example 1: Stream with visual feedback console.log('📝 Example 1: Stream with visual feedback\n'); try { const stream = await client.streamCompletion({ messages: [ { role: 'user', content: 'Write a haiku about coding' } ], config: { temperature: 0.9, maxTokens: 100 } }); let charCount = 0; process.stdout.write('Response: '); for await (const chunk of stream) { process.stdout.write(chunk); charCount += chunk.length; } console.log(`\n\nReceived ${charCount} characters`); console.log('---\n'); } catch (error: any) { console.error('Error:', error.message); } // Example 2: Collect stream into buffer console.log('📝 Example 2: Collect stream into buffer\n'); try { const stream = await client.streamCompletion({ messages: [ { role: 'user', content: 'List 5 programming languages' } ] }); let fullResponse = ''; const chunks: string[] = []; for await (const chunk of stream) { fullResponse += chunk; chunks.push(chunk); } console.log('Full response:', fullResponse); console.log(`Received in ${chunks.length} chunks`); console.log('---\n'); } catch (error: any) { console.error('Error:', error.message); } // Example 3: Raw stream access console.log('📝 Example 3: Raw stream access\n'); try { const rawStream = await client.getStream({ messages: [ { role: 'user', content: 'Count to 5' } ], config: { maxTokens: 50 } }); const reader = rawStream.getReader(); const decoder = new TextDecoder(); process.stdout.write('Raw chunks: '); while (true) { const { value, done } = await reader.read(); if (done) break; const text = decoder.decode(value, { stream: true }); process.stdout.write(text); } console.log('\n---\n'); } catch (error: any) { console.error('Error:', error.message); } // Example 4: Parallel streaming requests console.log('📝 Example 4: Parallel streaming requests\n'); try { const prompts = [ 'Name a color', 'Name an animal', 'Name a fruit' ]; const streamPromises = prompts.map(prompt => client.streamCompletion({ messages: [{ role: 'user', content: prompt }], config: { maxTokens: 20 } }) ); const streams = await Promise.all(streamPromises); for (let i = 0; i < streams.length; i++) { let response = ''; for await (const chunk of streams[i]) { response += chunk; } console.log(`${prompts[i]}: ${response.trim()}`); } console.log('---\n'); } catch (error: any) { console.error('Error:', error.message); } // Example 5: Timeout handling console.log('📝 Example 5: Timeout handling\n'); try { const timeoutMs = 5000; // 5 seconds const streamPromise = client.streamCompletion({ messages: [ { role: 'user', content: 'Explain quantum computing' } ], config: { maxTokens: 50 } }); const timeoutPromise = new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), timeoutMs) ); const stream = await Promise.race([streamPromise, timeoutPromise]); process.stdout.write('Response: '); for await (const chunk of stream) { process.stdout.write(chunk); } console.log('\n---\n'); } catch (error: any) { console.error('Error:', error.message); console.log('---\n'); } console.log('✅ All streaming examples completed!'); } main().catch(console.error);