Shreyas094/Sentinel-AI-Web-Search-Test-v2-Testing-Score
0
1import os2import json3import re4import gradio as gr5import requests6from duckduckgo_search import DDGS7from typing import List8from pydantic import BaseModel, Field9from langchain_community.vectorstores import FAISS10from langchain_community.embeddings import HuggingFaceEmbeddings11from langchain_core.documents import Document12from huggingface_hub import InferenceClient13import logging14import pandas as pd15import tempfile16 17# Set up basic configuration for logging18logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')19 20# Environment variables and configurations21huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")22 23MODELS = [24 "mistralai/Mistral-7B-Instruct-v0.3",25 "mistralai/Mixtral-8x7B-Instruct-v0.1",26 "mistralai/Mistral-Nemo-Instruct-2407",27 "meta-llama/Meta-Llama-3.1-8B-Instruct",28 "meta-llama/Meta-Llama-3.1-70B-Instruct"29]30 31MODEL_TOKEN_LIMITS = {32 "mistralai/Mistral-7B-Instruct-v0.3": 32768,33 "mistralai/Mixtral-8x7B-Instruct-v0.1": 32768,34 "mistralai/Mistral-Nemo-Instruct-2407": 32768,35 "meta-llama/Meta-Llama-3.1-8B-Instruct": 8192,36 "meta-llama/Meta-Llama-3.1-70B-Instruct": 8192,37}38 39DEFAULT_SYSTEM_PROMPT = """You are a world-class financial AI assistant, capable of complex reasoning and reflection.40Reason through the query inside <thinking> tags, and then provide your final response inside <output> tags.41Providing comprehensive and accurate information based on web search results is essential.42Your goal is to synthesize the given context into a coherent and detailed response that directly addresses the user's query.43Please ensure that your response is well-structured, factual.44If you detect that you made a mistake in your reasoning at any point, correct yourself inside <reflection> tags."""45 46def process_excel_file(file, model, temperature, num_calls, use_embeddings, system_prompt):47 try:48 df = pd.read_excel(file.name)49 results = []50 51 for _, row in df.iterrows():52 question = row['Question']53 custom_system_prompt = row['System Prompt']54 55 # Use the existing get_response_with_search function56 response_generator = get_response_with_search(question, model, num_calls, temperature, use_embeddings, custom_system_prompt)57 58 full_response = ""59 for partial_response, _ in response_generator:60 full_response = partial_response # Keep updating with the latest response61 62 if not full_response:63 full_response = "No response generated. Please check the input parameters and try again."64 65 results.append(full_response)66 67 df['Response'] = results68 69 # Save to a temporary file70 with tempfile.NamedTemporaryFile(delete=False, suffix='.xlsx') as tmp:71 df.to_excel(tmp.name, index=False)72 return tmp.name73 except Exception as e:74 logging.error(f"Error processing Excel file: {str(e)}")75 return None76 77def upload_file(file):78 return file.name if file else None79 80def download_file(file_path):81 return file_path82 83def get_embeddings():84 return HuggingFaceEmbeddings(model_name="sentence-transformers/stsb-roberta-large")85 86def duckduckgo_search(query):87 with DDGS() as ddgs:88 results = list(ddgs.text(query, max_results=5))89 return results90 91class CitingSources(BaseModel):92 sources: List[str] = Field(93 ...,94 description="List of sources to cite. Should be an URL of the source."95 )96 97def chatbot_interface(message, history, model, temperature, num_calls, use_embeddings, system_prompt):98 if not message.strip():99 return "", history100 101 history = history + [(message, "")]102 103 try:104 for response in respond(message, history, model, temperature, num_calls, use_embeddings, system_prompt):105 history[-1] = (message, response)106 yield history107 except Exception as e:108 logging.error(f"Error in chatbot_interface: {str(e)}")109 error_message = f"An error occurred: {str(e)}. Please try again."110 history[-1] = (message, error_message)111 yield history112 113def retry_last_response(history, model, temperature, num_calls, use_embeddings, system_prompt):114 if not history:115 return history116 117 last_user_msg = history[-1][0]118 history = history[:-1] # Remove the last response119 120 return chatbot_interface(last_user_msg, history, model, temperature, num_calls, use_embeddings, system_prompt)121 122def respond(message, history, model, temperature, num_calls, use_embeddings, system_prompt):123 logging.info(f"User Query: {message}")124 logging.info(f"Model Used: {model}")125 logging.info(f"Use Embeddings: {use_embeddings}")126 logging.info(f"System Prompt: {system_prompt}")127 128 try:129 for main_content, _ in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature, use_embeddings=use_embeddings, system_prompt=system_prompt):130 yield main_content131 except Exception as e:132 logging.error(f"Error with {model}: {str(e)}")133 yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."134 135def create_web_search_vectors(search_results):136 embed = get_embeddings()137 138 documents = []139 for result in search_results:140 if 'body' in result:141 content = f"{result['title']}\n{result['body']}\nSource: {result['href']}"142 documents.append(Document(page_content=content, metadata={"source": result['href']}))143 144 return FAISS.from_documents(documents, embed)145 146def summarize_article(article, content, model, system_prompt, user_query, client, temperature=0.2):147 prompt = f"""Summarize the following article in the context of broader web search results:148 149Article:150Title: {article['title']}151URL: {article['href']}152Content: {article['body'][:1000]}... # Truncate to avoid extremely long prompts153 154Additional Context:155{content[:1000]}... # Truncate additional context as well156 157User Query: {user_query}158 159 Write a detailed and complete research document which addresses the User Query, incorporating both the specific article and the broader context. Focus on the most relevant information.160"""161 162 # Calculate input tokens (this is an approximation, you might need a more accurate method)163 input_tokens = len(prompt.split()) // 4164 165 # Get the token limit for the current model166 model_token_limit = MODEL_TOKEN_LIMITS.get(model, 8192) # Default to 8192 if model not found167 168 # Calculate max_new_tokens169 max_new_tokens = min(model_token_limit - input_tokens, 6500) # Cap at 6500 to be safe170 171 try:172 response = client.chat_completion(173 messages=[174 {"role": "system", "content": system_prompt},175 {"role": "user", "content": prompt}176 ],177 max_tokens=max_new_tokens,178 temperature=temperature,179 stream=False,180 top_p=0.8,181 )182 183 if hasattr(response, 'choices') and response.choices:184 for choice in response.choices:185 if hasattr(choice, 'message') and hasattr(choice.message, 'content'):186 return choice.message.content.strip()187 except Exception as e:188 logging.error(f"Error summarizing article: {str(e)}")189 return f"Error summarizing article: {str(e)}"190 191 return "Unable to generate summary."192 193def get_response_with_search(query, model, num_calls=3, temperature=0.2, use_embeddings=True, system_prompt=DEFAULT_SYSTEM_PROMPT):194 search_results = duckduckgo_search(query)195 client = InferenceClient(model, token=huggingface_token)196 197 # Prepare overall context198 overall_context = "\n".join([f"{result['title']}\n{result['body']}" for result in search_results])199 200 summaries = []201 for result in search_results:202 summary = summarize_article(result, overall_context, model, system_prompt, query, client, temperature)203 summaries.append({204 "title": result['title'],205 "url": result['href'],206 "summary": summary207 })208 yield format_output(summaries), ""209 210def format_output(summaries):211 output = "Here are the summarized search results:\n\n"212 for item in summaries:213 output += f"News Title: {item['title']}\n"214 output += f"URL: {item['url']}\n"215 output += f"Summary: {item['summary']}\n\n"216 return output217 218def vote(data: gr.LikeData):219 if data.liked:220 print(f"You upvoted this response: {data.value}")221 else:222 print(f"You downvoted this response: {data.value}")223 224css = """225/* Fine-tune chatbox size */226"""227 228def initial_conversation():229 return [230 (None, "Welcome! I'm your AI assistant for web search. Here's how you can use me:\n\n"231 "1. Ask me any question, and I'll search the web for information.\n"232 "2. You can adjust the system prompt for fine-tuned responses, whether to use embeddings, and the temperature.\n"233 234 "To get started, ask me a question!")235 ]236 237# Modify the Gradio interface238with gr.Blocks() as demo:239 gr.Markdown("# AI-powered Web Search Assistant")240 gr.Markdown("Ask questions and get answers from web search results.")241 242 with gr.Row():243 chatbot = gr.Chatbot(244 show_copy_button=True,245 likeable=True,246 layout="bubble",247 height=400,248 value=initial_conversation()249 )250 251 with gr.Row():252 message = gr.Textbox(placeholder="Ask a question", container=False, scale=7)253 submit_button = gr.Button("Submit")254 255 with gr.Accordion("⚙️ Parameters", open=False):256 model = gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3])257 temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature")258 num_calls = gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls")259 use_embeddings = gr.Checkbox(label="Use Embeddings", value=False)260 system_prompt = gr.Textbox(label="System Prompt", lines=5, value=DEFAULT_SYSTEM_PROMPT)261 262 with gr.Accordion("Batch Processing", open=False):263 excel_file = gr.File(label="Upload Excel File", file_types=[".xlsx"])264 process_button = gr.Button("Process Excel File")265 download_button = gr.File(label="Download Processed File")266 267 # Event handlers268 submit_button.click(chatbot_interface, inputs=[message, chatbot, model, temperature, num_calls, use_embeddings, system_prompt], outputs=chatbot)269 message.submit(chatbot_interface, inputs=[message, chatbot, model, temperature, num_calls, use_embeddings, system_prompt], outputs=chatbot)270 271 # Excel processing272 excel_file.change(upload_file, inputs=[excel_file], outputs=[excel_file])273 process_button.click(274 process_excel_file,275 inputs=[excel_file, model, temperature, num_calls, use_embeddings, system_prompt],276 outputs=[download_button]277 )278 279if __name__ == "__main__":280 demo.launch(share=True)