-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_failover.py
More file actions
64 lines (50 loc) · 1.92 KB
/
Copy pathagent_failover.py
File metadata and controls
64 lines (50 loc) · 1.92 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
"""AI Agent with automatic failover across providers.
When using SoxAI, you don't need failover logic in your code —
the gateway handles it. But here's how you'd use multiple models
in different agent steps for cost optimization.
"""
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["SOXAI_API_KEY"],
base_url="https://api.soxai.io/v1",
)
def classify(text: str) -> str:
"""Step 1: Classify intent with a cheap, fast model."""
response = client.chat.completions.create(
model="gpt-4o-mini", # $0.15/1M tokens — cheapest option
messages=[
{"role": "system", "content": "Classify the user's intent in one word: question, task, or chat."},
{"role": "user", "content": text},
],
max_tokens=10,
)
return response.choices[0].message.content.strip().lower()
def reason(text: str) -> str:
"""Step 2: Deep reasoning with a capable model."""
response = client.chat.completions.create(
model="claude-sonnet-4-6", # Best quality for reasoning
messages=[
{"role": "system", "content": "You are a helpful assistant. Think step by step."},
{"role": "user", "content": text},
],
)
return response.choices[0].message.content
def summarize(text: str) -> str:
"""Step 3: Summarize with a balanced model."""
response = client.chat.completions.create(
model="deepseek-chat", # Good quality at $0.27/1M tokens
messages=[
{"role": "user", "content": f"Summarize in 2 sentences:\n\n{text}"},
],
max_tokens=100,
)
return response.choices[0].message.content
# Run the agent
user_input = "What are the pros and cons of microservices vs monolith architecture?"
intent = classify(user_input)
print(f"Intent: {intent}")
answer = reason(user_input)
print(f"\nFull answer:\n{answer}")
summary = summarize(answer)
print(f"\nSummary:\n{summary}")