-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfull_pipeline_fallback.py
More file actions
226 lines (182 loc) · 6.91 KB
/
full_pipeline_fallback.py
File metadata and controls
226 lines (182 loc) · 6.91 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""
Full Pipeline with Provider Fallback -- Resilient Voice Agent
Config: stt + llm + tts with fallback providers for each component
Demonstrates automatic failover between providers:
- STT: Deepgram -> OpenAI Whisper (if Deepgram fails)
- LLM: OpenAI GPT-4o -> Anthropic Claude -> Groq (if OpenAI fails)
- TTS: ElevenLabs -> OpenAI TTS (if ElevenLabs fails)
The FallbackAdapter retries automatically -- no customer code needed.
If a provider returns an error or times out, the next provider in
the list is tried seamlessly. The caller never hears a gap.
Usage:
1. Set provider API keys as env vars
2. pip install plivo_agentstack[all]
3. python full_pipeline_fallback.py
"""
import asyncio
import os
from plivo_agentstack import AsyncClient
from plivo_agentstack.agent import (
AgentSessionEnded,
AgentSessionStarted,
Interruption,
ToolCall,
TurnCompleted,
TurnMetrics,
VoiceApp,
)
PLIVO_AUTH_ID = os.environ.get("PLIVO_AUTH_ID", "")
PLIVO_AUTH_TOKEN = os.environ.get("PLIVO_AUTH_TOKEN", "")
BASE_URL = os.environ.get("PLIVO_API_URL", "https://api.plivo.com")
# Provider API keys -- set all providers you want in the fallback chain
DEEPGRAM_API_KEY = os.environ.get("DEEPGRAM_API_KEY", "")
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", "")
ANTHROPIC_API_KEY = os.environ.get("ANTHROPIC_API_KEY", "")
GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "")
ELEVENLABS_API_KEY = os.environ.get("ELEVENLABS_API_KEY", "")
client = AsyncClient(PLIVO_AUTH_ID, PLIVO_AUTH_TOKEN, base_url=BASE_URL)
# --- Tool definitions ---
CHECK_BALANCE_TOOL = {
"name": "check_balance",
"description": "Check the customer's account balance",
"parameters": {
"type": "object",
"properties": {
"account_id": {"type": "string", "description": "The account ID"},
},
"required": ["account_id"],
},
}
SYSTEM_PROMPT = (
"You are a helpful banking assistant. Be concise and professional. "
"When the customer asks about their balance, use the check_balance tool."
)
# --- Agent setup with fallback providers ---
async def init_agent():
"""Create an agent with fallback providers for resilience.
The primary provider is in stt/llm/tts. The fallback lists are tried
in order if the primary fails. The pipeline automatically retries
with the next provider -- no customer code needed.
"""
agent = await client.agent.agents.create(
agent_name="Resilient Banking Agent",
# --- Primary STT ---
stt={
"provider": "deepgram",
"model": "nova-3",
"language": "en",
"api_key": DEEPGRAM_API_KEY,
},
# --- STT Fallback: tried in order if Deepgram fails ---
stt_fallback=[
{
"provider": "openai",
"model": "gpt-4o-transcribe",
"api_key": OPENAI_API_KEY,
},
# Add more STT providers as needed:
# Supported: deepgram, google, azure, assemblyai, groq, openai
# {"provider": "google", "language": "en-US"},
# {"provider": "groq", "model": "whisper-large-v3", "api_key": GROQ_API_KEY},
],
# --- Primary LLM ---
llm={
"provider": "openai",
"model": "gpt-4o",
"temperature": 0.2,
"api_key": OPENAI_API_KEY,
"system_prompt": SYSTEM_PROMPT,
"tools": [CHECK_BALANCE_TOOL],
},
# --- LLM Fallback: tried in order if OpenAI fails ---
# Supported: openai, anthropic, groq, google, azure, together,
# fireworks, perplexity, mistral
llm_fallback=[
{
"provider": "anthropic",
"model": "claude-sonnet-4-20250514",
"api_key": ANTHROPIC_API_KEY,
},
{
"provider": "groq",
"model": "llama-3.3-70b-versatile",
"api_key": GROQ_API_KEY,
},
],
# --- Primary TTS ---
tts={
"provider": "elevenlabs",
"voice": "EXAVITQu4vr4xnSDxMaL", # Sarah
"model": "eleven_flash_v2_5",
"api_key": ELEVENLABS_API_KEY,
},
# --- TTS Fallback: tried in order if ElevenLabs fails ---
# Supported: elevenlabs, cartesia, google, azure, openai, deepgram
tts_fallback=[
{
"provider": "openai",
"model": "gpt-4o-mini-tts",
"voice": "alloy",
"api_key": OPENAI_API_KEY,
},
# Add more TTS providers as needed:
# {"provider": "cartesia", "voice": "...", "api_key": "..."},
# {"provider": "deepgram", "api_key": DEEPGRAM_API_KEY},
],
# --- Standard pipeline config ---
welcome_greeting="Hello! Welcome to Acme Bank. How can I help you today?",
websocket_url="ws://localhost:9000/ws",
speaks_first="agent",
allow_interruptions=True,
semantic_vad={
"completed_turn_delay_ms": 250,
"incomplete_turn_delay_ms": 1200,
"min_interruption_duration_ms": 200,
},
)
print(f"Agent created: {agent['agent_uuid']}")
return agent
# --- Event handlers ---
app = VoiceApp()
@app.on("session.started")
def on_started(session, event: AgentSessionStarted):
print(f"Session started: {session.agent_session_id}")
session.update(events={"metrics_events": True})
@app.on("tool.called")
def on_tool_call(session, event: ToolCall):
print(f" Tool call: {event.name}({event.arguments})")
if event.name == "check_balance":
# Simulated balance lookup
session.send_tool_result(event.id, {
"balance": "$12,345.67",
"account_type": "checking",
"last_transaction": "Feb 14 -- $45.00 at Coffee Shop",
})
else:
session.send_tool_error(event.id, f"Unknown tool: {event.name}")
@app.on("turn.metrics")
def on_metrics(session, event: TurnMetrics):
"""Monitor which provider handled each turn.
When a fallback fires, stt_provider/llm_provider/tts_provider will
show the fallback provider name instead of the primary.
"""
print(
f" Metrics [turn {event.turn_number}]: "
f"perceived={event.user_perceived_ms}ms "
f"stt={event.stt_provider} "
f"llm={event.llm_provider} "
f"tts={event.tts_provider}"
)
@app.on("turn.completed")
def on_turn(session, event: TurnCompleted):
print(f" User: {event.user_text}")
print(f" Agent: {event.agent_text}")
@app.on("agent.speech_interrupted")
def on_interruption(session, event: Interruption):
print(f" Interrupted: '{event.interrupted_text or ''}'")
@app.on("session.ended")
def on_ended(session, event: AgentSessionEnded):
print(f"Session ended: {event.duration_seconds}s, {event.turn_count} turns")
if __name__ == "__main__":
asyncio.run(init_agent())
app.run(port=9000)