Open History to see the last commit
1/**
2 * Fallback with Provider Options Example
3 *
4 * This example demonstrates how to use type-safe provider-specific options
5 * in fallback configurations. Each fallback can have its own providerOptions
6 * that are specific to that adapter.
7 */
9import { AI } from "@tanstack/ai";
10import { OpenAIAdapter } from "@tanstack/ai-openai";
11import { AnthropicAdapter } from "@tanstack/ai-anthropic";
13// Initialize AI with multiple adapters
14const ai = new AI({
15 adapters: {
16 openai: new OpenAIAdapter({
17 apiKey: process.env.OPENAI_API_KEY!,
18 }),
19 anthropic: new AnthropicAdapter({
20 apiKey: process.env.ANTHROPIC_API_KEY!,
21 }),
22 },
23});
25// ============================================================================
26// Example 1: Chat with fallbacks and provider-specific options
27// ============================================================================
29async function chatWithFallbacks() {
30 const response = await ai.chat({
31 adapter: "openai",
32 model: "gpt-4o",
33 messages: [
34 {
35 role: "user",
36 content: "Explain quantum computing in simple terms",
37 },
38 ],
39 // Primary adapter can have its own provider options
40 providerOptions: {
41 openai: {
42 reasoningSummary: "detailed",
43 textVerbosity: "high",
44 },
45 },
46 // Fallbacks can have different provider options
47 fallbacks: [
48 {
49 adapter: "anthropic",
50 model: "claude-3-5-sonnet-20241022",
51 // ✅ TypeScript knows this is Anthropic-specific options
52 providerOptions: {
53 anthropic: {
54 thinking: {
55 type: "enabled",
56 budgetTokens: 10000,
57 },
58 },
59 },
60 },
61 {
62 adapter: "openai",
63 model: "gpt-4o-mini",
64 // ✅ TypeScript knows this is OpenAI-specific options
65 providerOptions: {
66 openai: {
67 textVerbosity: "low", // Use low verbosity for fallback
68 store: false,
69 },
70 },
71 },
72 ],
73 });
75 console.log("Response:", response.content);
76}
78// ============================================================================
79// Example 2: Fallback-only mode with provider options
80// ============================================================================
82async function fallbackOnlyMode() {
83 // No primary adapter specified - tries fallbacks in order
84 const response = await ai.chat({
85 messages: [
86 {
87 role: "user",
88 content: "What is TypeScript?",
89 },
90 ],
91 fallbacks: [
92 {
93 adapter: "openai",
94 model: "gpt-4o",
95 providerOptions: {
96 openai: {
97 reasoningSummary: "auto",
98 maxCompletionTokens: 500,
99 },
100 },
101 },
102 {
103 adapter: "anthropic",
104 model: "claude-3-5-sonnet-20241022",
105 providerOptions: {
106 anthropic: {
107 cacheControl: {
108 type: "ephemeral",
109 ttl: "1h",
110 },
111 },
112 },
113 },
114 ],
115 });
117 console.log("Response:", response.content);
118}
120// ============================================================================
121// Example 3: Global fallbacks with provider options
122// ============================================================================
124const aiWithGlobalFallbacks = new AI({
125 adapters: {
126 openai: new OpenAIAdapter({
127 apiKey: process.env.OPENAI_API_KEY!,
128 }),
129 anthropic: new AnthropicAdapter({
130 apiKey: process.env.ANTHROPIC_API_KEY!,
131 }),
132 },
133 // Global fallbacks that apply to all operations
134 fallbacks: [
135 {
136 adapter: "openai",
137 model: "gpt-4o",
138 providerOptions: {
139 openai: {
140 serviceTier: "flex", // Cheaper tier for fallback
141 store: false,
142 },
143 },
144 },
145 {
146 adapter: "anthropic",
147 model: "claude-3-5-sonnet-20241022",
148 providerOptions: {
149 anthropic: {
150 sendReasoning: false, // Don't send reasoning in fallback
151 },
152 },
153 },
154 ],
155});
157async function useGlobalFallbacks() {
158 // This will use the global fallbacks if primary fails
159 const response = await aiWithGlobalFallbacks.chat({
160 adapter: "openai",
161 model: "gpt-4o", // Primary
162 messages: [{ role: "user", content: "Hello!" }],
163 providerOptions: {
164 openai: {
165 textVerbosity: "high",
166 },
167 },
168 // If this fails, will try global fallbacks with their providerOptions
169 });
171 console.log("Response:", response.content);
172}
174// ============================================================================
175// Example 4: Text generation with fallback provider options
176// ============================================================================
178async function textGenerationWithFallbacks() {
179 const result = await ai.generateText({
180 adapter: "openai",
181 model: "gpt-4o",
182 prompt: "Write a haiku about TypeScript",
183 providerOptions: {
184 openai: {
185 textVerbosity: "medium",
186 },
187 },
188 fallbacks: [
189 {
190 adapter: "anthropic",
191 model: "claude-3-5-sonnet-20241022",
192 providerOptions: {
193 anthropic: {
194 thinking: {
195 type: "enabled",
196 budgetTokens: 5000,
197 },
198 },
199 },
200 },
201 ],
202 });
204 console.log("Generated text:", result.text);
205}
207// ============================================================================
208// Example 5: Image generation with fallback provider options
209// ============================================================================
211async function imageGenerationWithFallbacks() {
212 const result = await ai.image({
213 adapter: "openai",
214 model: "dall-e-3",
215 prompt: "A futuristic city with flying cars",
216 size: "1024x1024",
217 providerOptions: {
218 openai: {
219 quality: "hd",
220 style: "vivid",
221 },
222 },
223 fallbacks: [
224 {
225 adapter: "openai",
226 model: "dall-e-2",
227 // Fallback to standard quality
228 providerOptions: {
229 openai: {
230 quality: "standard",
231 },
232 },
233 },
234 ],
235 });
237 console.log("Generated image:", result.image?.base64.substring(0, 50) + "...");
238}
240// ============================================================================
241// Type Safety Demonstration
242// ============================================================================
244async function demonstrateTypeSafety() {
245 // ✅ Correct - OpenAI options for OpenAI adapter
246 await ai.chat({
247 adapter: "openai",
248 model: "gpt-4o",
249 messages: [],
250 fallbacks: [
251 {
252 adapter: "openai",
253 model: "gpt-4o-mini",
254 providerOptions: {
255 openai: {
256 reasoningSummary: "detailed",
257 },
258 },
259 },
260 ],
261 });
263 // ✅ Correct - Anthropic options for Anthropic adapter
264 await ai.chat({
265 adapter: "anthropic",
266 model: "claude-3-5-sonnet-20241022",
267 messages: [],
268 fallbacks: [
269 {
270 adapter: "anthropic",
271 model: "claude-3-opus-20240229",
272 providerOptions: {
273 anthropic: {
274 thinking: {
275 type: "enabled",
276 budgetTokens: 8000,
277 },
278 },
279 },
280 },
281 ],
282 });
284 // ❌ This would be a TypeScript error:
285 // await ai.chat({
286 // adapter: "openai",
287 // model: "gpt-4o",
288 // messages: [],
289 // fallbacks: [
290 // {
291 // adapter: "openai",
292 // model: "gpt-4o-mini",
293 // providerOptions: {
294 // openai: {
295 // thinking: { ... }, // ERROR: thinking doesn't exist on OpenAI
296 // },
297 // },
298 // },
299 // ],
300 // });
301}
303// ============================================================================
304// Run Examples
305// ============================================================================
307async function main() {
308 console.log("\n=== Chat with Fallbacks ===");
309 await chatWithFallbacks();
311 console.log("\n=== Fallback-Only Mode ===");
312 await fallbackOnlyMode();
314 console.log("\n=== Global Fallbacks ===");
315 await useGlobalFallbacks();
317 console.log("\n=== Text Generation ===");
318 await textGenerationWithFallbacks();
320 console.log("\n=== Image Generation ===");
321 await imageGenerationWithFallbacks();
323 console.log("\n=== Type Safety Demo ===");
324 await demonstrateTypeSafety();
325}
327// Run if called directly
328if (require.main === module) {
329 main().catch(console.error);
330}