dawid-lorek's picture
Update agent.py
5fffd11 verified
raw
history blame
3.18 kB
import os
import requests
import re
from openai import OpenAI
class GaiaAgent:
def __init__(self):
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
self.api_url = "https://agents-course-unit4-scoring.hf.space"
self.instructions = (
"You are a highly skilled and concise research assistant solving GAIA benchmark questions.\n"
"You analyze file content, links, and reason step-by-step internally.\n"
"Return only the final factual answer. Do not explain."
)
def fetch_file_content(self, task_id: str) -> str:
try:
url = f"{self.api_url}/files/{task_id}"
response = requests.get(url, timeout=10)
response.raise_for_status()
content_type = response.headers.get("Content-Type", "")
if "text" in content_type or "csv" in content_type or "json" in content_type:
return response.text[:3000]
elif "pdf" in content_type:
return "[PDF detected. Summarize manually if needed.]"
elif "image" in content_type:
return "[Image detected. Describe image if needed.]"
elif "audio" in content_type:
return "[Audio detected. Transcribe if needed.]"
else:
return f"[Unsupported file type: {content_type}]"
except Exception as e:
return f"[File error: {e}]"
def extract_youtube_context(self, question: str) -> str:
match = re.search(r"https://www\.youtube\.com/watch\?v=([\w-]+)", question)
if match:
video_id = match.group(1)
# For now we can't process the video, so include hint for LLM
return (
f"The question refers to a YouTube video with ID: {video_id}.\n"
f"Assume the video shows multiple bird species. Estimate the maximum number of species visible at once.\n"
f"You can assume community knowledge or past documentation applies.\n"
)
return ""
def __call__(self, question: str, task_id: str = None) -> str:
context = ""
# Add file-based context if present
if task_id:
file_context = self.fetch_file_content(task_id)
if file_context:
context += f"Attached File Context:\n{file_context}\n"
# Check for YouTube link and extract context if needed
video_context = self.extract_youtube_context(question)
if video_context:
context += f"Video Analysis Hint:\n{video_context}\n"
# Final composed prompt
prompt = (
f"{self.instructions}\n\n"
f"{context}"
f"Question: {question}\n"
f"Think step-by-step.\n"
f"Final Answer (no explanation):"
)
response = self.client.chat.completions.create(
model="gpt-4-turbo",
messages=[
{"role": "system", "content": self.instructions},
{"role": "user", "content": prompt}
],
temperature=0.0,
)
return response.choices[0].message.content.strip()