Update app.py
Browse files53 unmodified lines54555657585960616263646566673 unmodified lines71727374757677787980818283848586878889909192939495969798991001011021031046 unmodified lines11111211311411511611711811976 unmodified lines1961971981992002012022032042052062072082097 unmodified lines21721821922022122222322422522622753 unmodified linesdescription="List of sources to cite. Should be an URL of the source.")def chatbot_interface(message, system_prompt, history, model, temperature, num_calls, use_embeddings):if not message.strip():return "", historyhistory = history + [(message, "")]try:for response in respond(message, system_prompt, history, model, temperature, num_calls, use_embeddings):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(system_prompt, history, model, temperature, num_calls, use_embeddings):if not history:return historylast_user_msg = history[-1][0]history = history[:-1] # Remove the last responsereturn chatbot_interface(last_user_msg, system_prompt, history, model, temperature, num_calls, use_embeddings)def respond(message, system_prompt, history, model, temperature, num_calls, use_embeddings):logging.info(f"User Query: {message}")logging.info(f"Model Used: {model}")logging.info(f"Use Embeddings: {use_embeddings}")logging.info(f"System Prompt: {system_prompt}")try:full_response = ""for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature, use_embeddings=use_embeddings, system_prompt=system_prompt):full_response += main_contentyield history + [(message, full_response)]if sources:full_response += f"\n\nSources:\n{sources}"yield history + [(message, full_response)]except Exception as e:logging.error(f"Error with {model}: {str(e)}")error_message = f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."yield history + [(message, error_message)]def create_web_search_vectors(search_results):embed = get_embeddings()6 unmodified linesreturn FAISS.from_documents(documents, embed)def get_response_with_search(query, model, system_prompt=DEFAULT_SYSTEM_PROMPT, num_calls=3, temperature=0.2, use_embeddings=True):logging.info(f"get_response_with_search - Query: {query}")logging.info(f"get_response_with_search - System Prompt: {system_prompt}")search_results = duckduckgo_search(query)if use_embeddings:76 unmodified lines/* Fine-tune chatbox size */"""demo = gr.ChatInterface(fn=respond,additional_inputs=[gr.Textbox(label="System Prompt", lines=5, value=DEFAULT_SYSTEM_PROMPT),gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),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"),gr.Checkbox(label="Use Embeddings", value=False),],title="AI-powered Web Search Assistant",description="Ask questions and get answers from web search results.",7 unmodified linescache_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))53 unmodified lines54555657585960616263646566673 unmodified lines71727374757677787980818283848586878889909192939495969798996 unmodified lines10610710810911011111276 unmodified lines1891901911921931941951961971981992002012022032042052062072082092102112127 unmodified lines22022122222322422522622722822923023153 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}")logging.info(f"System Prompt: {system_prompt}")try:for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature, use_embeddings=use_embeddings, system_prompt=system_prompt):response = f"{main_content}\n\n{sources}"first_line = response.split('\n')[0] if response else ''yield responseexcept Exception as e:logging.error(f"Error with {model}: {str(e)}")yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."def create_web_search_vectors(search_results):embed = get_embeddings()6 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:76 unmodified lines/* Fine-tune chatbox size */"""def initial_conversation():return [(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, whether to use embeddings, and the system prompt 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_accordion=gr.Accordion(label="⚙️ Parameters", open=True, render=False),additional_inputs=[gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),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"),gr.Checkbox(label="Use Embeddings", value=False),gr.Textbox(label="System Prompt", lines=5, value=DEFAULT_SYSTEM_PROMPT),],title="AI-powered Web Search Assistant",description="Ask questions and get answers from web search results.",7 unmodified linescache_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()))
app.py+25-21