Arafath10 commited on
Commit
1556bea
·
verified ·
1 Parent(s): 0f5f90c

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +41 -18
main.py CHANGED
@@ -35,6 +35,10 @@ async def update_user_guide_data():
35
  user_guide_sync.update_user_guide()
36
  return "guide updated"
37
 
 
 
 
 
38
  @app.post("/whatsapp")
39
  async def reply_whatsapp(request: Request):
40
 
@@ -47,36 +51,55 @@ async def reply_whatsapp(request: Request):
47
  response = MessagingResponse()
48
  #msg.media(GOOD_BOY_URL)
49
  try:
50
- global query_engine,index
51
- storage_context = StorageContext.from_defaults(persist_dir="llama_index")
52
- index = load_index_from_storage(storage_context=storage_context)
53
-
54
- # Set the GPT model (e.g., GPT-4, GPT-3.5 Turbo)
55
- llm = OpenAI(model="gpt-4o-mini", api_key=os.environ["OPENAI_API_KEY"]) # Use "gpt-4" if desired
 
 
 
 
 
 
 
 
 
56
 
57
- query_engine = index.as_query_engine(llm=llm)
58
- print("loaded")
59
 
60
- gpt_response = query_engine.query(f"""
61
- 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).
62
- if not find the answer from provided data then honestly say 'please contact our helpdesk'
63
-
64
- user question : {user_query}""")
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
- 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 """
67
-
68
  print(str(gpt_response).lower())
69
- if "please contact our helpdesk" in str(gpt_response).lower() or "please contact" in str(gpt_response).lower():
70
  print("help desk option")
71
 
72
- openai.api_key = os.environ["OPENAI_API_KEY"]
73
  prompt = f"""
74
  system:
75
  you are parallax technologies chatbot design for answer the user question like a real human.
76
  contact details Email : [email protected] Youtube : https://www.youtube.com/channel/UCFkX9Fa-Qe6Qi4V5f0RcfSA Facebook : https://www.facebook.com/storemateinventory web link : https://storemate.lk
77
 
78
  only give single answer and don't give answer for general answers(this is CRM system for only pos system clients)
79
- note : don't give nay steps for solve the issues
80
  user:{user_query}
81
  """
82
  messages = [
 
35
  user_guide_sync.update_user_guide()
36
  return "guide updated"
37
 
38
+
39
+
40
+ index = None
41
+
42
  @app.post("/whatsapp")
43
  async def reply_whatsapp(request: Request):
44
 
 
51
  response = MessagingResponse()
52
  #msg.media(GOOD_BOY_URL)
53
  try:
54
+ openai.api_key = os.environ["OPENAI_API_KEY"]
55
+ global index # Use the global index variable
56
+ if index is None: # Check if the index is already loaded
57
+ from llama_index import StorageContext, load_index_from_storage # Import necessary modules
58
+ storage_context = StorageContext.from_defaults(persist_dir="llama_index")
59
+ index = load_index_from_storage(storage_context=storage_context)
60
+ print("Index loaded")
61
+ else:
62
+ print("Index already loaded")
63
+ # Set up a retriever to fetch similar documents directly without full query processing
64
+ retriever = index.as_retriever()
65
+ # Retrieve the top similar documents based on the user query
66
+ similar_docs = retriever.retrieve(user_query) # Adjust `top_k` as needed
67
+ # Prepare the context for LLM by concatenating the content of similar documents
68
+ context = "\n\n".join([doc.node.text for doc in similar_docs])
69
 
70
+ prompt = f"""
71
+ context : {context}
72
 
73
+ user query : {user_query}
74
+
75
+ Instructions:
76
+ - First, understand the user question carefully.
77
+ - If you find the correct answer from the provided data, respond with detailed steps (1, 2, ...) and always include a more details link.
78
+ - If the correct answer is not found in the provided data or proide the correct solution to user using data then output is only this : "contact our help desk". dont add extra anything
79
+ """
80
+ messages = [
81
+ {"role": "user", "content": prompt }
82
+ ]
83
+
84
+ # Make the API call
85
+ response = openai.chat.completions.create(
86
+ model="gpt-4o-mini",
87
+ messages=messages
88
+ )
89
+ gpt_response = response.choices[0].message.content
90
 
 
 
91
  print(str(gpt_response).lower())
92
+ if "contact our help desk" in str(gpt_response).lower() or "our help desk" in str(gpt_response).lower():
93
  print("help desk option")
94
 
95
+
96
  prompt = f"""
97
  system:
98
  you are parallax technologies chatbot design for answer the user question like a real human.
99
  contact details Email : [email protected] Youtube : https://www.youtube.com/channel/UCFkX9Fa-Qe6Qi4V5f0RcfSA Facebook : https://www.facebook.com/storemateinventory web link : https://storemate.lk
100
 
101
  only give single answer and don't give answer for general answers(this is CRM system for only pos system clients)
102
+ note : don't give any steps for solve the issues but give steps for sytem slow and performance related questions
103
  user:{user_query}
104
  """
105
  messages = [