Update app.py
Browse files38 unmodified lines3940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929338 unmodified lineslogging.error(f"Error during DuckDuckGo search: {str(e)}")return []async def rephrase_query(query, history, model):logging.info("Rephrasing query based on history and model.")system_message = """You are an AI assistant tasked with analyzing and rephrasing user queries.Your goal is to determine whether the query is unique or related to the previous conversation, and then rephrase it appropriately for web search.The rephrased query should be succinct and optimized for search engines.If the query is unique (i.e., unrelated to the conversation), rephrase it to make it more specific and searchable.If the query is related to the previous conversation, incorporate relevant context from the conversation to enhance search relevance.Provide your analysis in the following format:<analysis>Your reasoning about whether the query is unique or related</analysis><rephrased_query>The rephrased query</rephrased_query>"""user_message = f"""Current query: {query}Previous conversation history:{history}Analyze the query and provide a rephrased version suitable for web search."""client = InferenceClient(model, token=huggingface_token)try:logging.info(f"Sending rephrase request to model {model}.")response = await asyncio.to_thread(client.text_generation,prompt=f"{system_message}\n\n{user_message}",max_new_tokens=150,temperature=0.2,)logging.info("Rephrase response received.")# Extract the rephrased query from the responseanalysis_start = response.find("<analysis>")analysis_end = response.find("</analysis>")rephrased_start = response.find("<rephrased_query>")rephrased_end = response.find("</rephrased_query>")if analysis_start != -1 and analysis_end != -1 and rephrased_start != -1 and rephrased_end != -1:analysis = response[analysis_start + 10:analysis_end].strip()rephrased_query = response[rephrased_start + 17:rephrased_end].strip()logging.info(f"Rephrased query: {rephrased_query}")return analysis, rephrased_queryelse:logging.error("Failed to parse the rephrase response.")return None, queryexcept Exception as e:logging.error(f"Error in rephrase_query: {str(e)}")return None, querydef create_web_search_vectors(search_results):logging.info(f"Creating web search vectors from {len(search_results)} search results.")38 unmodified lines3940414243444546474849505152535455565758596061626364656667686970717273747576777879808138 unmodified lineslogging.error(f"Error during DuckDuckGo search: {str(e)}")return []async def rephrase_query(query, context, model):# Log the original query for debugginglogging.info(f"Original query: {query}")prompt = f"""You are a highly intelligent conversational chatbot. Your task is to analyze the given context and new query, then decide whether to rephrase the query with or without incorporating the context. Follow these steps:1. Determine if the new query is a continuation of the previous conversation or an entirely new topic.2. If it's a continuation, rephrase the query by incorporating relevant information from the context to make it more specific and contextual.3. If it's a new topic, rephrase the query to make it more appropriate for a web search, focusing on clarity and accuracy without using the previous context.4. Provide ONLY the rephrased query without any additional explanation or reasoning.Context: {context}New query: {query}Rephrased query:"""client = InferenceClient(model, token=huggingface_token)try:response = await asyncio.to_thread(client.text_generation,prompt=prompt,max_new_tokens=100,temperature=0.2,)# The response should be the rephrased query as per your promptrephrased_query = response.strip()# Log the rephrased querylogging.info(f"Rephrased query: {rephrased_query}")return rephrased_queryexcept Exception as e:logging.error(f"Error in rephrase_query: {str(e)}")return query # Fallback to the original query if there's an errordef create_web_search_vectors(search_results):logging.info(f"Creating web search vectors from {len(search_results)} search results.")
app.py+28-40