Update app.py
Browse files622 unmodified lines6236246256266276286296306316326336346353 unmodified lines63964064164264364464564611 unmodified lines658659660661662663664665666667668669622 unmodified linesprint(f"Error parsing relevance score: {e}")return 0.00def rephrase_for_search(query, model):rephrase_prompt = PromptTemplate(input_variables=["query"],template="""Rephrase the following conversational query into a concise, search-engine-friendly format.Remove any conversational elements and focus on the core information need.Provide only the rephrased query without any explanation.Conversational query: {query}3 unmodified lineschain = 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()return rephrased_query11 unmodified lineselse:database = Noneif web_search:# Rephrase the query for web searchrephrased_query = rephrase_for_search(question, model)print(f"Original query: {question}")print(f"Rephrased query: {rephrased_query}")search_results = google_search(rephrased_query)web_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]622 unmodified lines6236246256266276286296306316326336346356363 unmodified lines64064164264364464564664764864965065165265365465565665765865966066111 unmodified lines673674675676677678679680681682683684685686687688622 unmodified linesprint(f"Error parsing relevance score: {e}")return 0.00def rephrase_for_search(query, model):rephrase_prompt = PromptTemplate(input_variables=["query"],template="""Rephrase the following conversational query into a concise, search-engine-friendly format.Remove any conversational elements and focus on the core information need.Provide ONLY the rephrased query without any explanation or additional text.Conversational query: {query}3 unmodified lineschain = 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 simple keyword extractionkeywords = ' '.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'})return keywordsreturn rephrased_query11 unmodified lineselse:database = None# In the ask_question function:if web_search:original_query = questionrephrased_query = rephrase_for_search(original_query, model)print(f"Original query: {original_query}")print(f"Rephrased query: {rephrased_query}")if rephrased_query == original_query:print("Warning: Query was not rephrased. Using original query for search.")search_results = google_search(rephrased_query)web_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]
app.py+25-6