dawid-lorek's picture
Update agent.py
239dbcb verified
raw
history blame
2.39 kB
import os
import requests
from openai import OpenAI
class GaiaAgent:
def __init__(self):
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
self.instructions = (
"You are a top-tier research assistant for the GAIA benchmark. "
"You analyze documents, reason step by step, and always provide a single, concise, and correct answer. "
"If a file is provided, extract all relevant information. Use only information from the question and file. "
"Show your reasoning before the answer, but end with 'Final Answer: <your answer>'."
)
self.api_url = "https://agents-course-unit4-scoring.hf.space"
def fetch_file_content(self, task_id: str) -> str:
try:
url = f"{self.api_url}/files/{task_id}"
response = requests.get(url, timeout=15)
response.raise_for_status()
content_type = response.headers.get("Content-Type", "")
if any(t in content_type for t in ["text", "csv", "json"]):
return response.text[:6000] # Allow more context for better answers
elif "application/pdf" in content_type:
return "[PDF file detected. Use a PDF parser to extract text.]"
else:
return f"[Unsupported file type: {content_type}]"
except Exception as e:
return f"[Error downloading or reading file: {e}]"
def __call__(self, question: str, task_id: str = None) -> str:
file_context = ""
if task_id:
file_context = self.fetch_file_content(task_id)
if file_context:
file_context = f"Here is the related file content:\n{file_context}\n"
prompt = (
f"{self.instructions}\n\n"
f"{file_context}"
f"Question: {question}\n"
"Show your reasoning step by step, then provide the final answer as 'Final Answer: <answer>'."
)
response = self.client.chat.completions.create(
model="gpt-4o", # Use the latest, most capable model for better accuracy
messages=[
{"role": "system", "content": self.instructions},
{"role": "user", "content": prompt}
],
temperature=0.0,
max_tokens=1024,
)
return response.choices[0].message.content.strip()