File size: 2,471 Bytes
83f5f40
 
 
 
 
 
 
 
 
 
 
404305b
 
 
 
 
 
 
 
535393a
83f5f40
535393a
83f5f40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85cc6c7
83f5f40
b996f5c
83f5f40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from twilio.rest import Client
import yaml
import json
import os

import yaml
import json
from langchain.embeddings import OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
from helper import retrieve_relevant_context, generate_response_with_context

import sys
from pathlib import Path
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 = '../Config/API_KEYS.yml'

with open(file_path, 'r') as file:
    api_keys = yaml.safe_load(file)


# Extract openai username and key
openai_key = api_keys['OPEN_AI']['Key']

os.environ["OPENAI_API_KEY"] = openai_key



# Extract openai username and key
account_sid = api_keys['TWILIO']['account_sid']
auth_token = api_keys['TWILIO']['auth_token']

account_sid = account_sid
auth_token = auth_token

print("====account_sid:=====",account_sid)
# Define the persist directory
persist_directory = './vector_db/chroma_v01'

# Initialize the embeddings model
embedding_model = OpenAIEmbeddings()

### Vectorstores
from langchain_community.vectorstores import Chroma

# Load the Chroma vector store
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_model)


#setup Twilio client
client = Client(account_sid, auth_token)


from flask import Flask, request, redirect
from twilio.twiml.messaging_response import MessagingResponse
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)
    
    ##### Process incoming text #############
    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)
    ##### Process incoming text Done #############
    
    
    # Start our TwiML response
    resp = MessagingResponse()
    print("TwiML resp :", resp)
    resp.message(response)
    return str(resp)

if __name__ == "__main__":
    app.run(port=5000, debug=True)