|
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 highly skilled and concise research assistant solving GAIA benchmark questions.\n" |
|
"Analyze attached files, extract relevant information, reason step-by-step internally,\n" |
|
"and return only the final factual answer in the correct format. Avoid explanations." |
|
) |
|
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=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 "application/pdf" in content_type: |
|
return "[PDF detected. Summarize manually if needed.]" |
|
elif "image" in content_type: |
|
return "[Image detected. Describe the image if needed.]" |
|
elif "audio" in content_type: |
|
return "[Audio detected. Provide transcription if needed.]" |
|
else: |
|
return f"[Unsupported file type: {content_type}]" |
|
except Exception as e: |
|
return f"[Error fetching 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"Attached File Context:\n{file_context}\n" |
|
|
|
|
|
prompt = ( |
|
f"{self.instructions}\n\n" |
|
f"{file_context}" |
|
f"Question: {question}\n" |
|
f"Think step-by-step to extract relevant facts and solve the task.\n" |
|
f"Final Answer (no explanation, just the answer):" |
|
) |
|
|
|
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() |
|
|