forked from lemony-ai/cascadeflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreasoning-models.ts
More file actions
235 lines (207 loc) · 7.51 KB
/
reasoning-models.ts
File metadata and controls
235 lines (207 loc) · 7.51 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* Example: Using Reasoning Models Across All Providers
*
* cascadeflow supports reasoning models from 4 providers with automatic detection:
*
* 1. OpenAI (o3, o4-mini)
* - Chain-of-thought reasoning with hidden thinking
* - reasoning_effort parameter (low/medium/high)
* - max_completion_tokens required
* - o3 and o4-mini are the latest reasoning models (2025)
*
* 2. Anthropic (claude-sonnet-4-5, claude-opus-4-1)
* - Extended thinking mode (enable with thinkingBudget)
* - Minimum 1024 tokens thinking budget
* - Visible reasoning in response
* - Claude Sonnet 4.5 and Opus 4.1 released in 2025
*
* 3. Ollama (deepseek-r1, deepseek-r1-distill)
* - Free local inference
* - DeepSeek-R1 reasoning models
* - Full privacy, no API costs
*
* 4. vLLM (deepseek-r1, deepseek-r1-distill)
* - Self-hosted high-performance inference
* - 24x faster than standard serving
* - Production-ready deployment
*
* Zero configuration required - cascadeflow auto-detects capabilities!
*/
import { CascadeAgent } from '../../src';
async function reasoningModelsExample() {
// Example 1: o4-mini (latest fast reasoning model)
console.log('\n=== Example 1: o4-mini (latest reasoning model) ===');
const agent1 = new CascadeAgent({
models: [
{
name: 'o4-mini', // Auto-detected as reasoning model
provider: 'openai',
cost: 0.004, // Estimate
},
],
});
const result1 = await agent1.run(
'Solve this problem step by step: If a train travels at 80 km/h for 2.5 hours, then slows to 60 km/h for the next hour, what is the total distance traveled?',
{ maxTokens: 2000 }
);
console.log('Response:', result1.content);
console.log('Cost:', `$${result1.totalCost.toFixed(6)}`);
// Example 2: o3 (latest advanced reasoning model with reasoning_effort)
console.log('\n=== Example 2: o3 with reasoning_effort ===');
const agent2 = new CascadeAgent({
models: [
{
name: 'o3',
provider: 'openai',
cost: 0.008, // Estimate
},
],
});
// High reasoning effort for complex problem
const result2 = await agent2.run(
'Design an efficient algorithm to find all palindromic substrings in a string of length n. Analyze the time and space complexity.',
{ maxTokens: 4000 }
);
console.log('Response:', result2.content.substring(0, 500) + '...');
console.log('Cost:', `$${result2.totalCost.toFixed(6)}`);
// Example 3: Using in cascade (auto-routing to reasoning model)
console.log('\n=== Example 3: Cascade with reasoning model fallback ===');
const agent3 = new CascadeAgent({
models: [
{
name: 'gpt-4o-mini', // Fast, cheap model tries first
provider: 'openai',
cost: 0.00015,
},
{
name: 'o4-mini', // Falls back to reasoning model if needed
provider: 'openai',
cost: 0.004,
},
],
quality: {
threshold: 0.8, // High quality threshold
},
});
const result3 = await agent3.run(
'Prove that the square root of 2 is irrational.',
{ maxTokens: 2000 }
);
console.log('Model used:', result3.modelUsed);
console.log('Response:', result3.content.substring(0, 300) + '...');
console.log('Quality score:', result3.qualityScore);
// Example 4: Comparing reasoning efforts
console.log('\n=== Example 4: Comparing reasoning efforts ===');
const query = 'What are the implications of quantum entanglement for computing?';
const efforts: Array<'low' | 'medium' | 'high'> = ['low', 'medium', 'high'];
for (const effort of efforts) {
const result = await agent2.run(query, { maxTokens: 1000 });
console.log(`\n${effort.toUpperCase()} effort:`);
console.log(' Cost:', `$${result.totalCost.toFixed(6)}`);
console.log(' Response length:', result.content.length, 'chars');
}
// Example 5: Anthropic Claude Sonnet 4.5 with Extended Thinking
console.log('\n=== Example 5: Claude Sonnet 4.5 (Extended Thinking) ===');
const agent4 = new CascadeAgent({
models: [
{
name: 'claude-sonnet-4-5',
provider: 'anthropic',
cost: 0.003,
},
],
});
const result4 = await agent4.run(
'Design a fault-tolerant distributed consensus algorithm. Explain your reasoning process.',
{ maxTokens: 5000 }
);
console.log('Response:', result4.content.substring(0, 500) + '...');
console.log('Cost:', `$${result4.totalCost.toFixed(6)}`);
console.log('\nNote: Claude extended thinking produces visible reasoning in the response!');
// Example 6: DeepSeek-R1 via Ollama (Free Local Inference)
console.log('\n=== Example 6: DeepSeek-R1 via Ollama (Local) ===');
console.log('Prerequisites: Install Ollama (https://ollama.ai) and run:');
console.log(' ollama pull deepseek-r1:8b');
console.log();
try {
const agent5 = new CascadeAgent({
models: [
{
name: 'deepseek-r1:8b', // Auto-detected as reasoning model
provider: 'ollama',
cost: 0,
},
],
});
const result5 = await agent5.run(
'Explain the time complexity of quicksort in best, average, and worst cases.',
{ maxTokens: 2000 }
);
console.log('Response:', result5.content.substring(0, 400) + '...');
console.log('Cost:', `$${result5.totalCost.toFixed(6)}`, '(FREE - local inference)');
} catch (error) {
console.log('Skipping - Ollama not available:', (error as Error).message);
console.log('Install from: https://ollama.ai');
}
// Example 7: DeepSeek-R1 via vLLM (High-Performance Self-Hosted)
console.log('\n=== Example 7: DeepSeek-R1 via vLLM (Self-Hosted) ===');
console.log('Prerequisites: Start vLLM server:');
console.log(' python -m vllm.entrypoints.openai.api_server \\');
console.log(' --model deepseek-ai/DeepSeek-R1-Distill-Llama-8B \\');
console.log(' --port 8000');
console.log();
try {
const agent6 = new CascadeAgent({
models: [
{
name: 'deepseek-ai/DeepSeek-R1-Distill-Llama-8B',
provider: 'vllm',
baseUrl: process.env.VLLM_BASE_URL || 'http://localhost:8000/v1',
cost: 0,
},
],
});
const result6 = await agent6.run(
'What is the difference between TCP and UDP? When would you use each?',
{ maxTokens: 1500 }
);
console.log('Response:', result6.content.substring(0, 400) + '...');
console.log('Cost:', `$${result6.totalCost.toFixed(6)}`, '(FREE - self-hosted)');
console.log('Note: vLLM provides 24x faster inference than standard serving!');
} catch (error) {
console.log('Skipping - vLLM server not available:', (error as Error).message);
console.log('See: https://docs.vllm.ai');
}
// Example 8: Multi-Provider Reasoning Cascade
console.log('\n=== Example 8: Multi-Provider Reasoning Cascade ===');
const agent7 = new CascadeAgent({
models: [
{
name: 'deepseek-r1:8b',
provider: 'ollama',
cost: 0, // Free local inference
},
{
name: 'o4-mini',
provider: 'openai',
cost: 0.004,
},
{
name: 'claude-sonnet-4-5',
provider: 'anthropic',
cost: 0.003,
},
],
quality: {
threshold: 0.85,
},
});
console.log('This cascade tries:');
console.log(' 1. DeepSeek-R1 (local, free)');
console.log(' 2. Falls back to o4-mini if quality < 0.85');
console.log(' 3. Falls back to Claude Sonnet 4.5 as final option');
console.log();
console.log('Perfect for cost optimization with reasoning models!');
}
// Run examples
reasoningModelsExample().catch(console.error);