Spaces:
Runtime error
Runtime error
Update app.py
Browse files26 unmodified lines27282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475763 unmodified lines80818239838485868 unmodified lines959697549899100101102596010310462636465666710510610710839 unmodified lines14814915011311411515115215315415515615711715815916016126 unmodified lines18818919015015119119219319419522 unmodified lines21821922018022122222326 unmodified lines results = ddgs.text(query, max_results=10) return results
async def rephrase_query(query, history, model): system_message = """You are an AI assistant tasked with analyzing and rephrasing user queries. Your goal is to determine if a query is unique or related to the previous conversation, and then rephrase it appropriately for web search. Keep the rephrased query succinct and in a web search query format.
If the query is unique, rephrase it to be more specific and searchable. If the query is related to the previous conversation, incorporate relevant context from the previous response.
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: response = await asyncio.to_thread( client.text_generation, prompt=f"{system_message}\n\n{user_message}", max_new_tokens=150, temperature=0.2, ) # Extract the rephrased query from the response analysis_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() return analysis, rephrased_query else: logging.error("Failed to parse the rephrase response") return None, query except Exception as e: logging.error(f"Error in rephrase_query: {str(e)}") return None, query
def create_web_search_vectors(search_results): embed = get_embeddings() documents = []3 unmodified lines documents.append(Document(page_content=content, metadata={"source": result['href']})) return FAISS.from_documents(documents, embed)
async def get_response_with_search(query, model, use_embeddings, conversation_history, num_calls=3, temperature=0.2):async def get_response_with_search(query, model, use_embeddings, num_calls=3, temperature=0.2): search_results = duckduckgo_search(query) if not search_results:8 unmodified lines else: context = "\n".join([f"{result['title']}\n{result['body']}\nSource: {result['href']}" for result in search_results])
system_message = """You are a world-class AI system, capable of complex reasoning and reflection. system_message = """ You are a world-class AI system, capable of complex reasoning and reflection. Reason through the query inside <thinking> tags, and then provide your final response inside <output> tags. Providing comprehensive and accurate information based on web search results is essential. Your goal is to synthesize the given context into a coherent and detailed response that directly addresses the user's query. Please ensure that your response is well-structured, factual, and cites sources where appropriate. If you detect that you made a mistake in your reasoning at any point, correct yourself inside <reflection> tags. Consider the conversation history when formulating your response to maintain context and coherence.""" If you detect that you made a mistake in your reasoning at any point, correct yourself inside <reflection> tags."""
conversation_context = "\n".join([f"Human: {msg['human']}\nAI: {msg['ai']}" for msg in conversation_history]) user_message = f"""Conversation history:{conversation_context}
Using the following context from web search results: user_message = f"""Using the following context from web search results:{context}
Write a detailed and complete research document that fulfills the following user request: '{query}'39 unmodified lines logging.info(f"Number of API Calls: {num_calls}") logging.info(f"Use Embeddings: {use_embeddings}")
# Convert Gradio history to a list of dictionaries conversation_history = [{"human": h, "ai": a} for h, a in history]
try: # Rephrase the query analysis, rephrased_query = await rephrase_query(message, history, model) if analysis: yield f"Query Analysis: {analysis}\n\nRephrased Query: {rephrased_query}\n\nSearching the web...\n\n" async for main_content, sources in get_response_with_search(message, model, use_embeddings, conversation_history, num_calls=num_calls, temperature=temperature): async for main_content, sources in get_response_with_search(rephrased_query, model, use_embeddings, num_calls=num_calls, temperature=temperature): response = f"{main_content}\n\n{sources}" yield response except asyncio.CancelledError:26 unmodified lines gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"), gr.Checkbox(label="Use Embeddings", value=False), ], title="AI-powered Conversational Web Search Assistant", description="Use web search to answer questions or generate summaries. The assistant remembers previous interactions.", title="AI-powered Web Search Assistant", description="Use web search to answer questions or generate summaries.", theme=gr.Theme.from_hub("allenai/gradio-theme"), css=css, examples=[22 unmodified lines 5. Check or uncheck the "Use Embeddings" box to toggle between using embeddings or direct text summarization. 6. Press Enter or click the submit button to get your answer. 7. Use the provided examples or ask your own questions. 8. The assistant will remember previous interactions and maintain context throughout the conversation. """)
return demoapp.py+57-17