-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagent_fastapi.py
More file actions
82 lines (56 loc) · 2.06 KB
/
agent_fastapi.py
File metadata and controls
82 lines (56 loc) · 2.06 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
"""FastAPI integration for Voice AI Agent.
Mounts the VoiceApp WebSocket handler inside a FastAPI application,
allowing you to combine voice agent endpoints with REST routes.
Prerequisites:
pip install plivo_agent[all] fastapi uvicorn
Run:
uvicorn agent_fastapi:fastapi_app --port 9000
"""
import logging
from fastapi import FastAPI, WebSocket
from plivo_agent.agent import (
AgentSessionEnded,
AgentSessionStarted,
ToolCall,
VoiceApp,
)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# -- FastAPI app --
fastapi_app = FastAPI(title="Voice Agent")
# -- VoiceApp --
voice = VoiceApp()
@voice.on("agent_session.started")
async def on_session_started(session, event: AgentSessionStarted):
logger.info("Session started: %s", event.agent_session_id)
@voice.on("tool_call")
async def on_tool_call(session, event: ToolCall):
"""Handle tool calls -- async handlers work natively with FastAPI."""
logger.info("Tool call: %s(%s)", event.name, event.arguments)
if event.name == "check_weather":
city = event.arguments.get("city", "unknown")
# In a real app, call an async HTTP API here
result = {"city": city, "temp_f": 72, "condition": "sunny"}
session.send_tool_result(event.id, result)
elif event.name == "transfer_to_human":
session.transfer("+18005551234")
session.send_tool_result(event.id, {"status": "transferring"})
else:
session.send_tool_error(event.id, f"Unknown tool: {event.name}")
@voice.on("agent_session.ended")
async def on_session_ended(session, event: AgentSessionEnded):
logger.info(
"Session ended: duration=%ds turns=%s",
event.duration_seconds,
event.turn_count,
)
# -- WebSocket endpoint --
@fastapi_app.websocket("/ws")
async def ws_endpoint(websocket: WebSocket):
"""Accept the WebSocket connection and hand off to VoiceApp."""
await websocket.accept()
await voice.handle_fastapi(websocket)
# -- Health check --
@fastapi_app.get("/health")
async def health():
return {"status": "ok"}