Spaces:
Runtime error
Runtime error
Update app.py
Browse files35 unmodified lines363738394041428 unmodified lines515253545556575859606162636439 unmodified lines10410510610710810911011126 unmodified lines13813914014114214314414522 unmodified lines16816917017117217335 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:async for main_content, sources in get_response_with_search(message, 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 demo35 unmodified lines363738394041428 unmodified lines515253545556575859606162636465666768697039 unmodified lines11011111211311411511611711811912026 unmodified lines14714814915015115215315422 unmodified lines17717817918018118218335 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 demo
app.py+17-7