Spaces:
Runtime error
Runtime error
from twilio.rest import Client | |
import yaml | |
import json | |
import os | |
import sys | |
from pathlib import Path | |
from flask import Flask, request, redirect | |
from twilio.twiml.messaging_response import MessagingResponse | |
from langchain_openai import OpenAIEmbeddings | |
from langchain_community.vectorstores import Chroma | |
from helper import retrieve_relevant_context, generate_response_with_context | |
# Setting up paths | |
file = Path(__file__).resolve() | |
parent, root = file.parent, file.parents[1] | |
sys.path.append(str(root)) | |
print("str(root):", str(root)) | |
print("parent:", parent) | |
print("CWD:", os.getcwd()) | |
# Load relevant API Keys | |
file_path = parent / 'Config/API_KEYS.yml' | |
persist_directory = str(parent / 'vector_db/chroma_v01/') | |
print("file_path:", file_path) | |
print("persist_directory:", str(persist_directory)) | |
with open(file_path, 'r') as file: | |
api_keys = yaml.safe_load(file) | |
# Extract OpenAI key | |
openai_key = api_keys['OPEN_AI']['Key'] | |
os.environ["OPENAI_API_KEY"] = openai_key | |
# Extract Twilio credentials | |
account_sid = api_keys['TWILIO']['account_sid'] | |
auth_token = api_keys['TWILIO']['auth_token'] | |
twilio_whatsapp_number = api_keys['TWILIO']['whatsapp_number'] | |
print("====account_sid:=====", account_sid) | |
# Initialize the embeddings model | |
embedding_model = OpenAIEmbeddings() | |
# Load the Chroma vector store | |
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_model) | |
# Setup Twilio client | |
client = Client(account_sid, auth_token) | |
# Example to send a WhatsApp message | |
def send_whatsapp_message(to_number, message): | |
""" | |
Send a WhatsApp message using Twilio. | |
:param to_number: str, recipient's WhatsApp number in the format 'whatsapp:+1234567890' | |
:param message: str, message text to send | |
""" | |
from_number = f'whatsapp:{twilio_whatsapp_number}' | |
to_number = f'whatsapp:{to_number}' | |
message = client.messages.create( | |
body=message, | |
from_=from_number, | |
to=to_number | |
) | |
print(f"Message sent with SID: {message.sid}") | |
# Example usage | |
if __name__ == "__main__": | |
recipient_number = '+91-9108843322' # Replace with the recipient's WhatsApp number | |
text_message = 'Hello from Twilio WhatsApp!' | |
send_whatsapp_message(recipient_number, text_message) | |
# Flask app setup | |
print("Flask app is running") | |
app = Flask(__name__) | |
def incoming_sms(): | |
"""Send a dynamic reply to an incoming text message""" | |
# Get the message the user sent our Twilio number | |
body = request.values.get('Body', None) | |
print("body:", body) | |
incoming_msg = body.strip() | |
if not incoming_msg: | |
return str(MessagingResponse()) | |
# Generate response using the RAG-powered system | |
retrieved_texts = retrieve_relevant_context(vectordb, incoming_msg) | |
context = "\n".join(retrieved_texts) | |
response = generate_response_with_context(incoming_msg, context) | |
print("response:", response) | |
# Start our TwiML response | |
resp = MessagingResponse() | |
print("TwiML resp:", resp) | |
resp.message(response) | |
return str(resp) | |
if __name__ == "__main__": | |
app.run(host='0.0.0.0', port=8001, debug=True) | |