🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
chore: minor fix and update (#1188)
Wendong-Fandb1d9ef3cf5a
diff --git a/camel/models/samba_model.py b/camel/models/samba_model.pyindex fa7a9442a..a14fcfe0b 100644--- a/camel/models/samba_model.py+++ b/camel/models/samba_model.py@@ -158,7 +158,8 @@ class SambaModel(BaseModelBackend):`ChatCompletion` in the non-stream mode, or`Stream[ChatCompletionChunk]` in the stream mode."""-+ if "tools" in self.model_config_dict:+ del self.model_config_dict["tools"]if self.model_config_dict.get("stream") is True:return self._run_streaming(messages)else:diff --git a/camel/societies/workforce/workforce.py b/camel/societies/workforce/workforce.pyindex 1745ab814..3c9c98ae1 100644--- a/camel/societies/workforce/workforce.py+++ b/camel/societies/workforce/workforce.py@@ -41,7 +41,7 @@ from camel.societies.workforce.utils import ()from camel.societies.workforce.worker import Workerfrom camel.tasks.task import Task, TaskState-from camel.toolkits import SEARCH_FUNCS, WEATHER_FUNCS, GoogleMapsToolkit+from camel.toolkits import GoogleMapsToolkit, SearchToolkit, WeatherToolkitfrom camel.types import ModelPlatformType, ModelTypelogger = logging.getLogger(__name__)@@ -353,8 +353,8 @@ class Workforce(BaseNode):# Default tools for a new agentfunction_list = [- *SEARCH_FUNCS,- *WEATHER_FUNCS,+ *SearchToolkit().get_tools(),+ *WeatherToolkit().get_tools(),*GoogleMapsToolkit().get_tools(),]diff --git a/camel/toolkits/__init__.py b/camel/toolkits/__init__.pyindex c394ac8b6..451d94760 100644--- a/camel/toolkits/__init__.py+++ b/camel/toolkits/__init__.py@@ -20,12 +20,11 @@ from .function_tool import ()from .open_api_specs.security_config import openapi_security_config-from .math_toolkit import MathToolkit, MATH_FUNCS-from .search_toolkit import SearchToolkit, SEARCH_FUNCS-from .weather_toolkit import WeatherToolkit, WEATHER_FUNCS-from .dalle_toolkit import DalleToolkit, DALLE_FUNCS+from .math_toolkit import MathToolkit+from .search_toolkit import SearchToolkit+from .weather_toolkit import WeatherToolkit+from .dalle_toolkit import DalleToolkitfrom .ask_news_toolkit import AskNewsToolkit, AsyncAskNewsToolkit-from .linkedin_toolkit import LinkedInToolkitfrom .reddit_toolkit import RedditToolkitfrom .base import BaseToolkit@@ -35,7 +34,7 @@ from .github_toolkit import GithubToolkitfrom .google_scholar_toolkit import GoogleScholarToolkitfrom .arxiv_toolkit import ArxivToolkitfrom .slack_toolkit import SlackToolkit-from .twitter_toolkit import TwitterToolkit, TWITTER_FUNCS+from .twitter_toolkit import TwitterToolkitfrom .open_api_toolkit import OpenAPIToolkitfrom .retrieval_toolkit import RetrievalToolkitfrom .notion_toolkit import NotionToolkit@@ -65,9 +64,4 @@ __all__ = ['GoogleScholarToolkit','NotionToolkit','ArxivToolkit',- 'MATH_FUNCS',- 'SEARCH_FUNCS',- 'WEATHER_FUNCS',- 'DALLE_FUNCS',- 'TWITTER_FUNCS',]diff --git a/camel/toolkits/dalle_toolkit.py b/camel/toolkits/dalle_toolkit.pyindex 8b2907e78..7f1811cee 100644--- a/camel/toolkits/dalle_toolkit.py+++ b/camel/toolkits/dalle_toolkit.py@@ -140,6 +140,3 @@ class DalleToolkit(BaseToolkit):representing the functions in the toolkit."""return [FunctionTool(self.get_dalle_img)]---DALLE_FUNCS: List[FunctionTool] = DalleToolkit().get_tools()diff --git a/camel/toolkits/function_tool.py b/camel/toolkits/function_tool.pyindex b70532bdd..efcf04b66 100644--- a/camel/toolkits/function_tool.py+++ b/camel/toolkits/function_tool.py@@ -42,6 +42,31 @@ def _remove_a_key(d: Dict, remove_key: Any) -> None:_remove_a_key(d[key], remove_key)+def _remove_title_recursively(data, parent_key=None):+ r"""Recursively removes the 'title' key from all levels of a nested+ dictionary, except when 'title' is an argument name in the schema.+ """+ if isinstance(data, dict):+ # Only remove 'title' if it's not an argument name+ if parent_key not in [+ "properties",+ "$defs",+ "items",+ "allOf",+ "oneOf",+ "anyOf",+ ]:+ data.pop("title", None)++ # Recursively process each key-value pair+ for key, value in data.items():+ _remove_title_recursively(value, parent_key=key)+ elif isinstance(data, list):+ # Recursively process each element in the list+ for item in data:+ _remove_title_recursively(item, parent_key=parent_key)++def get_openai_function_schema(func: Callable) -> Dict[str, Any]:r"""Generates a schema dict for an OpenAI function based on its signature.@@ -120,9 +145,11 @@ def get_openai_tool_schema(func: Callable) -> Dict[str, Any]:model = _create_mol(to_pascal(func.__name__), fields)parameters_dict = get_pydantic_object_schema(model)+# The `"title"` is generated by `model.model_json_schema()`- # but is useless for openai json schema- _remove_a_key(parameters_dict, "title")+ # but is useless for openai json schema, remove generated 'title' from+ # parameters_dict+ _remove_title_recursively(parameters_dict)docstring = parse(func.__doc__ or "")for param in docstring.params:diff --git a/camel/toolkits/math_toolkit.py b/camel/toolkits/math_toolkit.pyindex 0f3762f74..c728825ca 100644--- a/camel/toolkits/math_toolkit.py+++ b/camel/toolkits/math_toolkit.py@@ -74,6 +74,3 @@ class MathToolkit(BaseToolkit):FunctionTool(self.sub),FunctionTool(self.mul),]---MATH_FUNCS: List[FunctionTool] = MathToolkit().get_tools()diff --git a/camel/toolkits/search_toolkit.py b/camel/toolkits/search_toolkit.pyindex e2bb653be..34abd9d1d 100644--- a/camel/toolkits/search_toolkit.py+++ b/camel/toolkits/search_toolkit.py@@ -472,6 +472,3 @@ class SearchToolkit(BaseToolkit):FunctionTool(self.query_wolfram_alpha),FunctionTool(self.tavily_search),]---SEARCH_FUNCS: List[FunctionTool] = SearchToolkit().get_tools()diff --git a/camel/toolkits/twitter_toolkit.py b/camel/toolkits/twitter_toolkit.pyindex bd63446e5..95d82673a 100644--- a/camel/toolkits/twitter_toolkit.py+++ b/camel/toolkits/twitter_toolkit.py@@ -407,14 +407,6 @@ def _handle_http_error(response: requests.Response) -> str:return "Unexpected Exception"-TWITTER_FUNCS = [- FunctionTool(create_tweet),- FunctionTool(delete_tweet),- FunctionTool(get_my_user_profile),- FunctionTool(get_user_by_username),-]--class TwitterToolkit(BaseToolkit):r"""A class representing a toolkit for Twitter operations.@@ -442,4 +434,9 @@ class TwitterToolkit(BaseToolkit):List[FunctionTool]: A list of FunctionTool objectsrepresenting the functions in the toolkit."""- return TWITTER_FUNCS+ return [+ FunctionTool(create_tweet),+ FunctionTool(delete_tweet),+ FunctionTool(get_my_user_profile),+ FunctionTool(get_user_by_username),+ ]diff --git a/camel/toolkits/weather_toolkit.py b/camel/toolkits/weather_toolkit.pyindex 84e795fbc..f44530562 100644--- a/camel/toolkits/weather_toolkit.py+++ b/camel/toolkits/weather_toolkit.py@@ -168,6 +168,3 @@ class WeatherToolkit(BaseToolkit):return [FunctionTool(self.get_weather_data),]---WEATHER_FUNCS: List[FunctionTool] = WeatherToolkit().get_tools()diff --git a/docs/key_modules/tools.md b/docs/key_modules/tools.mdindex 63daa7845..c0b0d22f2 100644--- a/docs/key_modules/tools.md+++ b/docs/key_modules/tools.md@@ -21,7 +21,11 @@ In CAMEL, a tool is an `FunctionTool` that LLMs can call.### 2.1 How to Define Your Own Tool?-Developers can create custom tools tailored to their agent’s specific needs:+Developers can create custom tools tailored to their agent’s specific needs with ease. Here's how you can define and use a custom tool:++**Example: Adding Two Numbers**++First, define your function and wrap it using the `FunctionTool````pythonfrom camel.toolkits import FunctionTool@@ -37,10 +41,17 @@ def add(a: int, b: int) -> int:integer: The sum of the two numbers."""return a + b-++# Wrap the function with FunctionTooladd_tool = FunctionTool(add)```+**Accessing Tool Properties**++Once your tool is defined, you can inspect its properties using built-in methods:++Retrieve the function's name:+```pythonprint(add_tool.get_function_name())```@@ -49,6 +60,8 @@ print(add_tool.get_function_name())>>> add```+Get the description of what the function does:+```pythonprint(add_tool.get_function_description())```@@ -57,6 +70,8 @@ print(add_tool.get_function_description())>>> Adds two numbers.```+Fetch the schema representation for OpenAI functions:+```pythonprint(add_tool.get_openai_function_schema())```@@ -72,6 +87,8 @@ print(add_tool.get_openai_function_schema())'type': 'object'}}```+Retrieve the tool schema, compatible with OpenAI's structure:+```pythonprint(add_tool.get_openai_tool_schema())```@@ -111,6 +128,21 @@ google_tool = FunctionTool(SearchToolkit().search_google)wiki_tool = FunctionTool(SearchToolkit().search_wiki)```+### 2.3 Passing Tools to `ChatAgent`+Enhance the functionality of a ChatAgent by passing custom tools during initialization. Here's how you can do it:++```python+from camel.agents import ChatAgent++# Initialize a ChatAgent with your custom tools+tool_agent = ChatAgent(+ tools=tools,+)++# Interact with the agent+response = tool_agent.step("A query related to the tool you added")+```+Here is a list of the available CAMEL tools and their descriptions:| Toolkit | Description |diff --git a/examples/toolkits/post_weather_on_twitter.py b/examples/toolkits/post_weather_on_twitter.pyindex 598879bc8..2a60b7006 100644--- a/examples/toolkits/post_weather_on_twitter.py+++ b/examples/toolkits/post_weather_on_twitter.py@@ -15,7 +15,7 @@ from colorama import Forefrom camel.agents import ChatAgentfrom camel.messages import BaseMessage-from camel.toolkits import SEARCH_FUNCS, TWITTER_FUNCS, WEATHER_FUNCS+from camel.toolkits import SearchToolkit, TwitterToolkit, WeatherToolkitfrom camel.utils import print_text_animated@@ -38,7 +38,11 @@ def main():agent = ChatAgent(system_message=sys_msg,- tools=[*TWITTER_FUNCS, *WEATHER_FUNCS, *SEARCH_FUNCS],+ tools=[+ *TwitterToolkit().get_tools(),+ *WeatherToolkit().get_tools(),+ *SearchToolkit().get_tools(),+ ],)usr_msg = BaseMessage.make_user_message(diff --git a/examples/workforce/multiple_single_agents.py b/examples/workforce/multiple_single_agents.pyindex 0ad46170c..33052c4d3 100644--- a/examples/workforce/multiple_single_agents.py+++ b/examples/workforce/multiple_single_agents.py@@ -18,10 +18,10 @@ from camel.models import ModelFactoryfrom camel.societies.workforce import Workforcefrom camel.tasks.task import Taskfrom camel.toolkits import (- WEATHER_FUNCS,FunctionTool,GoogleMapsToolkit,SearchToolkit,+ WeatherToolkit,)from camel.types import ModelPlatformType, ModelType@@ -44,7 +44,7 @@ def main():content="You can search online for information",),model=search_agent_model,- tools=[*search_tools, *WEATHER_FUNCS],+ tools=[*search_tools, *WeatherToolkit().get_tools()],)# Set up tour guide agentdiff --git a/examples/workforce/role_playing_with_agents.py b/examples/workforce/role_playing_with_agents.pyindex a5127e7df..cd9ba5b8d 100644--- a/examples/workforce/role_playing_with_agents.py+++ b/examples/workforce/role_playing_with_agents.py@@ -17,7 +17,7 @@ from camel.messages.base import BaseMessagefrom camel.models import ModelFactoryfrom camel.societies.workforce import Workforcefrom camel.tasks.task import Task-from camel.toolkits import SEARCH_FUNCS, WEATHER_FUNCS, GoogleMapsToolkit+from camel.toolkits import GoogleMapsToolkit, SearchToolkit, WeatherToolkitfrom camel.types import ModelPlatformType, ModelType@@ -36,8 +36,8 @@ def main():planner_agent = ChatAgent(planner_sysmsg)function_list = [- *SEARCH_FUNCS,- *WEATHER_FUNCS,+ *SearchToolkit().get_tools(),+ *WeatherToolkit().get_tools(),*GoogleMapsToolkit().get_tools(),]