š« 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
feat: add `Terminator` for agent to terminate (#254)
Zecheng Zhang8ace5e4de28f
diff --git a/camel/agents/__init__.py b/camel/agents/__init__.pyindex 6f3e6a4a5..8d9dad1de 100644--- a/camel/agents/__init__.py+++ b/camel/agents/__init__.py@@ -12,7 +12,7 @@# limitations under the License.# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========from .base import BaseAgent-from .chat_agent import ChatAgent, ChatAgentResponse+from .chat_agent import ChatAgentfrom .task_agent import (TaskSpecifyAgent,TaskPlannerAgent,@@ -28,7 +28,6 @@ from .role_assignment_agent import RoleAssignmentAgent__all__ = ['BaseAgent','ChatAgent',- 'ChatAgentResponse','TaskSpecifyAgent','TaskPlannerAgent','TaskCreationAgent',diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.pyindex 5cddd64c4..fe523b84f 100644--- a/camel/agents/chat_agent.py+++ b/camel/agents/chat_agent.py@@ -26,35 +26,12 @@ from camel.configs import BaseConfig, ChatGPTConfigfrom camel.functions import OpenAIFunctionfrom camel.messages import BaseMessage, FunctionCallingMessage, OpenAIMessagefrom camel.models import BaseModelBackend, ModelFactory+from camel.responses import ChatAgentResponse+from camel.terminators import ResponseTerminator, TokenLimitTerminatorfrom camel.typing import ModelType, RoleTypefrom camel.utils import get_model_encoding, openai_api_key_required-@dataclass(frozen=True)-class ChatAgentResponse:- r"""Response of a ChatAgent.-- Attributes:- msgs (List[BaseMessage]): A list of zero, one or several messages.- If the list is empty, there is some error in message generation.- If the list has one message, this is normal mode.- If the list has several messages, this is the critic mode.- terminated (bool): A boolean indicating whether the agent decided- to terminate the chat session.- info (Dict[str, Any]): Extra information about the chat message.- """- msgs: List[BaseMessage]- terminated: bool- info: Dict[str, Any]-- @property- def msg(self):- if len(self.msgs) != 1:- raise RuntimeError("Property msg is only available "- "for a single message in msgs.")- return self.msgs[0]--@dataclass(frozen=True)class ChatRecord:r"""Historical records of who made what message.@@ -116,8 +93,11 @@ class ChatAgent(BaseAgent):is performed. (default: :obj:`None`)output_language (str, optional): The language to be output by theagent. (default: :obj:`None`)- function_list (Optional[List[OpenAIFunction]]): List of available+ function_list (List[OpenAIFunction], optional): List of available:obj:`OpenAIFunction`. (default: :obj:`None`)+ response_terminators (List[ResponseTerminator], optional): List of+ :obj:`ResponseTerminator` bind to one chat agent.+ (default: :obj:`None`)"""def __init__(@@ -128,6 +108,7 @@ class ChatAgent(BaseAgent):message_window_size: Optional[int] = None,output_language: Optional[str] = None,function_list: Optional[List[OpenAIFunction]] = None,+ response_terminators: Optional[List[ResponseTerminator]] = None,) -> None:self.orig_sys_message: BaseMessage = system_message@@ -153,6 +134,9 @@ class ChatAgent(BaseAgent):self.model_token_limit: int = self.model_backend.token_limitself.terminated: bool = False+ self.token_limit_terminator = TokenLimitTerminator(+ self.model_token_limit)+ self.response_terminators = response_terminators or []self.stored_messages: List[ChatRecord]self.init_messages()@@ -165,6 +149,9 @@ class ChatAgent(BaseAgent):"""self.terminated = Falseself.init_messages()+ self.token_limit_terminator.reset()+ for terminator in self.response_terminators:+ terminator.reset()@propertydef system_message(self) -> BaseMessage:@@ -252,6 +239,7 @@ class ChatAgent(BaseAgent):r"""Updates the stored messages list with a new message.Args:+ role (str): Role of the message at the backend.message (BaseMessage): The new message to add to the storedmessages.@@ -306,8 +294,11 @@ class ChatAgent(BaseAgent):openai_messages, num_tokens = self.preprocess_messages(messages)# Terminate when number of tokens exceeds the limit- if num_tokens >= self.model_token_limit:- return self.step_token_exceed(num_tokens, called_funcs)+ self.terminated, termination_reason = \+ self.token_limit_terminator.is_terminated(num_tokens)+ if self.terminated and termination_reason is not None:+ return self.step_token_exceed(num_tokens, called_funcs,+ termination_reason)# Obtain LLM's response and validate itresponse = self.model_backend.run(openai_messages)@@ -333,6 +324,23 @@ class ChatAgent(BaseAgent):called_funcs.append(func_record)else:# Function calling disabled or chat stopped++ # Loop over responses terminators, get list of termination+ # tuples with whether the terminator terminates the agent+ # and termination reason+ termination = [+ terminator.is_terminated(output_messages)+ for terminator in self.response_terminators+ ]+ # Terminate the agent if any of the terminator terminates+ self.terminated, termination_reason = next(+ ((terminated, termination_reason)+ for terminated, termination_reason in termination+ if terminated), (False, None))+ # For now only retain the first termination reason+ if self.terminated and termination_reason is not None:+ finish_reasons = [termination_reason] * len(finish_reasons)+info = self.get_info(response_id,usage_dict,@@ -456,9 +464,9 @@ class ChatAgent(BaseAgent):usage_dict = self.get_usage_dict(output_messages, prompt_tokens)return output_messages, finish_reasons, usage_dict, response_id- def step_token_exceed(- self, num_tokens: int,- called_funcs: List[FunctionCallingRecord]) -> ChatAgentResponse:+ def step_token_exceed(self, num_tokens: int,+ called_funcs: List[FunctionCallingRecord],+ termination_reason: str) -> ChatAgentResponse:r"""Return trivial response containing number of tokens and informationof called functions when the number of tokens exceeds.@@ -466,19 +474,19 @@ class ChatAgent(BaseAgent):num_tokens (int): Number of tokens in the messages.called_funcs (List[FunctionCallingRecord]): List of informationobjects of functions called in the current step.+ termination_reason (str): String of termination reason.Returns:ChatAgentResponse: The struct containing trivial outputs andinformation about token number and called functions."""- self.terminated = Trueoutput_messages: List[BaseMessage] = []info = self.get_info(None,None,- ["max_tokens_exceeded"],+ [termination_reason],num_tokens,called_funcs,)@@ -496,11 +504,11 @@ class ChatAgent(BaseAgent):r"""Execute the function with arguments following the model's response.Args:- response (Dict[str, Any]): the response obtained by calling the+ response (Dict[str, Any]): The response obtained by calling themodel.Returns:- tuple: a tuple consisting of two obj:`FunctionCallingMessage`,+ tuple: A tuple consisting of two obj:`FunctionCallingMessage`,one about the arguments and the other about the executionresult, and a struct for logging information about thisfunction call.diff --git a/camel/agents/critic_agent.py b/camel/agents/critic_agent.pyindex ce18b8ee8..88342287a 100644--- a/camel/agents/critic_agent.py+++ b/camel/agents/critic_agent.py@@ -17,8 +17,9 @@ from typing import Any, Dict, Optional, Sequencefrom colorama import Fore-from camel.agents import ChatAgent, ChatAgentResponse+from camel.agents import ChatAgentfrom camel.messages import BaseMessage+from camel.responses import ChatAgentResponsefrom camel.typing import ModelTypefrom camel.utils import get_first_int, print_text_animated@@ -149,7 +150,8 @@ class CriticAgent(ChatAgent):critic, getting the option, and parsing the choice.Args:- messages (Sequence[BaseMessage]): A list of BaseMessage objects.+ input_messages (Sequence[BaseMessage]): A list of BaseMessage+ objects.Returns:ChatAgentResponse: A `ChatAgentResponse` object includes thediff --git a/camel/agents/embodied_agent.py b/camel/agents/embodied_agent.pyindex ab18cc031..4782e9a4b 100644--- a/camel/agents/embodied_agent.py+++ b/camel/agents/embodied_agent.py@@ -15,13 +15,9 @@ from typing import Any, Dict, List, Optionalfrom colorama import Fore-from camel.agents import (- BaseToolAgent,- ChatAgent,- ChatAgentResponse,- HuggingFaceToolAgent,-)+from camel.agents import BaseToolAgent, ChatAgent, HuggingFaceToolAgentfrom camel.messages import BaseMessage+from camel.responses import ChatAgentResponsefrom camel.typing import ModelTypefrom camel.utils import PythonInterpreter, print_text_animateddiff --git a/camel/functions/search_functions.py b/camel/functions/search_functions.pyindex 2784ab41a..a40608255 100644--- a/camel/functions/search_functions.py+++ b/camel/functions/search_functions.py@@ -50,7 +50,7 @@ def search_wiki(entity: str) -> str:def search_google(query: str) -> List[Dict[str, Any]]:- r"""Use google search engine to search information for the given query.+ r"""Use Google search engine to search information for the given query.Args:query (string): The query to be searched.@@ -135,7 +135,7 @@ def search_google(query: str) -> List[Dict[str, Any]]:responses.append({"error": "google search failed."})except requests.RequestException:- responses.append({"erro": "google search failed."})+ responses.append({"error": "google search failed."})return responses@@ -266,7 +266,7 @@ def summarize_text(text: str, query: str) -> str:def search_google_and_summarize(query: str) -> str:r"""Search webs for information. Given a query, this function will use- the google search engine to search for related information from the+ the Google search engine to search for related information from theinternet, and then return a summarized answer.Args:diff --git a/camel/human.py b/camel/human.pyindex 97d96eff1..d11595177 100644--- a/camel/human.py+++ b/camel/human.py@@ -15,8 +15,8 @@ from typing import Any, Dict, Sequencefrom colorama import Fore-from camel.agents import ChatAgentResponsefrom camel.messages import BaseMessage+from camel.responses import ChatAgentResponsefrom camel.utils import print_text_animateddiff --git a/camel/messages/base.py b/camel/messages/base.pyindex 26b3b1860..4c85ca9b7 100644--- a/camel/messages/base.py+++ b/camel/messages/base.py@@ -34,8 +34,6 @@ class BaseMessage::obj:`RoleType.ASSISTANT` or :obj:`RoleType.USER`.meta_dict (Optional[Dict[str, str]]): Additional metadata dictionaryfor the message.- role (str): The role of the message in OpenAI chat system, either- :obj:`"system"`, :obj:`"user"`, or :obj:`"assistant"`.content (str): The content of the message."""role_name: strdiff --git a/camel/responses/__init__.py b/camel/responses/__init__.pynew file mode 100644index 000000000..2d319e6f1--- /dev/null+++ b/camel/responses/__init__.py@@ -0,0 +1,18 @@+# =========== 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 .agent_responses import ChatAgentResponse++__all__ = [+ 'ChatAgentResponse',+]diff --git a/camel/responses/agent_responses.py b/camel/responses/agent_responses.pynew file mode 100644index 000000000..9eac49d0b--- /dev/null+++ b/camel/responses/agent_responses.py@@ -0,0 +1,42 @@+# =========== 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 dataclasses import dataclass+from typing import Any, Dict, List++from camel.messages import BaseMessage+++@dataclass(frozen=True)+class ChatAgentResponse:+ r"""Response of a ChatAgent.++ Attributes:+ msgs (List[BaseMessage]): A list of zero, one or several messages.+ If the list is empty, there is some error in message generation.+ If the list has one message, this is normal mode.+ If the list has several messages, this is the critic mode.+ terminated (bool): A boolean indicating whether the agent decided+ to terminate the chat session.+ info (Dict[str, Any]): Extra information about the chat message.+ """+ msgs: List[BaseMessage]+ terminated: bool+ info: Dict[str, Any]++ @property+ def msg(self):+ if len(self.msgs) != 1:+ raise RuntimeError("Property msg is only available "+ "for a single message in msgs.")+ return self.msgs[0]diff --git a/camel/societies/role_playing.py b/camel/societies/role_playing.pyindex 00bc265d3..80ddbb9ca 100644--- a/camel/societies/role_playing.py+++ b/camel/societies/role_playing.py@@ -19,11 +19,11 @@ from camel.agents import (TaskPlannerAgent,TaskSpecifyAgent,)-from camel.agents.chat_agent import ChatAgentResponsefrom camel.generators import SystemMessageGeneratorfrom camel.human import Humanfrom camel.messages import BaseMessagefrom camel.prompts import TextPrompt+from camel.responses import ChatAgentResponsefrom camel.typing import ModelType, RoleType, TaskTypediff --git a/camel/terminators/__init__.py b/camel/terminators/__init__.pynew file mode 100644index 000000000..3d35102c2--- /dev/null+++ b/camel/terminators/__init__.py@@ -0,0 +1,23 @@+# =========== 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 .base import BaseTerminator+from .response_terminator import ResponseWordsTerminator, ResponseTerminator+from .token_limit_terminator import TokenLimitTerminator++__all__ = [+ 'BaseTerminator',+ 'ResponseTerminator',+ 'ResponseWordsTerminator',+ 'TokenLimitTerminator',+]diff --git a/camel/terminators/base.py b/camel/terminators/base.pynew file mode 100644index 000000000..9c03c9303--- /dev/null+++ b/camel/terminators/base.py@@ -0,0 +1,44 @@+# =========== 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 abc import ABC, abstractmethod+from typing import List, Optional, Tuple++from camel.messages import BaseMessage+++class BaseTerminator(ABC):++ def __init__(self, *args, **kwargs):+ self._terminated: bool = False+ self._termination_reason: Optional[str] = None++ @abstractmethod+ def is_terminated(self, *args, **kwargs) -> Tuple[bool, Optional[str]]:+ pass++ @abstractmethod+ def reset(self):+ pass+++class ResponseTerminator(BaseTerminator):++ @abstractmethod+ def is_terminated(+ self, messages: List[BaseMessage]) -> Tuple[bool, Optional[str]]:+ pass++ @abstractmethod+ def reset(self):+ passdiff --git a/camel/terminators/response_terminator.py b/camel/terminators/response_terminator.pynew file mode 100644index 000000000..945e83abc--- /dev/null+++ b/camel/terminators/response_terminator.py@@ -0,0 +1,118 @@+# =========== 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 collections import defaultdict+from typing import Dict, List, Optional, Tuple++from camel.messages import BaseMessage+from camel.typing import TerminationMode++from .base import ResponseTerminator+++class ResponseWordsTerminator(ResponseTerminator):+ r"""Terminate agent when some words reached to occurrence+ limit by any message of the response.++ Args:+ words_dict (dict): Dictionary of words and its occurrence+ threshold.+ case_sensitive (bool): Whether count the words as+ case-sensitive. (default: :obj:`False`)+ mode (TerminationMode): Whether terminate agent if any+ or all pre-set words reached the threshold.+ (default: :obj:`TerminationMode.ANY`)+ """++ def __init__(self, words_dict: Dict[str,+ int], case_sensitive: bool = False,+ mode: TerminationMode = TerminationMode.ANY):+ super().__init__()+ self.words_dict = words_dict+ self.case_sensitive = case_sensitive+ self.mode = mode+ self._word_count_dict: List[Dict[str, int]] = []+ self._validate()++ def _validate(self):+ if len(self.words_dict) == 0:+ raise ValueError("`words_dict` cannot be empty")+ for word in self.words_dict:+ threshold = self.words_dict[word]+ if threshold <= 0:+ raise ValueError(f"Threshold for word `{word}` should "+ f"be larger than 0, got `{threshold}`")++ def is_terminated(+ self, messages: List[BaseMessage]) -> Tuple[bool, Optional[str]]:+ r"""Whether terminate the agent by checking the occurrence+ of specified words reached to preset thresholds.++ Args:+ messages (list): List of :obj:`BaseMessage` from a response.++ Returns:+ tuple: A tuple containing whether the agent should be+ terminated and a string of termination reason.+ """+ if self._terminated:+ return True, self._termination_reason++ for i in range(len(messages)):+ if i >= len(self._word_count_dict):+ self._word_count_dict.append(defaultdict(int))++ for word in self.words_dict:+ special_word = word if self.case_sensitive else word.lower()+ for i, message in enumerate(messages):+ if self.case_sensitive:+ content = message.content+ else:+ content = message.content.lower()+ if special_word in content:+ self._word_count_dict[i][word] += 1++ num_reached: List[int] = []+ all_reasons: List[List[str]] = []+ for i in range(len(self._word_count_dict)):+ reached = 0+ reasons: List[str] = []+ for word, value in self._word_count_dict[i].items():+ if value >= self.words_dict[word]:+ reached += 1+ reason = (f"Word `{word}` appears {value} times in the "+ f"{i + 1} message of the response which has "+ f"reached termination threshold "+ f"{self.words_dict[word]}.")+ reasons.append(reason)+ all_reasons.append(reasons)+ num_reached.append(reached)++ for i, reached in enumerate(num_reached):+ if self.mode == TerminationMode.ANY:+ if reached > 0:+ self._terminated = True+ self._termination_reason = "\n".join(all_reasons[i])+ elif self.mode == TerminationMode.ALL:+ if reached >= len(self.words_dict):+ self._terminated = True+ self._termination_reason = "\n".join(all_reasons[i])+ else:+ raise ValueError(f"Unsupported termination mode "+ f"`{self.mode}`")+ return self._terminated, self._termination_reason++ def reset(self):+ self._terminated = False+ self._termination_reason = None+ self._word_count_dict = defaultdict(int)diff --git a/camel/terminators/token_limit_terminator.py b/camel/terminators/token_limit_terminator.pynew file mode 100644index 000000000..7fd9e8e9f--- /dev/null+++ b/camel/terminators/token_limit_terminator.py@@ -0,0 +1,55 @@+# =========== 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 Optional, Tuple++from camel.terminators import BaseTerminator+++class TokenLimitTerminator(BaseTerminator):+ r"""Terminate agent if number of tokens reached to token limit threshold.++ Args:+ token_limit (int): Token limit threshold.+ """++ def __init__(self, token_limit: int):+ super().__init__()+ self.token_limit = token_limit++ def _validate(self):+ if self.token_limit <= 0:+ raise ValueError(f"`token_limit` should be a "+ f"value larger than 0, got {self.token_limit}.")++ def is_terminated(self, num_tokens: int) -> Tuple[bool, Optional[str]]:+ r"""Whether terminate the agent by checking number of+ used tokens reached to token limit.++ Args:+ num_tokens (int): Number of tokens.++ Returns:+ tuple: A tuple containing whether the agent should be+ terminated and a string of termination reason.+ """+ if self._terminated:+ return True, self._termination_reason+ if num_tokens >= self.token_limit:+ self._terminated = True+ self._termination_reason = "max_tokens_exceeded"+ return self._terminated, self._termination_reason++ def reset(self):+ self._terminated = False+ self._termination_reason = Nonediff --git a/camel/typing.py b/camel/typing.pyindex de7de1c50..1a5279972 100644--- a/camel/typing.py+++ b/camel/typing.py@@ -125,4 +125,9 @@ class TaskType(Enum):DEFAULT = "default"-__all__ = ['RoleType', 'ModelType', 'TaskType']+class TerminationMode(Enum):+ ANY = "any"+ ALL = "all"+++__all__ = ['RoleType', 'ModelType', 'TaskType', 'TerminationMode']diff --git a/test/agents/test_chat_agent.py b/test/agents/test_chat_agent.pyindex 885a40a40..d991f5642 100644--- a/test/agents/test_chat_agent.py+++ b/test/agents/test_chat_agent.py@@ -21,6 +21,7 @@ from camel.configs import ChatGPTConfig, FunctionCallingConfigfrom camel.functions import MATH_FUNCSfrom camel.generators import SystemMessageGeneratorfrom camel.messages import BaseMessage+from camel.terminators import ResponseWordsTerminatorfrom camel.typing import ModelType, RoleType, TaskTypeparametrize = pytest.mark.parametrize('model', [@@ -110,6 +111,7 @@ def test_chat_agent_step_exceed_token_number():model=ModelType.GPT_3_5_TURBO,)assistant.model_token_limit = 1+ assistant.token_limit_terminator.token_limit = 1user_msg = BaseMessage(role_name="User", role_type=RoleType.USER,meta_dict=dict(), content="Tell me a joke.")@@ -243,7 +245,8 @@ def test_token_exceed_return():"num_tokens": 1000,"called_functions": [],}- response = agent.step_token_exceed(1000, [])+ agent.terminated = True+ response = agent.step_token_exceed(1000, [], "max_tokens_exceeded")assert response.msgs == []assert response.terminatedassert response.info == expect_info@@ -299,3 +302,22 @@ def test_function_calling():assert called_funcs[0].func_name == "mul"assert called_funcs[0].args == {"a": 2, "b": 8}assert called_funcs[0].result == 16+++def test_response_words_termination():+ system_message = BaseMessage(role_name="assistant",+ role_type=RoleType.ASSISTANT, meta_dict=None,+ content="You are a help assistant.")+ response_terminator = ResponseWordsTerminator(words_dict=dict(goodbye=1))+ model_config = ChatGPTConfig(temperature=0, n=2)+ agent = ChatAgent(system_message=system_message,+ model=ModelType.GPT_3_5_TURBO, model_config=model_config,+ response_terminators=[response_terminator])+ user_msg = BaseMessage(role_name="User", role_type=RoleType.USER,+ meta_dict=dict(),+ content="Just say 'goodbye' once.")+ agent_response = agent.step(user_msg)++ assert agent.terminated+ assert agent_response.terminated+ assert "goodbye" in agent_response.info['termination_reasons'][0]diff --git a/test/agents/test_role_assignment_agent.py b/test/agents/test_role_assignment_agent.pyindex e768d8cbe..f777db78a 100644--- a/test/agents/test_role_assignment_agent.py+++ b/test/agents/test_role_assignment_agent.py@@ -14,9 +14,10 @@import pytestfrom mock import patch-from camel.agents import ChatAgent, ChatAgentResponse, RoleAssignmentAgent+from camel.agents import ChatAgent, RoleAssignmentAgentfrom camel.configs import ChatGPTConfigfrom camel.messages import BaseMessage+from camel.responses import ChatAgentResponsefrom camel.typing import ModelType, RoleTypediff --git a/test/functions/test_search_functions.py b/test/functions/test_search_functions.pyindex 3b9885762..c086aabb4 100644--- a/test/functions/test_search_functions.py+++ b/test/functions/test_search_functions.py@@ -47,7 +47,7 @@ def test_search_wiki_with_ambiguity():def test_google_api():- # Check the google search api+ # Check the Google search api# https://developers.google.com/custom-search/v1/overviewGOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")diff --git a/test/terminators/test_response_terminator.py b/test/terminators/test_response_terminator.pynew file mode 100644index 000000000..cda958ab6--- /dev/null+++ b/test/terminators/test_response_terminator.py@@ -0,0 +1,59 @@+# =========== 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 List++import pytest++from camel.messages import BaseMessage+from camel.terminators import ResponseWordsTerminator+from camel.typing import RoleType, TerminationMode++NUM_TIMES = 2+++def _create_messages() -> List[BaseMessage]:+ messages = []+ for _ in range(3):+ message = BaseMessage(role_name="user", role_type=RoleType.USER,+ meta_dict={}, content="GoodBye")+ messages.append(message)+ return messages++[email protected]('mode', [TerminationMode.ANY, TerminationMode.ALL])+def test_response_words_termination(mode):+ words_dict = {+ "goodbye": NUM_TIMES,+ "good bye": NUM_TIMES,+ "thank": NUM_TIMES,+ "bye": NUM_TIMES,+ "welcome": NUM_TIMES,+ "language model": NUM_TIMES + 1,+ }+ termination = ResponseWordsTerminator(words_dict=words_dict, mode=mode)+ messages = _create_messages()+ terminated, termination_reason = termination.is_terminated(messages)+ assert not terminated+ assert termination_reason is None+ messages = _create_messages()+ terminated, termination_reason = termination.is_terminated(messages)+ if mode == TerminationMode.ANY:+ assert terminated+ assert "goodbye" in termination_reason+ if mode == TerminationMode.ALL:+ assert not terminated+ assert termination_reason is None+ termination.reset()+ assert not termination._terminated+ assert termination._termination_reason is Nonediff --git a/test/terminators/test_token_limit_terminator.py b/test/terminators/test_token_limit_terminator.pynew file mode 100644index 000000000..96dbb8dce--- /dev/null+++ b/test/terminators/test_token_limit_terminator.py@@ -0,0 +1,29 @@+# =========== 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 camel.terminators import TokenLimitTerminator++[email protected]('num_tokens', [5, 10])+def test_token_limit_termination(num_tokens):+ termination = TokenLimitTerminator(token_limit=10)+ terminated, termination_reason = termination.is_terminated(+ num_tokens=num_tokens)+ if num_tokens == 5:+ assert not terminated+ assert termination_reason is None+ if num_tokens == 10:+ assert terminated+ assert termination_reason == "max_tokens_exceeded"