-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfallback.py
More file actions
37 lines (29 loc) · 1.01 KB
/
fallback.py
File metadata and controls
37 lines (29 loc) · 1.01 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
"""
AIPower fallback pattern — reliability through multi-provider redundancy.
If OpenAI goes down, fall back to DeepSeek. If DeepSeek fails, try Claude.
"""
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.aipower.me/v1",
api_key=os.environ["AIPOWER_API_KEY"],
)
# Fallback chain: prefer GPT, then Claude, then DeepSeek
MODELS = [
"openai/gpt-5",
"anthropic/claude-sonnet",
"deepseek/deepseek-chat",
]
def chat_with_fallback(messages):
for model in MODELS:
try:
r = client.chat.completions.create(model=model, messages=messages, timeout=30)
return {"model_used": model, "response": r.choices[0].message.content}
except Exception as e:
print(f"[{model}] failed: {e}")
continue
raise RuntimeError("All models failed")
if __name__ == "__main__":
result = chat_with_fallback([{"role": "user", "content": "Hello!"}])
print(f"Used: {result['model_used']}")
print(f"Response: {result['response']}")