agents_gaia / app.py
Isics's picture
fixed some shit
6aef72f
import tempfile
import json
import yaml
from tqdm import tqdm
from smolagents import OpenAIServerModel, PythonInterpreterTool
from agents.file_reader import create_file_reader
from agents.manager import create_manager
from agents.mathematician import create_mathematician
from agents.utils import create_utils
from agents.web_browser import create_web_agent
from questions_api import QuestionsAPI
from tools.vision_tools import analyze_image
from config import IP_WINDOWS, authorized_libraries
model = OpenAIServerModel(
model_id="qwen2.5:14b",
#model_id="deepseek-r1:14b", #
api_base='http://localhost:11435/v1',
api_key="ollama")
# model_fast = OpenAIServerModel(model_id="qwen2.5:1.5b",
# api_base=f"http://{IP_WINDOWS}:11434/v1",
# api_key="ollama")
# model_light = OpenAIServerModel(model_id="phi3",
# api_base="http://localhost:11434/v1",
# api_key="ollama")
manager_agent = create_manager(model,
tools=[analyze_image,
PythonInterpreterTool(authorized_imports=authorized_libraries)],
agents=[create_file_reader(model),
create_web_agent(model),
create_utils(model),
create_mathematician(model)])
prompt = """
You are a high-level Orchestrator Agent.
Your role is to PLAN and DELEGATE. You do NOT have direct access to external tools like web search or wikipedia.
I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]
Instructions:
- Follow the following template: FINAL ANSWER: [YOUR FINAL ANSWER]
- YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
- If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
- If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
- If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
- CRITICAL: When giving the final answer, be extremely concise. If the user asks for a number, provide ONLY the number. If asked for a specific format, strictly follow it without chatting.
- IMPORTANT: Before giving the final answer or using a tool, you MUST think step by step. Break down the problem.
- If the deduction seems illogical, review it.
CRITICAL RULES:
1. **Delegation is Mandatory:** If you need to search the web, find a discography, or look up facts, you MUST use your managed agent named 'web_agent'.
2. **Forbidden Actions:** Do NOT attempt to call functions like 'wikipedia_search', 'google_search', or 'requests.get' directly. You will crash if you try.
3. **Syntax:** To use the web agent, you must generate a tool call for it.
Example: `result = web_agent(task="Find the discography of Mercedes Sosa...")`
4. **Team First Approach:** Before writing any Python code, you MUST evaluate if one of your managed agents (e.g., 'becario_windows', 'vision_tool') is capable of handling the task.
5. **Code Execution:** Only write and execute Python code for complex reasoning, data integration, or tasks that no other agent can perform.
CODING RULES:
1. **Print to Debug:** You cannot see the value of variables unless you print them. ALWAYS print the head of a dataframe or the result of a calculation to confirm it's correct.
2. **File Paths:** Assume files are in the current directory. Do not invent subfolders.
3. **Persistence:** If you create a plot or a file, you MUST save it to disk (e.g., 'output.png') and tell me the filename. Do not try to use plt.show().
4. **Libraries:** Use 'pandas' for data and 'requests' for web. Do not use complex scrapers like Selenium.
ALWAYS check your `managed_agents` list before acting. If a tool or agent exists for the job, use it. Do not reinvent the wheel.
Question:
{question_123blabla}
Extra info:
{extra_info_123blabla}
"""
answers_file = "answers.yaml"
def add_answer_to_yaml(file_path: str, task_id: str, answer: str):
with open(file_path, 'r') as f:
cur_yaml = yaml.safe_load(f)
cur_yaml[task_id] = answer
if cur_yaml:
with open(file_path,'w') as yamlfile:
yaml.safe_dump(cur_yaml, yamlfile)
def load_yaml(file_path: str):
with open(file_path, 'r') as f:
return yaml.safe_load(f)
current_answers = load_yaml(answers_file)
with tempfile.TemporaryDirectory() as tmpdir:
results = []
questions_api = QuestionsAPI(tmpdir)
for question in tqdm(questions_api.questions_generator(), total=len(questions_api.questions)):
if question["task_id"] in current_answers.keys():
continue
extra_info = {}
if question["file_name"] != "":
extra_info["file_name"] = f"{tmpdir}/{question['file_name']}"
formatted_question = prompt.format(question_123blabla=question["question"],
extra_info_123blabla=extra_info)
response = manager_agent.run(formatted_question, max_steps=30, return_full_result=False)
response = str(response)
results.append({"task_id": question["task_id"], "submitted_answer": response})
add_answer_to_yaml(file_path=answers_file, task_id=question["task_id"], answer=response)
print(questions_api.post_answers(results))