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

Integrate OpenAI Function Calling API (#213)

Wenxuan Lic97ebcda7d59
diff --git a/camel/__init__.py b/camel/__init__.py
index 5dadd39ea..3a4c73bde 100644
--- a/camel/__init__.py
+++ b/camel/__init__.py
@@ -18,6 +18,7 @@ import camel.messages
import camel.prompts
import camel.typing
import camel.utils
+import camel.functions
__version__ = '0.1.0'
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index 2f7f2184f..7b0226eff 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -11,18 +11,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+import json
from collections import defaultdict
from dataclasses import dataclass
from types import GeneratorType
-from typing import Any, Dict, List, Optional, Tuple
+from typing import Any, Callable, Dict, List, Optional, Tuple
from tenacity import retry
from tenacity.stop import stop_after_attempt
from tenacity.wait import wait_exponential
from camel.agents import BaseAgent
-from camel.configs import ChatGPTConfig
-from camel.messages import BaseMessage
+from camel.configs import BaseConfig, ChatGPTConfig
+from camel.functions import OpenAIFunction
+from camel.messages import BaseMessage, FunctionCallingMessage, OpenAIMessage
from camel.models import BaseModelBackend, ModelFactory
from camel.typing import ModelType, RoleType
from camel.utils import (
@@ -52,8 +54,8 @@ class ChatAgentResponse:
@property
def msg(self):
if len(self.msgs) != 1:
- raise RuntimeError("Property msg is only available"
- "for a single message in msgs")
+ raise RuntimeError("Property msg is only available "
+ "for a single message in msgs.")
return self.msgs[0]
@@ -78,6 +80,32 @@ class ChatRecord:
return self.message.to_openai_message(self.role_at_backend)
+@dataclass(frozen=True)
+class FunctionCallingRecord:
+ r"""Historical records of functions called in the conversation.
+
+ Attributes:
+ func_name (str): The name of the function being called.
+ args (Dict[str, Any]): The dictionary of arguments passed to
+ the function.
+ result (Any): The execution result of calling this function.
+ """
+ func_name: str
+ args: Dict[str, Any]
+ result: Any
+
+ def __str__(self) -> str:
+ r"""Overridden version of the string function.
+
+ Returns:
+ str: Modified string to represent the function calling.
+ """
+
+ return (f"Function Execution: {self.func_name}\n"
+ f"\tArgs: {self.args}\n"
+ f"\tResult: {self.result}")
+
+
class ChatAgent(BaseAgent):
r"""Class for managing conversations of CAMEL Chat Agents.
@@ -91,20 +119,23 @@ class ChatAgent(BaseAgent):
messages to include in the context window. If `None`, no windowing
is performed. (default: :obj:`None`)
output_language (str, optional): The language to be output by the
- agent. (default: :obj:`None`)
+ agent. (default: :obj:`None`)
+ function_list (Optional[List[OpenAIFunction]]): List of available
+ :obj:`OpenAIFunction`. (default: :obj:`None`)
"""
def __init__(
self,
system_message: BaseMessage,
model: Optional[ModelType] = None,
- model_config: Optional[Any] = None,
+ model_config: Optional[BaseConfig] = None,
message_window_size: Optional[int] = None,
output_language: Optional[str] = None,
+ function_list: Optional[List[OpenAIFunction]] = None,
) -> None:
self.orig_sys_message: BaseMessage = system_message
- self.system_message: BaseMessage = system_message
+ self.system_message = system_message
self.role_name: str = system_message.role_name
self.role_type: RoleType = system_message.role_type
self.output_language: Optional[str] = output_language
@@ -113,9 +144,14 @@ class ChatAgent(BaseAgent):
self.model: ModelType = (model if model is not None else
ModelType.GPT_3_5_TURBO)
- self.model_config: ChatGPTConfig = model_config or ChatGPTConfig()
self.message_window_size: Optional[int] = message_window_size
+ self.func_dict: Dict[str, Callable] = {}
+ if function_list is not None:
+ for func in function_list:
+ self.func_dict[func.name] = func.func
+ self.model_config = model_config or ChatGPTConfig()
+
self.model_backend: BaseModelBackend = ModelFactory.create(
self.model, self.model_config.__dict__)
self.model_token_limit: int = self.model_backend.token_limit
@@ -134,6 +170,35 @@ class ChatAgent(BaseAgent):
self.terminated = False
self.init_messages()
+ @property
+ def system_message(self) -> BaseMessage:
+ r"""The getter method for the property :obj:`system_message`.
+
+ Returns:
+ BaseMessage: The system message of this agent.
+ """
+ return self._system_message
+
+ @system_message.setter
+ def system_message(self, message: BaseMessage):
+ r"""The setter method for the property :obj:`system_message`.
+
+ Args:
+ message (BaseMessage): The message to be set as the
+ new system message of this agent.
+ """
+ self._system_message = message
+
+ def is_function_calling_enabled(self) -> bool:
+ r"""Whether OpenAI function calling is enabled for this agent.
+
+ Returns:
+ bool: Whether OpenAI function calling is enabled for this
+ agent, determined by whether the dictionary of functions
+ is empty.
+ """
+ return len(self.func_dict) > 0
+
def set_output_language(self, output_language: str) -> BaseMessage:
r"""Sets the output language for the system message. This method
updates the output language for the system message. The output
@@ -153,22 +218,21 @@ class ChatAgent(BaseAgent):
self.system_message = self.system_message.create_new_instance(content)
return self.system_message
- def get_info(
- self,
- id: Optional[str],
- usage: Optional[Dict[str, int]],
- termination_reasons: List[str],
- num_tokens: int,
- ) -> Dict[str, Any]:
+ def get_info(self, id: Optional[str], usage: Optional[Dict[str, int]],
+ termination_reasons: List[str], num_tokens: int,
+ called_funcs: List[FunctionCallingRecord]) -> Dict[str, Any]:
r"""Returns a dictionary containing information about the chat session.
Args:
id (str, optional): The ID of the chat session.
usage (Dict[str, int], optional): Information about the usage of
the LLM model.
- termination_reasons (List[str]): The reasons for the termination of
- the chat session.
+ termination_reasons (List[str]): The reasons for the termination
+ of the chat session.
num_tokens (int): The number of tokens used in the chat session.
+ called_funcs (List[FunctionCallingRecord]): The list of function
+ calling records, containing the information of called
+ functions.
Returns:
Dict[str, Any]: The chat session information.
@@ -178,6 +242,7 @@ class ChatAgent(BaseAgent):
"usage": usage,
"termination_reasons": termination_reasons,
"num_tokens": num_tokens,
+ "called_functions": called_funcs,
}
def init_messages(self) -> None:
@@ -197,7 +262,7 @@ class ChatAgent(BaseAgent):
Returns:
List[BaseMessage]: The updated stored messages.
"""
- if role not in {'system', 'user', 'assistant'}:
+ if role not in {'system', 'user', 'assistant', 'function'}:
raise ValueError(f"Unsupported role {role}")
self.stored_messages.append(ChatRecord(role, message))
return self.stored_messages
@@ -224,7 +289,7 @@ class ChatAgent(BaseAgent):
Args:
input_message (BaseMessage): The input message to the agent.
- Its `role` field that specifies the role at backen may be either
+ Its `role` field that specifies the role at backend may be either
`user` or `assistant` but it will be set to `user` anyway since
for the self agent any incoming message is external.
@@ -234,45 +299,89 @@ class ChatAgent(BaseAgent):
and information about the chat session.
"""
messages = self.update_messages('user', input_message)
- if self.message_window_size is not None and len(
- messages) > self.message_window_size:
- messages = [ChatRecord('system', self.system_message)
- ] + messages[-self.message_window_size:]
- openai_messages = [record.to_openai_message() for record in messages]
- num_tokens = num_tokens_from_messages(openai_messages, self.model)
- output_messages: Optional[List[BaseMessage]]
+ output_messages: List[BaseMessage]
info: Dict[str, Any]
-
- if num_tokens < self.model_token_limit:
+ called_funcs: List[FunctionCallingRecord] = []
+ while True:
+ # Format messages and get the token number
+ openai_messages: Optional[List[OpenAIMessage]]
+ num_tokens: int
+ 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)
+
+ # Obtain LLM's response and validate it
response = self.model_backend.run(openai_messages)
self.validate_model_response(response)
+
if not self.model_backend.stream:
- output_messages, finish_reasons, usage_dict, response_id = \
- self.handle_batch_response(response)
+ output_messages, finish_reasons, usage_dict, response_id = (
+ self.handle_batch_response(response))
else:
- output_messages, finish_reasons, usage_dict, response_id = \
- self.handle_stream_response(response, num_tokens)
- info = self.get_info(
- response_id,
- usage_dict,
- finish_reasons,
- num_tokens,
- )
- else:
- self.terminated = True
- output_messages = []
-
- info = self.get_info(
- None,
- None,
- ["max_tokens_exceeded"],
- num_tokens,
- )
+ output_messages, finish_reasons, usage_dict, response_id = (
+ self.handle_stream_response(response, num_tokens))
+
+ if self.is_function_calling_enabled(
+ ) and finish_reasons[0] == 'function_call':
+ # Do function calling
+ func_assistant_msg, func_result_msg, func_record = (
+ self.step_function_call(response))
+
+ # Update the messages
+ messages = self.update_messages('assistant',
+ func_assistant_msg)
+ messages = self.update_messages('function', func_result_msg)
+ called_funcs.append(func_record)
+ else:
+ # Function calling disabled or chat stopped
+ info = self.get_info(
+ response_id,
+ usage_dict,
+ finish_reasons,
+ num_tokens,
+ called_funcs,
+ )
+ break
return ChatAgentResponse(output_messages, self.terminated, info)
- def validate_model_response(self, response: Any):
+ def preprocess_messages(
+ self,
+ messages: List[ChatRecord]) -> Tuple[List[OpenAIMessage], int]:
+ r"""Truncate the list of messages if message window is defined and
+ the current length of message list is beyond the window size. Then
+ convert the list of messages to OpenAI's input format and calculate
+ the number of tokens.
+
+ Args:
+ messages (List[ChatRecord]): The list of structs containing
+ information about previous chat messages.
+
+ Returns:
+ tuple: A tuple containing the truncated list of messages in
+ OpenAI's input format and the number of tokens.
+ """
+
+ if (self.message_window_size
+ is not None) and (len(messages) > self.message_window_size):
+ messages = [ChatRecord('system', self.system_message)
+ ] + messages[-self.message_window_size:]
+
+ openai_messages: List[OpenAIMessage]
+ openai_messages = [record.to_openai_message() for record in messages]
+ num_tokens = num_tokens_from_messages(openai_messages, self.model)
+
+ return openai_messages, num_tokens
+
+ def validate_model_response(self, response: Any) -> None:
+ r"""Validate the type of the response returned by the model.
+
+ Args:
+ response (Any): The response returned by the model.
+ """
if not self.model_backend.stream:
if not isinstance(response, dict):
raise RuntimeError("OpenAI returned unexpected batch struct")
@@ -350,6 +459,94 @@ 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:
+ r"""Return trivial response containing number of tokens and information
+ of called functions when the number of tokens exceeds.
+
+ Args:
+ num_tokens (int): Number of tokens in the messages.
+ called_funcs (List[FunctionCallingRecord]): List of information
+ objects of functions called in the current step.
+
+ Returns:
+ ChatAgentResponse: The struct containing trivial outputs and
+ information about token number and called functions.
+ """
+
+ self.terminated = True
+ output_messages: List[BaseMessage] = []
+
+ info = self.get_info(
+ None,
+ None,
+ ["max_tokens_exceeded"],
+ num_tokens,
+ called_funcs,
+ )
+
+ return ChatAgentResponse(
+ output_messages,
+ self.terminated,
+ info,
+ )
+
+ def step_function_call(
+ self, response: Dict[str, Any]
+ ) -> Tuple[FunctionCallingMessage, FunctionCallingMessage,
+ FunctionCallingRecord]:
+ r"""Execute the function with arguments following the model's response.
+
+ Args:
+ response (Dict[str, Any]): the response obtained by calling the
+ model.
+
+ Returns:
+ tuple: a tuple consisting of two obj:`FunctionCallingMessage`,
+ one about the arguments and the other about the execution
+ result, and a struct for logging information about this
+ function call.
+ """
+
+ # Note that when function calling is enabled, `n` is set to 1.
+ choice = response["choices"][0]
+
+ func_name = choice["message"]["function_call"]["name"]
+ func = self.func_dict[func_name]
+
+ args_str: str = choice["message"]["function_call"]["arguments"]
+ args = json.loads(args_str.replace("\'", "\""))
+
+ # 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}.")
+
+ assist_msg = FunctionCallingMessage(
+ role_name=self.role_name,
+ role_type=self.role_type,
+ meta_dict=None,
+ content="",
+ func_name=func_name,
+ args=args,
+ )
+ func_msg = FunctionCallingMessage(
+ role_name=self.role_name,
+ role_type=self.role_type,
+ meta_dict=None,
+ content="",
+ func_name=func_name,
+ result=result,
+ )
+
+ # Record information about this function call
+ func_record = FunctionCallingRecord(func_name, args, result)
+ return assist_msg, func_msg, func_record
+
def get_usage_dict(self, output_messages: List[BaseMessage],
prompt_tokens: int) -> Dict[str, int]:
r"""Get usage dictionary when using the stream mode.
diff --git a/camel/configs.py b/camel/configs.py
index 1b90e33ac..a75c6a509 100644
--- a/camel/configs.py
+++ b/camel/configs.py
@@ -11,12 +11,20 @@
# 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
from dataclasses import dataclass, field
-from typing import Dict, Optional, Sequence, Union
+from typing import Any, Dict, List, Optional, Sequence, Union
+
+from camel.functions import OpenAIFunction
+
+
+@dataclass(frozen=True)
+class BaseConfig(ABC):
+ pass
@dataclass(frozen=True)
-class ChatGPTConfig:
+class ChatGPTConfig(BaseConfig):
r"""Defines the parameters for generating chat completions using the
OpenAI API.
@@ -31,7 +39,7 @@ class ChatGPTConfig:
the tokens comprising the top 10% probability mass are considered.
(default: :obj:`1.0`)
n (int, optional): How many chat completion choices to generate for
- each input message. ()default: :obj:`1`)
+ each input message. (default: :obj:`1`)
stream (bool, optional): If True, partial message deltas will be sent
as data-only server-sent events as they become available.
(default: :obj:`False`)
@@ -74,3 +82,49 @@ class ChatGPTConfig:
frequency_penalty: float = 0.0
logit_bias: Dict = field(default_factory=dict)
user: str = ""
+
+
+@dataclass(frozen=True)
+class FunctionCallingConfig(ChatGPTConfig):
+ r"""Defines the parameters for generating chat completions using the
+ OpenAI API with functions included.
+
+ Args:
+ functions (List[Dict[str, Any]]): A list of functions the model may
+ generate JSON inputs for.
+ function_call (Union[Dict[str, str], str], optional): Controls how the
+ model responds to function calls. :obj:`"none"` means the model
+ does not call a function, and responds to the end-user.
+ :obj:`"auto"` means the model can pick between an end-user or
+ calling a function. Specifying a particular function via
+ :obj:`{"name": "my_function"}` forces the model to call that
+ function. (default: :obj:`"auto"`)
+ """
+ functions: List[Dict[str, Any]] = field(default_factory=list)
+ function_call: Union[Dict[str, str], str] = "auto"
+
+ @classmethod
+ def from_openai_function_list(
+ cls,
+ function_list: List[OpenAIFunction],
+ function_call: Union[Dict[str, str], str] = "auto",
+ ):
+ r"""Class method for creating an instance given the function-related
+ arguments.
+
+ Args:
+ function_list (List[OpenAIFunction]): The list of function objects
+ to be loaded into this configuration and passed to the model.
+ function_call (Union[Dict[str, str], str], optional): Controls how
+ the model responds to function calls, as specified in the
+ creator's documentation.
+
+ Return:
+ FunctionCallingConfig: A new instance which loads the given
+ function list into a list of dictionaries and the input
+ :obj:`function_call` argument.
+ """
+ return cls(
+ functions=[func.as_dict() for func in function_list],
+ function_call=function_call,
+ )
diff --git a/camel/functions/__init__.py b/camel/functions/__init__.py
new file mode 100644
index 000000000..be6c8830e
--- /dev/null
+++ b/camel/functions/__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 .openai_function import OpenAIFunction
+from .math_functions import MATH_FUNCS
+from .search_functions import SEARCH_FUNCS
+
+__all__ = [
+ 'OpenAIFunction',
+ 'MATH_FUNCS',
+ 'SEARCH_FUNCS',
+]
diff --git a/camel/functions/math_functions.py b/camel/functions/math_functions.py
new file mode 100644
index 000000000..e967c353c
--- /dev/null
+++ b/camel/functions/math_functions.py
@@ -0,0 +1,61 @@
+# =========== 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
+
+from .openai_function import OpenAIFunction
+
+
+def add(a: int, b: int) -> int:
+ r"""Adds two numbers.
+
+ Args:
+ a (integer): The first number to be added.
+ b (integer): The second number to be added.
+
+ Returns:
+ integer: The sum of the two numbers.
+ """
+ return a + b
+
+
+def sub(a: int, b: int) -> int:
+ r"""Do subtraction between two numbers.
+
+ Args:
+ a (integer): The minuend in subtraction.
+ b (integer): The subtrahend in subtraction.
+
+ Returns:
+ integer: The result of subtracting :obj:`b` from :obj:`a`.
+ """
+ return a - b
+
+
+def mul(a: int, b: int) -> int:
+ r"""Multiplies two integers.
+
+ Args:
+ a (integer): The multiplier in the multiplication.
+ b (integer): The multiplicand in the multiplication.
+
+ Returns:
+ integer: The product of the two numbers.
+ """
+ return a * b
+
+
+MATH_FUNCS: List[OpenAIFunction] = [
+ OpenAIFunction(func) for func in [add, sub, mul]
+]
diff --git a/camel/functions/openai_function.py b/camel/functions/openai_function.py
new file mode 100644
index 000000000..3a8263581
--- /dev/null
+++ b/camel/functions/openai_function.py
@@ -0,0 +1,88 @@
+# =========== 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, Callable, Dict, Optional
+
+from jsonschema.validators import Draft202012Validator as JSONValidator
+
+from camel.utils import parse_doc
+
+
+class OpenAIFunction:
+ r"""An abstraction of a function that OpenAI chat models can call. See
+ https://platform.openai.com/docs/guides/gpt/function-calling. If
+ :obj:`description` and :obj:`parameters` are both :obj:`None`, try to use
+ document parser to generate them.
+
+ # flake8: noqa :E501
+ Args:
+ func (Callable): The function to call.
+ name (str, optional): The name of the function to be called. Must be
+ a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum
+ length of 64. If :obj:`None`, use the name of :obj:`func`.
+ (default: :obj:`None`)
+ description (Optional[str], optional): The description of what the
+ function does. (default: :obj:`None`)
+ parameters (Optional[Dict[str, Any]], optional): The parameters the
+ functions accepts, described as a JSON Schema object. See the
+ `Function calling guide <https://platform.openai.com/docs/guides/gpt/function-calling>`_
+ for examples, and the `JSON Schema reference <https://json-schema.org/understanding-json-schema/>`_
+ for documentation about the format.
+ """
+
+ def __init__(self, func: Callable, name: Optional[str] = None,
+ description: Optional[str] = None,
+ parameters: Optional[Dict[str, Any]] = None):
+ self.func = func
+ self.name = name or func.__name__
+
+ info = parse_doc(self.func)
+ self.description = description or info["description"]
+ self.parameters = parameters or info["parameters"]
+
+ @property
+ def parameters(self) -> Dict[str, Any]:
+ r"""Getter method for the property :obj:`parameters`.
+
+ Returns:
+ Dict[str, Any]: the dictionary containing information of
+ parameters of this function.
+ """
+ return self._parameters
+
+ @parameters.setter
+ def parameters(self, value: Dict[str, Any]):
+ r"""Setter method for the property :obj:`parameters`. It will
+ firstly check if the input parameters schema is valid. If invalid,
+ the method will raise :obj:`jsonschema.exceptions.SchemaError`.
+
+ Args:
+ value (Dict[str, Any]): the new dictionary value for the
+ function's parameters.
+ """
+ JSONValidator.check_schema(value)
+ self._parameters = value
+
+ def as_dict(self) -> Dict[str, Any]:
+ r"""Method to represent the information of this function into
+ a dictionary object.
+
+ Returns:
+ Dict[str, Any]: The dictionary object containing information
+ of this function's name, description and parameters.
+ """
+ return {
+ attr: getattr(self, attr)
+ for attr in ["name", "description", "parameters"]
+ if getattr(self, attr) is not None
+ }
diff --git a/camel/functions/search_functions.py b/camel/functions/search_functions.py
new file mode 100644
index 000000000..53b77ae49
--- /dev/null
+++ b/camel/functions/search_functions.py
@@ -0,0 +1,112 @@
+# =========== 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 requests
+from bs4 import BeautifulSoup
+
+from .openai_function import OpenAIFunction
+
+
+def clean_str(p: str) -> str:
+ r"""Cleans the input string by encoding and decoding it multiple times
+ to ensure it can be properly read and processed by Python code.
+
+ Args:
+ p (str): The input string to be cleaned, typically from the webpage.
+
+ Returns:
+ str: The cleaned string.
+ """
+ return p.encode().decode("unicode-"
+ "escape").encode("latin1").decode("utf-8")
+
+
+def get_page_abstract(page: str) -> str:
+ r"""Returns the first :obj:`5` sentences of the fetched page.
+
+ Args:
+ page (str): The fetched page.
+
+ Returns:
+ str: The concatenation of the first :obj:`5` sentences in the
+ given page.
+ """
+ paragraphs = page.split('\n')
+ paragraphs = [p.strip() for p in paragraphs if p.strip()]
+
+ # find all sentences
+ sentences = []
+ for p in paragraphs:
+ sents = p.split('. ')
+ if sents[-1].endswith('.'):
+ sents[-1] = sents[-1][:-1]
+ sentences += sents
+ sentences = [s.strip() + '.' for s in sentences if s.strip()]
+
+ return ' '.join(sentences[:5])
+
+
+def search_wiki(entity: str) -> str:
+ r"""Search the entity in WikiPedia and return (the first :obj:`5`
+ sentences of) the required page, containing factual information
+ about the given entity.
+
+ Args:
+ entity (string): The entity to be searched.
+
+ Returns:
+ string: The search result. If the page corresponding to the entity
+ exists, return the first :obj:`5` sentences in a string.
+ """
+ entity_ = entity.replace(" ", "+")
+ search_url = f"https://en.wikipedia.org/w/index.php?search={entity_}"
+
+ # request the target page
+ response_text = requests.get(search_url).text
+
+ # parse the obtained page
+ soup = BeautifulSoup(response_text, features="html.parser")
+ result_divs = soup.find_all("div", {"class": "mw-search-result-heading"})
+
+ observation: str
+ if result_divs:
+ # only similar concepts exist
+ result_titles = [
+ clean_str(div.get_text().strip()) for div in result_divs
+ ]
+ observation = (f"Could not find {entity}. "
+ f"Similar: {result_titles[:5]}.")
+ else:
+ # the page corresponding to the entity exists
+ page = [
+ p.get_text().strip()
+ for p in soup.find_all("p") + soup.find_all("ul")
+ ]
+
+ res_page = ""
+ for p in page:
+ if len(p.split(" ")) > 2:
+ res_page += clean_str(p)
+ if not p.endswith("\n"):
+ res_page += "\n"
+
+ observation = get_page_abstract(res_page)
+
+ return observation
+
+
+SEARCH_FUNCS: List[OpenAIFunction] = [
+ OpenAIFunction(func) for func in [search_wiki]
+]
diff --git a/camel/messages/__init__.py b/camel/messages/__init__.py
index 633918520..aa867bf22 100644
--- a/camel/messages/__init__.py
+++ b/camel/messages/__init__.py
@@ -20,6 +20,7 @@ OpenAIChatMessage = Union[OpenAIUserMessage, OpenAIAssistantMessage]
OpenAIMessage = Union[OpenAISystemMessage, OpenAIChatMessage]
from .base import BaseMessage # noqa: E402
+from .func_message import FunctionCallingMessage # noqa: E402
__all__ = [
'OpenAISystemMessage',
@@ -28,4 +29,5 @@ __all__ = [
'OpenAIChatMessage',
'OpenAIMessage',
'BaseMessage',
+ 'FunctionCallingMessage',
]
diff --git a/camel/messages/func_message.py b/camel/messages/func_message.py
new file mode 100644
index 000000000..2f373b669
--- /dev/null
+++ b/camel/messages/func_message.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 dataclasses import dataclass
+from typing import Any, Dict, Optional
+
+from camel.messages import OpenAIAssistantMessage, OpenAIMessage
+
+from .base import BaseMessage
+
+
+@dataclass
+class FunctionCallingMessage(BaseMessage):
+ r"""Class for message objects used specifically for
+ function-related messages.
+
+ Args:
+ func_name (Optional[str]): The name of the function used.
+ (default: :obj:`None`)
+ args (Optional[Dict]): The dictionary of arguments passed to the
+ function. (default: :obj:`None`)
+ result (Optional[Any]): The result of function execution.
+ (default: :obj:`None`)
+ """
+ func_name: Optional[str] = None
+ args: Optional[Dict] = None
+ result: Optional[Any] = None
+
+ def to_openai_message(self, role_at_backend: str) -> OpenAIMessage:
+ r"""Converts the message to an :obj:`OpenAIMessage` object.
+
+ Args:
+ role_at_backend (str): The role of the message in OpenAI chat
+ system, either :obj:`"system"`, :obj:`"user"`, or
+ obj:`"assistant"`.
+
+ Returns:
+ OpenAIMessage: The converted :obj:`OpenAIMessage` object.
+ """
+ if role_at_backend not in {"assistant", "function"}:
+ raise ValueError("Invalid role for function-related message: "
+ f"{role_at_backend}.")
+
+ if role_at_backend == "assistant":
+ return self.to_openai_assistant_message()
+ else:
+ return self.to_openai_function_message()
+
+ def to_openai_assistant_message(self) -> OpenAIAssistantMessage:
+ r"""Converts the message to an :obj:`OpenAIAssistantMessage` object.
+
+ Returns:
+ OpenAIAssistantMessage: The converted :obj:`OpenAIAssistantMessage`
+ object.
+ """
+ if (not self.func_name) or (not self.args):
+ raise ValueError(
+ "Invalid request for converting into assistant message"
+ " due to missing function name or arguments.")
+
+ msg_dict: Dict[str, Any]
+ msg_dict = {
+ "role": "assistant",
+ "content": self.content,
+ "function_call": {
+ "name": self.func_name,
+ "arguments": str(self.args),
+ }
+ }
+
+ return msg_dict
+
+ def to_openai_function_message(self) -> OpenAIMessage:
+ r"""Converts the message to an :obj:`OpenAIMessage` object
+ with the role being "function".
+
+ Returns:
+ OpenAIMessage: The converted :obj:`OpenAIMessage` object
+ with its role being "function".
+ """
+ if (not self.func_name) or (not self.result):
+ raise ValueError(
+ "Invalid request for converting into function message"
+ " due to missing function name or results.")
+
+ result_content = {"result": {str(self.result)}}
+ msg_dict: Dict[str, str] = {
+ "role": "function",
+ "name": self.func_name,
+ "content": f'{result_content}',
+ }
+
+ return msg_dict
diff --git a/camel/models/openai_model.py b/camel/models/openai_model.py
index 6898db184..b0a0fe78f 100644
--- a/camel/models/openai_model.py
+++ b/camel/models/openai_model.py
@@ -34,7 +34,10 @@ class OpenAIModel(BaseModelBackend):
"""
super().__init__(model_type, model_config_dict)
- def run(self, messages: List[Dict]) -> Dict[str, Any]:
+ def run(
+ self,
+ messages: List[Dict],
+ ) -> Dict[str, Any]:
r"""Run inference of OpenAI chat completion.
Args:
diff --git a/camel/societies/role_playing.py b/camel/societies/role_playing.py
index a21adebdb..eb6c1d10b 100644
--- a/camel/societies/role_playing.py
+++ b/camel/societies/role_playing.py
@@ -20,6 +20,8 @@ from camel.agents import (
TaskSpecifyAgent,
)
from camel.agents.chat_agent import ChatAgentResponse
+from camel.configs import FunctionCallingConfig
+from camel.functions import OpenAIFunction
from camel.generators import SystemMessageGenerator
from camel.human import Human
from camel.messages import BaseMessage
@@ -68,6 +70,9 @@ class RolePlaying:
task specify meta dict with. (default: :obj:`None`)
output_language (str, optional): The language to be output by the
agents. (default: :obj:`None`)
+ assistant_functions (Optional[List[OpenAIFunction]]): the function
+ collection objects to be loaded. If :obj:`None`, function calling
+ is disabled. (default: :obj:`None`)
"""
def __init__(
@@ -91,6 +96,7 @@ class RolePlaying:
extend_sys_msg_meta_dicts: Optional[List[Dict]] = None,
extend_task_specify_meta_dict: Optional[Dict] = None,
output_language: Optional[str] = None,
+ assistant_functions: Optional[List[OpenAIFunction]] = None,
) -> None:
self.with_task_specify = with_task_specify
self.with_task_planner = with_task_planner
@@ -98,6 +104,8 @@ class RolePlaying:
self.task_type = task_type
self.task_prompt = task_prompt
+ self.assistant_functions = assistant_functions
+
self.specified_task_prompt: Optional[TextPrompt] = None
self.init_specified_task_prompt(assistant_role_name, user_role_name,
task_specify_agent_kwargs,
@@ -119,8 +127,13 @@ class RolePlaying:
self.user_agent: ChatAgent
self.assistant_sys_msg: BaseMessage
self.user_sys_msg: BaseMessage
- self.init_agents(init_assistant_sys_msg, assistant_agent_kwargs,
- init_user_sys_msg, user_agent_kwargs, output_language)
+ self.init_agents(
+ init_assistant_sys_msg,
+ assistant_agent_kwargs,
+ init_user_sys_msg,
+ user_agent_kwargs,
+ output_language,
+ )
self.critic: Optional[Union[CriticAgent, Human]] = None
self.critic_sys_msg: Optional[BaseMessage] = None
@@ -238,11 +251,14 @@ class RolePlaying:
))
return init_assistant_sys_msg, init_user_sys_msg, sys_msg_meta_dicts
- def init_agents(self, init_assistant_sys_msg: BaseMessage,
- assistant_agent_kwargs: Optional[Dict],
- init_user_sys_msg: BaseMessage,
- user_agent_kwargs: Optional[Dict],
- output_language: Optional[str]):
+ def init_agents(
+ self,
+ init_assistant_sys_msg: BaseMessage,
+ assistant_agent_kwargs: Optional[Dict],
+ init_user_sys_msg: BaseMessage,
+ user_agent_kwargs: Optional[Dict],
+ output_language: Optional[str],
+ ):
r"""Initialize assistant and user agents with their system messages.
Args:
@@ -257,12 +273,22 @@ class RolePlaying:
output_language (str, optional): The language to be output by the
agents.
"""
+ if self.assistant_functions is not None:
+ assistant_config = FunctionCallingConfig.from_openai_function_list(
+ function_list=self.assistant_functions,
+ function_call="auto",
+ )
+ else:
+ assistant_config = None
self.assistant_agent = ChatAgent(
init_assistant_sys_msg,
+ model_config=assistant_config,
output_language=output_language,
+ function_list=self.assistant_functions,
**(assistant_agent_kwargs or {}),
)
self.assistant_sys_msg = self.assistant_agent.system_message
+
self.user_agent = ChatAgent(
init_user_sys_msg,
output_language=output_language,
@@ -392,6 +418,7 @@ class RolePlaying:
whether the user agent terminated the conversation, and any
additional user information.
"""
+
user_response = self.user_agent.step(assistant_msg)
if user_response.terminated or user_response.msgs is None:
return (ChatAgentResponse([], False, {}),
diff --git a/camel/utils/__init__.py b/camel/utils/__init__.py
index f5b3f8530..3a8ce79e8 100644
--- a/camel/utils/__init__.py
+++ b/camel/utils/__init__.py
@@ -21,6 +21,7 @@ from .functions import (
get_prompt_template_key_words,
get_first_int,
download_tasks,
+ parse_doc,
)
__all__ = [
@@ -33,4 +34,5 @@ __all__ = [
'get_first_int',
'download_tasks',
'PythonInterpreter',
+ 'parse_doc',
]
diff --git a/camel/utils/functions.py b/camel/utils/functions.py
index 0b4d6f84b..f74dfeefc 100644
--- a/camel/utils/functions.py
+++ b/camel/utils/functions.py
@@ -11,12 +11,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+import inspect
import os
import re
import time
import zipfile
from functools import wraps
-from typing import Any, Callable, List, Optional, Set, TypeVar, cast
+from typing import (
+ Any,
+ Callable,
+ Dict,
+ List,
+ Optional,
+ Set,
+ Tuple,
+ TypeVar,
+ cast,
+)
import requests
import tiktoken
@@ -68,7 +79,7 @@ def count_tokens_openai_chat_models(
for message in messages:
num_tokens += tokens_per_message
for key, value in message.items():
- num_tokens += len(encoding.encode(value))
+ num_tokens += len(encoding.encode(str(value)))
if key == "name":
num_tokens += tokens_per_name
num_tokens += 3 # every reply is primed with <|start|>assistant<|message|>
@@ -239,3 +250,67 @@ def download_tasks(task: TaskType, folder_path: str) -> None:
# Delete the zip file
os.remove(zip_file_path)
+
+
+def parse_doc(func: Callable) -> Dict[str, Any]:
+ r"""Parse the docstrings of a function to extract the function name,
+ description and parameters.
+
+ Args:
+ func (Callable): The function to be parsed.
+ Returns:
+ Dict[str, Any]: A dictionary with the function's name,
+ description, and parameters.
+ """
+
+ doc = inspect.getdoc(func)
+ if not doc:
+ raise ValueError(
+ f"Invalid function {func.__name__}: no docstring provided.")
+
+ properties = {}
+ required = []
+
+ parts = re.split('\n\s*\n', doc)
+ func_desc = parts[0].strip()
+
+ args_section = next((p for p in parts if 'Args:' in p), None)
+ if args_section:
+ args_descs: List[Tuple[str, str, str, ]] = re.findall(
+ r'(\w+)\s*\((\w+)\):\s*(.*)', args_section)
+ properties = {
+ name.strip(): {
+ 'type': type,
+ 'description': desc
+ }
+ for name, type, desc in args_descs
+ }
+ for name in properties:
+ required.append(name)
+
+ # Parameters from the function signature
+ sign_params = list(inspect.signature(func).parameters.keys())
+ if len(sign_params) != len(required):
+ raise ValueError(
+ f"Number of parameters in function signature ({len(sign_params)})"
+ f" does not match that in docstring ({len(required)}).")
+
+ for param in sign_params:
+ if param not in required:
+ raise ValueError(f"Parameter '{param}' in function signature"
+ " is missing in the docstring.")
+
+ parameters = {
+ "type": "object",
+ "properties": properties,
+ "required": required,
+ }
+
+ # Construct the function dictionary
+ function_dict = {
+ "name": func.__name__,
+ "description": func_desc,
+ "parameters": parameters,
+ }
+
+ return function_dict
diff --git a/examples/function_call/role_playing_with_function.py b/examples/function_call/role_playing_with_function.py
new file mode 100644
index 000000000..d1fc35863
--- /dev/null
+++ b/examples/function_call/role_playing_with_function.py
@@ -0,0 +1,90 @@
+# =========== 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
+
+from colorama import Fore
+
+from camel.agents.chat_agent import FunctionCallingRecord
+from camel.functions import MATH_FUNCS, SEARCH_FUNCS
+from camel.societies import RolePlaying
+from camel.typing import ModelType
+from camel.utils import print_text_animated
+
+
+def main(model_type=ModelType.GPT_4) -> None:
+ task_prompt = ("Assuming the current year is 2023, estimate KAUST's "
+ "current age and then add 10 more years to this age.")
+ role_play_session = RolePlaying(
+ assistant_role_name="Searcher",
+ user_role_name="Professor",
+ assistant_agent_kwargs=dict(model=model_type),
+ user_agent_kwargs=dict(model=model_type),
+ task_prompt=task_prompt,
+ with_task_specify=False,
+ task_specify_agent_kwargs=dict(model=model_type),
+ assistant_functions=[*MATH_FUNCS, *SEARCH_FUNCS],
+ )
+
+ print(
+ Fore.GREEN +
+ f"AI Assistant sys message:\n{role_play_session.assistant_sys_msg}\n")
+ print(Fore.BLUE +
+ f"AI User sys message:\n{role_play_session.user_sys_msg}\n")
+
+ print(Fore.YELLOW + f"Original task prompt:\n{task_prompt}\n")
+ print(
+ Fore.CYAN +
+ f"Specified task prompt:\n{role_play_session.specified_task_prompt}\n")
+ print(Fore.RED + f"Final task prompt:\n{role_play_session.task_prompt}\n")
+
+ chat_turn_limit, n = 10, 0
+ input_assistant_msg, _ = role_play_session.init_chat()
+ while n < chat_turn_limit:
+ n += 1
+ assistant_response, user_response = role_play_session.step(
+ input_assistant_msg)
+
+ input_assistant_msg = assistant_response.msg
+
+ if assistant_response.terminated:
+ print(Fore.GREEN +
+ ("AI Assistant terminated. Reason: "
+ f"{assistant_response.info['termination_reasons']}."))
+ break
+ if user_response.terminated:
+ print(Fore.GREEN +
+ ("AI User terminated. "
+ f"Reason: {user_response.info['termination_reasons']}."))
+ break
+
+ # Print output from the user
+ print_text_animated(Fore.BLUE +
+ f"AI User:\n\n{user_response.msg.content}\n")
+
+ # Print output from the assistant, including any function
+ # execution information
+ print_text_animated(Fore.GREEN + "AI Assistant:")
+ called_functions: List[
+ FunctionCallingRecord] = assistant_response.info[
+ 'called_functions']
+ for func_record in called_functions:
+ print_text_animated(f"{func_record}")
+ print_text_animated(f"{assistant_response.msg.content}\n")
+
+ if "CAMEL_TASK_DONE" in user_response.msg.content:
+ break
+
+
+if __name__ == "__main__":
+ main()
diff --git a/examples/test/test_ai_society_example.py b/examples/test/test_ai_society_example.py
index 9a023cf35..4787173d1 100644
--- a/examples/test/test_ai_society_example.py
+++ b/examples/test/test_ai_society_example.py
@@ -14,9 +14,15 @@
import pytest
import examples.ai_society.role_playing
+import examples.function_call.role_playing_with_function
from camel.typing import ModelType
@pytest.mark.slow
def test_ai_society_role_playing_example():
examples.ai_society.role_playing.main(ModelType.STUB)
+
+
+def test_role_playing_with_function_example():
+ examples.function_call.role_playing_with_function.main(ModelType.STUB)
diff --git a/pyproject.toml b/pyproject.toml
index c7345839b..3c81395c7 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -31,6 +31,7 @@ openai = "^0"
tenacity = "^8"
tiktoken = "^0"
colorama = "^0"
+jsonschema = "^4"
types-colorama = "^0"
types-requests = "^2"
yapf = "^0"
diff --git a/requirements.txt b/requirements.txt
index ff4f98fb8..c3c48bc75 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -4,6 +4,7 @@ openai
tenacity
tiktoken
colorama
+jsonschema
# Packages for development
yapf
diff --git a/test/agents/test_chat_agent.py b/test/agents/test_chat_agent.py
index a99a5460d..2a84785d3 100644
--- a/test/agents/test_chat_agent.py
+++ b/test/agents/test_chat_agent.py
@@ -11,13 +11,18 @@
# 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.agents import ChatAgent
-from camel.configs import ChatGPTConfig
+from camel.agents.chat_agent import ChatRecord, FunctionCallingRecord
+from camel.configs import ChatGPTConfig, FunctionCallingConfig
+from camel.functions import MATH_FUNCS
from camel.generators import SystemMessageGenerator
from camel.messages import BaseMessage
from camel.typing import ModelType, RoleType, TaskType
+from camel.utils import num_tokens_from_messages
parametrize = pytest.mark.parametrize('model', [
ModelType.STUB,
@@ -67,6 +72,79 @@ def test_chat_agent(model: ModelType):
"max_tokens_exceeded")
+def test_chat_agent_stored_messages():
+ system_msg = BaseMessage(role_name="assistant",
+ role_type=RoleType.ASSISTANT, meta_dict=None,
+ content="You are a help assistant.")
+ assistant = ChatAgent(system_msg)
+
+ expected_stored_msg = [ChatRecord('system', system_msg)]
+ assert assistant.stored_messages == expected_stored_msg
+
+ user_msg = BaseMessage(role_name="User", role_type=RoleType.USER,
+ meta_dict=dict(), content="Tell me a joke.")
+ assistant.update_messages("user", user_msg)
+ expected_stored_msg = [
+ ChatRecord('system', system_msg),
+ ChatRecord('user', user_msg)
+ ]
+ assert assistant.stored_messages == expected_stored_msg
+
+ illegal_role = "xxx"
+ with pytest.raises(ValueError, match=f"Unsupported role {illegal_role}"):
+ assistant.update_messages(illegal_role, user_msg)
+
+
+def test_chat_agent_preprocess_messages_window():
+ system_msg = BaseMessage(role_name="assistant",
+ role_type=RoleType.ASSISTANT, meta_dict=None,
+ content="You are a help assistant.")
+ assistant = ChatAgent(
+ system_message=system_msg,
+ model=ModelType.GPT_3_5_TURBO,
+ message_window_size=1,
+ )
+
+ user_msg = BaseMessage(role_name="User", role_type=RoleType.USER,
+ meta_dict=dict(), content="Tell me a joke.")
+ user_msg = ChatRecord("user", user_msg)
+ system_msg = ChatRecord("system", system_msg)
+
+ openai_messages, _ = assistant.preprocess_messages([system_msg, user_msg])
+ assert len(openai_messages) == 2
+
+
+def test_chat_agent_step_exceed_token_number():
+ system_msg = BaseMessage(role_name="assistant",
+ role_type=RoleType.ASSISTANT, meta_dict=None,
+ content="You are a help assistant.")
+ assistant = ChatAgent(
+ system_message=system_msg,
+ model=ModelType.GPT_3_5_TURBO,
+ )
+ assistant.model_token_limit = 1
+
+ user_msg = BaseMessage(role_name="User", role_type=RoleType.USER,
+ meta_dict=dict(), content="Tell me a joke.")
+ user_msg_record = ChatRecord("user", user_msg)
+ system_msg = ChatRecord("system", system_msg)
+ msgs = [system_msg, user_msg_record]
+
+ expect_openai_messages = [record.to_openai_message() for record in msgs]
+ expect_num_tokens = num_tokens_from_messages(
+ expect_openai_messages,
+ assistant.model,
+ )
+
+ response = assistant.step(user_msg)
+ assert len(response.msgs) == 0
+ assert response.terminated
+ assert response.info["num_tokens"] == expect_num_tokens
+
+
@pytest.mark.model_backend
@pytest.mark.parametrize('n', [1, 2, 3])
def test_chat_agent_multiple_return_messages(n):
@@ -82,6 +160,26 @@ def test_chat_agent_multiple_return_messages(n):
assert len(assistant_response.msgs) == n
+def test_chat_agent_multiple_return_message_error(n):
+ model_config = ChatGPTConfig(temperature=1.4, n=n)
+ system_msg = BaseMessage("Assistant", RoleType.ASSISTANT, meta_dict=None,
+ content="You are a helpful assistant.")
+
+ assistant = ChatAgent(system_msg, model_config=model_config)
+ assistant.reset()
+
+ user_msg = BaseMessage(role_name="User", role_type=RoleType.USER,
+ meta_dict=dict(), content="Tell me a joke.")
+ assistant_response = assistant.step(user_msg)
+
+ with pytest.raises(
+ RuntimeError, match=("Property msg is only available "
+ "for a single message in msgs.")):
+ _ = assistant_response.msg
+
+
@pytest.mark.model_backend
def test_chat_agent_stream_output():
system_msg = BaseMessage("Assistant", RoleType.ASSISTANT, meta_dict=None,
@@ -127,6 +225,15 @@ def test_set_output_language():
"\nRegardless of the input language, you must output text in Arabic.")
assert agent.system_message.content == updated_system_message.content
+
+def test_set_multiple_output_language():
+ system_message = BaseMessage(role_name="assistant",
+ role_type=RoleType.ASSISTANT, meta_dict=None,
+ content="You are a help assistant.")
+ agent = ChatAgent(system_message=system_message,
+ model=ModelType.GPT_3_5_TURBO)
+
# Verify that the length of the system message is kept constant even when
# multiple set_output_language operations are called
agent.set_output_language("Chinese")
@@ -137,3 +244,76 @@ def test_set_output_language():
content="You are a help assistant."
"\nRegardless of the input language, you must output text in French.")
assert agent.system_message.content == updated_system_message.content
+
+
+def test_token_exceed_return():
+ system_message = BaseMessage(role_name="assistant",
+ role_type=RoleType.ASSISTANT, meta_dict=None,
+ content="You are a help assistant.")
+ agent = ChatAgent(system_message=system_message,
+ model=ModelType.GPT_3_5_TURBO)
+
+ expect_info = {
+ "id": None,
+ "usage": None,
+ "termination_reasons": ["max_tokens_exceeded"],
+ "num_tokens": 1000,
+ "called_functions": [],
+ }
+ response = agent.step_token_exceed(1000, [])
+ assert response.msgs == []
+ assert response.terminated
+ assert response.info == expect_info
+
+
+def test_function_enabled():
+ system_message = BaseMessage(role_name="assistant",
+ role_type=RoleType.ASSISTANT, meta_dict=None,
+ content="You are a help assistant.")
+ model_config = FunctionCallingConfig(
+ functions=[func.as_dict() for func in MATH_FUNCS])
+ agent_no_func = ChatAgent(system_message=system_message,
+ model_config=model_config, model=ModelType.GPT_4)
+ agent_with_funcs = ChatAgent(system_message=system_message,
+ model_config=model_config,
+ model=ModelType.GPT_4,
+ function_list=MATH_FUNCS)
+
+ assert not agent_no_func.is_function_calling_enabled()
+ assert agent_with_funcs.is_function_calling_enabled()
+
+
+def test_function_calling():
+ system_message = BaseMessage(role_name="assistant",
+ role_type=RoleType.ASSISTANT, meta_dict=None,
+ content="You are a help assistant.")
+ model_config = FunctionCallingConfig(
+ functions=[func.as_dict() for func in MATH_FUNCS])
+ agent = ChatAgent(system_message=system_message, model_config=model_config,
+ model=ModelType.GPT_4, function_list=MATH_FUNCS)
+
+ ref_funcs = MATH_FUNCS
+
+ assert len(agent.func_dict) == len(ref_funcs)
+ model_config: FunctionCallingConfig = agent.model_config
+ assert len(model_config.functions) == len(ref_funcs)
+
+ user_msg = BaseMessage(role_name="User", role_type=RoleType.USER,
+ meta_dict=dict(),
+ content="Calculate the result of: 2*8-10.")
+ agent_response = agent.step(user_msg)
+
+ called_funcs: List[FunctionCallingRecord] = agent_response.info[
+ 'called_functions']
+ for called_func in called_funcs:
+ print(str(called_func))
+
+ assert len(called_funcs) > 0
+ assert str(called_funcs[0]).startswith("Function Execution")
+
+ assert called_funcs[0].func_name == "mul"
+ assert called_funcs[0].args == {"a": 2, "b": 8}
+ assert called_funcs[0].result == 16
diff --git a/test/functions/test_openai_function.py b/test/functions/test_openai_function.py
new file mode 100644
index 000000000..6ecabed1a
--- /dev/null
+++ b/test/functions/test_openai_function.py
@@ -0,0 +1,82 @@
+# =========== 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.functions import OpenAIFunction
+
+
+def add_with_doc(a: int, b: int) -> int:
+ r"""Adds two numbers.
+
+ Args:
+ a (integer): The first number to be added.
+ b (integer): The second number to be added.
+
+ Returns:
+ integer: The sum of the two numbers.
+ """
+ return a + b
+
+
+def add_without_doc(a: int, b: int) -> int:
+ return a + b
+
+
+def add_with_wrong_doc(a: int, b: int) -> int:
+ r"""Adds two numbers.
+
+ Args:
+ a (integer): The first number to be added.
+
+ Returns:
+ integer: The sum of the two numbers.
+ """
+ return a + b
+
+
+def test_correct_function():
+ add = OpenAIFunction(add_with_doc, name="add")
+ assert add.as_dict() == {
+ "name": "add",
+ "description": "Adds two numbers.",
+ "parameters": {
+ 'type': 'object',
+ 'properties': {
+ 'a': {
+ 'type': 'integer',
+ 'description': 'The first number to be added.'
+ },
+ 'b': {
+ 'type': 'integer',
+ 'description': 'The second number to be added.'
+ }
+ },
+ 'required': ['a', 'b']
+ }
+ }
+
+
+def test_function_without_doc():
+ with pytest.raises(
+ ValueError,
+ match="Invalid function add_without_doc: no docstring provided."):
+ _ = OpenAIFunction(add_without_doc, name="add")
+
+
+def test_function_with_wrong_doc():
+ with pytest.raises(
+ ValueError,
+ match=(r"Number of parameters in function signature \(2\)"
+ r" does not match that in docstring \(1\)")):
+ _ = OpenAIFunction(add_with_wrong_doc, name="add")
diff --git a/test/functions/test_search_functions.py b/test/functions/test_search_functions.py
new file mode 100644
index 000000000..0a5b7f05a
--- /dev/null
+++ b/test/functions/test_search_functions.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 camel.functions.search_functions import (
+ clean_str,
+ get_page_abstract,
+ search_wiki,
+)
+
+
+def test_clean_str():
+ input_str = "Some escaped string with unicode characters: \u2019"
+ expected_output = "Some escaped string with unicode characters: ’"
+ assert clean_str(input_str) == expected_output
+
+
+def test_get_page_abstract():
+ input_page = "\n".join([
+ "This is the first sentence", "This is the second sentence",
+ "This is the third sentence", "This is the fourth sentence",
+ "This is the fifth sentence", "This is the sixth sentence"
+ ])
+ expected_output = (
+ "This is the first sentence. This is the second sentence. "
+ "This is the third sentence. This is the fourth sentence. "
+ "This is the fifth sentence.")
+
+ assert get_page_abstract(input_page) == expected_output
+
+
+def test_search_wiki_normal():
+ expected_output = (
+ "Erygia sigillata is a species of moth in the family Erebidae found "
+ "in Himachal Pradesh, Northern India.[2] The moth was officially "
+ "recognized and classified in 1892. This Erebinae-related article "
+ "is a stub. You can help Wikipedia by expanding it. Main "
+ "pageContentsCurrent eventsRandom articleAbout WikipediaContact "
+ "usDonate. HelpLearn to editCommunity portalRecent "
+ "changesUpload file.")
+
+ # Test that `search_wiki` returns the expected output
+ assert search_wiki("Erygia sigillata") == expected_output
+
+
+def test_search_wiki_not_found():
+ search_output = search_output = search_wiki(
+ "South Africa Women Football Team")
+ assert search_output.startswith(
+ "Could not find South Africa Women Football Team.")
diff --git a/test/messages/test_func_message.py b/test/messages/test_func_message.py
new file mode 100644
index 000000000..ffb4304ca
--- /dev/null
+++ b/test/messages/test_func_message.py
@@ -0,0 +1,117 @@
+# =========== 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 pytest
+
+from camel.messages import FunctionCallingMessage
+from camel.typing import RoleType
+
+
+def assistant_func_message() -> FunctionCallingMessage:
+ role_name = "test_assistant"
+ role_type = RoleType.ASSISTANT
+ meta_dict = None
+ content = "test function message"
+
+ return FunctionCallingMessage(
+ role_name=role_name,
+ role_type=role_type,
+ meta_dict=meta_dict,
+ content=content,
+ func_name="add",
+ args={
+ "a": "1",
+ "b": "2"
+ },
+ )
+
+
+def function_func_message() -> FunctionCallingMessage:
+ role_name = "test_function"
+ role_type = RoleType.ASSISTANT
+ meta_dict = None
+ content = "test function message"
+
+ return FunctionCallingMessage(
+ role_name=role_name,
+ role_type=role_type,
+ meta_dict=meta_dict,
+ content=content,
+ func_name="add",
+ result=3,
+ )
+
+
+def test_assistant_func_message(
+ assistant_func_message: FunctionCallingMessage):
+ content = "test function message"
+
+ assert assistant_func_message.func_name == "add"
+ assert assistant_func_message.args == {"a": "1", "b": "2"}
+
+ msg_dict: Dict[str, Any]
+ msg_dict = {
+ "role": "assistant",
+ "content": content,
+ "function_call": {
+ "name": "add",
+ "arguments": str({
+ "a": "1",
+ "b": "2"
+ }),
+ }
+ }
+ assert assistant_func_message.to_openai_assistant_message() == msg_dict
+
+
+def test_function_func_message(function_func_message: FunctionCallingMessage):
+ assert function_func_message.func_name == "add"
+ assert function_func_message.result == 3
+
+ result_content = {"result": {str(3)}}
+ msg_dict: Dict[str, str] = {
+ "role": "function",
+ "name": "add",
+ "content": f'{result_content}',
+ }
+ assert function_func_message.to_openai_function_message() == msg_dict
+
+
+def test_assistant_func_message_to_openai_function_message(
+ assistant_func_message: FunctionCallingMessage):
+ with pytest.raises(
+ ValueError,
+ match=("Invalid request for converting into function message"
+ " due to missing function name or results.")):
+ assistant_func_message.to_openai_function_message()
+
+
+def test_function_func_message_to_openai_assistant_message(
+ function_func_message: FunctionCallingMessage):
+ with pytest.raises(
+ ValueError,
+ match=("Invalid request for converting into assistant message"
+ " due to missing function name or arguments.")):
+ function_func_message.to_openai_assistant_message()
+
+
+def test_to_openai_message_with_user_backend(
+ assistant_func_message: FunctionCallingMessage):
+ with pytest.raises(
+ ValueError,
+ match=("Invalid role for function-related message: user")):
+ assistant_func_message.to_openai_message("user")
diff --git a/test/utils/test_parse_doc.py b/test/utils/test_parse_doc.py
new file mode 100644
index 000000000..1eadbe1e7
--- /dev/null
+++ b/test/utils/test_parse_doc.py
@@ -0,0 +1,79 @@
+# =========== 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.utils import parse_doc
+
+
+def test_parse_doc_valid():
+
+ def add(a, b):
+ """Adds two numbers.
+
+ Args:
+ a (int): The first number to be added.
+ b (int): The second number to be added.
+ """
+ return a + b
+
+ parsed = parse_doc(add)
+ assert parsed['name'] == 'add'
+ assert parsed['description'] == 'Adds two numbers.'
+ assert parsed['parameters']['properties']['a']['type'] == 'int'
+ assert parsed['parameters']['properties']['b']['type'] == 'int'
+
+
+def test_parse_doc_no_docstring():
+
+ def sub(a, b):
+ return a - b
+
+ with pytest.raises(
+ ValueError,
+ match=f"Invalid function {sub.__name__}: no docstring provided."):
+ parse_doc(sub)
+
+
+def test_parse_doc_mismatch():
+
+ def mul(a, b, c):
+ """Multiplies three numbers.
+
+ Args:
+ a (int): The first number to be multiplied.
+ b (int): The second number to be multiplied.
+ """
+ return a * b * c
+
+ with pytest.raises(
+ ValueError,
+ match=(r"Number of parameters in function signature "
+ r"\(3\) does not match that in docstring \(2\).")):
+ parse_doc(mul)
+
+
+def test_parse_doc_no_args_section():
+
+ def return_param(a):
+ """Return the parameter.
+
+ Args:
+ b (int): The parameter to be returned.
+ """
+ return a
+
+ with pytest.raises(
+ ValueError, match=("Parameter 'a' in function signature"
+ " is missing in the docstring.")):
+ parse_doc(return_param)