File size: 1,955 Bytes
332e48b
eb7cc40
 
332e48b
 
 
 
 
eb7cc40
 
332e48b
eb7cc40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
332e48b
 
 
 
 
eb7cc40
332e48b
 
 
eb7cc40
332e48b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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 research assistant solving GAIA benchmark questions. "
            "You can analyze documents, perform reasoning, and answer with a single factual answer only."
        )
        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]  # Truncate to fit token limit
            elif "application/pdf" in content_type:
                return "[PDF file content detected. Summarize manually or use tool.]"
            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{file_context}Question: {question}\nAnswer:"

        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()