| from llama_index.llms.google_genai import GoogleGenAI |
| from llama_index.llms.gemini import Gemini |
| from llama_index.llms.groq import Groq |
| from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI |
| from llama_index.tools.arxiv import ArxivToolSpec |
| from llama_index.tools.wikipedia import WikipediaToolSpec |
| from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec |
| from llama_index.core.tools import FunctionTool |
| from llama_index.core.agent.workflow import AgentWorkflow, ReActAgent |
| from llama_index.llms.lmstudio import LMStudio |
| from llama_index.core.agent.workflow import ( |
| AgentStream, |
| AgentOutput |
| ) |
| from gradio import ChatMessage |
| from llama_index.core.base.llms.types import ChatMessage as llama_index_chat_message |
|
|
| from tools import interpret_python_math_code |
| from gaia_system_prompt import SYSTEM_PROMPT as GAIA_SYSTEM_PROMPT |
|
|
| import os |
| import asyncio |
|
|
| TIMEOUT=180 |
| GEMINI_API_KEY = os.getenv("GEMINI_TOKEN") |
| GROQ_API_KEY = os.getenv("GROQ_TOKEN") |
| GEMINI_OPENAI_API_DIR = "https://generativelanguage.googleapis.com/v1beta/openai/" |
| GEMINI_MODEL_NAME = "gemini-2.5-flash-preview-04-17" |
| LMSTUDIO_MODEL_NAME = "gemma-3-12B-it-qat-GGUF" |
| API_DIR = "http://host.docker.internal:1234/v1" |
|
|
| class FinalAgent: |
| def __init__(self): |
| |
| |
| |
| |
| |
| self.llm = HuggingFaceInferenceAPI(model_name="meta-llama/Llama-3.3-70B-Instruct", timeout=TIMEOUT) |
|
|
| |
| self.tools = [ |
| FunctionTool.from_defaults( |
| fn=interpret_python_math_code, |
| name="InterpretPythonMathCode", |
| description="Interprets Python code for mathematical expressions." |
| ) |
| ] |
| self.tools.extend( |
| ArxivToolSpec().to_tool_list() |
| ) |
| self.tools.extend( |
| WikipediaToolSpec().to_tool_list() |
| ) |
| self.tools.extend( |
| DuckDuckGoSearchToolSpec().to_tool_list() |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| self.agent = ReActAgent( |
| llm=self.llm, |
| verbose=True, |
| max_iterations=5, |
| system_prompt=GAIA_SYSTEM_PROMPT, |
| tools=self.tools |
| ) |
|
|
| print("FinalAgent initialized.") |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| async def __call__(self, question: str) -> str: |
| print(f"Agent received question: {question}") |
| |
| response_str = "" |
| try: |
| |
| agent_chat_response = await self.agent.run(question) |
| |
| potential_response_obj = agent_chat_response.response |
|
|
| if isinstance(potential_response_obj, ChatMessage): |
| |
| print(f"DEBUG: Response object is ChatMessage. Role: {potential_response_obj.role}") |
| response_str = potential_response_obj.content |
| if response_str is None: |
| print("DEBUG: ChatMessage content is None, defaulting to empty string.") |
| response_str = "" |
| elif isinstance(potential_response_obj, str): |
| |
| print("DEBUG: Response object is str.") |
| response_str = potential_response_obj |
| elif isinstance(potential_response_obj, llama_index_chat_message): |
| |
| print(f"DEBUG: Response object is llama_index ChatMessage. Role: {potential_response_obj.role}") |
| response_str = potential_response_obj.content |
| if response_str is None: |
| print("DEBUG: llama_index ChatMessage content is None, defaulting to empty string.") |
| response_str = "" |
| else: |
| |
| print(f"Warning: Agent response was of unexpected type: {type(potential_response_obj)}. Converting to string.") |
| response_str = str(potential_response_obj) |
|
|
| except Exception as e: |
| print(f"Error during agent execution with LLM {self.llm.__class__.__name__}: {e}") |
| |
| response_str = f"Agent error: {e}" |
| |
| |
| if "FINAL ANSWER: " in response_str: |
| response_str = response_str.split("FINAL ANSWER: ")[-1].strip() |
| else: |
| print("Warning: 'FINAL ANSWER:' not found in response string. Returning full response.") |
|
|
| print(f"Agent final response: {response_str}") |
| return response_str |
| |
|
|
| async def main(): |
| |
| agent = FinalAgent() |
| question = "How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia." |
| answer = await agent(question) |
| print(f"Final answer: {answer}") |
|
|
| if __name__ == "__main__": |
| asyncio.run(main()) |