Update agents.py
Browse files
agents.py
CHANGED
|
@@ -50,9 +50,33 @@ Format your final answer clearly and concisely."""
|
|
| 50 |
if response.get("tool_calls"):
|
| 51 |
print(f"\nIteration {iteration + 1}: Tool calls requested")
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
for tool_call in response["tool_calls"]:
|
| 54 |
tool_name = tool_call["name"]
|
| 55 |
tool_args = tool_call["arguments"]
|
|
|
|
| 56 |
|
| 57 |
print(f" β {tool_name}({tool_args})")
|
| 58 |
|
|
@@ -62,13 +86,9 @@ Format your final answer clearly and concisely."""
|
|
| 62 |
print(f" β Result: {str(result)[:100]}...")
|
| 63 |
|
| 64 |
# Add tool result to messages
|
| 65 |
-
messages.append({
|
| 66 |
-
"role": "assistant",
|
| 67 |
-
"content": f"Used {tool_name}",
|
| 68 |
-
"tool_calls": [tool_call]
|
| 69 |
-
})
|
| 70 |
messages.append({
|
| 71 |
"role": "tool",
|
|
|
|
| 72 |
"name": tool_name,
|
| 73 |
"content": str(result)
|
| 74 |
})
|
|
@@ -76,11 +96,18 @@ Format your final answer clearly and concisely."""
|
|
| 76 |
print(f" β Error: {e}")
|
| 77 |
messages.append({
|
| 78 |
"role": "tool",
|
|
|
|
| 79 |
"name": tool_name,
|
| 80 |
"content": f"Error: {str(e)}"
|
| 81 |
})
|
| 82 |
else:
|
| 83 |
print(f" β Tool not found!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
else:
|
| 85 |
# Model provided final answer
|
| 86 |
answer = response.get("content", "Unknown")
|
|
|
|
| 50 |
if response.get("tool_calls"):
|
| 51 |
print(f"\nIteration {iteration + 1}: Tool calls requested")
|
| 52 |
|
| 53 |
+
# Create assistant message with tool calls
|
| 54 |
+
assistant_msg = {
|
| 55 |
+
"role": "assistant",
|
| 56 |
+
"content": response.get("content") or "Using tools...",
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
# Add tool_calls if the model supports it
|
| 60 |
+
if "groq" in str(self.model.model_id).lower():
|
| 61 |
+
# For Groq, we need to include tool_calls in the message
|
| 62 |
+
assistant_msg["tool_calls"] = [
|
| 63 |
+
{
|
| 64 |
+
"id": tc.get("id", f"call_{tc['name']}"),
|
| 65 |
+
"type": "function",
|
| 66 |
+
"function": {
|
| 67 |
+
"name": tc["name"],
|
| 68 |
+
"arguments": str(tc["arguments"])
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
for tc in response["tool_calls"]
|
| 72 |
+
]
|
| 73 |
+
|
| 74 |
+
messages.append(assistant_msg)
|
| 75 |
+
|
| 76 |
for tool_call in response["tool_calls"]:
|
| 77 |
tool_name = tool_call["name"]
|
| 78 |
tool_args = tool_call["arguments"]
|
| 79 |
+
tool_id = tool_call.get("id", f"call_{tool_name}")
|
| 80 |
|
| 81 |
print(f" β {tool_name}({tool_args})")
|
| 82 |
|
|
|
|
| 86 |
print(f" β Result: {str(result)[:100]}...")
|
| 87 |
|
| 88 |
# Add tool result to messages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
messages.append({
|
| 90 |
"role": "tool",
|
| 91 |
+
"tool_call_id": tool_id,
|
| 92 |
"name": tool_name,
|
| 93 |
"content": str(result)
|
| 94 |
})
|
|
|
|
| 96 |
print(f" β Error: {e}")
|
| 97 |
messages.append({
|
| 98 |
"role": "tool",
|
| 99 |
+
"tool_call_id": tool_id,
|
| 100 |
"name": tool_name,
|
| 101 |
"content": f"Error: {str(e)}"
|
| 102 |
})
|
| 103 |
else:
|
| 104 |
print(f" β Tool not found!")
|
| 105 |
+
messages.append({
|
| 106 |
+
"role": "tool",
|
| 107 |
+
"tool_call_id": tool_id,
|
| 108 |
+
"name": tool_name,
|
| 109 |
+
"content": "Tool not found"
|
| 110 |
+
})
|
| 111 |
else:
|
| 112 |
# Model provided final answer
|
| 113 |
answer = response.get("content", "Unknown")
|