Skip to content

Commit

Permalink
feat: synthesize the execution of tool calling (#1140)
Browse files Browse the repository at this point in the history
Co-authored-by: Wendong-Fan <[email protected]>
Co-authored-by: Wendong <[email protected]>
  • Loading branch information
3 people authored Nov 14, 2024
1 parent bd0ad2f commit c7e726f
Show file tree
Hide file tree
Showing 9 changed files with 408 additions and 103 deletions.
32 changes: 8 additions & 24 deletions camel/agents/chat_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def __init__(
self.func_dict = {
tool.get_function_name(): tool.func for tool in all_tools
}
self.tool_dict = {tool.get_function_name(): tool for tool in all_tools}

# If the user hasn't configured tools in `BaseModelBackend`,
# the tools set from `ChatAgent` will be used.
Expand Down Expand Up @@ -882,6 +883,7 @@ def _structure_output_with_function(

# Replace the original tools with the structuring function
self.func_dict = {func.get_function_name(): func.func}
self.tool_dict = {func.get_function_name(): func}
self.model_backend.model_config_dict = original_model_dict.copy()
self.model_backend.model_config_dict["tools"] = [
func.get_openai_tool_schema()
Expand Down Expand Up @@ -1182,19 +1184,10 @@ def step_tool_call(
if choice.message.tool_calls is None:
raise RuntimeError("Tool call is None")
func_name = choice.message.tool_calls[0].function.name
func = self.func_dict[func_name]

args_str: str = choice.message.tool_calls[0].function.arguments
args = json.loads(args_str)

# Pass the extracted arguments to the indicated function
try:
result = func(**args)
except Exception:
raise ValueError(
f"Execution of function {func.__name__} failed with "
f"arguments being {args}."
)
args = json.loads(choice.message.tool_calls[0].function.arguments)
tool = self.tool_dict[func_name]
result = tool(**args)

assist_msg = FunctionCallingMessage(
role_name=self.role_name,
Expand Down Expand Up @@ -1243,19 +1236,10 @@ async def step_tool_call_async(
if choice.message.tool_calls is None:
raise RuntimeError("Tool call is None")
func_name = choice.message.tool_calls[0].function.name
func = self.func_dict[func_name]

args_str: str = choice.message.tool_calls[0].function.arguments
args = json.loads(args_str)

# Pass the extracted arguments to the indicated function
try:
result = await func(**args)
except Exception:
raise ValueError(
f"Execution of function {func.__name__} failed with "
f"arguments being {args}."
)
args = json.loads(choice.message.tool_calls[0].function.arguments)
tool = self.tool_dict[func_name]
result = await tool(**args)

assist_msg = FunctionCallingMessage(
role_name=self.role_name,
Expand Down
2 changes: 0 additions & 2 deletions camel/toolkits/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# ruff: noqa: I001
from .function_tool import (
FunctionTool,
OpenAIFunction,
get_openai_function_schema,
get_openai_tool_schema,
generate_docstring,
Expand Down Expand Up @@ -44,7 +43,6 @@
__all__ = [
'BaseToolkit',
'FunctionTool',
'OpenAIFunction',
'get_openai_function_schema',
'get_openai_tool_schema',
"generate_docstring",
Expand Down
Loading

0 comments on commit c7e726f

Please sign in to comment.