Skip to content

Instantly share code, notes, and snippets.

@si3mshady
Created September 24, 2024 10:17
Show Gist options
  • Save si3mshady/41231c471372762e4e7d499b24f241a7 to your computer and use it in GitHub Desktop.
Save si3mshady/41231c471372762e4e7d499b24f241a7 to your computer and use it in GitHub Desktop.
This Python script sets up a bi-directional Voice AI Agent using Flask, Twilio, and OpenAI. The agent receives and processes voice input from callers in real-time, leveraging Twilio's telephony capabilities to manage the call flow and OpenAI's GPT-4 model to generate intelligent, context-aware responses. The code ensures seamless, interactive co…
from flask import Flask, request, jsonify
import os
from twilio.rest import Client
from twilio.twiml.voice_response import Gather, VoiceResponse
from dotenv import load_dotenv
import openai
# Load environment variables from .env file
load_dotenv()
# Initialize Flask app
app = Flask(__name__)
# Initialize OpenAI with the API key
openai_api_key = os.environ.get('OPENAI_APIKEY')
oai = openai.OpenAI(api_key=openai_api_key)
# Twilio credentials
account_sid = os.environ.get('ACCOUNT_SID')
auth_token = os.environ.get('AUTH_TOKEN')
# Phone numbers
from_number = "+88888888"
to_number = "+1223978706"
def handle_ai_response(text):
response = oai.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are an assistant that responds to questions and statements."},
{"role": "user", "content": f"Respond to the following text: {text}"}
]
)
return response.choices[0].message.content.strip()
@app.route('/handleResponse', methods=['GET', 'POST'])
def handle_response():
caller_speech = request.values.get('SpeechResult', '')
print(caller_speech)
llm_completion = handle_ai_response(caller_speech)
# Create a TwiML response
resp = VoiceResponse()
resp.say(llm_completion, voice='male')
resp.redirect('/gather')
return str(resp)
@app.route("/gather", methods=['GET', 'POST'])
def gather():
response = VoiceResponse()
gather = Gather(input='speech', action='/handleResponse', speechTimeout='auto')
# This is the caller's voice transcript
response.append(gather)
response.redirect('/gather')
return str(response)
@app.route("/answer", methods=['GET', 'POST'])
def answer():
"""Respond to incoming phone calls with a brief message."""
# Generate a conversation using OpenAI GPT-4
response = oai.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are an easy-to-talk-to assistant."},
{"role": "user", "content": "Strike up a conversation and talk to me about movies."}
]
)
generated_response = response.choices[0].message.content.strip()
print(f"Response Generation Node: {generated_response}")
# Create a TwiML response
resp = VoiceResponse()
resp.say(generated_response, voice='male')
resp.redirect('/gather')
# Make the call using Twilio API
client = Client(account_sid, auth_token)
call = client.calls.create(
twiml=str(resp),
to=to_number,
from_=from_number,
method='GET'
)
return jsonify({'message': 'Call initiated', 'call_sid': call.sid})
if __name__ == "__main__":
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment