-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.py
More file actions
223 lines (187 loc) · 8.28 KB
/
main.py
File metadata and controls
223 lines (187 loc) · 8.28 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
import asyncio
import base64
import json
import os
import websockets
from dotenv import load_dotenv
from fastapi import FastAPI, Request, WebSocket
from fastapi.responses import HTMLResponse
from fastapi.websockets import WebSocketDisconnect
from pydantic import BaseModel
from twilio.rest import Client
from twilio.twiml.voice_response import Connect, VoiceResponse
load_dotenv()
def load_prompt(file_name: str) -> str:
"""Load system prompt from file."""
dir_path = os.path.dirname(os.path.realpath(__file__))
prompt_path = os.path.join(dir_path, "prompts", f"{file_name}.txt")
try:
with open(prompt_path, "r", encoding="utf-8") as file:
return file.read().strip()
except FileNotFoundError:
print(f"Could not find file: {prompt_path}")
raise
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
TWILIO_ACCOUNT_SID = os.getenv("TWILIO_ACCOUNT_SID")
TWILIO_AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")
TWILIO_PHONE_NUMBER = os.getenv("TWILIO_PHONE_NUMBER")
NGROK_URL = os.getenv("NGROK_URL")
PORT = int(os.getenv("PORT", 8000))
SYSTEM_MESSAGE = load_prompt("system_prompt")
VOICE = "echo"
LOG_EVENT_TYPES = [
"response.content.done",
"rate_limits.updated",
"response.done",
"input_audio_buffer.committed",
"input_audio_buffer.speech_stopped",
"input_audio_buffer.speech_started",
"session.created",
]
app = FastAPI()
if not OPENAI_API_KEY:
raise ValueError("Missing the OpenAI API key. Please set it in the .env file.")
if not TWILIO_ACCOUNT_SID or not TWILIO_AUTH_TOKEN or not TWILIO_PHONE_NUMBER:
raise ValueError("Missing Twilio configuration. Please set it in the .env file.")
@app.get("/")
async def health_check():
"""Health check endpoint."""
return {"status": "healthy", "message": "AI Voice Assistant is running!"}
class CallRequest(BaseModel):
to_phone_number: str
@app.post("/make-call")
async def make_call(request: CallRequest):
"""Initiate an outbound call to the specified phone number."""
if not request.to_phone_number:
return {"error": "Phone number is required"}
try:
client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
call = client.calls.create(
url=f"{NGROK_URL}/outgoing-call",
to=request.to_phone_number,
from_=TWILIO_PHONE_NUMBER,
record=True,
recording_status_callback=f"{NGROK_URL}/recording-status",
recording_status_callback_method="POST",
)
print(f"Call initiated with SID: {call.sid}")
return {"call_sid": call.sid, "status": "success"}
except Exception as e:
print(f"Error initiating call: {e}")
return {"error": str(e), "status": "failed"}
@app.api_route("/outgoing-call", methods=["GET", "POST"])
async def handle_outgoing_call(request: Request):
"""Handle outgoing call webhook and return TwiML response."""
response = VoiceResponse()
response.say("Hello! You are now connected to an AI assistant.")
response.pause(length=1)
response.say("Please wait while we establish the connection.")
connect = Connect()
connect.stream(url=f"wss://{request.url.hostname}/media-stream")
response.append(connect)
return HTMLResponse(content=str(response), media_type="application/xml")
@app.api_route("/recording-status", methods=["POST"])
async def handle_recording_status(request: Request):
"""Handle recording status updates from Twilio."""
form_data = await request.form()
recording_status = form_data.get("RecordingStatus")
recording_sid = form_data.get("RecordingSid")
call_sid = form_data.get("CallSid")
recording_url = form_data.get("RecordingUrl")
recording_duration = form_data.get("RecordingDuration")
print(f"Recording status update:")
print(f" Call SID: {call_sid}")
print(f" Recording SID: {recording_sid}")
print(f" Status: {recording_status}")
if recording_status == "completed":
print(f" Recording URL: {recording_url}")
print(f" Duration: {recording_duration} seconds")
return {"status": "received"}
@app.websocket("/media-stream")
async def handle_media_stream(websocket: WebSocket):
"""Handle WebSocket connections between Twilio and OpenAI."""
print("Client connected")
await websocket.accept()
async with websockets.connect(
"wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01",
extra_headers={
"Authorization": f"Bearer {OPENAI_API_KEY}",
"OpenAI-Beta": "realtime=v1",
},
) as openai_ws:
await send_session_update(openai_ws)
stream_sid = None
session_id = None
async def receive_from_twilio():
"""Receive audio data from Twilio and send it to the OpenAI Realtime API."""
nonlocal stream_sid
try:
async for message in websocket.iter_text():
data = json.loads(message)
if data["event"] == "media" and openai_ws.open:
audio_append = {
"type": "input_audio_buffer.append",
"audio": data["media"]["payload"],
}
await openai_ws.send(json.dumps(audio_append))
elif data["event"] == "start":
stream_sid = data["start"]["streamSid"]
print(f"Incoming stream has started {stream_sid}")
except WebSocketDisconnect:
print("Client disconnected.")
if openai_ws.open:
await openai_ws.close()
async def send_to_twilio():
"""Receive events from the OpenAI Realtime API, send audio back to Twilio."""
nonlocal stream_sid, session_id
try:
async for openai_message in openai_ws:
response = json.loads(openai_message)
if response["type"] in LOG_EVENT_TYPES:
print(f"Received event: {response['type']}", response)
if response["type"] == "session.created":
session_id = response["session"]["id"]
if response["type"] == "session.updated":
print("Session updated successfully:", response)
if response["type"] == "response.audio.delta" and response.get(
"delta"
):
try:
audio_payload = base64.b64encode(
base64.b64decode(response["delta"])
).decode("utf-8")
audio_delta = {
"event": "media",
"streamSid": stream_sid,
"media": {"payload": audio_payload},
}
await websocket.send_json(audio_delta)
except Exception as e:
print(f"Error processing audio data: {e}")
if response["type"] == "conversation.item.created":
print(f"conversation.item.created event: {response}")
if response["type"] == "input_audio_buffer.speech_started":
print("Speech started, interrupting AI response")
await websocket.send_json(
{"streamSid": stream_sid, "event": "clear"}
)
interrupt_message = {"type": "response.cancel"}
await openai_ws.send(json.dumps(interrupt_message))
except Exception as e:
print(f"Error in send_to_twilio: {e}")
await asyncio.gather(receive_from_twilio(), send_to_twilio())
async def send_session_update(openai_ws):
"""Configure OpenAI session with audio settings and instructions."""
session_update = {
"type": "session.update",
"session": {
"input_audio_format": "g711_ulaw",
"output_audio_format": "g711_ulaw",
"voice": VOICE,
"instructions": SYSTEM_MESSAGE,
"modalities": ["text", "audio"],
"temperature": 0.2,
},
}
print("Configuring OpenAI session")
await openai_ws.send(json.dumps(session_update))