Shreyas094 commited on
Commit
ac147b1
1 Parent(s): 12bf58a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -4
app.py CHANGED
@@ -622,7 +622,23 @@ def calculate_relevance_score(summary, model):
622
  except ValueError as e:
623
  print(f"Error parsing relevance score: {e}")
624
  return 0.00
 
 
 
 
 
 
 
 
 
 
 
 
625
 
 
 
 
 
626
  def ask_question(question, temperature, top_p, repetition_penalty, web_search, google_news_rss):
627
  global conversation_history
628
 
@@ -632,13 +648,19 @@ 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
  if os.path.exists("faiss_database"):
636
  database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
637
  else:
638
  database = None
639
 
640
  if web_search:
641
- search_results = google_search(question)
 
 
 
 
 
642
  web_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]
643
 
644
  if database is None:
@@ -654,12 +676,13 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, g
654
  Answer the question based on the following web search results:
655
  Web Search Results:
656
  {context}
657
- Current Question: {question}
 
658
  If the web search results don't contain relevant information, state that the information is not available in the search results.
659
- Provide a concise and direct answer to the question without mentioning the web search or these instructions:
660
  """
661
  prompt_val = ChatPromptTemplate.from_template(prompt_template)
662
- formatted_prompt = prompt_val.format(context=context_str, question=question)
663
 
664
  elif google_news_rss:
665
  if database is None:
 
622
  except ValueError as e:
623
  print(f"Error parsing relevance score: {e}")
624
  return 0.00
625
+
626
+ def rephrase_for_search(query, model):
627
+ rephrase_prompt = PromptTemplate(
628
+ input_variables=["query"],
629
+ template="""
630
+ Rephrase the following conversational query into a concise, search-engine-friendly format.
631
+ Remove any conversational elements and focus on the core information need.
632
+
633
+ Conversational query: {query}
634
+
635
+ Search query:"""
636
+ )
637
 
638
+ chain = LLMChain(llm=model, prompt=rephrase_prompt)
639
+ rephrased_query = chain.run(query=query).strip()
640
+ return rephrased_query
641
+
642
  def ask_question(question, temperature, top_p, repetition_penalty, web_search, google_news_rss):
643
  global conversation_history
644
 
 
648
  model = get_model(temperature, top_p, repetition_penalty)
649
  embed = get_embeddings()
650
 
651
+ # Check if the FAISS database exists
652
  if os.path.exists("faiss_database"):
653
  database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
654
  else:
655
  database = None
656
 
657
  if web_search:
658
+ # Rephrase the query for web search
659
+ rephrased_query = rephrase_for_search(question, model)
660
+ print(f"Original query: {question}")
661
+ print(f"Rephrased query: {rephrased_query}")
662
+
663
+ search_results = google_search(rephrased_query)
664
  web_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]
665
 
666
  if database is None:
 
676
  Answer the question based on the following web search results:
677
  Web Search Results:
678
  {context}
679
+ Original Question: {original_question}
680
+ Rephrased Search Query: {rephrased_query}
681
  If the web search results don't contain relevant information, state that the information is not available in the search results.
682
+ Provide a concise and direct answer to the original question without mentioning the web search or these instructions:
683
  """
684
  prompt_val = ChatPromptTemplate.from_template(prompt_template)
685
+ formatted_prompt = prompt_val.format(context=context_str, original_question=question, rephrased_query=rephrased_query)
686
 
687
  elif google_news_rss:
688
  if database is None: