Update app.py
Browse files53 unmodified lines545556575758596061626364646566673 unmodified lines7172737474757677787980818182838384858687888990909293949596979192939495100101969798996 unmodified lines10610710811411511610911011111276 unmodified lines1891901911921931941951961971981992002012002022032042022052062072082092102112127 unmodified lines22022122222022322422522622422722822923023153 unmodified lines description="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):def chatbot_interface(message, history, model, temperature, num_calls, use_embeddings, system_prompt): if not message.strip(): return "", history
history = history + [(message, "")]
try: for response in respond(message, system_prompt, history, model, temperature, num_calls, use_embeddings): for response in respond(message, history, model, temperature, num_calls, use_embeddings, system_prompt): history[-1] = (message, response) yield history except gr.CancelledError:3 unmodified lines history[-1] = (message, f"An unexpected error occurred: {str(e)}") yield history
def retry_last_response(system_prompt, history, model, temperature, num_calls, use_embeddings):def retry_last_response(history, model, temperature, num_calls, use_embeddings, system_prompt): if not history: return history last_user_msg = history[-1][0] history = history[:-1] # Remove the last response return chatbot_interface(last_user_msg, system_prompt, history, model, temperature, num_calls, use_embeddings) return chatbot_interface(last_user_msg, history, model, temperature, num_calls, use_embeddings, system_prompt)
def respond(message, system_prompt, history, model, temperature, num_calls, use_embeddings):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: 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_content yield history + [(message, full_response)] if sources: full_response += f"\n\nSources:\n{sources}" yield history + [(message, full_response)] response = f"{main_content}\n\n{sources}" first_line = response.split('\n')[0] if response else '' yield 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)] 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 lines return 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}")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( fn=respond, respond, additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=True, render=False), 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), 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 lines cache_examples=False, analytics_enabled=False, textbox=gr.Textbox(placeholder="Ask a question", container=False, scale=7), chatbot=gr.Chatbot( chatbot = gr.Chatbot( show_copy_button=True, likeable=True, layout="bubble", height=400 height=400, value=initial_conversation() ))
app.py+25-21