Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, Request, HTTPException | |
from fastapi.responses import PlainTextResponse | |
from fastapi.middleware.cors import CORSMiddleware | |
from twilio.twiml.messaging_response import MessagingResponse | |
import os,openai | |
import google.generativeai as genai | |
from llama_index.llms import OpenAI | |
from llama_index import VectorStoreIndex, SimpleDirectoryReader | |
from llama_index.llms import OpenAI | |
from llama_index import StorageContext, load_index_from_storage | |
secret = os.environ["key"] | |
genai.configure(api_key=secret) | |
model = genai.GenerativeModel('gemini-1.5-flash') | |
import user_guide_sync | |
global index,query_engine | |
query_engine = index = None | |
#query_engine = (user_guide_sync.update_user_guide).as_query_engine() | |
app = FastAPI() | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=["*"], | |
allow_credentials=True, | |
allow_methods=["*"], | |
allow_headers=["*"], | |
) | |
async def update_user_guide_data(): | |
user_guide_sync.update_user_guide() | |
return "guide updated" | |
async def reply_whatsapp(request: Request): | |
form_data = await request.form() | |
num_media = int(form_data.get("NumMedia", 0)) | |
from_number = form_data.get("From") | |
message_body = form_data.get("Body") | |
user_query = message_body | |
response = MessagingResponse() | |
#msg.media(GOOD_BOY_URL) | |
try: | |
global query_engine,index | |
storage_context = StorageContext.from_defaults(persist_dir="llama_index") | |
index = load_index_from_storage(storage_context=storage_context) | |
# Set the GPT model (e.g., GPT-4, GPT-3.5 Turbo) | |
llm = OpenAI(model="gpt-4o-mini", api_key=os.environ["OPENAI_API_KEY"]) # Use "gpt-4" if desired | |
query_engine = index.as_query_engine(llm=llm) | |
print("loaded") | |
gpt_response = query_engine.query(f""" | |
if you find the very correct answer from provided data then only give the realistic(like real human) answer with steps and add the more details link and always add propper line breaks(\n). | |
if not find the answer from provided data then honestly say 'please contact our helpdesk' | |
user question : {user_query}""") | |
default = """Dear\n\nIf you have a specific question or need assistance, please feel free to submit a ticket, and our support team will be happy to help you \n\nSubmit a Ticket: \n\tEmail: [email protected]\nThank You """ | |
print(str(gpt_response).lower()) | |
if "please contact our helpdesk" in str(gpt_response).lower() or "please contact" in str(gpt_response).lower(): | |
print("help desk option") | |
openai.api_key = os.environ["OPENAI_API_KEY"] | |
prompt = f""" | |
system: | |
you are parallax technologies chatbot design for answer the user question like a real human. | |
contact details Email : [email protected] Youtube : https://www.youtube.com/channel/UCFkX9Fa-Qe6Qi4V5f0RcfSA Facebook : https://www.facebook.com/storemateinventory web link : https://storemate.lk | |
only give single answer and don't give answer for general answers(this is CRM system for only pos system clients) | |
note : don't give nay steps for solve the issues | |
user:{user_query} | |
""" | |
messages = [ | |
{"role": "system", "content": "you are parallax technologies chatbot design for answer the user question like a real human"}, | |
{"role": "user", "content": prompt} | |
] | |
gen_response = model.generate_content(prompt) | |
gpt_response = openai.chat.completions.create( | |
model="gpt-4o-mini", | |
messages=messages, | |
temperature=0, | |
) | |
response.message(str(gpt_response.choices[0].message.content)) | |
#response.message(gen_response.text) | |
#response.message(gen_response.text +"\n\n"+default) | |
return PlainTextResponse(str(response), media_type="application/xml") | |
response.message(str(gpt_response)) | |
#response.message("https://storemate.lk") | |
return PlainTextResponse(str(response), media_type="application/xml") | |
except Exception as e: | |
print(str(e)) | |
response.message("please ask again...!") | |
return PlainTextResponse(str(response), media_type="application/xml") | |
# Run the application (Make sure you have the necessary setup to run FastAPI) | |