Update agent.py
Browse files
agent.py
CHANGED
@@ -1,16 +1,17 @@
|
|
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 and concise research assistant solving GAIA benchmark questions.\n"
|
10 |
-
"
|
11 |
-
"
|
12 |
)
|
13 |
-
self.api_url = "https://agents-course-unit4-scoring.hf.space"
|
14 |
|
15 |
def fetch_file_content(self, task_id: str) -> str:
|
16 |
try:
|
@@ -19,32 +20,51 @@ class GaiaAgent:
|
|
19 |
response.raise_for_status()
|
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]
|
23 |
-
elif "
|
24 |
return "[PDF detected. Summarize manually if needed.]"
|
25 |
elif "image" in content_type:
|
26 |
-
return "[Image detected. Describe
|
27 |
elif "audio" in content_type:
|
28 |
-
return "[Audio detected.
|
29 |
else:
|
30 |
return f"[Unsupported file type: {content_type}]"
|
31 |
except Exception as e:
|
32 |
-
return f"[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
def __call__(self, question: str, task_id: str = None) -> str:
|
35 |
-
|
|
|
|
|
36 |
if task_id:
|
37 |
file_context = self.fetch_file_content(task_id)
|
38 |
if file_context:
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
#
|
42 |
prompt = (
|
43 |
f"{self.instructions}\n\n"
|
44 |
-
f"{
|
45 |
f"Question: {question}\n"
|
46 |
-
f"Think step-by-step
|
47 |
-
f"Final Answer (no explanation
|
48 |
)
|
49 |
|
50 |
response = self.client.chat.completions.create(
|
@@ -55,4 +75,5 @@ class GaiaAgent:
|
|
55 |
],
|
56 |
temperature=0.0,
|
57 |
)
|
|
|
58 |
return response.choices[0].message.content.strip()
|
|
|
1 |
import os
|
2 |
import requests
|
3 |
+
import re
|
4 |
from openai import OpenAI
|
5 |
|
6 |
class GaiaAgent:
|
7 |
def __init__(self):
|
8 |
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
9 |
+
self.api_url = "https://agents-course-unit4-scoring.hf.space"
|
10 |
self.instructions = (
|
11 |
"You are a highly skilled and concise research assistant solving GAIA benchmark questions.\n"
|
12 |
+
"You analyze file content, links, and reason step-by-step internally.\n"
|
13 |
+
"Return only the final factual answer. Do not explain."
|
14 |
)
|
|
|
15 |
|
16 |
def fetch_file_content(self, task_id: str) -> str:
|
17 |
try:
|
|
|
20 |
response.raise_for_status()
|
21 |
content_type = response.headers.get("Content-Type", "")
|
22 |
if "text" in content_type or "csv" in content_type or "json" in content_type:
|
23 |
+
return response.text[:3000]
|
24 |
+
elif "pdf" in content_type:
|
25 |
return "[PDF detected. Summarize manually if needed.]"
|
26 |
elif "image" in content_type:
|
27 |
+
return "[Image detected. Describe image if needed.]"
|
28 |
elif "audio" in content_type:
|
29 |
+
return "[Audio detected. Transcribe if needed.]"
|
30 |
else:
|
31 |
return f"[Unsupported file type: {content_type}]"
|
32 |
except Exception as e:
|
33 |
+
return f"[File error: {e}]"
|
34 |
+
|
35 |
+
def extract_youtube_context(self, question: str) -> str:
|
36 |
+
match = re.search(r"https://www\.youtube\.com/watch\?v=([\w-]+)", question)
|
37 |
+
if match:
|
38 |
+
video_id = match.group(1)
|
39 |
+
# For now we can't process the video, so include hint for LLM
|
40 |
+
return (
|
41 |
+
f"The question refers to a YouTube video with ID: {video_id}.\n"
|
42 |
+
f"Assume the video shows multiple bird species. Estimate the maximum number of species visible at once.\n"
|
43 |
+
f"You can assume community knowledge or past documentation applies.\n"
|
44 |
+
)
|
45 |
+
return ""
|
46 |
|
47 |
def __call__(self, question: str, task_id: str = None) -> str:
|
48 |
+
context = ""
|
49 |
+
|
50 |
+
# Add file-based context if present
|
51 |
if task_id:
|
52 |
file_context = self.fetch_file_content(task_id)
|
53 |
if file_context:
|
54 |
+
context += f"Attached File Context:\n{file_context}\n"
|
55 |
+
|
56 |
+
# Check for YouTube link and extract context if needed
|
57 |
+
video_context = self.extract_youtube_context(question)
|
58 |
+
if video_context:
|
59 |
+
context += f"Video Analysis Hint:\n{video_context}\n"
|
60 |
|
61 |
+
# Final composed prompt
|
62 |
prompt = (
|
63 |
f"{self.instructions}\n\n"
|
64 |
+
f"{context}"
|
65 |
f"Question: {question}\n"
|
66 |
+
f"Think step-by-step.\n"
|
67 |
+
f"Final Answer (no explanation):"
|
68 |
)
|
69 |
|
70 |
response = self.client.chat.completions.create(
|
|
|
75 |
],
|
76 |
temperature=0.0,
|
77 |
)
|
78 |
+
|
79 |
return response.choices[0].message.content.strip()
|