Open History to see the last commit
1/**
2 * Fallback Error Handling Test
3 *
4 * This demonstrates that when the primary adapter fails (e.g., rate limit, no API key),
5 * the system automatically falls back to the configured fallback adapters.
6 */
8import { AI } from "../packages/ai/src";
9import { OpenAIAdapter } from "../packages/ai-openai/src";
10import { OllamaAdapter } from "../packages/ai-ollama/src";
12const ai = new AI({
13 adapters: {
14 openai: new OpenAIAdapter({
15 // Intentionally using invalid or missing API key to simulate failure
16 apiKey: process.env.OPENAI_API_KEY || "invalid-key",
17 }),
18 ollama: new OllamaAdapter({
19 baseUrl: process.env.OLLAMA_BASE_URL || "http://localhost:11434",
20 }),
21 },
22});
24async function testFallbackOnError() {
25 console.log("=== Testing Fallback on Primary Adapter Failure ===\n");
27 // Test 1: Single adapter with fallback - if OpenAI fails, use Ollama
28 console.log("1. OpenAI (primary) -> Ollama (fallback on error):");
29 try {
30 const result = await ai.chat({
31 adapter: "openai",
32 model: "gpt-4",
33 messages: [{ role: "user", content: "Say hello in one word" }],
34 fallbacks: [
35 { adapter: "ollama", model: "llama2" },
36 ],
37 });
38 console.log("✓ Response:", result.content);
39 console.log(" Model used:", result.model);
40 } catch (error: any) {
41 console.log("✗ All adapters failed:", error.message);
42 }
43 console.log();
45 // Test 2: Streaming with fallback
46 console.log("2. Streaming - OpenAI -> Ollama fallback:");
47 try {
48 const chunks: string[] = [];
49 for await (const chunk of ai.streamChat({
50 adapter: "openai",
51 model: "gpt-3.5-turbo",
52 messages: [{ role: "user", content: "Count to 3" }],
53 fallbacks: [
54 { adapter: "ollama", model: "llama2" },
55 ],
56 })) {
57 if (chunk.type === "content") {
58 chunks.push(chunk.delta);
59 }
60 }
61 console.log("✓ Streamed response:", chunks.join(""));
62 } catch (error: any) {
63 console.log("✗ Failed:", error.message);
64 }
65 console.log();
67 // Test 3: Text generation with fallback
68 console.log("3. Text generation - OpenAI -> Ollama fallback:");
69 try {
70 const result = await ai.generateText({
71 adapter: "openai",
72 model: "gpt-3.5-turbo",
73 prompt: "Say hello",
74 fallbacks: [
75 { adapter: "ollama", model: "llama2" },
76 ],
77 });
78 console.log("✓ Generated text:", result.text);
79 console.log(" Model used:", result.model);
80 } catch (error: any) {
81 console.log("✗ Failed:", error.message);
82 }
83 console.log();
85 // Test 4: Text streaming with fallback
86 console.log("4. Text streaming - OpenAI -> Ollama fallback:");
87 try {
88 const chunks: string[] = [];
89 for await (const chunk of ai.generateTextStream({
90 adapter: "openai",
91 model: "gpt-3.5-turbo",
92 prompt: "Say hello",
93 fallbacks: [
94 { adapter: "ollama", model: "llama2" },
95 ],
96 })) {
97 chunks.push(chunk);
98 }
99 console.log("✓ Streamed text:", chunks.join(""));
100 } catch (error: any) {
101 console.log("✗ Failed:", error.message);
102 }
103 console.log();
105 // Test 5: Multiple fallbacks - try OpenAI, then Ollama llama2, then Ollama mistral
106 console.log("5. Multiple fallbacks - OpenAI -> Ollama (llama2) -> Ollama (mistral):");
107 try {
108 const result = await ai.chat({
109 adapter: "openai",
110 model: "gpt-4",
111 messages: [{ role: "user", content: "Hello" }],
112 fallbacks: [
113 { adapter: "ollama", model: "llama2" },
114 { adapter: "ollama", model: "mistral" },
115 ],
116 });
117 console.log("✓ Response:", result.content);
118 console.log(" Model used:", result.model);
119 } catch (error: any) {
120 console.log("✗ All adapters failed:", error.message);
121 }
122 console.log();
124 console.log("=== Test Complete ===");
125 console.log("\nExpected behavior:");
126 console.log("- If OpenAI fails (no credits, rate limit, invalid key), it logs a warning");
127 console.log("- System automatically tries the fallback adapter (Ollama)");
128 console.log("- If fallback succeeds, you see the response");
129 console.log("- If all adapters fail, you see comprehensive error with all failures");
130}
132testFallbackOnError().catch(console.error);