Skip to content

Commit e8e205c

Browse files
fix: ensure all custom tools are available in chatbot integration
Previously, when agents were integrated into the chatbot UI, only the last tool in the tools list was available due to the agent.tools assignment being inside the tool loading loop. This caused the tools to be overwritten on each iteration. This commit moves the tool assignment outside the loop, ensuring all tools are collected before being assigned to the agent. This fixes the issue where custom tools were not working properly in the chatbot context while maintaining full backward compatibility. Fixes #616 Co-authored-by: Mervin Praison <[email protected]>
1 parent 53c0b84 commit e8e205c

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

src/praisonai/praisonai/ui/agents.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,12 @@ def step_callback_sync(step_details):
467467
role_tools.append(callable_func)
468468
# Add API tool definition to task's tools
469469
task_tools.append(tool_def)
470-
# Also set the agent's tools to include both
471-
agent.tools = role_tools
472470
else:
473471
logger.warning(f"Tool '{tool_name}' not found. Skipping.")
472+
473+
# Set the agent's tools after collecting all tools
474+
if role_tools:
475+
agent.tools = role_tools
474476

475477
for tname, tdetails in details.get('tasks', {}).items():
476478
description_filled = tdetails['description'].format(topic=topic)

test_custom_tools_fix.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Test script to verify the custom tools fix in chatbot integration.
4+
This demonstrates that agents can now use multiple custom tools when integrated into the chatbot.
5+
"""
6+
7+
import yaml
8+
import os
9+
10+
# Create a sample agents.yaml file
11+
agents_yaml_content = """
12+
roles:
13+
researcher:
14+
name: "Research Agent"
15+
role: "Information Researcher"
16+
goal: "Find and analyze information using multiple tools"
17+
backstory: "You are an expert researcher with access to multiple information sources"
18+
llm: "gpt-4o-mini"
19+
tools:
20+
- search_web
21+
- get_weather
22+
- calculate
23+
tasks:
24+
test_task:
25+
description: "Use all available tools to gather information about {topic}"
26+
expected_output: "A comprehensive report using data from all tools"
27+
"""
28+
29+
# Create a sample tools.py file
30+
tools_py_content = '''
31+
def search_web(query: str) -> str:
32+
"""Search the web for information.
33+
34+
Args:
35+
query: The search query
36+
37+
Returns:
38+
Search results
39+
"""
40+
return f"Web search results for: {query}"
41+
42+
def get_weather(location: str) -> str:
43+
"""Get weather information for a location.
44+
45+
Args:
46+
location: The location to get weather for
47+
48+
Returns:
49+
Weather information
50+
"""
51+
return f"Weather in {location}: Sunny, 25°C"
52+
53+
def calculate(expression: str) -> str:
54+
"""Perform a calculation.
55+
56+
Args:
57+
expression: Mathematical expression to evaluate
58+
59+
Returns:
60+
Calculation result
61+
"""
62+
try:
63+
result = eval(expression)
64+
return f"Result: {result}"
65+
except:
66+
return "Invalid expression"
67+
'''
68+
69+
# Write the test files
70+
with open("test_agents.yaml", "w") as f:
71+
f.write(agents_yaml_content)
72+
73+
with open("test_tools.py", "w") as f:
74+
f.write(tools_py_content)
75+
76+
print("Test files created successfully!")
77+
print("\nTo test the fix:")
78+
print("1. Copy test_agents.yaml to agents.yaml")
79+
print("2. Copy test_tools.py to tools.py")
80+
print("3. Run the chatbot UI")
81+
print("4. Ask the agent to use multiple tools")
82+
print("\nThe agent should now be able to use all three tools (search_web, get_weather, calculate)")
83+
print("instead of just the last one.")

0 commit comments

Comments
 (0)