Skip to content

Commit 55c6fad

Browse files
Feat: Add external tool support to ChatAgent & Refactor (#830)
Co-authored-by: Wendong-Fan <133094783+Wendong-Fan@users.noreply.github.com> Co-authored-by: Wendong <w3ndong.fan@gmail.com>
1 parent 2b39b7f commit 55c6fad

File tree

13 files changed

+492
-412
lines changed

13 files changed

+492
-412
lines changed

.github/ISSUE_TEMPLATE/bug_report.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ body:
2727
attributes:
2828
label: What version of camel are you using?
2929
description: Run command `python3 -c 'print(__import__("camel").__version__)'` in your shell and paste the output here.
30-
placeholder: E.g., 0.1.7.2
30+
placeholder: E.g., 0.1.8
3131
validations:
3232
required: true
3333

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ conda create --name camel python=3.10
119119
conda activate camel
120120
121121
# Clone github repo
122-
git clone -b v0.1.7.2 https://github.com/camel-ai/camel.git
122+
git clone -b v0.1.8 https://github.com/camel-ai/camel.git
123123
124124
# Change directory into project directory
125125
cd camel

camel/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# limitations under the License.
1313
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
1414

15-
__version__ = '0.1.7.2'
15+
__version__ = '0.1.8'
1616

1717
__all__ = [
1818
'__version__',

camel/agents/chat_agent.py

Lines changed: 223 additions & 244 deletions
Large diffs are not rendered by default.

camel/types/enums.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ def value_for_tiktoken(self) -> str:
9292
return self.value
9393
return "gpt-4o-mini"
9494

95+
@property
96+
def supports_tool_calling(self) -> bool:
97+
return any([self.is_openai, self.is_gemini, self.is_mistral])
98+
9599
@property
96100
def is_openai(self) -> bool:
97101
r"""Returns whether this type of models is an OpenAI-released model."""

camel/utils/commons.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
Mapping,
3030
Optional,
3131
Set,
32+
Type,
3233
TypeVar,
3334
cast,
3435
)
@@ -329,12 +330,12 @@ def get_pydantic_major_version() -> int:
329330
return 0
330331

331332

332-
def get_pydantic_object_schema(pydantic_params: BaseModel) -> Dict:
333+
def get_pydantic_object_schema(pydantic_params: Type[BaseModel]) -> Dict:
333334
r"""Get the JSON schema of a Pydantic model.
334335
335336
Args:
336-
pydantic_params (BaseModel): The Pydantic model to retrieve the schema
337-
for.
337+
pydantic_params (Type[BaseModel]): The Pydantic model class to retrieve
338+
the schema for.
338339
339340
Returns:
340341
dict: The JSON schema of the Pydantic model.
@@ -354,7 +355,7 @@ def func_string_to_callable(code: str):
354355
"""
355356
local_vars: Mapping[str, object] = {}
356357
exec(code, globals(), local_vars)
357-
func = local_vars.get(Constants.FUNC_NAME_FOR_STRUCTURE_OUTPUT)
358+
func = local_vars.get(Constants.FUNC_NAME_FOR_STRUCTURED_OUTPUT)
358359
return func
359360

360361

@@ -397,7 +398,7 @@ def json_to_function_code(json_obj: Dict) -> str:
397398

398399
# function template
399400
function_code = f'''
400-
def {Constants.FUNC_NAME_FOR_STRUCTURE_OUTPUT}({args_str}):
401+
def {Constants.FUNC_NAME_FOR_STRUCTURED_OUTPUT}({args_str}):
401402
r"""Return response with a specified json format.
402403
Args:
403404
{docstring_args_str}

camel/utils/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Constants:
2626
VIDEO_DEFAULT_PLUG_PYAV = "pyav"
2727

2828
# Return response with json format
29-
FUNC_NAME_FOR_STRUCTURE_OUTPUT = "return_json_response"
29+
FUNC_NAME_FOR_STRUCTURED_OUTPUT = "return_json_response"
3030

3131
# Default top k vaule for RAG
3232
DEFAULT_TOP_K_RESULTS = 1

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
project = 'CAMEL'
2828
copyright = '2023, CAMEL-AI.org'
2929
author = 'CAMEL-AI.org'
30-
release = '0.1.7.2'
30+
release = '0.1.8'
3131

3232
html_favicon = (
3333
'https://raw.githubusercontent.com/camel-ai/camel/master/misc/favicon.png'

docs/get_started/setup.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ conda create --name camel python=3.10
6161
conda activate camel
6262
6363
# Clone github repo
64-
git clone -b v0.1.7.2 https://github.com/camel-ai/camel.git
64+
git clone -b v0.1.8 https://github.com/camel-ai/camel.git
6565
6666
# Change directory into project directory
6767
cd camel
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
2+
# Licensed under the Apache License, Version 2.0 (the “License”);
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an “AS IS” BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
14+
15+
from camel.agents import ChatAgent
16+
from camel.configs import ChatGPTConfig
17+
from camel.messages import BaseMessage
18+
from camel.models import ModelFactory
19+
from camel.toolkits import MATH_FUNCS, SEARCH_FUNCS
20+
from camel.types import ModelPlatformType, ModelType
21+
22+
23+
def main():
24+
# Set the tools for the external_tools
25+
internal_tools = SEARCH_FUNCS
26+
external_tools = MATH_FUNCS
27+
tool_list = internal_tools + external_tools
28+
29+
model_config_dict = ChatGPTConfig(
30+
tools=tool_list,
31+
temperature=0.0,
32+
).as_dict()
33+
34+
model = ModelFactory.create(
35+
model_platform=ModelPlatformType.OPENAI,
36+
model_type=ModelType.GPT_3_5_TURBO,
37+
model_config_dict=model_config_dict,
38+
)
39+
40+
# Set external_tools
41+
external_tool_agent = ChatAgent(
42+
system_message=BaseMessage.make_assistant_message(
43+
role_name="Tools calling operator",
44+
content="You are a helpful assistant",
45+
),
46+
model=model,
47+
tools=internal_tools,
48+
external_tools=external_tools,
49+
)
50+
51+
usr_msg = BaseMessage.make_user_message(
52+
role_name="User",
53+
content="When is the release date of the video game Portal?",
54+
)
55+
56+
# This will directly run the internal tool
57+
response = external_tool_agent.step(usr_msg)
58+
print(response.msg.content)
59+
60+
usr_msg = BaseMessage.make_user_message(
61+
role_name="User",
62+
content="What's the result of the release year of Portal subtracted by"
63+
"the year that United States was founded?",
64+
)
65+
# This will first automatically run the internal tool to check the years
66+
# Then it will request the external tool to calculate the sum
67+
response = external_tool_agent.step(usr_msg)
68+
# This should be empty
69+
print(response.msg.content)
70+
external_tool_request = response.info["external_tool_request"]
71+
# This will print the info of the external tool request
72+
print(external_tool_request)
73+
74+
75+
if __name__ == "__main__":
76+
main()
77+
78+
79+
# flake8: noqa :E501
80+
"""
81+
Output:
82+
The video game "Portal" was released in 2007 as part of a bundle called The Orange Box for Windows, Xbox 360, and PlayStation 3.
83+
84+
ChatCompletionMessageToolCall(id='call_U5Xju7vYtAQAEW4D1M8R1kgs', function=Function(arguments='{"a": 2007, "b": 1776}', name='sub'), type='function')
85+
"""

0 commit comments

Comments
 (0)