This project creates a voice assistant that uses Twilio Voice and ConversationRelay, and the Open AI API that can engage in two-way conversations over a phone call.
This application allows users to call a Twilio number and interact with an AI assistant powered by OpenAI's GPT-4o-mini model. The assistant will respond to user queries in natural language.
- Python 3.10+
- A Twilio Account: Sign up for a free trial here.
- A Twilio Number with Voice Capabilities: Instructions to purchase a number.
- An Open AI Account and API Key: Visit Open AI's platform here for more information.
-
Clone this repository
-
Install the required dependencies:
pip install -r requirements.txt
-
Configure your environment variables in the .env file:
cp .env.example .env
OPENAI_API_KEY
: Your OpenAI API keyNGROK_URL
: Your ngrok URL (without https://)
-
Start ngrok to expose your local server:
ngrok http 8080
-
Update the
NGROK_URL
in your .env file with the new URL from ngrok -
Run the application:
python main.py
-
Configure your Twilio phone number to use the
/twiml
endpoint as the webhook for incoming calls (https://your-ngrok-url/twiml) -
Call your Twilio number to interact with the assistant
- When a user calls the Twilio number, Twilio requests the TwiML from
/twiml
- The TwiML instructs Twilio to connect to the WebSocket at
/ws
- Voice input from the user is sent to the server via WebSocket
- The server sends the input to OpenAI and gets a response
- The response is sent back to Twilio to be converted to speech
- The conversation continues until the call ends
main.py
: The main application file containing the FastAPI server, WebSocket handler, and OpenAI integrationrequirements.txt
: Python dependencies.env
: Environment variables for API keys and configuration