Spaces:
Runtime error
Runtime error
Update app.py
Browse files22 unmodified lines23242526272829305 unmodified lines3637383940418 unmodified lines50515253545556575859606162633 unmodified lines67686970717273747576777879808182838485868712 unmodified lines10010110210310410510610710810911011111211311411511611711811912015 unmodified lines13613713813914014114238 unmodified lines18118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423522 unmodified lines"mistralai/Mixtral-8x7B-Instruct-v0.1","mistralai/Mistral-Nemo-Instruct-2407","meta-llama/Meta-Llama-3.1-8B-Instruct","meta-llama/Meta-Llama-3.1-70B-Instruct","meta-llama/Meta-Llama-3.1-8B-Instruct","meta-llama/Meta-Llama-3.1-70B-Instruct"]5 unmodified lines"meta-llama/Meta-Llama-3.1-70B-Instruct": 8192,}def get_embeddings():return HuggingFaceEmbeddings(model_name="sentence-transformers/stsb-roberta-large")8 unmodified linesdescription="List of sources to cite. Should be an URL of the source.")def chatbot_interface(message, history, model, temperature, num_calls):if not message.strip():return "", historyhistory = history + [(message, "")]try:for response in respond(message, history, model, temperature, num_calls):history[-1] = (message, response)yield historyexcept gr.CancelledError:3 unmodified lineshistory[-1] = (message, f"An unexpected error occurred: {str(e)}")yield historydef retry_last_response(history, model, temperature, num_calls):if not history:return historylast_user_msg = history[-1][0]history = history[:-1] # Remove the last responsereturn chatbot_interface(last_user_msg, history, model, temperature, num_calls)def respond(message, history, model, temperature, num_calls):logging.info(f"User Query: {message}")logging.info(f"Model Used: {model}")try:for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature):response = f"{main_content}\n\n{sources}"first_line = response.split('\n')[0] if response else ''yield response12 unmodified linesreturn FAISS.from_documents(documents, embed)import jsondef get_response_with_search(query, model, num_calls=3, temperature=0.2):search_results = duckduckgo_search(query)web_search_database = create_web_search_vectors(search_results)if not web_search_database:yield "No web search results available. Please try again.", ""returnretriever = web_search_database.as_retriever(search_kwargs={"k": 5})relevant_docs = retriever.get_relevant_documents(query)context = "\n".join([doc.page_content for doc in relevant_docs])prompt = f"""Using the following context from web search results:{context}Write a detailed and complete research document that fulfills the following user request: '{query}'15 unmodified linesfor i in range(num_calls):try:response = client.chat_completion(messages=[{"role": "user", "content": prompt}],max_tokens=max_new_tokens,temperature=temperature,stream=False,38 unmodified linesreturn [(None, "Welcome! I'm your AI assistant for web search. Here's how you can use me:\n\n""1. Ask me any question, and I'll search the web for information.\n""2. You can adjust the model, temperature, and number of API calls for fine-tuned responses.\n""3. For any queries, feel free to reach out @desai.shreyas94@gmail.com or discord - shreyas094\n\n""To get started, ask me a question!")]demo = gr.ChatInterface(respond,additional_inputs=[gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[2]),gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),],title="AI-powered Web Search Assistant",description="Ask questions and get answers from web search results.",theme=gr.themes.Soft(primary_hue="orange",secondary_hue="amber",neutral_hue="gray",font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]).set(body_background_fill_dark="#0c0505",block_background_fill_dark="#0c0505",block_border_width="1px",block_title_background_fill_dark="#1b0f0f",input_background_fill_dark="#140b0b",button_secondary_background_fill_dark="#140b0b",border_color_accent_dark="#1b0f0f",border_color_primary_dark="#1b0f0f",background_fill_secondary_dark="#0c0505",color_accent_soft_dark="transparent",code_background_fill_dark="#140b0b"),css=css,examples=[["What are the latest developments in artificial intelligence?"],["Can you explain the basics of quantum computing?"],["What are the current global economic trends?"]],cache_examples=False,analytics_enabled=False,textbox=gr.Textbox(placeholder="Ask a question", container=False, scale=7),chatbot = gr.Chatbot(show_copy_button=True,likeable=True,layout="bubble",height=400,value=initial_conversation()))if __name__ == "__main__":demo.launch(share=True)22 unmodified lines2324252627285 unmodified lines3435363738394041424344458 unmodified lines54555657585960616263646566673 unmodified lines7172737475767778798081828384858687888990919212 unmodified lines10510610710810911011111211311411511611711811912012112212312415 unmodified lines14014114214314414514614714814938 unmodified lines18818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725822 unmodified lines"mistralai/Mixtral-8x7B-Instruct-v0.1","mistralai/Mistral-Nemo-Instruct-2407","meta-llama/Meta-Llama-3.1-8B-Instruct","meta-llama/Meta-Llama-3.1-70B-Instruct"]5 unmodified lines"meta-llama/Meta-Llama-3.1-70B-Instruct": 8192,}DEFAULT_SYSTEM_PROMPT = """You are a world-class financial AI assistant, 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."""def get_embeddings():return HuggingFaceEmbeddings(model_name="sentence-transformers/stsb-roberta-large")8 unmodified linesdescription="List of sources to cite. Should be an URL of the source.")def chatbot_interface(message, history, model, temperature, num_calls, use_embeddings, system_prompt):if not message.strip():return "", historyhistory = history + [(message, "")]try:for response in respond(message, history, model, temperature, num_calls, use_embeddings, system_prompt):history[-1] = (message, response)yield historyexcept gr.CancelledError:3 unmodified lineshistory[-1] = (message, f"An unexpected error occurred: {str(e)}")yield historydef retry_last_response(history, model, temperature, num_calls, use_embeddings, system_prompt):if not history:return historylast_user_msg = history[-1][0]history = history[:-1] # Remove the last responsereturn chatbot_interface(last_user_msg, history, model, temperature, num_calls, use_embeddings, system_prompt)def respond(message, history, model, temperature, num_calls, use_embeddings, system_prompt):logging.info(f"User Query: {message}")logging.info(f"Model Used: {model}")logging.info(f"Use Embeddings: {use_embeddings}")try:for main_content, sources in get_response_with_search(message, model, num_calls, temperature, use_embeddings, system_prompt):response = f"{main_content}\n\n{sources}"first_line = response.split('\n')[0] if response else ''yield response12 unmodified linesreturn FAISS.from_documents(documents, embed)def get_response_with_search(query, model, num_calls=3, temperature=0.2, use_embeddings=True, system_prompt=DEFAULT_SYSTEM_PROMPT):search_results = duckduckgo_search(query)if use_embeddings:web_search_database = create_web_search_vectors(search_results)if not web_search_database:yield "No web search results available. Please try again.", ""returnretriever = web_search_database.as_retriever(search_kwargs={"k": 5})relevant_docs = retriever.get_relevant_documents(query)context = "\n".join([doc.page_content for doc in relevant_docs])else:context = "\n".join([f"{result['title']}\n{result['body']}\nSource: {result['href']}" for result in search_results])prompt = f"""Using the following context from web search results:{context}Write a detailed and complete research document that fulfills the following user request: '{query}'15 unmodified linesfor i in range(num_calls):try:response = client.chat_completion(messages=[{"role": "system", "content": system_prompt},{"role": "user", "content": prompt}],max_tokens=max_new_tokens,temperature=temperature,stream=False,38 unmodified linesreturn [(None, "Welcome! I'm your AI assistant for web search. Here's how you can use me:\n\n""1. Ask me any question, and I'll search the web for information.\n""2. You can adjust the model, temperature, number of API calls, and whether to use embeddings for fine-tuned responses.\n""3. You can also customize the system prompt to guide my behavior.\n""4. For any queries, feel free to reach out @desai.shreyas94@gmail.com or discord - shreyas094\n\n""To get started, ask me a question!")]with gr.Blocks(css=css, theme=gr.themes.Soft(primary_hue="orange",secondary_hue="amber",neutral_hue="gray",font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]).set(body_background_fill_dark="#0c0505",block_background_fill_dark="#0c0505",block_border_width="1px",block_title_background_fill_dark="#1b0f0f",input_background_fill_dark="#140b0b",button_secondary_background_fill_dark="#140b0b",border_color_accent_dark="#1b0f0f",border_color_primary_dark="#1b0f0f",background_fill_secondary_dark="#0c0505",color_accent_soft_dark="transparent",code_background_fill_dark="#140b0b")) as demo:gr.Markdown("# AI-powered Web Search Assistant")gr.Markdown("Ask questions and get answers from web search results.")chatbot = gr.Chatbot(show_copy_button=True,likeable=True,layout="bubble",height=400,value=initial_conversation())with gr.Row():msg = gr.Textbox(placeholder="Ask a question", container=False, scale=7)submit = gr.Button("Submit")with gr.Accordion("Advanced Options", open=False):model = gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[2])temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature")num_calls = gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls")use_embeddings = gr.Checkbox(label="Use Embeddings", value=True)system_prompt = gr.Textbox(label="System Prompt", value=DEFAULT_SYSTEM_PROMPT, lines=5)clear = gr.Button("Clear")retry = gr.Button("Retry Last Response")# Set up event handlerssubmit.click(chatbot_interface, [msg, chatbot, model, temperature, num_calls, use_embeddings, system_prompt], chatbot)msg.submit(chatbot_interface, [msg, chatbot, model, temperature, num_calls, use_embeddings, system_prompt], chatbot)clear.click(lambda: None, None, chatbot, queue=False)retry.click(retry_last_response, [chatbot, model, temperature, num_calls, use_embeddings, system_prompt], chatbot)chatbot.like(vote, None, None)gr.Examples(examples=[["What are the latest developments in artificial intelligence?"],["Can you explain the basics of quantum computing?"],["What are the current global economic trends?"]],inputs=msg)if __name__ == "__main__":demo.launch(share=True)
app.py+85-62