-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathfallback_models.py
More file actions
32 lines (26 loc) · 932 Bytes
/
fallback_models.py
File metadata and controls
32 lines (26 loc) · 932 Bytes
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
import os
import requests
OPENROUTER_API_URL = "https://openrouter.ai/api/v1/chat/completions"
api_key = os.getenv("OPENROUTER_API_KEY")
def make_request_with_fallback(models_list, messages):
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
payload = {"models": models_list, "messages": messages}
return requests.post(OPENROUTER_API_URL, headers=headers, json=payload)
response = make_request_with_fallback(
models_list=[
"openai/gpt-5",
"openai/gpt-3.5-turbo",
"openai/gpt-3.5-turbo-16k"
],
messages=[{"role": "user", "content": "What is the capital of France?"}]
)
data = response.json()
if model := data.get('model'):
print(f"Model: {model} by {data['provider']}")
print(f"Response: {data['choices'][0]['message']['content']}")
else:
print("No model found in the response.")
print(f"Response: {data}")