Update agent.py
Browse files
agent.py
CHANGED
@@ -1,22 +1,48 @@
|
|
1 |
-
from openai import OpenAI
|
2 |
-
import requests
|
3 |
import os
|
|
|
|
|
4 |
|
5 |
class GaiaAgent:
|
6 |
def __init__(self):
|
7 |
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
8 |
self.instructions = (
|
9 |
-
"You are a
|
10 |
-
"
|
11 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
def __call__(self, question: str) -> str:
|
14 |
response = self.client.chat.completions.create(
|
15 |
model="gpt-4-turbo",
|
16 |
messages=[
|
17 |
{"role": "system", "content": self.instructions},
|
18 |
-
{"role": "user", "content":
|
19 |
],
|
20 |
temperature=0.0,
|
21 |
)
|
|
|
22 |
return response.choices[0].message.content.strip()
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import requests
|
3 |
+
from openai import OpenAI
|
4 |
|
5 |
class GaiaAgent:
|
6 |
def __init__(self):
|
7 |
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
8 |
self.instructions = (
|
9 |
+
"You are a highly skilled research assistant solving GAIA benchmark questions. "
|
10 |
+
"You can analyze documents, perform reasoning, and answer with a single factual answer only."
|
11 |
)
|
12 |
+
self.api_url = "https://agents-course-unit4-scoring.hf.space"
|
13 |
+
|
14 |
+
def fetch_file_content(self, task_id: str) -> str:
|
15 |
+
try:
|
16 |
+
url = f"{self.api_url}/files/{task_id}"
|
17 |
+
response = requests.get(url, timeout=10)
|
18 |
+
response.raise_for_status()
|
19 |
+
|
20 |
+
content_type = response.headers.get("Content-Type", "")
|
21 |
+
if "text" in content_type or "csv" in content_type or "json" in content_type:
|
22 |
+
return response.text[:3000] # Truncate to fit token limit
|
23 |
+
elif "application/pdf" in content_type:
|
24 |
+
return "[PDF file content detected. Summarize manually or use tool.]"
|
25 |
+
else:
|
26 |
+
return f"[Unsupported file type: {content_type}]"
|
27 |
+
except Exception as e:
|
28 |
+
return f"[Error downloading or reading file: {e}]"
|
29 |
+
|
30 |
+
def __call__(self, question: str, task_id: str = None) -> str:
|
31 |
+
file_context = ""
|
32 |
+
if task_id:
|
33 |
+
file_context = self.fetch_file_content(task_id)
|
34 |
+
if file_context:
|
35 |
+
file_context = f"Here is the related file content:\n{file_context}\n"
|
36 |
+
|
37 |
+
prompt = f"{self.instructions}\n\n{file_context}Question: {question}\nAnswer:"
|
38 |
|
|
|
39 |
response = self.client.chat.completions.create(
|
40 |
model="gpt-4-turbo",
|
41 |
messages=[
|
42 |
{"role": "system", "content": self.instructions},
|
43 |
+
{"role": "user", "content": prompt}
|
44 |
],
|
45 |
temperature=0.0,
|
46 |
)
|
47 |
+
|
48 |
return response.choices[0].message.content.strip()
|