Shreyas094 commited on
Commit
12bf58a
·
verified ·
1 Parent(s): 54be6d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -8
app.py CHANGED
@@ -203,7 +203,7 @@ def manage_conversation_history(question, answer, history, max_history=5):
203
  history.pop(0)
204
  return history
205
 
206
- def is_related_to_history(question, history, threshold=0.3):
207
  if not history:
208
  return False
209
  history_text = " ".join([f"{h['question']} {h['answer']}" for h in history])
@@ -632,7 +632,6 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, g
632
  model = get_model(temperature, top_p, repetition_penalty)
633
  embed = get_embeddings()
634
 
635
- # Check if the FAISS database exists
636
  if os.path.exists("faiss_database"):
637
  database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
638
  else:
@@ -686,12 +685,13 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, g
686
 
687
  history_str = "\n".join([f"Q: {item['question']}\nA: {item['answer']}" for item in conversation_history])
688
 
689
- if is_related_to_history(question, conversation_history):
690
- context_str = "No additional context needed. Please refer to the conversation history."
691
- else:
692
- retriever = database.as_retriever()
693
- relevant_docs = retriever.get_relevant_documents(question)
694
- context_str = "\n".join([doc.page_content for doc in relevant_docs])
 
695
 
696
  prompt_val = ChatPromptTemplate.from_template(prompt)
697
  formatted_prompt = prompt_val.format(history=history_str, context=context_str, question=question)
 
203
  history.pop(0)
204
  return history
205
 
206
+ def is_related_to_history(question, history, threshold=0.5): # Increased threshold from 0.3 to 0.5
207
  if not history:
208
  return False
209
  history_text = " ".join([f"{h['question']} {h['answer']}" for h in history])
 
632
  model = get_model(temperature, top_p, repetition_penalty)
633
  embed = get_embeddings()
634
 
 
635
  if os.path.exists("faiss_database"):
636
  database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
637
  else:
 
685
 
686
  history_str = "\n".join([f"Q: {item['question']}\nA: {item['answer']}" for item in conversation_history])
687
 
688
+ # Always retrieve relevant documents
689
+ retriever = database.as_retriever()
690
+ relevant_docs = retriever.get_relevant_documents(question)
691
+ doc_context = "\n".join([doc.page_content for doc in relevant_docs])
692
+
693
+ # Combine document context with conversation history
694
+ context_str = f"Document context:\n{doc_context}\n\nConversation history:\n{history_str}"
695
 
696
  prompt_val = ChatPromptTemplate.from_template(prompt)
697
  formatted_prompt = prompt_val.format(history=history_str, context=context_str, question=question)