Spaces:
Build error
Build error
Update app.py
Browse files26 unmodified lines2728293031323 unmodified lines363738394041428 unmodified lines515253545556575859606162636465666768697039 unmodified lines11011111211311411511611711811912026 unmodified lines14714814915015115215315422 unmodified lines17717817918018118218326 unmodified linesresults = ddgs.text(query, max_results=10)return resultsdef create_web_search_vectors(search_results):embed = get_embeddings()documents = []3 unmodified linesdocuments.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):search_results = duckduckgo_search(query)if not search_results:8 unmodified lineselse: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.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."""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:{context}Write a detailed and complete research document that fulfills the following user request: '{query}'39 unmodified lineslogging.info(f"Number of API Calls: {num_calls}")logging.info(f"Use Embeddings: {use_embeddings}")# Convert Gradio history to a list of dictionariesconversation_history = [{"human": h, "ai": a} for h, a in history]try:async for main_content, sources in get_response_with_search(message, model, use_embeddings, conversation_history, num_calls=num_calls, temperature=temperature):response = f"{main_content}\n\n{sources}"yield responseexcept asyncio.CancelledError:26 unmodified linesgr.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.",theme=gr.Theme.from_hub("allenai/gradio-theme"),css=css,examples=[22 unmodified lines5. 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 demo26 unmodified lines27282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475763 unmodified lines808182838485868 unmodified lines959697989910010110210310410510610710839 unmodified lines14814915015115215315415515615715815916016126 unmodified lines18818919019119219319419522 unmodified lines21821922022122222326 unmodified linesresults = ddgs.text(query, max_results=10)return resultsasync 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 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()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):embed = get_embeddings()documents = []3 unmodified linesdocuments.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, num_calls=3, temperature=0.2):search_results = duckduckgo_search(query)if not search_results:8 unmodified lineselse: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.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."""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 lineslogging.info(f"Number of API Calls: {num_calls}")logging.info(f"Use Embeddings: {use_embeddings}")try:# Rephrase the queryanalysis, 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(rephrased_query, model, use_embeddings, num_calls=num_calls, temperature=temperature):response = f"{main_content}\n\n{sources}"yield responseexcept asyncio.CancelledError:26 unmodified linesgr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),gr.Checkbox(label="Use Embeddings", value=False),],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 lines5. 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.""")return demo
app.py+57-17