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

feat(memory): chat history memory support (#280)

Tianqi Xuda517a8e2774
diff --git a/camel/__init__.py b/camel/__init__.py
index 3a4c73bde..d0e5a6468 100644
--- a/camel/__init__.py
+++ b/camel/__init__.py
@@ -19,6 +19,8 @@ import camel.prompts
import camel.typing
import camel.utils
import camel.functions
+import camel.memories
+import camel.storages
__version__ = '0.1.0'
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index fe523b84f..baab366ad 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -24,35 +24,20 @@ from tenacity.wait import wait_exponential
from camel.agents import BaseAgent
from camel.configs import BaseConfig, ChatGPTConfig
from camel.functions import OpenAIFunction
+from camel.memories import (
+ BaseMemory,
+ ChatHistoryMemory,
+ MemoryRecord,
+ ScoreBasedContextCreator,
+)
from camel.messages import BaseMessage, FunctionCallingMessage, OpenAIMessage
from camel.models import BaseModelBackend, ModelFactory
from camel.responses import ChatAgentResponse
-from camel.terminators import ResponseTerminator, TokenLimitTerminator
-from camel.typing import ModelType, RoleType
+from camel.terminators import ResponseTerminator
+from camel.typing import ModelType, OpenAIBackendRole, RoleType
from camel.utils import get_model_encoding, openai_api_key_required
-@dataclass(frozen=True)
-class ChatRecord:
- r"""Historical records of who made what message.
-
- Attributes:
- role_at_backend (str): Role of the message that mirrors OpenAI
- message role that may be `system` or `user` or `assistant`.
- message (BaseMessage): Message payload.
- """
- role_at_backend: str
- message: BaseMessage
-
- def to_openai_message(self):
- r"""Converts the payload message to OpenAI-compatible format.
-
- Returns:
- OpenAIMessage: OpenAI-compatible message
- """
- return self.message.to_openai_message(self.role_at_backend)
-
-
@dataclass(frozen=True)
class FunctionCallingRecord:
r"""Historical records of functions called in the conversation.
@@ -86,11 +71,18 @@ class ChatAgent(BaseAgent):
system_message (BaseMessage): The system message for the chat agent.
model (ModelType, optional): The LLM model to use for generating
responses. (default :obj:`ModelType.GPT_3_5_TURBO`)
- model_config (Any, optional): Configuration options for the LLM model.
+ model_config (BaseConfig, optional): Configuration options for the
+ LLM model. (default: :obj:`None`)
+ memory (BaseMemory, optional): The agent memory for managing chat
+ messages. If `None`, a :obj:`ChatHistoryMemory` will be used.
(default: :obj:`None`)
message_window_size (int, optional): The maximum number of previous
messages to include in the context window. If `None`, no windowing
is performed. (default: :obj:`None`)
+ token_limit (int, optional): The maxinum number of tokens in a context.
+ The context will be automatically pruned to fulfill the limitation.
+ If `None`, it will be set according to the backend model.
+ (default: :obj:`None`)
output_language (str, optional): The language to be output by the
agent. (default: :obj:`None`)
function_list (List[OpenAIFunction], optional): List of available
@@ -105,7 +97,9 @@ class ChatAgent(BaseAgent):
system_message: BaseMessage,
model: Optional[ModelType] = None,
model_config: Optional[BaseConfig] = None,
+ memory: Optional[BaseMemory] = None,
message_window_size: Optional[int] = None,
+ token_limit: Optional[int] = None,
output_language: Optional[str] = None,
function_list: Optional[List[OpenAIFunction]] = None,
response_terminators: Optional[List[ResponseTerminator]] = None,
@@ -121,7 +115,6 @@ class ChatAgent(BaseAgent):
self.model: ModelType = (model if model is not None else
ModelType.GPT_3_5_TURBO)
- self.message_window_size: Optional[int] = message_window_size
self.func_dict: Dict[str, Callable] = {}
if function_list is not None:
@@ -131,13 +124,16 @@ class ChatAgent(BaseAgent):
self.model_backend: BaseModelBackend = ModelFactory.create(
self.model, self.model_config.__dict__)
- self.model_token_limit: int = self.model_backend.token_limit
+ self.model_token_limit = token_limit or self.model_backend.token_limit
+ context_creator = ScoreBasedContextCreator(
+ self.model_backend.token_counter,
+ self.model_token_limit,
+ )
+ self.memory: BaseMemory = memory or ChatHistoryMemory(
+ context_creator, window_size=message_window_size)
self.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()
def reset(self):
@@ -149,7 +145,6 @@ class ChatAgent(BaseAgent):
"""
self.terminated = False
self.init_messages()
- self.token_limit_terminator.reset()
for terminator in self.response_terminators:
terminator.reset()
@@ -182,6 +177,17 @@ class ChatAgent(BaseAgent):
"""
return len(self.func_dict) > 0
+ def update_memory(self, message: BaseMessage,
+ role: OpenAIBackendRole) -> None:
+ r"""Updates the agent memory with a new message.
+
+ Args:
+ message (BaseMessage): The new message to add to the stored
+ messages.
+ role (OpenAIBackendRole): The backend role type.
+ """
+ self.memory.write_record(MemoryRecord(message, role))
+
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
@@ -232,35 +238,21 @@ class ChatAgent(BaseAgent):
r"""Initializes the stored messages list with the initial system
message.
"""
- self.stored_messages = [ChatRecord('system', self.system_message)]
+ system_record = MemoryRecord(self.system_message,
+ OpenAIBackendRole.SYSTEM)
+ self.memory.clear()
+ self.memory.write_record(system_record)
- def update_messages(self, role: str,
- message: BaseMessage) -> List[ChatRecord]:
- r"""Updates the stored messages list with a new message.
+ def record_message(self, message: BaseMessage) -> None:
+ r"""Records the externally provided message into the agent memory as if
+ it were an answer of the :obj:`ChatAgent` from the backend. Currently,
+ the choice of the critic is submitted with this method.
Args:
- role (str): Role of the message at the backend.
- message (BaseMessage): The new message to add to the stored
- messages.
-
- Returns:
- List[BaseMessage]: The updated stored messages.
+ message (BaseMessage): An external message to be recorded in the
+ memory.
"""
- 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
-
- def submit_message(self, message: BaseMessage) -> None:
- r"""Submits the externally provided message as if it were an answer of
- the chat LLM from the backend. Currently, the choice of the critic is
- submitted with this method.
-
- Args:
- message (BaseMessage): An external message to be added as an
- assistant response.
- """
- self.stored_messages.append(ChatRecord('assistant', message))
+ self.update_memory(message, OpenAIBackendRole.ASSISTANT)
@retry(wait=wait_exponential(min=5, max=60), stop=stop_after_attempt(5))
@openai_api_key_required
@@ -282,7 +274,7 @@ class ChatAgent(BaseAgent):
a boolean indicating whether the chat session has terminated,
and information about the chat session.
"""
- messages = self.update_messages('user', input_message)
+ self.update_memory(input_message, OpenAIBackendRole.USER)
output_messages: List[BaseMessage]
info: Dict[str, Any]
@@ -290,17 +282,14 @@ class ChatAgent(BaseAgent):
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
- 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)
+ try:
+ openai_messages, num_tokens = self.memory.get_context()
+ except RuntimeError as e:
+ return self.step_token_exceed(e.args[1], called_funcs,
+ "max_tokens_exceeded")
- # Obtain LLM's response and validate it
+ # Obtain the model's response and validate it
response = self.model_backend.run(openai_messages)
self.validate_model_response(response)
@@ -311,19 +300,21 @@ class ChatAgent(BaseAgent):
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':
+ 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)
+ self.update_memory(func_assistant_msg,
+ OpenAIBackendRole.ASSISTANT)
+ self.update_memory(func_result_msg, OpenAIBackendRole.FUNCTION)
+
+ # Record the function calling
called_funcs.append(func_record)
else:
- # Function calling disabled or chat stopped
+ # Function calling disabled or not a function calling
# Loop over responses terminators, get list of termination
# tuples with whether the terminator terminates the agent
@@ -352,35 +343,6 @@ class ChatAgent(BaseAgent):
return ChatAgentResponse(output_messages, self.terminated, info)
- 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 = self.model_backend.count_tokens_from_messages(
- openai_messages)
-
- return openai_messages, num_tokens
-
def validate_model_response(self, response: Any) -> None:
r"""Validate the type of the response returned by the model.
@@ -480,7 +442,7 @@ class ChatAgent(BaseAgent):
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(
diff --git a/camel/agents/critic_agent.py b/camel/agents/critic_agent.py
index 88342287a..7ddfedb8d 100644
--- a/camel/agents/critic_agent.py
+++ b/camel/agents/critic_agent.py
@@ -18,6 +18,7 @@ from typing import Any, Dict, Optional, Sequence
from colorama import Fore
from camel.agents import ChatAgent
+from camel.memories import BaseMemory
from camel.messages import BaseMessage
from camel.responses import ChatAgentResponse
from camel.typing import ModelType
@@ -49,13 +50,14 @@ class CriticAgent(ChatAgent):
system_message: BaseMessage,
model: ModelType = ModelType.GPT_3_5_TURBO,
model_config: Optional[Any] = None,
+ memory: Optional[BaseMemory] = None,
message_window_size: int = 6,
retry_attempts: int = 2,
verbose: bool = False,
logger_color: Any = Fore.MAGENTA,
) -> None:
super().__init__(system_message, model=model,
- model_config=model_config,
+ model_config=model_config, memory=memory,
message_window_size=message_window_size)
self.options_dict: Dict[str, str] = dict()
self.retry_attempts = retry_attempts
@@ -106,7 +108,7 @@ class CriticAgent(ChatAgent):
raise RuntimeError("Critic step failed.")
critic_msg = critic_response.msg
- self.update_messages('assistant', critic_msg)
+ self.record_message(critic_msg)
if self.verbose:
print_text_animated(self.logger_color + "\n> Critic response: "
f"\x1b[3m{critic_msg.content}\x1b[0m\n")
diff --git a/camel/memories/__init__.py b/camel/memories/__init__.py
new file mode 100644
index 000000000..ab1a30b46
--- /dev/null
+++ b/camel/memories/__init__.py
@@ -0,0 +1,28 @@
+# =========== 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 .records import MemoryRecord, ContextRecord
+from .base import BaseMemory
+from .context_creators.base import BaseContextCreator
+from .context_creators.score_based import ScoreBasedContextCreator
+from .chat_history_memory import ChatHistoryMemory
+
+__all__ = [
+ 'MemoryRecord',
+ 'ContextRecord',
+ 'BaseMemory',
+ 'ChatHistoryMemory',
+ "BaseContextCreator",
+ "ScoreBasedContextCreator",
+]
diff --git a/camel/memories/base.py b/camel/memories/base.py
new file mode 100644
index 000000000..dd5bf72b5
--- /dev/null
+++ b/camel/memories/base.py
@@ -0,0 +1,75 @@
+# =========== 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, Tuple
+
+from camel.memories import MemoryRecord
+from camel.messages import OpenAIMessage
+
+
+class BaseMemory(ABC):
+ r"""An abstract base class that defines the foundational operations for a
+ memory component within an agent's memory system.
+
+ The memory component is tasked with functions like saving chat histories,
+ fetching or storing information in vector databases, and other related
+ operations. Every memory system should incorporate at least one instance of
+ a subclass derived from :obj:`BaseMemory`.
+
+ These instances, known as "memories", typically communicate using the
+ :obj:`MemoryRecord` object. Usually, a memory has at least one "storage"
+ mechanism, allowing it to interface with various storage systems, such as
+ disks or vector databases. Additionally, some memories might embed other
+ memory instances, enabling them to function as a high-level controller
+ within the broader memory system.
+
+ By default, when executing the :obj:`step()` method, an agent retrieves
+ messages from its designated memory and combines them with an incoming
+ message for input to the agent. Subsequently, both the response message and
+ the incoming messages are archived back into the memory.
+ """
+
+ @abstractmethod
+ def get_context(self) -> Tuple[List[OpenAIMessage], int]:
+ r"""Gets chat context with a proper size for the agent.
+
+ Returns:
+ (List[OpenAIMessage], int): A tuple containing the constructed
+ context in OpenAIMessage format and the total token count.
+ """
+ pass
+
+ @abstractmethod
+ def write_records(self, records: List[MemoryRecord]) -> None:
+ r"""Writes records to the memory, appending them to existing ones.
+
+ Args:
+ records (List[MemoryRecord]): Records to be added to the memory.
+ """
+ pass
+
+ def write_record(self, record: MemoryRecord) -> None:
+ r"""Writes a record to the memory, appending it to existing ones.
+
+ Args:
+ record (MemoryRecord): Record to be added to the memory.
+ """
+ self.write_records([record])
+
+ @abstractmethod
+ def clear(self) -> None:
+ r"""Clears all messages from the memory.
+ """
+ pass
diff --git a/camel/memories/chat_history_memory.py b/camel/memories/chat_history_memory.py
new file mode 100644
index 000000000..bcdb13a9c
--- /dev/null
+++ b/camel/memories/chat_history_memory.py
@@ -0,0 +1,111 @@
+# =========== 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, Optional, Tuple
+
+from camel.memories import BaseMemory, ContextRecord, MemoryRecord
+from camel.memories.context_creators import BaseContextCreator
+from camel.messages import OpenAIMessage
+from camel.storages import BaseKeyValueStorage, InMemoryKeyValueStorage
+from camel.typing import OpenAIBackendRole
+
+
+class ChatHistoryMemory(BaseMemory):
+ r"""An implementation of the :obj:`BaseMemory` abstract base class for
+ maintaining a record of chat histories.
+
+ This memory class helps manage conversation histories with a designated
+ storage mechanism, either provided by the user or using a default
+ in-memory storage. It offers a windowed approach to retrieving chat
+ histories, allowing users to specify how many recent messages they'd
+ like to fetch.
+
+ `ChatHistoryMemory` requires messages to be stored with certain
+ metadata (e.g., `role_at_backend`) to maintain consistency and validate
+ the chat history.
+
+ Args:
+ context_creator (BaseContextCreator): A context creator contianing
+ the context limit and the message pruning strategy.
+ storage (BaseKeyValueStorage, optional): A storage mechanism for
+ storing chat history. If `None`, an :obj:`InMemoryKeyValueStorage`
+ will be used. (default: :obj:`None`)
+ window_size (int, optional): Specifies the number of recent chat
+ messages to retrieve. If not provided, the entire chat history
+ will be retrieved. (default: :obj:`None`)
+ """
+
+ def __init__(
+ self,
+ context_creator: BaseContextCreator,
+ storage: Optional[BaseKeyValueStorage] = None,
+ window_size: Optional[int] = None,
+ ) -> None:
+ self.context_creator = context_creator
+ self.storage = storage or InMemoryKeyValueStorage()
+ self.window_size = window_size
+
+ def get_context(self) -> Tuple[List[OpenAIMessage], int]:
+ r"""Gets chat context with a proper size for the agent from the memory
+ based on the window size or fetches the entire chat history if no
+ window size is specified.
+
+ Returns:
+ (List[OpenAIMessage], int): A tuple containing the constructed
+ context in OpenAIMessage format and the total token count.
+ Raises:
+ ValueError: If the memory is empty or if the first message in the
+ memory is not a system message.
+ """
+ record_dicts = self.storage.load()
+ if len(record_dicts) == 0:
+ raise ValueError("The `ChatHistoryMemory` is empty.")
+
+ chat_records: List[MemoryRecord] = []
+ truncate_idx = -self.window_size if self.window_size is not None else 0
+ for record_dict in record_dicts[truncate_idx:]:
+ chat_records.append(MemoryRecord.from_dict(record_dict))
+
+ # We assume that, in the chat history memory, the closer the record is
+ # to the current message, the more score it will be.
+ output_records = []
+ score = 1.0
+ for record in reversed(chat_records):
+ if record.role_at_backend == OpenAIBackendRole.SYSTEM:
+ # System messages are always kept.
+ output_records.append(ContextRecord(record, 1.0))
+ else:
+ # Other messages' score drops down gradually
+ score *= 0.99
+ output_records.append(ContextRecord(record, score))
+
+ output_records.reverse()
+ return self.context_creator.create_context(output_records)
+
+ def write_records(self, records: List[MemoryRecord]) -> None:
+ r"""Writes memory records to the memory. Additionally, performs
+ validation checks on the messages.
+
+ Args:
+ records (List[MemoryRecord]): Memory records to be added to the
+ memory.
+ """
+ stored_records = []
+ for record in records:
+ stored_records.append(record.to_dict())
+ self.storage.save(stored_records)
+
+ def clear(self) -> None:
+ r"""Clears all chat messages from the memory.
+ """
+ self.storage.clear()
diff --git a/camel/memories/context_creators/__init__.py b/camel/memories/context_creators/__init__.py
new file mode 100644
index 000000000..5b1db8e27
--- /dev/null
+++ b/camel/memories/context_creators/__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 .base import BaseContextCreator
+from .score_based import ScoreBasedContextCreator
+
+__all__ = ['BaseContextCreator', 'ScoreBasedContextCreator']
diff --git a/camel/memories/context_creators/base.py b/camel/memories/context_creators/base.py
new file mode 100644
index 000000000..cdc6046b2
--- /dev/null
+++ b/camel/memories/context_creators/base.py
@@ -0,0 +1,72 @@
+# =========== 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, Tuple
+
+from camel.memories import ContextRecord
+from camel.messages import OpenAIMessage
+from camel.utils import BaseTokenCounter
+
+
+class BaseContextCreator(ABC):
+ r"""An abstract base class defining the interface for context creation
+ strategies.
+
+ This class provides a foundational structure for different strategies to
+ generate conversational context from a list of context records. The
+ primary goal is to create a context that is aligned with a specified token
+ count limit, allowing subclasses to define their specific approach.
+
+ Subclasses should implement the `token_counter`, `token_limit`, and
+ `create_context` methods to provide specific context creation logic.
+
+ Attributes:
+ token_counter (BaseTokenCounter): A token counter instance responsible
+ for counting tokens in a message.
+ token_limit (int): The maximum number of tokens allowed in the
+ generated context.
+ """
+
+ @property
+ @abstractmethod
+ def token_counter(self) -> BaseTokenCounter:
+ pass
+
+ @property
+ @abstractmethod
+ def token_limit(self) -> int:
+ pass
+
+ @abstractmethod
+ def create_context(
+ self,
+ records: List[ContextRecord],
+ ) -> Tuple[List[OpenAIMessage], int]:
+ r"""An abstract method to create conversational context from the chat
+ history.
+
+ Constructs the context from provided records. The specifics of how this
+ is done and how the token count is managed should be provided by
+ subclasses implementing this method. The the output messages order
+ should keep same as the input order.
+
+ Args:
+ records (List[ContextRecord]): A list of context records from
+ which to generate the context.
+
+ Returns:
+ Tuple[List[OpenAIMessage], int]: A tuple containing the constructed
+ context in OpenAIMessage format and the total token count.
+ """
+ pass
diff --git a/camel/memories/context_creators/score_based.py b/camel/memories/context_creators/score_based.py
new file mode 100644
index 000000000..70be64bdb
--- /dev/null
+++ b/camel/memories/context_creators/score_based.py
@@ -0,0 +1,130 @@
+# =========== 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 List, Tuple
+
+from camel.memories import ContextRecord
+from camel.memories.context_creators import BaseContextCreator
+from camel.messages import OpenAIMessage
+from camel.utils import BaseTokenCounter
+
+
+@dataclass(frozen=True)
+class _ContextUnit:
+ idx: int
+ record: ContextRecord
+ num_tokens: int
+
+
+class ScoreBasedContextCreator(BaseContextCreator):
+ r"""A default implementation of context creation strategy, which inherits
+ from :obj:`BaseContextCreator`.
+
+ This class provides a strategy to generate a conversational context from
+ a list of chat history records while ensuring the total token count of
+ the context does not exceed a specified limit. It prunes messages based
+ on their score if the total token count exceeds the limit.
+
+ Args:
+ token_counter (BaseTokenCounter): An instance responsible for counting
+ tokens in a message.
+ token_limit (int): The maximum number of tokens allowed in the
+ generated context.
+ """
+
+ def __init__(self, token_counter: BaseTokenCounter,
+ token_limit: int) -> None:
+ self._token_counter = token_counter
+ self._token_limit = token_limit
+
+ @property
+ def token_counter(self) -> BaseTokenCounter:
+ return self._token_counter
+
+ @property
+ def token_limit(self) -> int:
+ return self._token_limit
+
+ def create_context(
+ self,
+ records: List[ContextRecord],
+ ) -> Tuple[List[OpenAIMessage], int]:
+ r"""Creates conversational context from chat history while respecting
+ token limits.
+
+ Constructs the context from provided records and ensures that the total
+ token count does not exceed the specified limit by pruning the least
+ score messages if necessary.
+
+ Args:
+ records (List[ContextRecord]): A list of message records from which
+ to generate the context.
+
+ Returns:
+ Tuple[List[OpenAIMessage], int]: A tuple containing the constructed
+ context in OpenAIMessage format and the total token count.
+
+ Raises:
+ RuntimeError: If it's impossible to create a valid context without
+ exceeding the token limit.
+ """
+ context_units = [
+ _ContextUnit(
+ idx,
+ record,
+ self.token_counter.count_tokens_from_messages(
+ [record.memory_record.to_openai_message()]),
+ ) for idx, record in enumerate(records)
+ ]
+ # TODO: optimize the process, may give information back to memory
+
+ # If not exceed token limit, simply return
+ total_tokens = sum([unit.num_tokens for unit in context_units])
+ if total_tokens <= self.token_limit:
+ return self._create_output(context_units)
+
+ # Sort by score
+ context_units = sorted(context_units,
+ key=lambda unit: unit.record.score)
+
+ # Remove least score messages until total token number is smaller
+ # than token limit
+ truncate_idx = None
+ for i, unit in enumerate(context_units):
+ if unit.record.score == 1:
+ raise RuntimeError(
+ "Cannot create context: exceed token limit.", total_tokens)
+ total_tokens -= unit.num_tokens
+ if total_tokens <= self.token_limit:
+ truncate_idx = i
+ break
+ if truncate_idx is None:
+ raise RuntimeError("Cannot create context: exceed token limit.",
+ total_tokens)
+ return self._create_output(context_units[truncate_idx + 1:])
+
+ def _create_output(
+ self, context_units: List[_ContextUnit]
+ ) -> Tuple[List[OpenAIMessage], int]:
+ r"""Helper method to generate output from context units.
+
+ This method converts the provided context units into a format suitable
+ for output, specifically a list of OpenAIMessages and an integer
+ representing the total token count.
+ """
+ context_units = sorted(context_units, key=lambda unit: unit.idx)
+ return [
+ unit.record.memory_record.to_openai_message()
+ for unit in context_units
+ ], sum([unit.num_tokens for unit in context_units])
diff --git a/camel/memories/records.py b/camel/memories/records.py
new file mode 100644
index 000000000..95c145cd6
--- /dev/null
+++ b/camel/memories/records.py
@@ -0,0 +1,93 @@
+# =========== 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 asdict, dataclass, field
+from typing import Any, ClassVar, Dict
+from uuid import UUID, uuid4
+
+from camel.messages import BaseMessage, FunctionCallingMessage, OpenAIMessage
+from camel.typing import OpenAIBackendRole
+
+
+@dataclass(frozen=True)
+class MemoryRecord:
+ r"""The basic message storing unit in the CAMEL memory system.
+
+ Attributes:
+ message (BaseMessage): The main content of the record.
+ role_at_backend (OpenAIBackendRole): An enumeration value representing
+ the role this message played at the OpenAI backend. Note that this
+ value is different from the :obj:`RoleType` used in the CAMEL role
+ playing system.
+ uuid (UUID, optional): A universally unique identifier for this record.
+ This is used to uniquely identify this record in the memory system.
+ If not given, it will be assigned with a random UUID.
+ extra_info (Dict[str, str], optional): A dictionary of additional
+ key-value pairs that provide more information. If not given, it
+ will be an empty `Dict`.
+ """
+ message: BaseMessage
+ role_at_backend: OpenAIBackendRole
+ uuid: UUID = field(default_factory=uuid4)
+ extra_info: Dict[str, str] = field(default_factory=dict)
+
+ _MESSAGE_TYPES: ClassVar[dict] = {
+ "BaseMessage": BaseMessage,
+ "FunctionCallingMessage": FunctionCallingMessage
+ }
+
+ @classmethod
+ def from_dict(cls, record_dict: Dict[str, Any]) -> "MemoryRecord":
+ r"""Reconstruct a :obj:`MemoryRecord` from the input dict.
+
+ Args:
+ record_dict(Dict[str, Any]): A dict generated by :meth:`to_dict`.
+ """
+ message_cls = cls._MESSAGE_TYPES[record_dict["message"]["__class__"]]
+ kwargs: Dict = record_dict["message"].copy()
+ kwargs.pop("__class__")
+ reconstructed_message = message_cls(**kwargs)
+ return cls(
+ uuid=UUID(record_dict["uuid"]),
+ message=reconstructed_message,
+ role_at_backend=record_dict["role_at_backend"],
+ extra_info=record_dict["extra_info"],
+ )
+
+ def to_dict(self) -> Dict[str, Any]:
+ r"""Convert the :obj:`MemoryRecord` to a dict for serialization
+ purposes.
+ """
+ return {
+ "uuid": str(self.uuid),
+ "message": {
+ "__class__": self.message.__class__.__name__,
+ **asdict(self.message)
+ },
+ "role_at_backend": self.role_at_backend,
+ "extra_info": self.extra_info
+ }
+
+ def to_openai_message(self) -> OpenAIMessage:
+ r"""Converts the record to an :obj:`OpenAIMessage` object.
+ """
+ return self.message.to_openai_message(self.role_at_backend.value)
+
+
+@dataclass(frozen=True)
+class ContextRecord:
+ r"""The result of memory retrieving.
+ """
+ memory_record: MemoryRecord
+ score: float
diff --git a/camel/societies/babyagi_playing.py b/camel/societies/babyagi_playing.py
index 7a5e27053..7c0227618 100644
--- a/camel/societies/babyagi_playing.py
+++ b/camel/societies/babyagi_playing.py
@@ -224,9 +224,9 @@ class BabyAGI:
assistant_response = self.assistant_agent.step(assistant_msg_msg)
assistant_msg = assistant_response.msgs[0]
- self.assistant_agent.submit_message(assistant_msg)
- self.task_creation_agent.submit_message(assistant_msg)
- self.task_prioritization_agent.submit_message(assistant_msg)
+ self.assistant_agent.record_message(assistant_msg)
+ self.task_creation_agent.record_message(assistant_msg)
+ self.task_prioritization_agent.record_message(assistant_msg)
self.solved_subtasks.append(task_name)
past_tasks = self.solved_subtasks + list(self.subtasks)
diff --git a/camel/societies/role_playing.py b/camel/societies/role_playing.py
index 80ddbb9ca..9ba51dd1c 100644
--- a/camel/societies/role_playing.py
+++ b/camel/societies/role_playing.py
@@ -436,7 +436,7 @@ class RolePlaying:
ChatAgentResponse([], user_response.terminated,
user_response.info))
user_msg = self.reduce_message_options(user_response.msgs)
- self.user_agent.submit_message(user_msg)
+ self.user_agent.record_message(user_msg)
assistant_response = self.assistant_agent.step(user_msg)
if assistant_response.terminated or assistant_response.msgs is None:
@@ -444,7 +444,7 @@ class RolePlaying:
assistant_response.info),
ChatAgentResponse([user_msg], False, user_response.info))
assistant_msg = self.reduce_message_options(assistant_response.msgs)
- self.assistant_agent.submit_message(assistant_msg)
+ self.assistant_agent.record_message(assistant_msg)
return (
ChatAgentResponse([assistant_msg], assistant_response.terminated,
diff --git a/camel/storages/__init__.py b/camel/storages/__init__.py
new file mode 100644
index 000000000..f6b8f4540
--- /dev/null
+++ b/camel/storages/__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 .key_value_storages.base import BaseKeyValueStorage
+from .key_value_storages.in_memory import InMemoryKeyValueStorage
+from .key_value_storages.json import JsonStorage
+
+__all__ = [
+ 'BaseKeyValueStorage',
+ 'InMemoryKeyValueStorage',
+ 'JsonStorage',
+]
diff --git a/camel/storages/key_value_storages/__init__.py b/camel/storages/key_value_storages/__init__.py
new file mode 100644
index 000000000..3c23a08f2
--- /dev/null
+++ b/camel/storages/key_value_storages/__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 BaseKeyValueStorage
+from .in_memory import InMemoryKeyValueStorage
+from .json import JsonStorage
+
+__all__ = [
+ 'BaseKeyValueStorage',
+ 'InMemoryKeyValueStorage',
+ 'JsonStorage',
+]
diff --git a/camel/storages/key_value_storages/base.py b/camel/storages/key_value_storages/base.py
new file mode 100644
index 000000000..4495b9c3c
--- /dev/null
+++ b/camel/storages/key_value_storages/base.py
@@ -0,0 +1,57 @@
+# =========== 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 Any, Dict, List
+
+
+class BaseKeyValueStorage(ABC):
+ r"""An abstract base class for key-value storage systems. Provides a
+ consistent interface for saving, loading, and clearing data records without
+ any loss of information.
+
+ An abstract base class designed to serve as a foundation for various
+ key-value storage systems. The class primarily interacts through Python
+ dictionaries.
+
+ This class is meant to be inherited by multiple types of key-value storage
+ implementations, including, but not limited to, JSON file storage, NoSQL
+ databases like MongoDB and Redis, as well as in-memory Python dictionaries.
+ """
+
+ @abstractmethod
+ def save(self, records: List[Dict[str, Any]]) -> None:
+ r"""Saves a batch of records to the key-value storage system.
+
+ Args:
+ records (List[Dict[str, Any]]): A list of dictionaries, where each
+ dictionary represents a unique record to be stored.
+ """
+ pass
+
+ @abstractmethod
+ def load(self) -> List[Dict[str, Any]]:
+ r"""Loads all stored records from the key-value storage system.
+
+ Returns:
+ List[Dict[str, Any]]: A list of dictionaries, where each dictionary
+ represents a stored record.
+ """
+ pass
+
+ @abstractmethod
+ def clear(self) -> None:
+ r"""Removes all records from the key-value storage system.
+ """
+ pass
diff --git a/camel/storages/key_value_storages/in_memory.py b/camel/storages/key_value_storages/in_memory.py
new file mode 100644
index 000000000..f0b10ad07
--- /dev/null
+++ b/camel/storages/key_value_storages/in_memory.py
@@ -0,0 +1,51 @@
+# =========== 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 copy import deepcopy
+from typing import Any, Dict, List
+
+from camel.storages.key_value_storages import BaseKeyValueStorage
+
+
+class InMemoryKeyValueStorage(BaseKeyValueStorage):
+ r"""A concrete implementation of the :obj:`BaseKeyValueStorage` using
+ in-memory list. Ideal for temporary storage purposes, as data will be lost
+ when the program ends.
+ """
+
+ def __init__(self) -> None:
+ self.memory_list: List[Dict] = []
+
+ def save(self, records: List[Dict[str, Any]]) -> None:
+ r"""Saves a batch of records to the key-value storage system.
+
+ Args:
+ records (List[Dict[str, Any]]): A list of dictionaries, where each
+ dictionary represents a unique record to be stored.
+ """
+ self.memory_list.extend(deepcopy(records))
+
+ def load(self) -> List[Dict[str, Any]]:
+ r"""Loads all stored records from the key-value storage system.
+
+ Returns:
+ List[Dict[str, Any]]: A list of dictionaries, where each dictionary
+ represents a stored record.
+ """
+ return deepcopy(self.memory_list)
+
+ def clear(self) -> None:
+ r"""Removes all records from the key-value storage system.
+ """
+ self.memory_list.clear()
diff --git a/camel/storages/key_value_storages/json.py b/camel/storages/key_value_storages/json.py
new file mode 100644
index 000000000..7d31e8012
--- /dev/null
+++ b/camel/storages/key_value_storages/json.py
@@ -0,0 +1,97 @@
+# =========== 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 json
+from pathlib import Path
+from typing import Any, Dict, List, Optional
+
+from camel.storages.key_value_storages import BaseKeyValueStorage
+from camel.typing import (
+ ModelType,
+ OpenAIBackendRole,
+ RoleType,
+ TaskType,
+ VectorDistance,
+)
+
+
+class _CamelJSONEncoder(json.JSONEncoder):
+ r"""A custom JSON encoder for serializing specifically enumerated types.
+ Ensures enumerated types can be stored in and retrieved from JSON format.
+ """
+ CAMEL_ENUMS = {
+ "RoleType": RoleType,
+ "TaskType": TaskType,
+ "ModelType": ModelType,
+ "OpenAIBackendRole": OpenAIBackendRole,
+ "VectorDistance": VectorDistance,
+ }
+
+ def default(self, obj) -> Any:
+ if type(obj) in self.CAMEL_ENUMS.values():
+ return {"__enum__": str(obj)}
+ # Let the base class default method raise the TypeError
+ return json.JSONEncoder.default(self, obj)
+
+
+class JsonStorage(BaseKeyValueStorage):
+ r"""A concrete implementation of the :obj:`BaseKeyValueStorage` using JSON
+ files. Allows for persistent storage of records in a human-readable format.
+
+ Args:
+ path (Path, optional): Path to the desired JSON file. If `None`, a
+ default path `./chat_history.json` will be used.
+ (default: :obj:`None`)
+ """
+
+ def __init__(self, path: Optional[Path] = None) -> None:
+ self.json_path = path or Path("./chat_history.json")
+ self.json_path.touch()
+
+ def _json_object_hook(self, d) -> Any:
+ if "__enum__" in d:
+ name, member = d["__enum__"].split(".")
+ return getattr(_CamelJSONEncoder.CAMEL_ENUMS[name], member)
+ else:
+ return d
+
+ def save(self, records: List[Dict[str, Any]]) -> None:
+ r"""Saves a batch of records to the key-value storage system.
+
+ Args:
+ records (List[Dict[str, Any]]): A list of dictionaries, where each
+ dictionary represents a unique record to be stored.
+ """
+ with self.json_path.open("a") as f:
+ f.writelines(
+ [json.dumps(r, cls=_CamelJSONEncoder) + "\n" for r in records])
+
+ def load(self) -> List[Dict[str, Any]]:
+ r"""Loads all stored records from the key-value storage system.
+
+ Returns:
+ List[Dict[str, Any]]: A list of dictionaries, where each dictionary
+ represents a stored record.
+ """
+ with self.json_path.open("r") as f:
+ return [
+ json.loads(r, object_hook=self._json_object_hook)
+ for r in f.readlines()
+ ]
+
+ def clear(self) -> None:
+ r"""Removes all records from the key-value storage system.
+ """
+ with self.json_path.open("w"):
+ pass
diff --git a/camel/typing.py b/camel/typing.py
index 1a5279972..77596acae 100644
--- a/camel/typing.py
+++ b/camel/typing.py
@@ -125,9 +125,29 @@ class TaskType(Enum):
DEFAULT = "default"
+class VectorDistance(Enum):
+ DOT = 1
+ COSINE = 2
+ EUCLIDEAN = 3
+
+
+class OpenAIBackendRole(Enum):
+ ASSISTANT = "assistant"
+ SYSTEM = "system"
+ USER = "user"
+ FUNCTION = "function"
+
+
class TerminationMode(Enum):
ANY = "any"
ALL = "all"
-__all__ = ['RoleType', 'ModelType', 'TaskType', 'TerminationMode']
+__all__ = [
+ 'RoleType',
+ 'ModelType',
+ 'TaskType',
+ 'TerminationMode',
+ 'OpenAIBackendRole',
+ 'VectorDistance',
+]
diff --git a/camel/utils/functions.py b/camel/utils/functions.py
index a4a1cd048..640295d80 100644
--- a/camel/utils/functions.py
+++ b/camel/utils/functions.py
@@ -55,10 +55,7 @@ def openai_api_key_required(func: F) -> F:
@wraps(func)
def wrapper(self, *args, **kwargs):
- from camel.agents.chat_agent import ChatAgent
- if not isinstance(self, ChatAgent):
- raise ValueError("Expected ChatAgent")
- if self.model == ModelType.STUB:
+ if hasattr(self, "model") and self.model == ModelType.STUB:
return func(self, *args, **kwargs)
elif self.model.is_open_source:
return func(self, *args, **kwargs)
diff --git a/docs/camel.memory.rst b/docs/camel.memory.rst
new file mode 100644
index 000000000..64e18aba0
--- /dev/null
+++ b/docs/camel.memory.rst
@@ -0,0 +1,10 @@
+camel.memories package
+======================
+
+Module contents
+---------------
+
+.. automodule:: camel.memories
+ :members:
+ :undoc-members:
+ :show-inheritancek
diff --git a/docs/camel.rst b/docs/camel.rst
index e1c507e83..413a86b13 100644
--- a/docs/camel.rst
+++ b/docs/camel.rst
@@ -9,6 +9,8 @@ Subpackages
camel.agents
camel.prompts
+ camel.memories
+ camel.storages
Submodules
----------
diff --git a/docs/camel.storage.rst b/docs/camel.storage.rst
new file mode 100644
index 000000000..fe54bae4b
--- /dev/null
+++ b/docs/camel.storage.rst
@@ -0,0 +1,10 @@
+camel.storages package
+======================
+
+Module contents
+---------------
+
+.. automodule:: camel.storages
+ :members:
+ :undoc-members:
+ :show-inheritancek
\ No newline at end of file
diff --git a/examples/code/role_playing_multiprocess.py b/examples/code/role_playing_multiprocess.py
index ec1c2d7e1..026a84c5b 100644
--- a/examples/code/role_playing_multiprocess.py
+++ b/examples/code/role_playing_multiprocess.py
@@ -130,7 +130,7 @@ def generate_data(language_idx: int, language_name: str, domain_idx: int,
f"{user_response.info['termination_reasons'][0]}")
break
- user_agent.submit_message(user_response.msg)
+ user_agent.record_message(user_response.msg)
print(f"User:\n{user_response.msg.content}\n")
assistant_response = assistant_agent.step(user_response.msg)
@@ -142,7 +142,7 @@ def generate_data(language_idx: int, language_name: str, domain_idx: int,
f"{assistant_response.info['termination_reasons'][0]}")
break
- assistant_agent.submit_message(assistant_response.msg)
+ assistant_agent.record_message(assistant_response.msg)
print(f"Assistant:\n{assistant_response.msg.content}\n")
# Condition 3: Break if user does not give instruction
diff --git a/examples/role_description/role_playing_with_role_description.py b/examples/role_description/role_playing_with_role_description.py
index d6ba87436..9e0092e31 100644
--- a/examples/role_description/role_playing_with_role_description.py
+++ b/examples/role_description/role_playing_with_role_description.py
@@ -51,7 +51,7 @@ def main(model_type_for_role_generation=None, model_type=None) -> None:
user_role_name=ai_user_role,
task_prompt=task_prompt,
model_type=model_type,
- task_type=TaskType.ROLE_DESCRIPTION, # Important for role description
+ task_type=TaskType.ROLE_DESCRIPTION, # Score for role description
with_task_specify=True,
task_specify_agent_kwargs=dict(model=model_type),
extend_sys_msg_meta_dicts=sys_msg_meta_dicts,
diff --git a/poetry.lock b/poetry.lock
index 31326fb17..756a7726e 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -1039,13 +1039,13 @@ tqdm = ["tqdm"]
[[package]]
name = "gradio"
-version = "3.43.2"
+version = "3.44.0"
description = "Python library for easily interacting with trained machine learning models"
optional = false
python-versions = ">=3.8"
files = [
- {file = "gradio-3.43.2-py3-none-any.whl", hash = "sha256:3b0f92a5eb604a0d79066e36c983aa9e50ba8d8bda0a1f5533b3389a3839a07b"},
- {file = "gradio-3.43.2.tar.gz", hash = "sha256:f15fae8c418d7bd9f1a0b0d38e4b6f8354405123925ee6a6ae4bfade492c870b"},
+ {file = "gradio-3.44.0-py3-none-any.whl", hash = "sha256:cfb2807b52b542b276a215418bd7f4fec3719e85fb8544ffd9bccf21247f9126"},
+ {file = "gradio-3.44.0.tar.gz", hash = "sha256:88cc8f6bb59237d037c1ca23e3a7471f99b416d258259cf01985bf6ad741b215"},
]
[package.dependencies]
@@ -2691,7 +2691,6 @@ files = [
{file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:69b023b2b4daa7548bcfbd4aa3da05b3a74b772db9e23b982788168117739938"},
{file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:81e0b275a9ecc9c0c0c07b4b90ba548307583c125f54d5b6946cfee6360c733d"},
{file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ba336e390cd8e4d1739f42dfe9bb83a3cc2e80f567d8805e11b46f4a943f5515"},
- {file = "PyYAML-6.0.1-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:326c013efe8048858a6d312ddd31d56e468118ad4cdeda36c719bf5bb6192290"},
{file = "PyYAML-6.0.1-cp310-cp310-win32.whl", hash = "sha256:bd4af7373a854424dabd882decdc5579653d7868b8fb26dc7d0e99f823aa5924"},
{file = "PyYAML-6.0.1-cp310-cp310-win_amd64.whl", hash = "sha256:fd1592b3fdf65fff2ad0004b5e363300ef59ced41c2e6b3a99d4089fa8c5435d"},
{file = "PyYAML-6.0.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:6965a7bc3cf88e5a1c3bd2e0b5c22f8d677dc88a455344035f03399034eb3007"},
@@ -2699,15 +2698,8 @@ files = [
{file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:42f8152b8dbc4fe7d96729ec2b99c7097d656dc1213a3229ca5383f973a5ed6d"},
{file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:062582fca9fabdd2c8b54a3ef1c978d786e0f6b3a1510e0ac93ef59e0ddae2bc"},
{file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d2b04aac4d386b172d5b9692e2d2da8de7bfb6c387fa4f801fbf6fb2e6ba4673"},
- {file = "PyYAML-6.0.1-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:e7d73685e87afe9f3b36c799222440d6cf362062f78be1013661b00c5c6f678b"},
{file = "PyYAML-6.0.1-cp311-cp311-win32.whl", hash = "sha256:1635fd110e8d85d55237ab316b5b011de701ea0f29d07611174a1b42f1444741"},
{file = "PyYAML-6.0.1-cp311-cp311-win_amd64.whl", hash = "sha256:bf07ee2fef7014951eeb99f56f39c9bb4af143d8aa3c21b1677805985307da34"},
- {file = "PyYAML-6.0.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:855fb52b0dc35af121542a76b9a84f8d1cd886ea97c84703eaa6d88e37a2ad28"},
- {file = "PyYAML-6.0.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:40df9b996c2b73138957fe23a16a4f0ba614f4c0efce1e9406a184b6d07fa3a9"},
- {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c22bec3fbe2524cde73d7ada88f6566758a8f7227bfbf93a408a9d86bcc12a0"},
- {file = "PyYAML-6.0.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8d4e9c88387b0f5c7d5f281e55304de64cf7f9c0021a3525bd3b1c542da3b0e4"},
- {file = "PyYAML-6.0.1-cp312-cp312-win32.whl", hash = "sha256:d483d2cdf104e7c9fa60c544d92981f12ad66a457afae824d146093b8c294c54"},
- {file = "PyYAML-6.0.1-cp312-cp312-win_amd64.whl", hash = "sha256:0d3304d8c0adc42be59c5f8a4d9e3d7379e6955ad754aa9d6ab7a398b59dd1df"},
{file = "PyYAML-6.0.1-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:50550eb667afee136e9a77d6dc71ae76a44df8b3e51e41b77f6de2932bfe0f47"},
{file = "PyYAML-6.0.1-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:1fe35611261b29bd1de0070f0b2f47cb6ff71fa6595c077e42bd0c419fa27b98"},
{file = "PyYAML-6.0.1-cp36-cp36m-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:704219a11b772aea0d8ecd7058d0082713c3562b4e271b849ad7dc4a5c90c13c"},
@@ -2724,7 +2716,6 @@ files = [
{file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a0cd17c15d3bb3fa06978b4e8958dcdc6e0174ccea823003a106c7d4d7899ac5"},
{file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:28c119d996beec18c05208a8bd78cbe4007878c6dd15091efb73a30e90539696"},
{file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7e07cbde391ba96ab58e532ff4803f79c4129397514e1413a7dc761ccd755735"},
- {file = "PyYAML-6.0.1-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:49a183be227561de579b4a36efbb21b3eab9651dd81b1858589f796549873dd6"},
{file = "PyYAML-6.0.1-cp38-cp38-win32.whl", hash = "sha256:184c5108a2aca3c5b3d3bf9395d50893a7ab82a38004c8f61c258d4428e80206"},
{file = "PyYAML-6.0.1-cp38-cp38-win_amd64.whl", hash = "sha256:1e2722cc9fbb45d9b87631ac70924c11d3a401b2d7f410cc0e3bbf249f2dca62"},
{file = "PyYAML-6.0.1-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:9eb6caa9a297fc2c2fb8862bc5370d0303ddba53ba97e71f08023b6cd73d16a8"},
@@ -2732,7 +2723,6 @@ files = [
{file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5773183b6446b2c99bb77e77595dd486303b4faab2b086e7b17bc6bef28865f6"},
{file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:b786eecbdf8499b9ca1d697215862083bd6d2a99965554781d0d8d1ad31e13a0"},
{file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:bc1bf2925a1ecd43da378f4db9e4f799775d6367bdb94671027b73b393a7c42c"},
- {file = "PyYAML-6.0.1-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:04ac92ad1925b2cff1db0cfebffb6ffc43457495c9b3c39d3fcae417d7125dc5"},
{file = "PyYAML-6.0.1-cp39-cp39-win32.whl", hash = "sha256:faca3bdcf85b2fc05d06ff3fbc1f83e1391b3e724afa3feba7d13eeab355484c"},
{file = "PyYAML-6.0.1-cp39-cp39-win_amd64.whl", hash = "sha256:510c9deebc5c0225e8c96813043e62b680ba2f9c50a08d3724c7f28a747d1486"},
{file = "PyYAML-6.0.1.tar.gz", hash = "sha256:bfdf460b1736c775f2ba9f6a92bca30bc2095067b8a9d77876d1fad6cc3b4a43"},
@@ -3006,9 +2996,6 @@ files = [
{file = "safetensors-0.3.3-cp310-cp310-macosx_12_0_x86_64.whl", hash = "sha256:17f41344d9a075f2f21b289a49a62e98baff54b5754240ba896063bce31626bf"},
{file = "safetensors-0.3.3-cp310-cp310-macosx_13_0_arm64.whl", hash = "sha256:f1045f798e1a16a6ced98d6a42ec72936d367a2eec81dc5fade6ed54638cd7d2"},
{file = "safetensors-0.3.3-cp310-cp310-macosx_13_0_x86_64.whl", hash = "sha256:eaf0e4bc91da13f21ac846a39429eb3f3b7ed06295a32321fa3eb1a59b5c70f3"},
- {file = "safetensors-0.3.3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:25149180d4dc8ca48bac2ac3852a9424b466e36336a39659b35b21b2116f96fc"},
- {file = "safetensors-0.3.3-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:c9e943bf78c39de8865398a71818315e7d5d1af93c7b30d4da3fc852e62ad9bc"},
- {file = "safetensors-0.3.3-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:cccfcac04a010354e87c7a2fe16a1ff004fc4f6e7ef8efc966ed30122ce00bc7"},
{file = "safetensors-0.3.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a07121f427e646a50d18c1be0fa1a2cbf6398624c31149cd7e6b35486d72189e"},
{file = "safetensors-0.3.3-cp310-cp310-win32.whl", hash = "sha256:a85e29cbfddfea86453cc0f4889b4bcc6b9c155be9a60e27be479a34e199e7ef"},
{file = "safetensors-0.3.3-cp310-cp310-win_amd64.whl", hash = "sha256:e13adad4a3e591378f71068d14e92343e626cf698ff805f61cdb946e684a218e"},
@@ -3017,9 +3004,6 @@ files = [
{file = "safetensors-0.3.3-cp311-cp311-macosx_12_0_universal2.whl", hash = "sha256:f84a74cbe9859b28e3d6d7715ac1dd3097bebf8d772694098f6d42435245860c"},
{file = "safetensors-0.3.3-cp311-cp311-macosx_13_0_arm64.whl", hash = "sha256:10d637423d98ab2e6a4ad96abf4534eb26fcaf8ca3115623e64c00759374e90d"},
{file = "safetensors-0.3.3-cp311-cp311-macosx_13_0_universal2.whl", hash = "sha256:3b46f5de8b44084aff2e480874c550c399c730c84b2e8ad1bddb062c94aa14e9"},
- {file = "safetensors-0.3.3-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e76da691a82dfaf752854fa6d17c8eba0c8466370c5ad8cf1bfdf832d3c7ee17"},
- {file = "safetensors-0.3.3-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:c4e342fd54e66aa9512dd13e410f791e47aa4feeb5f4c9a20882c72f3d272f29"},
- {file = "safetensors-0.3.3-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:178fd30b5dc73bce14a39187d948cedd0e5698e2f055b7ea16b5a96c9b17438e"},
{file = "safetensors-0.3.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:3e8fdf7407dba44587ed5e79d5de3533d242648e1f2041760b21474bd5ea5c8c"},
{file = "safetensors-0.3.3-cp311-cp311-win32.whl", hash = "sha256:7d3b744cee8d7a46ffa68db1a2ff1a1a432488e3f7a5a97856fe69e22139d50c"},
{file = "safetensors-0.3.3-cp311-cp311-win_amd64.whl", hash = "sha256:f579877d30feec9b6ba409d05fa174633a4fc095675a4a82971d831a8bb60b97"},
@@ -3027,9 +3011,6 @@ files = [
{file = "safetensors-0.3.3-cp37-cp37m-macosx_11_0_x86_64.whl", hash = "sha256:41adb1d39e8aad04b16879e3e0cbcb849315999fad73bc992091a01e379cb058"},
{file = "safetensors-0.3.3-cp37-cp37m-macosx_12_0_x86_64.whl", hash = "sha256:0f2b404250b3b877b11d34afcc30d80e7035714a1116a3df56acaca6b6c00096"},
{file = "safetensors-0.3.3-cp37-cp37m-macosx_13_0_x86_64.whl", hash = "sha256:b43956ef20e9f4f2e648818a9e7b3499edd6b753a0f5526d4f6a6826fbee8446"},
- {file = "safetensors-0.3.3-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d61a99b34169981f088ccfbb2c91170843efc869a0a0532f422db7211bf4f474"},
- {file = "safetensors-0.3.3-cp37-cp37m-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:c0008aab36cd20e9a051a68563c6f80d40f238c2611811d7faa5a18bf3fd3984"},
- {file = "safetensors-0.3.3-cp37-cp37m-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:93d54166072b143084fdcd214a080a088050c1bb1651016b55942701b31334e4"},
{file = "safetensors-0.3.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:1c32ee08f61cea56a5d62bbf94af95df6040c8ab574afffaeb7b44ae5da1e9e3"},
{file = "safetensors-0.3.3-cp37-cp37m-win32.whl", hash = "sha256:351600f367badd59f7bfe86d317bb768dd8c59c1561c6fac43cafbd9c1af7827"},
{file = "safetensors-0.3.3-cp37-cp37m-win_amd64.whl", hash = "sha256:034717e297849dae1af0a7027a14b8647bd2e272c24106dced64d83e10d468d1"},
@@ -3039,9 +3020,6 @@ files = [
{file = "safetensors-0.3.3-cp38-cp38-macosx_12_0_x86_64.whl", hash = "sha256:69ccee8d05f55cdf76f7e6c87d2bdfb648c16778ef8acfd2ecc495e273e9233e"},
{file = "safetensors-0.3.3-cp38-cp38-macosx_13_0_arm64.whl", hash = "sha256:c08a9a4b7a4ca389232fa8d097aebc20bbd4f61e477abc7065b5c18b8202dede"},
{file = "safetensors-0.3.3-cp38-cp38-macosx_13_0_x86_64.whl", hash = "sha256:a002868d2e3f49bbe81bee2655a411c24fa1f8e68b703dec6629cb989d6ae42e"},
- {file = "safetensors-0.3.3-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3bd2704cb41faa44d3ec23e8b97330346da0395aec87f8eaf9c9e2c086cdbf13"},
- {file = "safetensors-0.3.3-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:4b2951bf3f0ad63df5e6a95263652bd6c194a6eb36fd4f2d29421cd63424c883"},
- {file = "safetensors-0.3.3-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:07114cec116253ca2e7230fdea30acf76828f21614afd596d7b5438a2f719bd8"},
{file = "safetensors-0.3.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6ab43aeeb9eadbb6b460df3568a662e6f1911ecc39387f8752afcb6a7d96c087"},
{file = "safetensors-0.3.3-cp38-cp38-win32.whl", hash = "sha256:f2f59fce31dd3429daca7269a6b06f65e6547a0c248f5116976c3f1e9b73f251"},
{file = "safetensors-0.3.3-cp38-cp38-win_amd64.whl", hash = "sha256:c31ca0d8610f57799925bf08616856b39518ab772c65093ef1516762e796fde4"},
@@ -3051,9 +3029,6 @@ files = [
{file = "safetensors-0.3.3-cp39-cp39-macosx_12_0_x86_64.whl", hash = "sha256:12b83f1986cd16ea0454c636c37b11e819d60dd952c26978310a0835133480b7"},
{file = "safetensors-0.3.3-cp39-cp39-macosx_13_0_arm64.whl", hash = "sha256:f439175c827c2f1bbd54df42789c5204a10983a30bc4242bc7deaf854a24f3f0"},
{file = "safetensors-0.3.3-cp39-cp39-macosx_13_0_x86_64.whl", hash = "sha256:0085be33b8cbcb13079b3a8e131656e05b0bc5e6970530d4c24150f7afd76d70"},
- {file = "safetensors-0.3.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3e3ec70c87b1e910769034206ad5efc051069b105aac1687f6edcd02526767f4"},
- {file = "safetensors-0.3.3-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:f490132383e5e490e710608f4acffcb98ed37f91b885c7217d3f9f10aaff9048"},
- {file = "safetensors-0.3.3-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:79d1b6c7ed5596baf79c80fbce5198c3cdcc521ae6a157699f427aba1a90082d"},
{file = "safetensors-0.3.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ad3cc8006e7a86ee7c88bd2813ec59cd7cc75b03e6fa4af89b9c7b235b438d68"},
{file = "safetensors-0.3.3-cp39-cp39-win32.whl", hash = "sha256:ab29f54c6b8c301ca05fa014728996bd83aac6e21528f893aaf8945c71f42b6d"},
{file = "safetensors-0.3.3-cp39-cp39-win_amd64.whl", hash = "sha256:0fa82004eae1a71e2aa29843ef99de9350e459a0fc2f65fc6ee0da9690933d2d"},
@@ -3143,13 +3118,13 @@ files = [
[[package]]
name = "setuptools"
-version = "68.2.1"
+version = "68.2.2"
description = "Easily download, build, install, upgrade, and uninstall Python packages"
optional = false
python-versions = ">=3.8"
files = [
- {file = "setuptools-68.2.1-py3-none-any.whl", hash = "sha256:eff96148eb336377ab11beee0c73ed84f1709a40c0b870298b0d058828761bae"},
- {file = "setuptools-68.2.1.tar.gz", hash = "sha256:56ee14884fd8d0cd015411f4a13f40b4356775a0aefd9ebc1d3bfb9a1acb32f1"},
+ {file = "setuptools-68.2.2-py3-none-any.whl", hash = "sha256:b454a35605876da60632df1a60f736524eb73cc47bbc9f3f1ef1b644de74fd2a"},
+ {file = "setuptools-68.2.2.tar.gz", hash = "sha256:4ac1475276d2f1c48684874089fefcd83bd7162ddaafb81fac866ba0db282a87"},
]
[package.extras]
@@ -3423,40 +3398,40 @@ doc = ["reno", "sphinx", "tornado (>=4.5)"]
[[package]]
name = "tiktoken"
-version = "0.5.0"
+version = "0.5.1"
description = "tiktoken is a fast BPE tokeniser for use with OpenAI's models"
optional = false
python-versions = ">=3.8"
files = [
- {file = "tiktoken-0.5.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:6eaf6b593f09e321446e0940d5bc72960687b2d1889c0431d42718d437bb3285"},
- {file = "tiktoken-0.5.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:7c96a1234e0edd4a7dd616c5da2658268a55c0931e72d319a998807feba8aa77"},
- {file = "tiktoken-0.5.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3578d1d49757d9a2770d1aa36915a342804da1a419c7db218d3565309599e378"},
- {file = "tiktoken-0.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:bf10dff18a1b6b009aa89f0850a8024b393696045a65e82d10e55d7ae5c8b5bd"},
- {file = "tiktoken-0.5.0-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:f16e1bb26a36841bb58b857ed3bbe1e6be845829e5dbb28cea80e2e622ebd753"},
- {file = "tiktoken-0.5.0-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:5939137cc4d2b3628a00254ad22b1d520a945a100d8bf760bc0e2963ebd6d173"},
- {file = "tiktoken-0.5.0-cp310-cp310-win_amd64.whl", hash = "sha256:e31972f9594a34e2546b69b87e921e035b2f52b12559c9cd5231f796c6473ffc"},
- {file = "tiktoken-0.5.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:8e2ac182d9c975c7c5068382bb17f56e468c35007ea075e37f4bebe85c95f9fc"},
- {file = "tiktoken-0.5.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:89e2fdba5ab5a13a2f174e76897d6135caf31b0aa9fa97c8df63eaa8acfea46a"},
- {file = "tiktoken-0.5.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:11ce4800f5708cf43997fbcec9cd69939462b8c5856715aae6ddb244a5d71eed"},
- {file = "tiktoken-0.5.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:74280005ad0c6aceb53b12aad84d9b3753b72da6603d57bbc35212631e6a3bb1"},
- {file = "tiktoken-0.5.0-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:2fbb444c0183701a2f012a720b040b6116b0e061c7ea70a3a8828b850fb2c71a"},
- {file = "tiktoken-0.5.0-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:f823a3034abc838e53a42271c6a15369ccbf467abbba517ee3595d6741dab107"},
- {file = "tiktoken-0.5.0-cp311-cp311-win_amd64.whl", hash = "sha256:caaf5db8705d7d39286361a2ef71315ff1673b8787fe64f457c63770ceda1f6b"},
- {file = "tiktoken-0.5.0-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:89bc1d136ab6abfcc0aab0ab52b5641ca45c23bd63f65da957c2d13122e5ae3c"},
- {file = "tiktoken-0.5.0-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:3aab8676862ccc923867864f5339fdb1a5322fc21f9f06bbba0ae7a845d7fe5a"},
- {file = "tiktoken-0.5.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2c5a3a1aa7bc2490c2e64f7a1d14159e03b1c252ed253e0ca712c1952d51ca2f"},
- {file = "tiktoken-0.5.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:bd88e25af7463d5b15812262f66d6f3096a6e4b60e0c0712b917a51175f57f0d"},
- {file = "tiktoken-0.5.0-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:f84d9bc41da27961516b8e56254ad886e16ac64a3f5b5fb7c4335678e23579b0"},
- {file = "tiktoken-0.5.0-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:2f1c85626ed1bf68104715893dad9dce8153cab44cfeeeab5c56bb5ec92b813c"},
- {file = "tiktoken-0.5.0-cp38-cp38-win_amd64.whl", hash = "sha256:0f994aefe5f2c69dc6ee822c5323c5bef2cb4c84533af813c4b867419701cc2d"},
- {file = "tiktoken-0.5.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:9635a6a5aadeb116c950b285d97987199cada3e28531a84f32b38941dead759f"},
- {file = "tiktoken-0.5.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:dda8f19ce057d7aa5f8015ffea3beacc20661a42afbc81f7a599534716214a8e"},
- {file = "tiktoken-0.5.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:dbb94200722590d2cae477967f0470f17846e5cada4fc1e4754ec8701cdd6494"},
- {file = "tiktoken-0.5.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:bbf1af57d68340447ca1827a93ae0a788f97787b3a17851709156636df47d815"},
- {file = "tiktoken-0.5.0-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:8d45410f89b7efb26b53eb8993f1122e0442abe0bb913ccb0f5d28be7e36e2a9"},
- {file = "tiktoken-0.5.0-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:2a9324122f8e33475b575f7bfff035c15a789dc50edeca907ecaa9c8d72cc168"},
- {file = "tiktoken-0.5.0-cp39-cp39-win_amd64.whl", hash = "sha256:02c35c78849ead31b69fd42b021031554fd1f3813fd1bf064185f6404768eea6"},
- {file = "tiktoken-0.5.0.tar.gz", hash = "sha256:c8dfd3280f5fca0d8ed2ec18c0f11f7cba305af48faaf4b914c71b7d221f39ed"},
+ {file = "tiktoken-0.5.1-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:2b0bae3fd56de1c0a5874fb6577667a3c75bf231a6cef599338820210c16e40a"},
+ {file = "tiktoken-0.5.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:e529578d017045e2f0ed12d2e00e7e99f780f477234da4aae799ec4afca89f37"},
+ {file = "tiktoken-0.5.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:edd2ffbb789712d83fee19ab009949f998a35c51ad9f9beb39109357416344ff"},
+ {file = "tiktoken-0.5.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:e4c73d47bdc1a3f1f66ffa019af0386c48effdc6e8797e5e76875f6388ff72e9"},
+ {file = "tiktoken-0.5.1-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:46b8554b9f351561b1989157c6bb54462056f3d44e43aa4e671367c5d62535fc"},
+ {file = "tiktoken-0.5.1-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:92ed3bbf71a175a6a4e5fbfcdb2c422bdd72d9b20407e00f435cf22a68b4ea9b"},
+ {file = "tiktoken-0.5.1-cp310-cp310-win_amd64.whl", hash = "sha256:714efb2f4a082635d9f5afe0bf7e62989b72b65ac52f004eb7ac939f506c03a4"},
+ {file = "tiktoken-0.5.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:a10488d1d1a5f9c9d2b2052fdb4cf807bba545818cb1ef724a7f5d44d9f7c3d4"},
+ {file = "tiktoken-0.5.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:8079ac065572fe0e7c696dbd63e1fdc12ce4cdca9933935d038689d4732451df"},
+ {file = "tiktoken-0.5.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:7ef730db4097f5b13df8d960f7fdda2744fe21d203ea2bb80c120bb58661b155"},
+ {file = "tiktoken-0.5.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:426e7def5f3f23645dada816be119fa61e587dfb4755de250e136b47a045c365"},
+ {file = "tiktoken-0.5.1-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:323cec0031358bc09aa965c2c5c1f9f59baf76e5b17e62dcc06d1bb9bc3a3c7c"},
+ {file = "tiktoken-0.5.1-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:5abd9436f02e2c8eda5cce2ff8015ce91f33e782a7423de2a1859f772928f714"},
+ {file = "tiktoken-0.5.1-cp311-cp311-win_amd64.whl", hash = "sha256:1fe99953b63aabc0c9536fbc91c3c9000d78e4755edc28cc2e10825372046a2d"},
+ {file = "tiktoken-0.5.1-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:dcdc630461927718b317e6f8be7707bd0fc768cee1fdc78ddaa1e93f4dc6b2b1"},
+ {file = "tiktoken-0.5.1-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:1f2b3b253e22322b7f53a111e1f6d7ecfa199b4f08f3efdeb0480f4033b5cdc6"},
+ {file = "tiktoken-0.5.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:43ce0199f315776dec3ea7bf86f35df86d24b6fcde1babd3e53c38f17352442f"},
+ {file = "tiktoken-0.5.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a84657c083d458593c0235926b5c993eec0b586a2508d6a2020556e5347c2f0d"},
+ {file = "tiktoken-0.5.1-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:c008375c0f3d97c36e81725308699116cd5804fdac0f9b7afc732056329d2790"},
+ {file = "tiktoken-0.5.1-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:779c4dea5edd1d3178734d144d32231e0b814976bec1ec09636d1003ffe4725f"},
+ {file = "tiktoken-0.5.1-cp38-cp38-win_amd64.whl", hash = "sha256:b5dcfcf9bfb798e86fbce76d40a1d5d9e3f92131aecfa3d1e5c9ea1a20f1ef1a"},
+ {file = "tiktoken-0.5.1-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:9b180a22db0bbcc447f691ffc3cf7a580e9e0587d87379e35e58b826ebf5bc7b"},
+ {file = "tiktoken-0.5.1-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:2b756a65d98b7cf760617a6b68762a23ab8b6ef79922be5afdb00f5e8a9f4e76"},
+ {file = "tiktoken-0.5.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ba9873c253ca1f670e662192a0afcb72b41e0ba3e730f16c665099e12f4dac2d"},
+ {file = "tiktoken-0.5.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:74c90d2be0b4c1a2b3f7dde95cd976757817d4df080d6af0ee8d461568c2e2ad"},
+ {file = "tiktoken-0.5.1-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:709a5220891f2b56caad8327fab86281787704931ed484d9548f65598dea9ce4"},
+ {file = "tiktoken-0.5.1-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:5d5a187ff9c786fae6aadd49f47f019ff19e99071dc5b0fe91bfecc94d37c686"},
+ {file = "tiktoken-0.5.1-cp39-cp39-win_amd64.whl", hash = "sha256:e21840043dbe2e280e99ad41951c00eff8ee3b63daf57cd4c1508a3fd8583ea2"},
+ {file = "tiktoken-0.5.1.tar.gz", hash = "sha256:27e773564232004f4f810fd1f85236673ec3a56ed7f1206fc9ed8670ebedb97a"},
]
[package.dependencies]
diff --git a/pyproject.toml b/pyproject.toml
index 18859e378..03d17724b 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -35,9 +35,9 @@ types-colorama = "^0"
types-requests = "^2"
bs4 = "^0"
protobuf = "^4"
-docx2txt = "^0.8"
-PyMuPDF = "^1.22.5"
-pathlib = "^1.0.1"
+docx2txt = "^0.8"
+PyMuPDF = "^1.22.5"
+pathlib = "^1.0.1"
transformers = { version = "^4", optional = true }
diffusers = { version = "^0", optional = true }
@@ -141,6 +141,6 @@ module = [
"bs4.*",
"docx2txt",
"PyMuPDF",
- "fitz"
+ "fitz",
]
ignore_missing_imports = true
diff --git a/test/agents/test_babyagi_playing.py b/test/agents/test_babyagi_playing.py
index 0285de5c5..8472896d3 100644
--- a/test/agents/test_babyagi_playing.py
+++ b/test/agents/test_babyagi_playing.py
@@ -82,7 +82,3 @@ def test_babyagi_playing_step(model: ModelType):
assert len(babyagi_playing.subtasks) > 0
assert len(babyagi_playing.solved_subtasks) == 1
-
- assert len(babyagi_playing.assistant_agent.stored_messages) > 0
- assert len(babyagi_playing.task_creation_agent.stored_messages) > 0
- assert len(babyagi_playing.task_prioritization_agent.stored_messages) > 0
diff --git a/test/agents/test_chat_agent.py b/test/agents/test_chat_agent.py
index d991f5642..eb2edfc50 100644
--- a/test/agents/test_chat_agent.py
+++ b/test/agents/test_chat_agent.py
@@ -16,13 +16,14 @@ from typing import List
import pytest
from camel.agents import ChatAgent
-from camel.agents.chat_agent import ChatRecord, FunctionCallingRecord
+from camel.agents.chat_agent import FunctionCallingRecord
from camel.configs import ChatGPTConfig, FunctionCallingConfig
from camel.functions import MATH_FUNCS
from camel.generators import SystemMessageGenerator
+from camel.memories import MemoryRecord
from camel.messages import BaseMessage
from camel.terminators import ResponseWordsTerminator
-from camel.typing import ModelType, RoleType, TaskType
+from camel.typing import ModelType, OpenAIBackendRole, RoleType, TaskType
parametrize = pytest.mark.parametrize('model', [
ModelType.STUB,
@@ -57,47 +58,44 @@ def test_chat_agent(model: ModelType):
assert assistant_response.info['id'] is not None
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
+ expected_context = [system_msg.to_openai_system_message()]
+ context, _ = assistant.memory.get_context()
+ assert context == expected_context
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)
+ assistant.update_memory(user_msg, OpenAIBackendRole.USER)
+ expected_context = [
+ system_msg.to_openai_system_message(),
+ user_msg.to_openai_user_message()
]
- 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)
+ context, _ = assistant.memory.get_context()
+ assert context == expected_context
@pytest.mark.model_backend
-def test_chat_agent_preprocess_messages_window():
+def test_chat_agent_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,
+ message_window_size=2,
)
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)
+ user_record = MemoryRecord(user_msg, OpenAIBackendRole.USER)
- openai_messages, _ = assistant.preprocess_messages([system_msg, user_msg])
+ assistant.memory.write_records([user_record] * 5)
+ openai_messages, _ = assistant.memory.get_context()
assert len(openai_messages) == 2
@@ -106,27 +104,15 @@ 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
- assistant.token_limit_terminator.token_limit = 1
+ assistant = ChatAgent(system_message=system_msg,
+ model=ModelType.GPT_3_5_TURBO, 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 = assistant.model_backend.count_tokens_from_messages(
- expect_openai_messages)
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
diff --git a/test/agents/test_role_playing.py b/test/agents/test_role_playing.py
index d2406026c..c9afc12f1 100644
--- a/test/agents/test_role_playing.py
+++ b/test/agents/test_role_playing.py
@@ -167,10 +167,10 @@ def test_role_playing_role_sequence(model_type=None):
input_assistant_msg = assistant_response.msg
assistant_response, user_response = role_playing.step(input_assistant_msg)
- for record in role_playing.user_agent.stored_messages:
- user_role_sequence.append(record.role_at_backend)
- for record in role_playing.assistant_agent.stored_messages:
- assistant_role_sequence.append(record.role_at_backend)
+ for record in role_playing.user_agent.memory.get_context()[0]:
+ user_role_sequence.append(record["role"])
+ for record in role_playing.assistant_agent.memory.get_context()[0]:
+ assistant_role_sequence.append(record["role"])
assert user_role_sequence == \
['system', 'user', 'assistant', 'user', 'assistant']
diff --git a/test/functions/test_search_functions.py b/test/functions/test_search_functions.py
index c086aabb4..a4eb3f8e9 100644
--- a/test/functions/test_search_functions.py
+++ b/test/functions/test_search_functions.py
@@ -41,9 +41,9 @@ def test_search_wiki_not_found():
def test_search_wiki_with_ambiguity():
- expected_output = wikipedia.summary("New York City", sentences=5,
+ expected_output = wikipedia.summary("Google", sentences=5,
auto_suggest=False)
- assert search_wiki("New York") == expected_output
+ assert search_wiki("Google LLC") == expected_output
def test_google_api():
diff --git a/test/memories/context_creators/test_score_based.py b/test/memories/context_creators/test_score_based.py
new file mode 100644
index 000000000..dae3708dc
--- /dev/null
+++ b/test/memories/context_creators/test_score_based.py
@@ -0,0 +1,72 @@
+# =========== 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.memories import (
+ ContextRecord,
+ MemoryRecord,
+ ScoreBasedContextCreator,
+)
+from camel.messages import BaseMessage
+from camel.typing import ModelType, OpenAIBackendRole, RoleType
+from camel.utils import OpenAITokenCounter
+
+
+def test_score_based_context_creator():
+ context_creator = ScoreBasedContextCreator(
+ OpenAITokenCounter(ModelType.GPT_4), 21)
+ context_records = [
+ ContextRecord(
+ memory_record=MemoryRecord(
+ message=BaseMessage(
+ "test",
+ RoleType.ASSISTANT,
+ meta_dict=None,
+ content="Hello world!", # 10
+ ),
+ role_at_backend=OpenAIBackendRole.ASSISTANT,
+ ),
+ score=0.9,
+ ),
+ ContextRecord(
+ memory_record=MemoryRecord(
+ message=BaseMessage(
+ "test",
+ RoleType.ASSISTANT,
+ meta_dict=None,
+ content="Nice to meet you.", # 12
+ ),
+ role_at_backend=OpenAIBackendRole.ASSISTANT,
+ ),
+ score=0.3,
+ ),
+ ContextRecord(
+ memory_record=MemoryRecord(
+ message=BaseMessage(
+ "test",
+ RoleType.ASSISTANT,
+ meta_dict=None,
+ content="How are you?", # 11
+ ),
+ role_at_backend=OpenAIBackendRole.ASSISTANT,
+ ),
+ score=0.7,
+ ),
+ ]
+
+ expected_output = [
+ r.memory_record.to_openai_message()
+ for r in [context_records[0], context_records[2]]
+ ]
+ output, _ = context_creator.create_context(records=context_records)
+ assert expected_output == output
diff --git a/test/memories/test_chat_history_memory.py b/test/memories/test_chat_history_memory.py
new file mode 100644
index 000000000..6ffed2ed4
--- /dev/null
+++ b/test/memories/test_chat_history_memory.py
@@ -0,0 +1,73 @@
+# =========== 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 tempfile
+from pathlib import Path
+
+import pytest
+
+from camel.memories import ChatHistoryMemory, MemoryRecord
+from camel.memories.context_creators import ScoreBasedContextCreator
+from camel.messages import BaseMessage
+from camel.storages.key_value_storages import (
+ InMemoryKeyValueStorage,
+ JsonStorage,
+)
+from camel.typing import ModelType, OpenAIBackendRole, RoleType
+from camel.utils.token_counting import OpenAITokenCounter
+
+
+def memory(request):
+ context_creator = ScoreBasedContextCreator(
+ OpenAITokenCounter(ModelType.GPT_4), ModelType.GPT_4.token_limit)
+ if request.param == "in-memory":
+ yield ChatHistoryMemory(context_creator=context_creator,
+ storage=InMemoryKeyValueStorage())
+ elif request.param == "json":
+ _, path = tempfile.mkstemp()
+ path = Path(path)
+ yield ChatHistoryMemory(context_creator=context_creator,
+ storage=JsonStorage(path))
+ path.unlink()
+
+
[email protected]("memory", ["in-memory", "json"], indirect=True)
+def test_chat_history_memory(memory: ChatHistoryMemory):
+ system_msg = BaseMessage(
+ "system",
+ role_type=RoleType.DEFAULT,
+ meta_dict=None,
+ content="You are a helpful assistant",
+ )
+ user_msg = BaseMessage(
+ "AI user",
+ role_type=RoleType.USER,
+ meta_dict=None,
+ content="Do a task",
+ )
+ assistant_msg = BaseMessage(
+ "AI assistant",
+ role_type=RoleType.ASSISTANT,
+ meta_dict=None,
+ content="OK",
+ )
+ system_record = MemoryRecord(system_msg, OpenAIBackendRole.SYSTEM)
+ user_record = MemoryRecord(user_msg, OpenAIBackendRole.USER)
+ assistant_record = MemoryRecord(assistant_msg, OpenAIBackendRole.ASSISTANT)
+ memory.write_records([system_record, user_record, assistant_record])
+ output_messages, _ = memory.get_context()
+ assert output_messages[0] == system_msg.to_openai_system_message()
+ assert output_messages[1] == user_msg.to_openai_user_message()
+ assert output_messages[2] == assistant_msg.to_openai_assistant_message()
diff --git a/test/storages/key_value_storages/test_key_value_storages.py b/test/storages/key_value_storages/test_key_value_storages.py
new file mode 100644
index 000000000..80d427112
--- /dev/null
+++ b/test/storages/key_value_storages/test_key_value_storages.py
@@ -0,0 +1,66 @@
+# =========== 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 tempfile
+from pathlib import Path
+
+import pytest
+
+from camel.storages.key_value_storages import (
+ BaseKeyValueStorage,
+ InMemoryKeyValueStorage,
+ JsonStorage,
+)
+from camel.typing import RoleType
+
+
+def storage(request):
+ if request.param == "in-memory":
+ yield InMemoryKeyValueStorage()
+ elif request.param == "json":
+ _, path = tempfile.mkstemp()
+ path = Path(path)
+ yield JsonStorage(path)
+ path.unlink()
+
+
[email protected]("storage", ["in-memory", "json"], indirect=True)
+def test_key_value_storage(storage: BaseKeyValueStorage):
+ msg1 = {
+ "key1": "value1",
+ "role": RoleType.USER,
+ "message": "Do a task",
+ "additional_dict": {
+ "1": 1,
+ "2": 2
+ },
+ }
+ msg2 = {
+ "key1": "value2",
+ "role": RoleType.ASSISTANT,
+ "assistant_msg": "Ok",
+ }
+ storage.save([msg1, msg2])
+ load_msg = storage.load()
+ assert load_msg[0] == msg1
+ assert load_msg[1] == msg2
+
+ msg1_copy = msg1.copy()
+ msg1.clear()
+ assert load_msg[0] == msg1_copy
+
+ storage.clear()
+ load_msg = storage.load()
+ assert load_msg == []