Update app.py
Browse files627 unmodified lines628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668627 unmodified linesrephrase_prompt = PromptTemplate(input_variables=["query"],template="""Rephrase the following conversational query into a concise, search-engine-friendly format.Maintain key terms and add relevant context if necessary.Provide ONLY the rephrased query without any explanation or additional text.Conversational query: {query}Rephrased query:""")chain = LLMChain(llm=model, prompt=rephrase_prompt)response = chain.run(query=query).strip()# Extract only the rephrased query using regexmatch = re.search(r'^(.*?)(?:\n|$)', response)if match:rephrased_query = match.group(1).strip()else:rephrased_query = response.strip()# Remove any "Rephrased query:" prefix if presentrephrased_query = re.sub(r'^Rephrased query:\s*', '', rephrased_query, flags=re.IGNORECASE)# Check if the rephrased query is actually a rephrasing and not the original prompt or instructionsif (rephrased_query.lower().startswith(("rephrase", "your task")) orlen(rephrased_query.split()) > len(query.split()) * 2):# If it's not a proper rephrasing, use a more sophisticated keyword extractionkeywords = query.lower()# Remove common stop words and question wordsstop_words = {'how', 'did', 'the', 'in', 'a', 'an', 'and', 'or', 'but', 'is', 'are', 'was', 'were', 'perform'}keywords = ' '.join(word for word in keywords.split() if word not in stop_words)# Ensure important terms are includedif 'kkr' not in keywords:keywords = 'kkr ' + keywordsif 'q1 2024' not in keywords:keywords += ' q1 2024'return keywordsreturn rephrased_query627 unmodified lines628629630631632633634635636637638639640641642643644645646647648649650651627 unmodified linesrephrase_prompt = PromptTemplate(input_variables=["query"],template="""Your task is to rephrase the given conversational query into a concise, search-engine-friendly format.Remove any conversational elements and focus on the core information need.Conversational query: {query}Rephrased query: """)chain = LLMChain(llm=model, prompt=rephrase_prompt)response = chain.run(query=query).strip()# Extract only the rephrased query, ignoring any explanationsrephrased_query = response.split('\n')[0].strip()# Check if the rephrased query is actually a rephrasing and not the original promptif rephrased_query.lower().startswith("rephrase") or len(rephrased_query) > len(query) * 2:# If it's not a proper rephrasing, use a simple keyword extractionkeywords = ' '.join(query.lower().split())return keywordsreturn rephrased_query
app.py+12-29