Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

chore: fix_schema-is-broken-in-workforce (#3170)

Tao Sun2dfcc0f7324f
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index 899e429f0..a9ea5d2aa 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -1687,7 +1687,6 @@ class ChatAgent(BaseAgent):
return self._step_terminate(
e.args[1], tool_call_records, "max_tokens_exceeded"
)
-
response = await self._aget_model_response(
openai_messages,
num_tokens=num_tokens,
@@ -3770,7 +3769,7 @@ class ChatAgent(BaseAgent):
self.memory.get_context_creator(), "token_limit", None
),
output_language=self._output_language,
- tools=cloned_tools,
+ tools=cast(List[Union[FunctionTool, Callable]], cloned_tools),
toolkits_to_register_agent=toolkits_to_register,
external_tools=[
schema for schema in self._external_tool_schemas.values()
@@ -3799,9 +3798,7 @@ class ChatAgent(BaseAgent):
def _clone_tools(
self,
- ) -> Tuple[
- List[Union[FunctionTool, Callable]], List[RegisteredAgentToolkit]
- ]:
+ ) -> Tuple[List[FunctionTool], List[RegisteredAgentToolkit]]:
r"""Clone tools and return toolkits that need agent registration.
This method handles stateful toolkits by cloning them if they have
@@ -3858,13 +3855,45 @@ class ChatAgent(BaseAgent):
method_name = tool.func.__name__
if hasattr(toolkit, method_name):
new_method = getattr(toolkit, method_name)
- cloned_tools.append(new_method)
+ # Wrap cloned method into a new FunctionTool,
+ # preserving schema
+ try:
+ new_tool = FunctionTool(
+ func=new_method,
+ openai_tool_schema=tool.get_openai_tool_schema(),
+ )
+ cloned_tools.append(new_tool)
+ except Exception as e:
+ # If wrapping fails, fallback to wrapping the original
+ # function with its schema to maintain consistency
+ logger.warning(
+ f"Failed to wrap cloned toolkit "
+ f"method '{method_name}' "
+ f"with FunctionTool: {e}. Using original "
+ f"function with preserved schema instead."
+ )
+ cloned_tools.append(
+ FunctionTool(
+ func=tool.func,
+ openai_tool_schema=tool.get_openai_tool_schema(),
+ )
+ )
else:
- # Fallback to original function
- cloned_tools.append(tool.func)
+ # Fallback to original function wrapped in FunctionTool
+ cloned_tools.append(
+ FunctionTool(
+ func=tool.func,
+ openai_tool_schema=tool.get_openai_tool_schema(),
+ )
+ )
else:
- # Not a toolkit method, just use the original function
- cloned_tools.append(tool.func)
+ # Not a toolkit method, preserve FunctionTool schema directly
+ cloned_tools.append(
+ FunctionTool(
+ func=tool.func,
+ openai_tool_schema=tool.get_openai_tool_schema(),
+ )
+ )
return cloned_tools, toolkits_to_register
diff --git a/camel/societies/workforce/workforce.py b/camel/societies/workforce/workforce.py
index 8e0c91015..0d612e849 100644
--- a/camel/societies/workforce/workforce.py
+++ b/camel/societies/workforce/workforce.py
@@ -23,6 +23,7 @@ from collections import deque
from enum import Enum
from typing import (
Any,
+ Callable,
Coroutine,
Deque,
Dict,
@@ -32,6 +33,7 @@ from typing import (
Set,
Tuple,
Union,
+ cast,
)
from colorama import Fore
@@ -71,6 +73,7 @@ from camel.tasks.task import (
)
from camel.toolkits import (
CodeExecutionToolkit,
+ FunctionTool,
SearchToolkit,
TaskPlanningToolkit,
ThinkingToolkit,
@@ -345,10 +348,7 @@ class Workforce(BaseNode):
None,
),
output_language=coordinator_agent.output_language,
- tools=[
- tool.func
- for tool in coordinator_agent._internal_tools.values()
- ],
+ tools=list(coordinator_agent._internal_tools.values()),
external_tools=[
schema
for schema in coordinator_agent._external_tool_schemas.values() # noqa: E501
@@ -398,9 +398,11 @@ class Workforce(BaseNode):
# Since ChatAgent constructor uses a dictionary with
# function names as keys, we don't need to manually deduplicate.
- combined_tools = [
- tool.func for tool in task_agent._internal_tools.values()
- ] + [tool.func for tool in task_planning_tools]
+ combined_tools: List[Union[FunctionTool, Callable]] = cast(
+ List[Union[FunctionTool, Callable]],
+ list(task_agent._internal_tools.values())
+ + task_planning_tools,
+ )
# Create a new agent with the provided agent's configuration
# but with the combined system message and tools