Update app.py
Browse files622 unmodified lines6236246256266276286296306316326326336346356363 unmodified lines64064164264264364364464564664764864965065165265365465565665765865966066111 unmodified lines673674675676677662663664678679680681682683684685686687688622 unmodified lines print(f"Error parsing relevance score: {e}") return 0.00
def 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. Provide ONLY the rephrased query without any explanation or additional text. Conversational query: {query} 3 unmodified lines chain = LLMChain(llm=model, prompt=rephrase_prompt) response = chain.run(query=query).strip() # Extract only the rephrased query, ignoring any explanations rephrased_query = response.split('\n')[0].strip() # Extract only the rephrased query using regex match = re.search(r'^(.*?)(?:\n|$)', response) if match: rephrased_query = match.group(1).strip() else: rephrased_query = response.strip() # Remove any "Rephrased query:" prefix if present rephrased_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 instructions if (rephrased_query.lower().startswith(("rephrase", "your task")) or len(rephrased_query.split()) > len(query.split()) * 2): # If it's not a proper rephrasing, use a simple keyword extraction 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'}) return keywords return rephrased_query
11 unmodified lines else: database = None
# In the ask_question function: if web_search: # Rephrase the query for web search rephrased_query = rephrase_for_search(question, model) print(f"Original query: {question}") original_query = question rephrased_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