Spaces:
Runtime error
Runtime error
Update agent.py
Browse files
agent.py
CHANGED
@@ -103,7 +103,7 @@ def gemini_transcribe_audio(file_path: str, prompt: str = "Transcribe the audio.
|
|
103 |
{"type": "media", "data": b64, "mime_type": mime},
|
104 |
]
|
105 |
)
|
106 |
-
resp = asyncio.run(
|
107 |
return resp.content if hasattr(resp, "content") else str(resp)
|
108 |
|
109 |
# ---------------------------------------------------------------------
|
@@ -121,7 +121,7 @@ def describe_image(file_path: str, prompt: str = "Describe this image.") -> str:
|
|
121 |
img, # langchain übernimmt Encoding
|
122 |
]
|
123 |
)
|
124 |
-
resp = asyncio.run(
|
125 |
return resp.content
|
126 |
|
127 |
# ---------------------------------------------------------------------
|
@@ -177,37 +177,35 @@ gemini_llm = ChatGoogleGenerativeAI(
|
|
177 |
).bind_tools([
|
178 |
fetch_gaia_file, parse_csv, parse_excel,
|
179 |
gemini_transcribe_audio, describe_image, ocr_image,
|
180 |
-
web_search, simple_calculator,
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
async def safe_invoke(msgs:
|
186 |
-
|
187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
|
189 |
# ---------------------------------------------------------------------
|
190 |
# System-Prompt
|
191 |
# ---------------------------------------------------------------------
|
192 |
system_prompt = SystemMessage(content="""
|
193 |
-
You are
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
Otherwise decide between web_search or simple_calculator.
|
200 |
-
|
201 |
-
Format for a tool call:
|
202 |
-
Thought: Do I need to use a tool? Yes
|
203 |
-
Action: <tool name>
|
204 |
-
Action Input: <JSON arguments>
|
205 |
-
|
206 |
-
Format for final answer:
|
207 |
-
Thought: Do I need to use a tool? No
|
208 |
-
Final Answer: <your answer>
|
209 |
-
|
210 |
-
Stop once you output "Final Answer:".
|
211 |
""")
|
212 |
|
213 |
# ---------------------------------------------------------------------
|
|
|
103 |
{"type": "media", "data": b64, "mime_type": mime},
|
104 |
]
|
105 |
)
|
106 |
+
resp = asyncio.run(safe_invoke([message]))
|
107 |
return resp.content if hasattr(resp, "content") else str(resp)
|
108 |
|
109 |
# ---------------------------------------------------------------------
|
|
|
121 |
img, # langchain übernimmt Encoding
|
122 |
]
|
123 |
)
|
124 |
+
resp = asyncio.run(safe_invoke([message]))
|
125 |
return resp.content
|
126 |
|
127 |
# ---------------------------------------------------------------------
|
|
|
177 |
).bind_tools([
|
178 |
fetch_gaia_file, parse_csv, parse_excel,
|
179 |
gemini_transcribe_audio, describe_image, ocr_image,
|
180 |
+
web_search, simple_calculator,] ,return_named_tools=True)
|
181 |
+
|
182 |
+
LLM_SEMA = asyncio.Semaphore(2) # 3 gleichz. Anfragen ≈ < 15/min
|
183 |
+
|
184 |
+
# safe_invoke neu (ersetzt die alte Funktion)
|
185 |
+
async def safe_invoke(msgs, tries: int = 4):
|
186 |
+
"""Gemini-Aufruf mit Semaphor + Exponential-Back-off bei 429 / Netzfehlern."""
|
187 |
+
delay = 4
|
188 |
+
for t in range(tries):
|
189 |
+
async with LLM_SEMA:
|
190 |
+
try:
|
191 |
+
return await gemini_llm.ainvoke(msgs)
|
192 |
+
except Exception as e:
|
193 |
+
# nur bei Rate-Limit oder Netzwerk erneut versuchen
|
194 |
+
if ("429" in str(e) or "RateLimit" in str(e)) and t < tries - 1:
|
195 |
+
await asyncio.sleep(delay)
|
196 |
+
delay *= 2 # 4 s, 8 s, 16 s …
|
197 |
+
continue
|
198 |
+
raise
|
199 |
|
200 |
# ---------------------------------------------------------------------
|
201 |
# System-Prompt
|
202 |
# ---------------------------------------------------------------------
|
203 |
system_prompt = SystemMessage(content="""
|
204 |
+
You are a helpful assistant tasked with answering questions using a set of tools.
|
205 |
+
Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
206 |
+
FINAL ANSWER: [YOUR FINAL ANSWER].
|
207 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
208 |
+
Your answer should only start with "FINAL ANSWER: ", then follows with the answer.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
""")
|
210 |
|
211 |
# ---------------------------------------------------------------------
|