Update agent.py
Browse files
agent.py
CHANGED
@@ -134,35 +134,35 @@ def gaia_file_reader(file_id: str) -> str:
|
|
134 |
# Final agent class
|
135 |
# --------------------------------------------------------------------------- #
|
136 |
class GeminiAgent:
|
137 |
-
"""
|
138 |
-
Exposed to `app.py` – instantiated once and then called per question.
|
139 |
-
"""
|
140 |
-
|
141 |
def __init__(self):
|
142 |
-
model = GeminiModel()
|
143 |
-
tools = [
|
144 |
-
PythonInterpreterTool(), # maths, csv, small image ops
|
145 |
-
DuckDuckGoSearchTool(), # quick web look-ups
|
146 |
-
gaia_file_reader, # our custom file tool
|
147 |
-
]
|
148 |
self.system_prompt = (
|
149 |
"You are a concise, highly accurate assistant. "
|
150 |
"Unless explicitly required, reply with ONE short sentence. "
|
151 |
"Use the provided tools if needed. "
|
152 |
"All answers are graded by exact string match."
|
153 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
self.agent = CodeAgent(
|
155 |
model=model,
|
156 |
tools=tools,
|
157 |
-
|
|
|
158 |
)
|
159 |
-
print("✅ GeminiAgent
|
160 |
|
161 |
-
# ----------- Main entry point for app.py ----------- #
|
162 |
def __call__(self, question: str) -> str:
|
163 |
file_ids = FILE_TAG.findall(question)
|
|
|
|
|
164 |
if file_ids:
|
165 |
-
# Build multimodal user content
|
166 |
parts: list[gtypes.Part] = []
|
167 |
text_part = FILE_TAG.sub("", question).strip()
|
168 |
if text_part:
|
@@ -171,15 +171,23 @@ class GeminiAgent:
|
|
171 |
try:
|
172 |
img_bytes = _download_file(fid)
|
173 |
mime = mimetypes.guess_type(fid)[0] or "image/png"
|
174 |
-
parts.append(
|
|
|
|
|
175 |
except Exception as exc:
|
176 |
-
parts.append(
|
|
|
|
|
177 |
messages = [
|
178 |
{"role": "system", "content": self.system_prompt},
|
179 |
{"role": "user", "content": parts},
|
180 |
]
|
181 |
answer = self.agent.model.call_messages(messages)
|
|
|
|
|
182 |
else:
|
183 |
-
|
184 |
-
|
|
|
|
|
185 |
return answer.rstrip(" .\n\r\t")
|
|
|
134 |
# Final agent class
|
135 |
# --------------------------------------------------------------------------- #
|
136 |
class GeminiAgent:
|
|
|
|
|
|
|
|
|
137 |
def __init__(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
self.system_prompt = (
|
139 |
"You are a concise, highly accurate assistant. "
|
140 |
"Unless explicitly required, reply with ONE short sentence. "
|
141 |
"Use the provided tools if needed. "
|
142 |
"All answers are graded by exact string match."
|
143 |
)
|
144 |
+
|
145 |
+
model = GeminiModel()
|
146 |
+
tools = [
|
147 |
+
PythonInterpreterTool(),
|
148 |
+
DuckDuckGoSearchTool(),
|
149 |
+
gaia_file_reader,
|
150 |
+
]
|
151 |
+
|
152 |
+
# ✨ system_prompt removed – newest smolagents doesn't take it
|
153 |
self.agent = CodeAgent(
|
154 |
model=model,
|
155 |
tools=tools,
|
156 |
+
# any other kwargs (executor_type, additional_authorized_imports…)
|
157 |
+
verbosity_level=0,
|
158 |
)
|
159 |
+
print("✅ GeminiAgent ready.")
|
160 |
|
|
|
161 |
def __call__(self, question: str) -> str:
|
162 |
file_ids = FILE_TAG.findall(question)
|
163 |
+
|
164 |
+
# -------- multimodal branch -------- #
|
165 |
if file_ids:
|
|
|
166 |
parts: list[gtypes.Part] = []
|
167 |
text_part = FILE_TAG.sub("", question).strip()
|
168 |
if text_part:
|
|
|
171 |
try:
|
172 |
img_bytes = _download_file(fid)
|
173 |
mime = mimetypes.guess_type(fid)[0] or "image/png"
|
174 |
+
parts.append(
|
175 |
+
gtypes.Part.from_bytes(data=img_bytes, mime_type=mime)
|
176 |
+
)
|
177 |
except Exception as exc:
|
178 |
+
parts.append(
|
179 |
+
gtypes.Part.from_text(f"[FILE {fid} ERROR: {exc}]")
|
180 |
+
)
|
181 |
messages = [
|
182 |
{"role": "system", "content": self.system_prompt},
|
183 |
{"role": "user", "content": parts},
|
184 |
]
|
185 |
answer = self.agent.model.call_messages(messages)
|
186 |
+
|
187 |
+
# -------- text-only branch -------- #
|
188 |
else:
|
189 |
+
# prepend system prompt to the user question
|
190 |
+
full_prompt = f"{self.system_prompt}\n\n{question}"
|
191 |
+
answer = self.agent(full_prompt)
|
192 |
+
|
193 |
return answer.rstrip(" .\n\r\t")
|