Wajahat698 commited on
Commit
9f32ae4
·
verified ·
1 Parent(s): d3e2c19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -22
app.py CHANGED
@@ -1074,31 +1074,30 @@ def google_search(query):
1074
  return ["Error occurred during Google search"]
1075
 
1076
 
 
1077
  def rag_response(query):
 
 
 
1078
  try:
1079
- # Check if the query references uploaded documents
1080
  if "using uploaded document" in query.lower():
1081
- document_response = handle_document_query(query) # Use your existing `handle_document_query` function
1082
- if document_response:
1083
- return document_response
1084
- else:
1085
-
1086
- # Proceed with the existing knowledge base logic if no uploaded document context is specified
1087
- retrieved_docs = search_knowledge_base(query)
1088
- context = "\n".join(doc.page_content for doc in retrieved_docs)
1089
-
1090
- # Prepare the prompt with the retrieved context
1091
- prompt = f"Context:\n{context}\n\nQuestion: {query}\nAnswer:"
1092
- llm = ChatOpenAI(model="gpt-4o", temperature=0.1, api_key=openai_api_key)
1093
- response = llm.invoke(prompt)
1094
-
1095
- # Replace terms in the final output as per your restrictions
1096
- response_content = response.content
1097
-
1098
- return response_content
1099
  except Exception as e:
1100
  logger.error(f"Error generating RAG response: {e}")
1101
- return "Error occurred during RAG response generation"
1102
 
1103
 
1104
  # Define tools
@@ -1840,6 +1839,7 @@ def get_document_content(doc_name=None):
1840
  else:
1841
  return None, "The most recently uploaded document does not contain any content."
1842
 
 
1843
  def handle_document_query(query):
1844
  """
1845
  Handle queries related to uploaded documents for response generation.
@@ -1848,6 +1848,9 @@ def handle_document_query(query):
1848
  doc_name_match = re.search(r"[\"']?([^\"']+\.(pdf|docx|doc|txt))[\"']?", query, re.IGNORECASE)
1849
  doc_name = doc_name_match.group(1) if doc_name_match else None
1850
 
 
 
 
1851
  # Fetch document content
1852
  doc_content, error = get_document_content(doc_name)
1853
  if error:
@@ -1856,13 +1859,13 @@ def handle_document_query(query):
1856
  # Generate AI response with document context
1857
  full_prompt = f"Document Content:\n{doc_content}\n\nUser Query: {query}\n\nResponse:"
1858
  try:
1859
- llm = ChatOpenAI(model="gpt-4o", temperature=0.5, api_key=openai_api_key)
1860
  response = llm.invoke(full_prompt)
1861
  return response.content
1862
  except Exception as e:
 
1863
  return f"Error generating response using the document: {e}"
1864
 
1865
-
1866
 
1867
 
1868
 
 
1074
  return ["Error occurred during Google search"]
1075
 
1076
 
1077
+
1078
  def rag_response(query):
1079
+ """
1080
+ Handle RAG-based queries when uploaded document context is not mentioned.
1081
+ """
1082
  try:
 
1083
  if "using uploaded document" in query.lower():
1084
+ # Handle document-specific queries
1085
+ return handle_document_query(query)
1086
+
1087
+ # Proceed with the existing knowledge base logic
1088
+ retrieved_docs = search_knowledge_base(query) # Replace with actual KB search logic
1089
+ if not retrieved_docs:
1090
+ return "No relevant information found in the knowledge base."
1091
+
1092
+ context = "\n".join(doc.page_content for doc in retrieved_docs)
1093
+ prompt = f"Context:\n{context}\n\nQuestion: {query}\nAnswer:"
1094
+ llm = ChatOpenAI(model="gpt-4o", temperature=0.3, api_key=openai_api_key)
1095
+ response = llm.invoke(prompt)
1096
+
1097
+ return response.content
 
 
 
 
1098
  except Exception as e:
1099
  logger.error(f"Error generating RAG response: {e}")
1100
+ return "Error occurred during RAG response generation."
1101
 
1102
 
1103
  # Define tools
 
1839
  else:
1840
  return None, "The most recently uploaded document does not contain any content."
1841
 
1842
+
1843
  def handle_document_query(query):
1844
  """
1845
  Handle queries related to uploaded documents for response generation.
 
1848
  doc_name_match = re.search(r"[\"']?([^\"']+\.(pdf|docx|doc|txt))[\"']?", query, re.IGNORECASE)
1849
  doc_name = doc_name_match.group(1) if doc_name_match else None
1850
 
1851
+ if not doc_name:
1852
+ return "No document mentioned in the query."
1853
+
1854
  # Fetch document content
1855
  doc_content, error = get_document_content(doc_name)
1856
  if error:
 
1859
  # Generate AI response with document context
1860
  full_prompt = f"Document Content:\n{doc_content}\n\nUser Query: {query}\n\nResponse:"
1861
  try:
1862
+ llm = ChatOpenAI(model="gpt-4", temperature=0.5, api_key=openai_api_key)
1863
  response = llm.invoke(full_prompt)
1864
  return response.content
1865
  except Exception as e:
1866
+ logger.error(f"Error generating response using the document: {e}")
1867
  return f"Error generating response using the document: {e}"
1868
 
 
1869
 
1870
 
1871