Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codespace opulent garbanzo g4549wgjpqr4cwxpx #11

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added llamaindex/10k/rapa_fischer_2015.pdf
Binary file not shown.
Binary file added llamaindex/10k/rapa_sorge_2014.pdf
Binary file not shown.
489 changes: 411 additions & 78 deletions llamaindex/openai_rag_agent_w_evals.ipynb

Large diffs are not rendered by default.

431 changes: 431 additions & 0 deletions llamaindex/openai_rag_agent_w_evals_twilio.ipynb

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions ngrok_kill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pyngrok import ngrok

# List all running tunnels
tunnels = ngrok.get_tunnels()
print("Running tunnels:")
for tunnel in tunnels:
print(f"- {tunnel.public_url} -> {tunnel.config['addr']}")

# Kill all tunnels
ngrok.kill()
print("\nAll ngrok tunnels killed!")
30 changes: 30 additions & 0 deletions python_ngrok.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from pyngrok import ngrok
import logging

# Set up logging to see what's happening
logging.basicConfig(level=logging.INFO)

try:
# Start ngrok on port 5005
public_url = ngrok.connect(5005)

# Display connection info
print(f"\nNgrok is running!")
print(f"Public URL: {public_url}")
print(f"Local port: 5005")

# Get all active tunnels (optional, for verification)
tunnels = ngrok.get_tunnels()
print("\nActive tunnels:")
for tunnel in tunnels:
print(f"- {tunnel.public_url} -> {tunnel.config['addr']}")

except Exception as e:
print(f"Error starting ngrok: {str(e)}")
print("\nTroubleshooting tips:")
print("1. Verify NGROK_AUTH_TOKEN is in your .env file")
print("2. Check if port 5005 is available")
print("3. Ensure ngrok is authenticated")

# To stop ngrok when you're done, run this:
ngrok.disconnect(public_url)
126 changes: 126 additions & 0 deletions stuff_with_twilio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import logging
import os

from dotenv import load_dotenv
from flask import Flask, request
from openai import OpenAI

from twilio.rest import Client
from twilio.twiml.voice_response import VoiceResponse, Gather
from flask_socketio import SocketIO, emit

from arize import process_call

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Load environment variables
load_dotenv()

# Initialize Flask app
app = Flask(__name__)

# Initialize Twilio client
twilio_client = Client(os.getenv("TWILIO_ACCOUNT_SID"), os.getenv("TWILIO_AUTH_TOKEN"))

# Initialize OpenAI client
openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

socketio = SocketIO(app)


@app.route("/", methods=["GET"])
def home():
"""Test route to verify server is running"""
return "Twilio Flask app is running!"


@app.route("/answer", methods=["POST"])
def answer_call():
"""Handle incoming phone calls"""
logger.info("Received incoming call")
response = VoiceResponse()

# Add a greeting and wait for the caller to speak
gather = Gather(
input="speech", action="/process_speech", timeout=3, speech_timeout="auto"
)
gather.say("Nine-one-one what is your emergency.")
response.append(gather)

# If the user doesn't say anything, try again
response.redirect("/answer")

return str(response)


# @socketio.on("send_message")
# def handle_source(json_data):
# text = json_data["message"].encode("ascii", "ignore")
# print("Server Says: " + text)


# Route for a webpage that displays call results
@app.route("/call_results", methods=["GET"])
def call_results():
"""Display the results of the call processing"""
# Get ui.html from the current directory
with open("ui.html", "r") as f:
return f.read()


# Debug route to test the websocket
@app.route("/test_socket", methods=["GET"])
def test_socket():
"""Test the websocket connection"""
socketio.emit(
"send_message",
{
"priority": "GREEN",
"department": "POLICEDEPT",
"summary": "This is a test message",
"confidence": 95,
},
)
return {
"priority": "GREEN",
"department": "POLICEDEPT",
"summary": "This is a test message",
"confidence": 95,
}


@app.route("/process_speech", methods=["POST"])
def process_speech():
"""Process the speech input from the caller"""
logger.info("Processing speech from call")
# Get the speech input
speech_result = request.values.get("SpeechResult", "")
logger.info(f"Received speech: {speech_result}")

if speech_result:
# Process the text using the process call function
result = process_call(speech_result)
logger.info(f"Processing result: {result}")

# Create a response
response = VoiceResponse()
response.say(f"I heard: {speech_result}")
socketio.emit("send_message", result)
response.say(f"Processing result: {result}")
else:
# If no speech was detected
logger.warning("No speech detected")
response = VoiceResponse()
response.say("I'm sorry, I didn't catch that. Please try again.")
response.redirect("/answer")

return str(response)


