-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathstreaming-cascade.ts
More file actions
84 lines (67 loc) · 2.57 KB
/
streaming-cascade.ts
File metadata and controls
84 lines (67 loc) · 2.57 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
/**
* Streaming Cascade Example
*
* Demonstrates real-time streaming with CascadeFlow:
* 1. Stream drafter optimistically (user sees output immediately)
* 2. Check quality after drafter completes
* 3. If quality insufficient, stream verifier output after drafter output (optimistic streaming)
*/
import { ChatOpenAI } from '@langchain/openai';
import { withCascade } from '../src/index.js';
async function main() {
console.log('🌊 CascadeFlow Streaming Example\n');
// Configure cascade with drafter and verifier
const cascade = withCascade({
drafter: new ChatOpenAI({
model: 'gpt-4o-mini',
temperature: 0.7,
}),
verifier: new ChatOpenAI({
model: 'gpt-4o',
temperature: 0.7,
}),
qualityThreshold: 0.7,
}).bind({ metadata: { cascadeflow_emit_switch_message: true } });
// Example 1: Simple query (likely accepted by drafter)
console.log('Example 1: Simple Query (may cascade)\n');
console.log('Q: What is 2+2?\n');
console.log('A: ');
const stream1 = await cascade.stream('What is 2+2?');
for await (const chunk of stream1) {
const content = typeof chunk.content === 'string' ? chunk.content : '';
process.stdout.write(content);
}
console.log('\n\n---\n');
// Example 2: Complex query (likely escalated to verifier)
console.log('Example 2: Complex Query (may escalate)\n');
console.log('Q: Explain quantum entanglement and its implications for quantum computing\n');
console.log('A: ');
const stream2 = await cascade.stream(
'Explain quantum entanglement and its implications for quantum computing'
);
for await (const chunk of stream2) {
const content = typeof chunk.content === 'string' ? chunk.content : '';
process.stdout.write(content);
}
console.log('\n\n---\n');
// Example 3: Low quality query (forces cascade)
console.log('Example 3: Ambiguous Query (likely escalates)\n');
console.log('Q: Tell me about it\n');
console.log('A: ');
const stream3 = await cascade.stream('Tell me about it');
for await (const chunk of stream3) {
const content = typeof chunk.content === 'string' ? chunk.content : '';
process.stdout.write(content);
}
console.log('\n\n---\n');
// Show final cascade statistics
const stats = cascade.getLastCascadeResult();
if (stats) {
console.log('\n📊 Cascade Statistics:');
console.log(` Model Used: ${stats.modelUsed}`);
console.log(` Drafter Quality: ${stats.drafterQuality.toFixed(2)}`);
console.log(` Accepted: ${stats.accepted}`);
console.log(` Latency: ${stats.latencyMs}ms`);
}
}
main().catch(console.error);