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

feat: synthesize the execution of tool calling (#1140)

Zeyu Zhangc7e726fdcfba
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index 112b0584a..a9efebb80 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -193,6 +193,7 @@ class ChatAgent(BaseAgent):
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.
@@ -882,6 +883,7 @@ class ChatAgent(BaseAgent):
# 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()
@@ -1182,19 +1184,10 @@ class ChatAgent(BaseAgent):
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,
@@ -1243,19 +1236,10 @@ class ChatAgent(BaseAgent):
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,
diff --git a/camel/toolkits/__init__.py b/camel/toolkits/__init__.py
index 8634d6941..c394ac8b6 100644
--- a/camel/toolkits/__init__.py
+++ b/camel/toolkits/__init__.py
@@ -14,7 +14,6 @@
# ruff: noqa: I001
from .function_tool import (
FunctionTool,
- OpenAIFunction,
get_openai_function_schema,
get_openai_tool_schema,
generate_docstring,
@@ -44,7 +43,6 @@ from .notion_toolkit import NotionToolkit
__all__ = [
'BaseToolkit',
'FunctionTool',
- 'OpenAIFunction',
'get_openai_function_schema',
'get_openai_tool_schema',
"generate_docstring",
diff --git a/camel/toolkits/function_tool.py b/camel/toolkits/function_tool.py
index 926de62cb..b70532bdd 100644
--- a/camel/toolkits/function_tool.py
+++ b/camel/toolkits/function_tool.py
@@ -11,20 +11,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+import ast
+import inspect
import logging
import warnings
from inspect import Parameter, getsource, signature
-from typing import Any, Callable, Dict, Mapping, Optional, Tuple
+from typing import Any, Callable, Dict, Mapping, Optional, Tuple, Type
from docstring_parser import parse
from jsonschema.exceptions import SchemaError
from jsonschema.validators import Draft202012Validator as JSONValidator
-from pydantic import create_model
+from pydantic import BaseModel, create_model
from pydantic.fields import FieldInfo
from camel.agents import ChatAgent
-from camel.models import BaseModelBackend
-from camel.types import ModelType
+from camel.models import BaseModelBackend, ModelFactory
+from camel.types import ModelPlatformType, ModelType
from camel.utils import get_pydantic_object_schema, to_pascal
logger = logging.getLogger(__name__)
@@ -227,47 +229,105 @@ class FunctionTool:
openai_tool_schema (Optional[Dict[str, Any]], optional): A
user-defined OpenAI tool schema to override the default result.
(default: :obj:`None`)
- use_schema_assistant (Optional[bool], optional): Whether to enable the
- use of a schema assistant model to automatically generate the
+ synthesize_schema (Optional[bool], optional): Whether to enable the
+ use of a schema assistant model to automatically synthesize the
schema if validation fails or no valid schema is provided.
(default: :obj:`False`)
- schema_assistant_model (Optional[BaseModelBackend], optional): An
- assistant model (e.g., an LLM model) used to generate the schema
- if `use_schema_assistant` is enabled and no valid schema is
+ synthesize_schema_model (Optional[BaseModelBackend], optional): An
+ assistant model (e.g., an LLM model) used to synthesize the schema
+ if `synthesize_schema` is enabled and no valid schema is
provided. (default: :obj:`None`)
- schema_generation_max_retries (int, optional): The maximum
- number of attempts to retry schema generation using the schema
+ synthesize_schema_max_retries (int, optional): The maximum
+ number of attempts to retry schema synthesis using the schema
assistant model if the previous attempts fail. (default: 2)
+ synthesize_output (Optional[bool], optional): Flag for enabling
+ synthesis output mode, where output is synthesized based on the
+ function's execution. (default: :obj:`False`)
+ synthesize_output_model (Optional[BaseModelBackend], optional):
+ Model used for output synthesis in synthesis mode.
+ (default: :obj:`None`)
+ synthesize_output_format (Optional[Type[BaseModel]], optional): Format
+ for the response when synthesizing output. (default: :obj:`None`)
"""
def __init__(
self,
func: Callable,
openai_tool_schema: Optional[Dict[str, Any]] = None,
- use_schema_assistant: Optional[bool] = False,
- schema_assistant_model: Optional[BaseModelBackend] = None,
- schema_generation_max_retries: int = 2,
+ synthesize_schema: Optional[bool] = False,
+ synthesize_schema_model: Optional[BaseModelBackend] = None,
+ synthesize_schema_max_retries: int = 2,
+ synthesize_output: Optional[bool] = False,
+ synthesize_output_model: Optional[BaseModelBackend] = None,
+ synthesize_output_format: Optional[Type[BaseModel]] = None,
) -> None:
self.func = func
self.openai_tool_schema = openai_tool_schema or get_openai_tool_schema(
func
)
+ self.synthesize_output = synthesize_output
+ self.synthesize_output_model = synthesize_output_model
+ if synthesize_output and synthesize_output_model is None:
+ self.synthesize_output_model = ModelFactory.create(
+ model_platform=ModelPlatformType.DEFAULT,
+ model_type=ModelType.DEFAULT,
+ )
+ logger.warning(
+ "Warning: No synthesize_output_model provided. "
+ f"Use `{self.synthesize_output_model.model_type}` to "
+ "synthesize the output."
+ )
+ self.synthesize_output_format: Optional[type[BaseModel]] = None
+ return_annotation = inspect.signature(self.func).return_annotation
+ if synthesize_output_format is not None:
+ self.synthesize_output_format = synthesize_output_format
+ elif isinstance(return_annotation, type) and issubclass(
+ return_annotation, BaseModel
+ ):
+ self.synthesize_output_format = return_annotation
- if use_schema_assistant:
+ self.synthesize_schema_model = synthesize_schema_model
+ if synthesize_schema:
if openai_tool_schema:
logger.warning("""The user-defined OpenAI tool schema will be
overridden by the schema assistant model.""")
- schema = self.generate_openai_tool_schema(
- schema_generation_max_retries, schema_assistant_model
+ if self.synthesize_schema_model is None:
+ self.synthesize_schema_model = ModelFactory.create(
+ model_platform=ModelPlatformType.DEFAULT,
+ model_type=ModelType.DEFAULT,
+ )
+ logger.warning(
+ "Warning: No synthesize_schema_model provided. "
+ f"Use `{self.synthesize_schema_model.model_type}` to "
+ "synthesize the schema."
+ )
+ schema = self.synthesize_openai_tool_schema(
+ synthesize_schema_max_retries
)
if schema:
self.openai_tool_schema = schema
else:
raise ValueError(
- f"Failed to generate valid schema for "
+ f"Failed to synthesize a valid schema for "
f"{self.func.__name__}."
)
+ def __call__(self, *args: Any, **kwargs: Any) -> Any:
+ if self.synthesize_output:
+ result = self.synthesize_execution_output(args, kwargs)
+ return result
+ else:
+ # Pass the extracted arguments to the indicated function
+ try:
+ result = self.func(*args, **kwargs)
+ return result
+ except Exception as e:
+ raise ValueError(
+ f"Execution of function {self.func.__name__} failed with "
+ f"arguments {args} and {kwargs}. "
+ f"Error: {e}"
+ )
+
@staticmethod
def validate_openai_tool_schema(
openai_tool_schema: Dict[str, Any],
@@ -464,52 +524,42 @@ class FunctionTool:
param_name
] = value
- def generate_openai_tool_schema(
+ def synthesize_openai_tool_schema(
self,
max_retries: Optional[int] = None,
- schema_assistant_model: Optional[BaseModelBackend] = None,
) -> Dict[str, Any]:
- r"""Generates an OpenAI tool schema for the specified function.
+ r"""Synthesizes an OpenAI tool schema for the specified function.
- This method uses a language model (LLM) to generate the OpenAI tool
+ This method uses a language model (LLM) to synthesize the OpenAI tool
schema for the specified function by first generating a docstring and
- then creating a schema based on the function's source code. If no LLM
- is provided, it defaults to initializing a gpt-4o-mini model. The
- schema generation and validation process is retried up to
+ then creating a schema based on the function's source code. The
+ schema synthesis and validation process is retried up to
`max_retries` times in case of failure.
-
Args:
max_retries (Optional[int], optional): The maximum number of
- retries for schema generation and validation if the process
+ retries for schema synthesis and validation if the process
fails. (default: :obj:`None`)
- schema_assistant_model (Optional[BaseModelBackend], optional): An
- optional LLM backend model used for generating the docstring
- and schema. If not provided, a gpt-4o-mini model
- will be created. (default: :obj:`None`)
Returns:
- Dict[str, Any]: The generated OpenAI tool schema for the function.
+ Dict[str, Any]: The synthesis OpenAI tool schema for the function.
Raises:
- ValueError: If schema generation or validation fails after the
+ ValueError: If schema synthesis or validation fails after the
maximum number of retries, a ValueError is raised, prompting
manual schema setting.
"""
- if not schema_assistant_model:
- logger.warning(
- "Warning: No model provided. "
- f"Use `{ModelType.GPT_4O_MINI.value}` to generate the schema."
- )
code = getsource(self.func)
retries = 0
if max_retries is None:
max_retries = 0
- # Retry loop to handle schema generation and validation
+ # Retry loop to handle schema synthesis and validation
while retries <= max_retries:
try:
# Generate the docstring and the schema
- docstring = generate_docstring(code, schema_assistant_model)
+ docstring = generate_docstring(
+ code, self.synthesize_schema_model
+ )
self.func.__doc__ = docstring
schema = get_openai_tool_schema(self.func)
# Validate the schema
@@ -520,7 +570,7 @@ class FunctionTool:
retries += 1
if retries == max_retries:
raise ValueError(
- f"Failed to generate the OpenAI tool Schema after "
+ f"Failed to synthesize the OpenAI tool Schema after "
f"{max_retries} retries. "
f"Please set the OpenAI tool schema for "
f"function {self.func.__name__} manually."
@@ -529,6 +579,102 @@ class FunctionTool:
return {}
+ def synthesize_execution_output(
+ self,
+ args: Optional[tuple[Any, ...]] = None,
+ kwargs: Optional[Dict[str, Any]] = None,
+ ) -> Any:
+ r"""Synthesizes the output of the function based on the provided
+ positional arguments and keyword arguments.
+
+ Args:
+ args (Optional[tuple]): Positional arguments to pass to the
+ function during synthesis. (default: :obj:`None`)
+ kwargs (Optional[Dict[str, Any]]): Keyword arguments to pass to the
+ function during synthesis. (default: :obj:`None`)
+
+ Returns:
+ Any: Synthesized output from the function execution. If no
+ synthesis model is provided, a warning is logged.
+ """
+ import textwrap
+
+ # Retrieve the function source code
+ function_string = inspect.getsource(self.func)
+
+ # Check and update docstring if necessary
+ if self.func.__doc__ is not None:
+ function_string = textwrap.dedent(function_string)
+ tree = ast.parse(function_string)
+ func_node = (
+ tree.body[0]
+ if isinstance(tree.body[0], ast.FunctionDef)
+ else None
+ )
+ if func_node:
+ existing_docstring = ast.get_docstring(func_node)
+ if existing_docstring != self.func.__doc__:
+ func_node.body[0] = ast.Expr(
+ value=ast.Constant(value=self.func.__doc__, kind=None)
+ )
+ function_string = ast.unparse(tree)
+
+ # Append the args and kwargs information to the function string
+ if args:
+ function_string += f"\nargs:\n{list(args)}"
+ if kwargs:
+ function_string += f"\nkwargs:\n{kwargs}"
+
+ # Define the assistant system message
+ assistant_sys_msg = '''
+**Role:** AI Assistant specialized in synthesizing tool execution outputs
+without actual execution.
+
+**Capabilities:**
+- Analyzes function to understand their
+ purpose and expected outputs.
+- Generates synthetic outputs based on the function logic.
+- Ensures the synthesized output is contextually accurate and aligns with the
+ function's intended behavior.
+
+**Instructions:**
+1. **Input:** Provide the function code, function docstring, args, and kwargs.
+2. **Output:** Synthesize the expected output of the function based on the
+ provided args and kwargs.
+
+**Example:**
+- **User Input:**
+def sum(a, b, c=0):
+ """Adds three numbers together."""
+ return a + b + c
+
+- **Input Arguments:**
+args: (1, 2)
+kwargs: {"c": 3}
+
+- **Output:**
+6
+
+**Note:**
+- Just return the synthesized output of the function without any explanation.
+- The output should be in plain text without any formatting.
+'''
+
+ # Initialize the synthesis agent
+ synthesis_agent = ChatAgent(
+ assistant_sys_msg,
+ model=self.synthesize_output_model,
+ )
+
+ # User message combining function string and additional context
+ user_msg = function_string
+ response = synthesis_agent.step(
+ user_msg,
+ response_format=self.synthesize_output_format,
+ )
+
+ return response.msg.content
+
@property
def parameters(self) -> Dict[str, Any]:
r"""Getter method for the property :obj:`parameters`.
@@ -555,25 +701,3 @@ class FunctionTool:
except SchemaError as e:
raise e
self.openai_tool_schema["function"]["parameters"]["properties"] = value
-
-
-warnings.simplefilter('always', DeprecationWarning)
-
-
-# Alias for backwards compatibility
-class OpenAIFunction(FunctionTool):
- r"""Alias for backwards compatibility."""
-
- def __init__(self, *args, **kwargs):
- PURPLE = '\033[95m'
- RESET = '\033[0m'
-
- def purple_warning(msg):
- warnings.warn(
- PURPLE + msg + RESET, DeprecationWarning, stacklevel=2
- )
-
- purple_warning(
- "OpenAIFunction is deprecated, please use FunctionTool instead."
- )
- super().__init__(*args, **kwargs)
diff --git a/camel/toolkits/whatsapp_toolkit.py b/camel/toolkits/whatsapp_toolkit.py
index db504a661..4902a41b4 100644
--- a/camel/toolkits/whatsapp_toolkit.py
+++ b/camel/toolkits/whatsapp_toolkit.py
@@ -163,11 +163,11 @@ class WhatsAppToolkit(BaseToolkit):
return f"Failed to retrieve business profile: {e!s}"
def get_tools(self) -> List[FunctionTool]:
- r"""Returns a list of OpenAIFunction objects representing the
+ r"""Returns a list of FunctionTool objects representing the
functions in the toolkit.
Returns:
- List[OpenAIFunction]: A list of OpenAIFunction objects for the
+ List[FunctionTool]: A list of FunctionTool objects for the
toolkit methods.
"""
return [
diff --git a/docs/cookbooks/agents_with_tools.ipynb b/docs/cookbooks/agents_with_tools.ipynb
index 4fa571e87..74ef11d94 100644
--- a/docs/cookbooks/agents_with_tools.ipynb
+++ b/docs/cookbooks/agents_with_tools.ipynb
@@ -187,12 +187,13 @@
]
},
{
+ "attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "uv0pYDQvXGER"
},
"source": [
- "Add these 2 customized functions as CAMEL’s OpenAIFunction list:"
+ "Add these 2 customized functions as CAMEL’s FunctionTool list:"
]
},
{
diff --git a/examples/toolkits/generate_openai_tool_schema.py b/examples/toolkits/generate_openai_tool_schema.py
index 720504486..14a2edec2 100644
--- a/examples/toolkits/generate_openai_tool_schema.py
+++ b/examples/toolkits/generate_openai_tool_schema.py
@@ -25,7 +25,7 @@ def get_perfect_square(n: int) -> int:
# Create a FunctionTool with the function
function_tool = FunctionTool(
get_perfect_square,
- use_schema_assistant=True,
+ synthesize_schema=True,
)
print("\nGenerated OpenAI Tool Schema:")
print(function_tool.get_openai_tool_schema())
diff --git a/examples/toolkits/synthesize_function_execution.py b/examples/toolkits/synthesize_function_execution.py
new file mode 100644
index 000000000..4f6ebd4b9
--- /dev/null
+++ b/examples/toolkits/synthesize_function_execution.py
@@ -0,0 +1,103 @@
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+# Licensed under the Apache License, Version 2.0 (the ā€œLicenseā€);
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an ā€œAS ISā€ BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+
+from typing import Any, Dict
+
+import requests
+from pydantic import BaseModel, Field
+
+from camel.agents import ChatAgent
+from camel.toolkits import FunctionTool
+
+
+# example function
+def movie_data_by_id(id: int) -> Dict[str, Any]:
+ r"""Fetch movie data by its ID from the IMDB Top 100 Movies API.
+
+ Args:
+ id (int): The ID of the movie to retrieve information for.
+
+ Returns:
+ Dict[str, Any]: A dictionary with the following keys:
+ - rank (int): The rank of the movie in the top 100 list.
+ - movie_title (str): The title of the movie.
+ - rating (str): The movie's rating.
+ - id (str): The unique identifier of the movie.
+ - year (int): The release year of the movie.
+ - description (str): A brief description of the movie.
+
+ Raises:
+ Exception: If an unexpected error occurs while fetching the data.
+ """
+ try:
+ url = f"https://imdb-top-100-movies.p.rapidapi.com/{id}"
+ headers = {
+ "x-rapidapi-key": "Your API Key",
+ "x-rapidapi-host": "imdb-top-100-movies.p.rapidapi.com",
+ }
+ response = requests.get(url, headers=headers)
+ return response.json()
+ except Exception as e:
+ return {
+ "error": str(e),
+ }
+
+
+# Define the response format for movie data
+class MovieResponse(BaseModel):
+ rating: str = Field(description="The movie's rating.")
+ description: str = Field(description="A brief description of the movie.")
+ movie_title: str = Field(description="The title of the movie.")
+
+
+real_get_movie = FunctionTool(movie_data_by_id)
+synthesized_get_movie = FunctionTool(movie_data_by_id, synthesize_output=True)
+
+assistant_sys_msg = "You are a helpful assistant."
+user_msg = (
+ "What is the rating, description and movie_title of the movie with id 2048"
+)
+
+print("Synthesize output: False")
+real_agent = ChatAgent(assistant_sys_msg, tools=[real_get_movie])
+assistant_response = real_agent.step(user_msg)
+print(assistant_response.msg.content)
+
+
+print("\nSynthesize output: True")
+synthesized_agent = ChatAgent(assistant_sys_msg, tools=[synthesized_get_movie])
+assistant_response = synthesized_agent.step(
+ user_msg, response_format=MovieResponse
+)
+print(assistant_response.msg.content)
+
+"""
+===============================================================================
+Warning: No synthesize_output_model provided. Use `gpt-4o-mini` to synthesize
+the output.
+Synthesize output: False
+It seems that I'm unable to access the movie data at the moment due to a
+subscription issue with the API. However, if you provide me with the title of
+the movie or any other details, I can help you find information about it!
+===============================================================================
+"""
+
+"""
+===============================================================================
+Synthesize output: True
+{'rating': '8.8', 'description': 'A thief who steals corporate secrets through
+the use of dream-sharing technology is given the inverse task of planting an
+idea into the mind of a CEO.', 'movie_title': 'Inception'}
+===============================================================================
+"""
diff --git a/test/toolkits/test_generate_openai_tool_schema.py b/test/toolkits/test_generate_openai_tool_schema.py
index 83731e8ee..1a7ab168c 100644
--- a/test/toolkits/test_generate_openai_tool_schema.py
+++ b/test/toolkits/test_generate_openai_tool_schema.py
@@ -36,14 +36,14 @@ def sample_function(a: int, b: str = "default") -> bool:
@patch.object(FunctionTool, 'validate_openai_tool_schema')
[email protected](FunctionTool, 'generate_openai_tool_schema')
-def test_generate_openai_tool_schema(
[email protected](FunctionTool, 'synthesize_openai_tool_schema')
+def test_synthesize_openai_tool_schema(
mock_generate_schema, mock_validate_schema
):
# Mock the validate_openai_tool_schema return None
mock_validate_schema.return_value = None
- # Mock the generate_openai_tool_schema to return a specific schema
+ # Mock the synthesize_openai_tool_schema to return a specific schema
mock_schema = {
'type': 'function',
'function': {
@@ -73,11 +73,11 @@ def test_generate_openai_tool_schema(
}
mock_generate_schema.return_value = mock_schema
- # Create FunctionTool instance with use_schema_assistant=True
+ # Create FunctionTool instance with synthesize_schema=True
function_tool = FunctionTool(
func=sample_function,
- use_schema_assistant=True,
- schema_assistant_model=None,
+ synthesize_schema=True,
+ synthesize_schema_model=None,
)
# Assert that the generated schema matches the expected schema
@@ -189,12 +189,12 @@ def test_function_tool_generate_schema_with_retries(
mock_response,
]
- # Create FunctionTool instance with use_schema_assistant=True
+ # Create FunctionTool instance with synthesize_schema=True
function_tool = FunctionTool(
func=sample_function,
- use_schema_assistant=True,
- schema_assistant_model=mock_model,
- schema_generation_max_retries=2,
+ synthesize_schema=True,
+ synthesize_schema_model=mock_model,
+ synthesize_schema_max_retries=2,
)
expected_schema = {
diff --git a/test/toolkits/test_synthesize_output.py b/test/toolkits/test_synthesize_output.py
new file mode 100644
index 000000000..83c624aa1
--- /dev/null
+++ b/test/toolkits/test_synthesize_output.py
@@ -0,0 +1,95 @@
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+# Licensed under the Apache License, Version 2.0 (the ā€œLicenseā€);
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an ā€œAS ISā€ BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+import pytest
+from pydantic import BaseModel, Field
+
+from camel.models import OpenAIModel
+from camel.toolkits import FunctionTool
+from camel.types import ModelType
+
+
+def sample_function(a, b):
+ """Adds two numbers together."""
+ return a + b
+
+
+def test_synthesize_execution_output_with_kwargs():
+ function_tool = FunctionTool(
+ func=sample_function,
+ synthesize_output=True,
+ # Not providing synthesize_output_model to use default model
+ )
+
+ kwargs = {'a': 2, 'b': 3}
+ output = function_tool.synthesize_execution_output(kwargs=kwargs)
+
+ assert output.strip() == '5'
+
+
+def test_synthesize_execution_output_without_kwargs():
+ function_tool = FunctionTool(
+ func=sample_function,
+ synthesize_output=True,
+ # Not providing synthesize_output_model to use default model
+ )
+
+ output = function_tool.synthesize_execution_output()
+
+ # Since no arguments are provided, the assistant may return an error
+ # message or synthesized output.
+ # We can assert that the output is not empty.
+ assert output is not None
+ assert len(output.strip()) > 0
+
+
+class FormattedResponse(BaseModel):
+ sum_value: int = Field(description="The sum value.")
+
+
+def test_synthesize_execution_output_with_synthesize_output_format():
+ # Assuming you have a custom response format
+
+ # Create a response format if applicable (e.g., a Pydantic model)
+ synthesize_output_format = FormattedResponse
+
+ function_tool = FunctionTool(
+ func=sample_function,
+ synthesize_output=True,
+ synthesize_output_format=synthesize_output_format,
+ )
+
+ kwargs = {'a': 2, 'b': 3}
+ output = function_tool.synthesize_execution_output(kwargs=kwargs)
+
+ assert output.strip() == "{'sum_value': 5}"
+
+
+def test_synthesize_output_with_custom_model():
+ # Use a specific model by specifying model_type
+ custom_model = OpenAIModel(model_type=ModelType.GPT_4O_MINI)
+
+ function_tool = FunctionTool(
+ func=sample_function,
+ synthesize_output=True,
+ synthesize_output_model=custom_model,
+ )
+
+ kwargs = {'a': 10, 'b': 15}
+ output = function_tool.synthesize_execution_output(kwargs=kwargs)
+
+ assert output.strip() == '25'