if __name__ == "__main__":
# Run the Flask app on port 5000
logger.info("Starting Flask application...")
app.run(debug=True, port=5000)
socketio.run(app, debug=True)
96 changes: 96 additions & 0 deletions twilio_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from flask import Flask, request
from twilio.twiml.voice_response import VoiceResponse
from twilio.rest import Client
from os import environ
from dotenv import load_dotenv

load_dotenv()

app = Flask(__name__)

# Your Twilio credentials
TWILIO_ACCOUNT_SID = environ["TWILIO_ACCOUNT_SID"]
TWILIO_AUTH_TOKEN = environ["TWILIO_AUTH_TOKEN"]

# Initialize Twilio client
client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)

@app.route("/answer", methods=['POST'])
def answer_call():
"""Handle incoming calls and generate TwiML response"""
# Create TwiML response object
response = VoiceResponse()

# Get the spoken text from the call
spoken_text = request.values.get('SpeechResult', '')

if not spoken_text:
# If no speech detected, prompt the user to speak
gather = response.gather(
input='speech',
action='/process_speech',
timeout=3,
language='en-US'
)
gather.say('Please ask your question about Rapamycin.')
else:
# Forward to processing endpoint
response.redirect('/process_speech')

return str(response)

@app.route("/process_speech", methods=['POST'])
def process_speech():
"""Process the speech and get response from agent"""
# Create TwiML response object
response = VoiceResponse()

# Get the spoken text
spoken_text = request.values.get('SpeechResult', '')

try:
# Get response from your agent
agent_response = agent.chat(spoken_text)

# Convert agent response to string and clean up if needed
tts_text = str(agent_response)

# Have Twilio speak the response
response.say(tts_text, voice='alice')

except Exception as e:
# Handle any errors
response.say("I'm sorry, there was an error processing your request.")
print(f"Error: {str(e)}")

return str(response)

@app.route("/call", methods=['POST'])
def make_call():
"""Initiate a call to a specified number"""
to_number = request.form['to']

try:
call = client.calls.create(
to=to_number,
from_='your_twilio_phone_number',
url=request.url_root + 'answer'
)
return f"Call initiated with SID: {call.sid}"
except Exception as e:
return f"Error making call: {str(e)}"

if __name__ == "__main__":
try:
# Try different ports if 5000 is in use
port = 5000
while port < 5010: # Try up to port 5009
try:
app.run(debug=True, port=port)
break
except OSError:
port += 1
if port == 5010:
print("Could not find an available port")
except Exception as e:
print(f"Error starting the server: {str(e)}")
45 changes: 45 additions & 0 deletions voice_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from flask import Flask
from twilio.twiml.voice_response import VoiceResponse
from pyngrok import ngrok
import os
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# Create Flask app
app = Flask(__name__)

# This is your agent response - you can change this variable anytime
agent_response = "Hello! I am your virtual agent. I can say whatever you put in this string variable!"

@app.route("/")
def home():
"""Display the agent response on web page"""
return agent_response

@app.route("/voice", methods=['GET', 'POST'])
def voice():
"""Handle incoming calls and read the agent response"""
# Create Twilio response
resp = VoiceResponse()

# Have Twilio read the agent response
resp.say(agent_response, voice='alice')

return str(resp)

if __name__ == '__main__':
# Start ngrok
ngrok_tunnel = ngrok.connect(5006)
print('\nServer Links:')
print(f'Ngrok URL: {ngrok_tunnel.public_url}')
print(f'View response at: {ngrok_tunnel.public_url}')
print(f'Twilio webhook URL: {ngrok_tunnel.public_url}/voice')
print('\nIMPORTANT: Set your Twilio voice webhook to the URL above')
print('\nTo test:')
print('1. Visit the Ngrok URL to see the text')
print('2. Call your Twilio number to hear it')

# Run Flask app
app.run(port=5006)
34 changes: 34 additions & 0 deletions voice_amy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from flask import Flask
from twilio.twiml.voice_response import VoiceResponse
from pyngrok import ngrok

app = Flask(__name__)

# This is the message that will be displayed and read
agent_message = "This is your agent message. Change this text to whatever you want Twilio to say!"

@app.route("/")
def home():
"""Display the current message on the webpage"""
return agent_message

@app.route("/voice", methods=['GET', 'POST'])
def voice():
"""Respond to incoming phone calls by reading the agent message."""
# Create TwiML response
resp = VoiceResponse()

# Read the agent message aloud
resp.say(agent_message, voice='Polly.Amy')

return str(resp)

if __name__ == "__main__":
# Start ngrok on port 5005
public_url = ngrok.connect(5005)
print("\nServer is running!")
print(f"\nView your message at: {public_url}")
print(f"Set Twilio webhook to: {public_url}/voice")

# Run Flask app
app.run(debug=True, port=5005)