Shreyas094 commited on
Commit
a09a99d
·
verified ·
1 Parent(s): cefe755

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -6
app.py CHANGED
@@ -623,13 +623,14 @@ def calculate_relevance_score(summary, model):
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
- Provide only the rephrased query without any explanation.
633
 
634
  Conversational query: {query}
635
 
@@ -639,8 +640,22 @@ def rephrase_for_search(query, model):
639
  chain = LLMChain(llm=model, prompt=rephrase_prompt)
640
  response = chain.run(query=query).strip()
641
 
642
- # Extract only the rephrased query, ignoring any explanations
643
- rephrased_query = response.split('\n')[0].strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
644
 
645
  return rephrased_query
646
 
@@ -658,12 +673,16 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, g
658
  else:
659
  database = None
660
 
 
661
  if web_search:
662
- # Rephrase the query for web search
663
- rephrased_query = rephrase_for_search(question, model)
664
- print(f"Original query: {question}")
665
  print(f"Rephrased query: {rephrased_query}")
666
 
 
 
 
667
  search_results = google_search(rephrased_query)
668
  web_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]
669
 
 
623
  print(f"Error parsing relevance score: {e}")
624
  return 0.00
625
 
626
+
627
  def rephrase_for_search(query, model):
628
  rephrase_prompt = PromptTemplate(
629
  input_variables=["query"],
630
  template="""
631
  Rephrase the following conversational query into a concise, search-engine-friendly format.
632
  Remove any conversational elements and focus on the core information need.
633
+ Provide ONLY the rephrased query without any explanation or additional text.
634
 
635
  Conversational query: {query}
636
 
 
640
  chain = LLMChain(llm=model, prompt=rephrase_prompt)
641
  response = chain.run(query=query).strip()
642
 
643
+ # Extract only the rephrased query using regex
644
+ match = re.search(r'^(.*?)(?:\n|$)', response)
645
+ if match:
646
+ rephrased_query = match.group(1).strip()
647
+ else:
648
+ rephrased_query = response.strip()
649
+
650
+ # Remove any "Rephrased query:" prefix if present
651
+ rephrased_query = re.sub(r'^Rephrased query:\s*', '', rephrased_query, flags=re.IGNORECASE)
652
+
653
+ # Check if the rephrased query is actually a rephrasing and not the original prompt or instructions
654
+ if (rephrased_query.lower().startswith(("rephrase", "your task")) or
655
+ len(rephrased_query.split()) > len(query.split()) * 2):
656
+ # If it's not a proper rephrasing, use a simple keyword extraction
657
+ keywords = ' '.join(word for word in query.lower().split() if word not in {'how', 'did', 'the', 'in', 'a', 'an', 'and', 'or', 'but', 'is', 'are', 'was', 'were'})
658
+ return keywords
659
 
660
  return rephrased_query
661
 
 
673
  else:
674
  database = None
675
 
676
+ # In the ask_question function:
677
  if web_search:
678
+ original_query = question
679
+ rephrased_query = rephrase_for_search(original_query, model)
680
+ print(f"Original query: {original_query}")
681
  print(f"Rephrased query: {rephrased_query}")
682
 
683
+ if rephrased_query == original_query:
684
+ print("Warning: Query was not rephrased. Using original query for search.")
685
+
686
  search_results = google_search(rephrased_query)
687
  web_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]
688