Update app.py
Browse files
app.py
CHANGED
|
@@ -628,41 +628,24 @@ def rephrase_for_search(query, model):
|
|
| 628 |
rephrase_prompt = PromptTemplate(
|
| 629 |
input_variables=["query"],
|
| 630 |
template="""
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 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 |
-
# Extract only the rephrased query
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 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 more sophisticated keyword extraction
|
| 657 |
-
keywords = query.lower()
|
| 658 |
-
# Remove common stop words and question words
|
| 659 |
-
stop_words = {'how', 'did', 'the', 'in', 'a', 'an', 'and', 'or', 'but', 'is', 'are', 'was', 'were', 'perform'}
|
| 660 |
-
keywords = ' '.join(word for word in keywords.split() if word not in stop_words)
|
| 661 |
-
# Ensure important terms are included
|
| 662 |
-
if 'kkr' not in keywords:
|
| 663 |
-
keywords = 'kkr ' + keywords
|
| 664 |
-
if 'q1 2024' not in keywords:
|
| 665 |
-
keywords += ' q1 2024'
|
| 666 |
return keywords
|
| 667 |
|
| 668 |
return rephrased_query
|
|
|
|
| 628 |
rephrase_prompt = PromptTemplate(
|
| 629 |
input_variables=["query"],
|
| 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
|