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: Rebuild record memory (#788)

Ray4c3b023b9613
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index 3ad16a596..87755282d 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -14,6 +14,7 @@
from __future__ import annotations
import json
+import logging
from collections import defaultdict
from typing import (
TYPE_CHECKING,
@@ -61,6 +62,9 @@ if TYPE_CHECKING:
from camel.terminators import ResponseTerminator
from camel.toolkits import OpenAIFunction
+
+logger = logging.getLogger(__name__)
+
# AgentOps decorator setting
try:
import os
@@ -437,10 +441,22 @@ class ChatAgent(BaseAgent):
for base_message_item in output_messages:
base_message_item.content = str(info['tool_calls'][-1].result)
- return ChatAgentResponse(
+ chat_agent_response = ChatAgentResponse(
msgs=output_messages, terminated=self.terminated, info=info
)
+ # If the output result is single message, it will be
+ # automatically added to the memory.
+ if len(chat_agent_response.msgs) == 1:
+ self.record_message(chat_agent_response.msg)
+ else:
+ logger.warning(
+ "Multiple messages are available in `ChatAgentResponse`. "
+ "Please manually run the `record_message` function to "
+ "record the selected message."
+ )
+ return chat_agent_response
+
async def step_async(
self,
input_message: BaseMessage,
@@ -563,10 +579,23 @@ class ChatAgent(BaseAgent):
for base_message_item in output_messages:
base_message_item.content = str(info['tool_calls'][0].result)
- return ChatAgentResponse(
+ chat_agent_response = ChatAgentResponse(
msgs=output_messages, terminated=self.terminated, info=info
)
+ # If the output result is single message, it will be
+ # automatically added to the memory.
+ if len(chat_agent_response.msgs) == 1:
+ self.record_message(chat_agent_response.msg)
+ else:
+ logger.warning(
+ "Multiple messages are presented in `chat_agent_response`. "
+ "Please manually call the `record_message` function to "
+ "record the chosen message."
+ )
+
+ return chat_agent_response
+
def _add_tools_for_func_call(
self,
response: ChatCompletion,
diff --git a/camel/agents/critic_agent.py b/camel/agents/critic_agent.py
index 22e80ecf9..10ada108a 100644
--- a/camel/agents/critic_agent.py
+++ b/camel/agents/critic_agent.py
@@ -123,7 +123,6 @@ class CriticAgent(ChatAgent):
raise RuntimeError("Critic step failed.")
critic_msg = critic_response.msg
- self.record_message(critic_msg)
if self.verbose:
print_text_animated(
self.logger_color + "\n> Critic response: "
diff --git a/camel/societies/babyagi_playing.py b/camel/societies/babyagi_playing.py
index 2455754cb..45c5c4f72 100644
--- a/camel/societies/babyagi_playing.py
+++ b/camel/societies/babyagi_playing.py
@@ -243,9 +243,6 @@ class BabyAGI:
assistant_response = self.assistant_agent.step(assistant_msg_msg)
assistant_msg = assistant_response.msgs[0]
- 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 f546cb4f4..20f72a123 100644
--- a/camel/societies/role_playing.py
+++ b/camel/societies/role_playing.py
@@ -486,7 +486,15 @@ class RolePlaying:
),
)
user_msg = self._reduce_message_options(user_response.msgs)
- self.user_agent.record_message(user_msg)
+
+ # To prevent recording the same memory more than once (once in chat
+ # step and once in role play), and the model generates only one
+ # response when multi-response support is enabled.
+ if (
+ 'n' in self.user_agent.model_config_dict.keys()
+ and self.user_agent.model_config_dict['n'] > 1
+ ):
+ 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:
@@ -501,7 +509,15 @@ class RolePlaying:
),
)
assistant_msg = self._reduce_message_options(assistant_response.msgs)
- self.assistant_agent.record_message(assistant_msg)
+
+ # To prevent recording the same memory more than once (once in chat
+ # step and once in role play), and the model generates only one
+ # response when multi-response support is enabled.
+ if (
+ 'n' in self.assistant_agent.model_config_dict.keys()
+ and self.assistant_agent.model_config_dict['n'] > 1
+ ):
+ self.assistant_agent.record_message(assistant_msg)
return (
ChatAgentResponse(
diff --git a/examples/code/role_playing_multiprocess.py b/examples/code/role_playing_multiprocess.py
index 68535ae0c..7427dbe26 100644
--- a/examples/code/role_playing_multiprocess.py
+++ b/examples/code/role_playing_multiprocess.py
@@ -147,7 +147,6 @@ def generate_data(
)
break
- user_agent.record_message(user_response.msg)
print(f"User:\n{user_response.msg.content}\n")
assistant_response = assistant_agent.step(user_response.msg)
@@ -160,7 +159,6 @@ def generate_data(
)
break
- 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