import dspy import requests import os from . import observability_config import mlflow import pandas as pd from smolagents import WikipediaSearchTool, VisitWebpageTool from faster_whisper import WhisperModel openrouter_api_key = os.getenv("OPEN_ROUTER_API_KEY") openrouter_model_name = "openrouter/google/gemini-2.5-flash-preview" if not openrouter_api_key: raise ValueError("OPEN_ROUTER_API_KEY environment variable not found.") llm_from_openrouter = dspy.LM( model=openrouter_model_name, api_key=openrouter_api_key, api_base="https://openrouter.ai/api/v1", model_type='chat' ) dspy.configure(lm=llm_from_openrouter) @mlflow.trace def web_search_tool(search_query: str) -> str: """ Performs a web search using Serper API and returns the results as a formatted string. """ print(f"Executing Serper web search for: {search_query}") try: serper_api_key = os.getenv("SERPER_API_KEY") if not serper_api_key: return "SERPER_API_KEY environment variable not set." search_url = "https://google.serper.dev/search" payload = {"q": search_query, "num": 5} # Get up to 5 results headers = { 'X-API-KEY': serper_api_key, 'Content-Type': 'application/json' } response = requests.post(search_url, headers=headers, json=payload, timeout=10) response.raise_for_status() # Raise an exception for bad status codes search_results = response.json() if not search_results or "organic" not in search_results or not search_results["organic"]: return "No significant search results found from Serper." # Format the results formatted_results = [] for i, r in enumerate(search_results["organic"][:5]): # Ensure we take at most 5 title = r.get('title', 'N/A') link = r.get('link', '#') snippet = r.get('snippet', 'No snippet available.') formatted_results.append(f"Result {i+1}: Title: {title} | URL: {link} | Snippet: {snippet}") return "\n---\n".join(formatted_results) except requests.exceptions.HTTPError as http_err: return f"Serper API HTTP error occurred: {http_err} - {response.text}" except requests.exceptions.RequestException as req_err: return f"Error during Serper API request: {req_err}" except Exception as e: return f"An error occurred during Serper web search: {e}" @mlflow.trace def evaluate_code(code: str): """ Evaluates and executes Python code safely using dspy's PythonInterpreter. Args: code: String containing Python code to evaluate Returns: Result of the code execution """ return dspy.PythonInterpreter({}).execute(code) @mlflow.trace def get_transcript_from_audio_file(file_name: str) -> str: """ Transcribes an audio file and returns the transcript as a string. Do not use this tool if the file is not an audio file and is not local. """ model = WhisperModel("medium", device="cpu") segments, _ = model.transcribe(file_name, language="en") return "\n".join([s.text for s in segments]) @mlflow.trace def get_file_as_string(file_name: str) -> str: """ Reads a file and returns the content as a string. Handles text files, CSV files, and Excel files (converts Excel/CSV to a string representation of the first sheet). """ try: _, file_extension = os.path.splitext(file_name) file_extension = file_extension.lower() if file_extension == '.xlsx': # For .xlsx, openpyxl is commonly used. df = pd.read_excel(file_name, engine='openpyxl') return df.to_string(index=False) elif file_extension == '.csv': df = pd.read_csv(file_name) return df.to_string(index=False) else: # Assume text file with open(file_name, 'r', encoding='utf-8') as file: return file.read() except FileNotFoundError: return f"Error: File '{file_name}' not found." except pd.errors.EmptyDataError: return f"Error: The file '{file_name}' is empty or not a valid CSV/Excel file." except ImportError: return "Error: The 'pandas' library and/or its Excel engines (e.g., 'openpyxl' for .xlsx, 'xlrd' for .xls) are not installed. Please install them." except Exception as e: return f"Error reading file '{file_name}': {e}" @mlflow.trace def visit_webpage_tool(url: str) -> str: """ Visits a webpage and returns the content as a string. """ return VisitWebpageTool().forward(url) @mlflow.trace def search_wikipedia_tool(query: str) -> str: """ Searches Wikipedia for a given query to find the best matching article title, then uses the wikipedia-api library to retrieve the plain text content of that article. This is useful for finding factual information or summaries on a wide range of topics. Please set a meaningful User-Agent for your application. """ return WikipediaSearchTool().forward(query) class StrictAnswerSignature(dspy.Signature): """ An AI assistant that answers a question, reports its thoughts, and provides the final answer in a strict format. """ question = dspy.InputField(desc="The user's question to be answered.") details = dspy.InputField(desc="Any additional details about the question that might help answer it.", default=None) thoughts = dspy.OutputField(desc="Report your thought process before giving the final answer. Make sure that your answer is precise and you have thought things through step by step.") formatted_answer = dspy.OutputField( desc="Finish your answer with the exact answer to the question. " "YOUR FINAL ANSWER must be a number OR as few words as possible OR a comma separated list of numbers and/or strings (a comma separated list of strings is a list of strings separated by a comma and a space). " "If YOUR FINAL ANSWER is a number, do not use commas to write the number, neither use units such as $ or percent sign unless specified otherwise. " "If YOUR FINAL ANSWER is a string, use as few words as possible, do not use articles (a, an, the), neither abbreviations (e.g., for cities), and write the digits in plain text (e.g., 'one') unless specified otherwise. " "If YOUR FINAL ANSWER is a comma separated list, apply the above rules depending of whether each element is a number or a string." ) class BaseAgent(dspy.Module): def __init__(self): super().__init__() # Use the ReAct module. Provide the signature and the tools it can use. self.react = dspy.ReAct(StrictAnswerSignature, tools=[visit_webpage_tool, search_wikipedia_tool, evaluate_code, get_transcript_from_audio_file, get_file_as_string, web_search_tool], max_iters=3) def forward(self, question: str, extra_details: str | None = None) -> dspy.Prediction: prediction = self.react(question=question, details=extra_details) print(prediction) print(f"Agent returning fixed answer: {prediction.formatted_answer}") return prediction