Shreyas094 commited on
Commit
4c65765
·
verified ·
1 Parent(s): b5f8745

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -8
app.py CHANGED
@@ -630,23 +630,26 @@ def rephrase_for_search(query, model):
630
  template="""
631
  Your task is to rephrase the given conversational query into a concise, search-engine-friendly format.
632
  Remove any conversational elements and focus on the core information need.
 
633
 
634
  Conversational query: {query}
635
 
636
- Rephrased query: """
637
  )
638
 
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
- # Check if the rephrased query is actually a rephrasing and not the original prompt
646
- if rephrased_query.lower().startswith("rephrase") or len(rephrased_query) > len(query) * 2:
647
- # If it's not a proper rephrasing, use a simple keyword extraction
648
- keywords = ' '.join(query.lower().split())
649
- return keywords
 
 
650
 
651
  return rephrased_query
652
 
 
630
  template="""
631
  Your task is to rephrase the given 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 additional text or explanations.
634
 
635
  Conversational query: {query}
636
 
637
+ Rephrased query:"""
638
  )
639
 
640
  chain = LLMChain(llm=model, prompt=rephrase_prompt)
641
  response = chain.run(query=query).strip()
642
 
643
+ # Remove any potential "Rephrased query:" prefix
644
+ rephrased_query = response.replace("Rephrased query:", "").strip()
645
 
646
+ # If the rephrased query is too similar to the original, extract keywords
647
+ if rephrased_query.lower() == query.lower() or len(rephrased_query) > len(query) * 1.5:
648
+ # Simple keyword extraction: remove common words and punctuation
649
+ common_words = set(['the', 'a', 'an', 'in', 'on', 'at', 'to', 'for', 'of', 'with', 'by', 'from', 'up', 'about', 'into', 'over', 'after'])
650
+ keywords = [word.lower() for word in query.split() if word.lower() not in common_words]
651
+ keywords = [word for word in keywords if word.isalnum()]
652
+ return ' '.join(keywords)
653
 
654
  return rephrased_query
655