Update app.py
Browse files22 unmodified lines232425262726272825 unmodified lines5455565957585960616263666465666768697071727374757677787980818274838485868788808182899091929394959 unmodified lines1051061079810810911011111211311411511611711811912012112212320 unmodified lines14414514613414714814915041 unmodified lines19219319418218319519619719819918820020120220320420520619420720820922 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"]
25 unmodified lines description="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):def chatbot_interface(message, history, model, temperature, num_calls, use_embeddings): if not message.strip(): return "", history
history = history + [(message, "")]
try: for response in respond(message, history, model, temperature, num_calls, use_embeddings, system_prompt): for response in respond(message, history, model, temperature, num_calls, use_embeddings): history[-1] = (message, response) yield history except gr.CanceledError: yield history except Exception as e: logging.error(f"Unexpected error in chatbot_interface: {str(e)}") history[-1] = (message, f"An unexpected error occurred: {str(e)}") yield history
def retry_last_response(history, model, temperature, num_calls, use_embeddings): if not history: return history last_user_msg = history[-1][0] history = history[:-1] # Remove the last response return chatbot_interface(last_user_msg, history, model, temperature, num_calls, use_embeddings)
def respond(message, history, model, temperature, num_calls, use_embeddings, system_prompt):def respond(message, 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}")
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}" yield main_content for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature, use_embeddings=use_embeddings): 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)}") yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."9 unmodified lines return FAISS.from_documents(documents, embed)
def get_response_with_search(query, model, num_calls=3, temperature=0.2, use_embeddings=True, system_prompt):def get_response_with_search(query, model, num_calls=3, temperature=0.2, use_embeddings=True): 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.", "" return retriever = 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])20 unmodified lines try: response = client.chat_completion( messages=[ {"role": "system", "content": system_prompt}, {"role": "system", "content": DEFAULT_SYSTEM_PROMPT}, {"role": "user", "content": prompt} ], max_tokens=max_new_tokens,41 unmodified lines (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" "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( chatbot_interface, respond, additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=True, render=False), 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"), gr.Checkbox(label="Use Embeddings", value=True), gr.Textbox(label="System Prompt", value=DEFAULT_SYSTEM_PROMPT, lines=5), ], title="AI-powered Web Search Assistant", description="Ask questions and get answers from web search results.",app.py+26-14