Spaces:
Running
Running
Pulastya B commited on
Commit ·
d812573
1
Parent(s): 227cb22
fix: Resolve duplicate 'tools' argument in Gemini API calls
Browse files- Pass tools parameter only on first send_message call
- Remove tools parameter from subsequent function response calls
- Gemini chat session maintains tools context after first call
- Fixes TypeError: got multiple values for keyword argument 'tools'
- Resolves Render deployment error with Gemini 2.0 API
- src/orchestrator.py +14 -20
src/orchestrator.py
CHANGED
|
@@ -1311,10 +1311,15 @@ You are a DOER. Complete workflows based on user intent."""
|
|
| 1311 |
iteration = 0
|
| 1312 |
tool_call_counter = {} # Track how many times each tool has been called
|
| 1313 |
|
| 1314 |
-
#
|
|
|
|
|
|
|
|
|
|
| 1315 |
gemini_chat = None
|
|
|
|
| 1316 |
if self.provider == "gemini":
|
| 1317 |
-
|
|
|
|
| 1318 |
|
| 1319 |
while iteration < max_iterations:
|
| 1320 |
iteration += 1
|
|
@@ -1326,9 +1331,6 @@ You are a DOER. Complete workflows based on user intent."""
|
|
| 1326 |
messages = [messages[0], messages[1]] + messages[-8:]
|
| 1327 |
print(f"📊 Pruned conversation history (keeping last 8 messages)")
|
| 1328 |
|
| 1329 |
-
# Use compressed tools registry (all 46 tools but shorter descriptions)
|
| 1330 |
-
tools_to_use = self._compress_tools_registry()
|
| 1331 |
-
|
| 1332 |
# Rate limiting - wait if needed (for Gemini free tier: 10 RPM)
|
| 1333 |
if self.min_api_call_interval > 0:
|
| 1334 |
time_since_last_call = time.time() - self.last_api_call_time
|
|
@@ -1356,20 +1358,17 @@ You are a DOER. Complete workflows based on user intent."""
|
|
| 1356 |
final_content = response_message.content
|
| 1357 |
|
| 1358 |
elif self.provider == "gemini":
|
| 1359 |
-
#
|
| 1360 |
-
gemini_tools = self._convert_to_gemini_tools(tools_to_use)
|
| 1361 |
-
|
| 1362 |
-
# First iteration: send system + user message
|
| 1363 |
if iteration == 1:
|
| 1364 |
combined_message = f"{messages[0]['content']}\n\n{messages[1]['content']}"
|
|
|
|
| 1365 |
response = gemini_chat.send_message(
|
| 1366 |
combined_message,
|
| 1367 |
tools=gemini_tools
|
| 1368 |
)
|
| 1369 |
else:
|
| 1370 |
-
# Subsequent iterations: send function responses
|
| 1371 |
-
#
|
| 1372 |
-
# The last message should be a tool response
|
| 1373 |
last_tool_msg = messages[-1]
|
| 1374 |
if last_tool_msg.get("role") == "tool":
|
| 1375 |
# Send function response back to Gemini using proper format
|
|
@@ -1382,16 +1381,11 @@ You are a DOER. Complete workflows based on user intent."""
|
|
| 1382 |
)
|
| 1383 |
)
|
| 1384 |
|
| 1385 |
-
|
| 1386 |
-
|
| 1387 |
-
tools=gemini_tools
|
| 1388 |
-
)
|
| 1389 |
else:
|
| 1390 |
# Shouldn't happen, but fallback
|
| 1391 |
-
response = gemini_chat.send_message(
|
| 1392 |
-
"Continue with the next step.",
|
| 1393 |
-
tools=gemini_tools
|
| 1394 |
-
)
|
| 1395 |
|
| 1396 |
self.api_calls_made += 1
|
| 1397 |
self.last_api_call_time = time.time()
|
|
|
|
| 1311 |
iteration = 0
|
| 1312 |
tool_call_counter = {} # Track how many times each tool has been called
|
| 1313 |
|
| 1314 |
+
# Prepare tools once
|
| 1315 |
+
tools_to_use = self._compress_tools_registry()
|
| 1316 |
+
|
| 1317 |
+
# For Gemini, maintain a persistent chat session with tools configured
|
| 1318 |
gemini_chat = None
|
| 1319 |
+
gemini_tools = None
|
| 1320 |
if self.provider == "gemini":
|
| 1321 |
+
gemini_tools = self._convert_to_gemini_tools(tools_to_use)
|
| 1322 |
+
gemini_chat = self.gemini_model.start_chat(history=[], enable_automatic_function_calling=False)
|
| 1323 |
|
| 1324 |
while iteration < max_iterations:
|
| 1325 |
iteration += 1
|
|
|
|
| 1331 |
messages = [messages[0], messages[1]] + messages[-8:]
|
| 1332 |
print(f"📊 Pruned conversation history (keeping last 8 messages)")
|
| 1333 |
|
|
|
|
|
|
|
|
|
|
| 1334 |
# Rate limiting - wait if needed (for Gemini free tier: 10 RPM)
|
| 1335 |
if self.min_api_call_interval > 0:
|
| 1336 |
time_since_last_call = time.time() - self.last_api_call_time
|
|
|
|
| 1358 |
final_content = response_message.content
|
| 1359 |
|
| 1360 |
elif self.provider == "gemini":
|
| 1361 |
+
# First iteration: send system + user message with tools
|
|
|
|
|
|
|
|
|
|
| 1362 |
if iteration == 1:
|
| 1363 |
combined_message = f"{messages[0]['content']}\n\n{messages[1]['content']}"
|
| 1364 |
+
# Pass tools only on the first call
|
| 1365 |
response = gemini_chat.send_message(
|
| 1366 |
combined_message,
|
| 1367 |
tools=gemini_tools
|
| 1368 |
)
|
| 1369 |
else:
|
| 1370 |
+
# Subsequent iterations: send function responses WITHOUT tools parameter
|
| 1371 |
+
# The chat session already has tools configured from the first call
|
|
|
|
| 1372 |
last_tool_msg = messages[-1]
|
| 1373 |
if last_tool_msg.get("role") == "tool":
|
| 1374 |
# Send function response back to Gemini using proper format
|
|
|
|
| 1381 |
)
|
| 1382 |
)
|
| 1383 |
|
| 1384 |
+
# Don't pass tools again - already configured in chat session
|
| 1385 |
+
response = gemini_chat.send_message(function_response_part)
|
|
|
|
|
|
|
| 1386 |
else:
|
| 1387 |
# Shouldn't happen, but fallback
|
| 1388 |
+
response = gemini_chat.send_message("Continue with the next step.")
|
|
|
|
|
|
|
|
|
|
| 1389 |
|
| 1390 |
self.api_calls_made += 1
|
| 1391 |
self.last_api_call_time = time.time()
|