File size: 3,172 Bytes
83f5f40
 
 
 
b0287e2
 
 
 
6176936
83f5f40
 
 
b0287e2
404305b
 
 
b0287e2
 
 
c7b1c7d
83f5f40
c7b1c7d
 
b0287e2
 
83f5f40
 
 
 
b0287e2
83f5f40
 
 
b0287e2
83f5f40
 
b0287e2
 
83f5f40
 
 
 
 
 
 
b0287e2
83f5f40
 
b0287e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83f5f40
b0287e2
 
83f5f40
 
 
 
 
 
 
b0287e2
83f5f40
 
 
 
 
 
 
 
 
b0287e2
83f5f40
 
 
b0287e2
83f5f40
 
 
 
472ca78
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
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__)

@app.route("/whatsapp", methods=['GET', 'POST'])
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)