Skip to content

Commit 0b6734e

Browse files
feat: Refactor OpenAIFunction to FunctionTool (#966)
Co-authored-by: Wendong <w3ndong.fan@gmail.com> Co-authored-by: Wendong-Fan <133094783+Wendong-Fan@users.noreply.github.com>
1 parent 20af84e commit 0b6734e

32 files changed

+176
-152
lines changed

camel/agents/chat_agent.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
from openai import Stream
6464

6565
from camel.terminators import ResponseTerminator
66-
from camel.toolkits import OpenAIFunction
66+
from camel.toolkits import FunctionTool
6767

6868

6969
logger = logging.getLogger(__name__)
@@ -131,10 +131,10 @@ class ChatAgent(BaseAgent):
131131
(default: :obj:`None`)
132132
output_language (str, optional): The language to be output by the
133133
agent. (default: :obj:`None`)
134-
tools (List[OpenAIFunction], optional): List of available
135-
:obj:`OpenAIFunction`. (default: :obj:`None`)
136-
external_tools (List[OpenAIFunction], optional): List of external tools
137-
(:obj:`OpenAIFunction`) bind to one chat agent. When these tools
134+
tools (List[FunctionTool], optional): List of available
135+
:obj:`FunctionTool`. (default: :obj:`None`)
136+
external_tools (List[FunctionTool], optional): List of external tools
137+
(:obj:`FunctionTool`) bind to one chat agent. When these tools
138138
are called, the agent will directly return the request instead of
139139
processing it. (default: :obj:`None`)
140140
response_terminators (List[ResponseTerminator], optional): List of
@@ -150,8 +150,8 @@ def __init__(
150150
message_window_size: Optional[int] = None,
151151
token_limit: Optional[int] = None,
152152
output_language: Optional[str] = None,
153-
tools: Optional[List[OpenAIFunction]] = None,
154-
external_tools: Optional[List[OpenAIFunction]] = None,
153+
tools: Optional[List[FunctionTool]] = None,
154+
external_tools: Optional[List[FunctionTool]] = None,
155155
response_terminators: Optional[List[ResponseTerminator]] = None,
156156
) -> None:
157157
self.orig_sys_message: BaseMessage = system_message
@@ -795,12 +795,12 @@ def _structure_output_with_function(
795795
r"""Internal function of structuring the output of the agent based on
796796
the given output schema.
797797
"""
798-
from camel.toolkits import OpenAIFunction
798+
from camel.toolkits import FunctionTool
799799

800800
schema_json = get_pydantic_object_schema(output_schema)
801801
func_str = json_to_function_code(schema_json)
802802
func_callable = func_string_to_callable(func_str)
803-
func = OpenAIFunction(func_callable)
803+
func = FunctionTool(func_callable)
804804

805805
original_func_dict = self.func_dict
806806
original_model_dict = self.model_backend.model_config_dict

camel/configs/base_config.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ class BaseConfig(ABC, BaseModel):
3939
@classmethod
4040
def fields_type_checking(cls, tools):
4141
if tools is not None:
42-
from camel.toolkits import OpenAIFunction
42+
from camel.toolkits import FunctionTool
4343

4444
for tool in tools:
45-
if not isinstance(tool, OpenAIFunction):
45+
if not isinstance(tool, FunctionTool):
4646
raise ValueError(
4747
f"The tool {tool} should "
48-
"be an instance of `OpenAIFunction`."
48+
"be an instance of `FunctionTool`."
4949
)
5050
return tools
5151

@@ -54,14 +54,14 @@ def as_dict(self) -> dict[str, Any]:
5454

5555
tools_schema = None
5656
if self.tools:
57-
from camel.toolkits import OpenAIFunction
57+
from camel.toolkits import FunctionTool
5858

5959
tools_schema = []
6060
for tool in self.tools:
61-
if not isinstance(tool, OpenAIFunction):
61+
if not isinstance(tool, FunctionTool):
6262
raise ValueError(
6363
f"The tool {tool} should "
64-
"be an instance of `OpenAIFunction`."
64+
"be an instance of `FunctionTool`."
6565
)
6666
tools_schema.append(tool.get_openai_tool_schema())
6767
config_dict["tools"] = tools_schema

camel/configs/groq_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class GroqConfig(BaseConfig):
7373
user (str, optional): A unique identifier representing your end-user,
7474
which can help OpenAI to monitor and detect abuse.
7575
(default: :obj:`""`)
76-
tools (list[OpenAIFunction], optional): A list of tools the model may
76+
tools (list[FunctionTool], optional): A list of tools the model may
7777
call. Currently, only functions are supported as a tool. Use this
7878
to provide a list of functions the model may generate JSON inputs
7979
for. A max of 128 functions are supported.

camel/configs/openai_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class ChatGPTConfig(BaseConfig):
8181
user (str, optional): A unique identifier representing your end-user,
8282
which can help OpenAI to monitor and detect abuse.
8383
(default: :obj:`""`)
84-
tools (list[OpenAIFunction], optional): A list of tools the model may
84+
tools (list[FunctionTool], optional): A list of tools the model may
8585
call. Currently, only functions are supported as a tool. Use this
8686
to provide a list of functions the model may generate JSON inputs
8787
for. A max of 128 functions are supported.

camel/configs/samba_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class SambaCloudAPIConfig(BaseConfig):
172172
user (str, optional): A unique identifier representing your end-user,
173173
which can help OpenAI to monitor and detect abuse.
174174
(default: :obj:`""`)
175-
tools (list[OpenAIFunction], optional): A list of tools the model may
175+
tools (list[FunctionTool], optional): A list of tools the model may
176176
call. Currently, only functions are supported as a tool. Use this
177177
to provide a list of functions the model may generate JSON inputs
178178
for. A max of 128 functions are supported.

camel/configs/zhipuai_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ZhipuAIConfig(BaseConfig):
4545
in the chat completion. The total length of input tokens and
4646
generated tokens is limited by the model's context length.
4747
(default: :obj:`None`)
48-
tools (list[OpenAIFunction], optional): A list of tools the model may
48+
tools (list[FunctionTool], optional): A list of tools the model may
4949
call. Currently, only functions are supported as a tool. Use this
5050
to provide a list of functions the model may generate JSON inputs
5151
for. A max of 128 functions are supported.

camel/messages/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
'OpenAISystemMessage',
3333
'OpenAIAssistantMessage',
3434
'OpenAIUserMessage',
35+
'OpenAIFunctionMessage',
3536
'OpenAIMessage',
3637
'BaseMessage',
3738
'FunctionCallingMessage',

camel/toolkits/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
# limitations under the License.
1313
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
1414
# ruff: noqa: I001
15-
from .openai_function import (
15+
from .function_tool import (
16+
FunctionTool,
1617
OpenAIFunction,
1718
get_openai_function_schema,
1819
get_openai_tool_schema,
@@ -35,6 +36,7 @@
3536
from .github_toolkit import GithubToolkit
3637

3738
__all__ = [
39+
'FunctionTool',
3840
'OpenAIFunction',
3941
'get_openai_function_schema',
4042
'get_openai_tool_schema',

camel/toolkits/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
from camel.utils import AgentOpsMeta
1818

19-
from .openai_function import OpenAIFunction
19+
from .function_tool import FunctionTool
2020

2121

2222
class BaseToolkit(metaclass=AgentOpsMeta):
23-
def get_tools(self) -> List[OpenAIFunction]:
23+
def get_tools(self) -> List[FunctionTool]:
2424
raise NotImplementedError("Subclasses must implement this method.")

camel/toolkits/code_execution.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from typing import List, Literal
1515

1616
from camel.interpreters import InternalPythonInterpreter
17-
from camel.toolkits import OpenAIFunction
17+
from camel.toolkits import FunctionTool
1818

1919
from .base import BaseToolkit
2020

@@ -58,12 +58,12 @@ def execute_code(self, code: str) -> str:
5858
print(content)
5959
return content
6060

61-
def get_tools(self) -> List[OpenAIFunction]:
62-
r"""Returns a list of OpenAIFunction objects representing the
61+
def get_tools(self) -> List[FunctionTool]:
62+
r"""Returns a list of FunctionTool objects representing the
6363
functions in the toolkit.
6464
6565
Returns:
66-
List[OpenAIFunction]: A list of OpenAIFunction objects
66+
List[FunctionTool]: A list of FunctionTool objects
6767
representing the functions in the toolkit.
6868
"""
69-
return [OpenAIFunction(self.execute_code)]
69+
return [FunctionTool(self.execute_code)]

0 commit comments

Comments
 (0)