Skip to content

Commit 1eeda03

Browse files
Moved all set_user_role_at_backend into Agent step (camel-ai#170)
1 parent 6a27e23 commit 1eeda03

File tree

5 files changed

+11
-11
lines changed

5 files changed

+11
-11
lines changed

camel/agents/chat_agent.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,18 @@ def step(
161161
162162
Args:
163163
input_message (ChatMessage): The input message to the agent.
164+
Its `role` field that specifies the role at backen may be either
165+
`user` or `assistant` but it will be set to `user` anyway since
166+
for the self agent any incoming message is external.
164167
165168
Returns:
166169
ChatAgentResponse: A struct
167170
containing the output messages, a boolean indicating whether
168171
the chat session has terminated, and information about the chat
169172
session.
170173
"""
171-
messages = self.update_messages(input_message)
174+
msg_user_at_backend = input_message.set_user_role_at_backend()
175+
messages = self.update_messages(msg_user_at_backend)
172176
if self.message_window_size is not None and len(
173177
messages) > self.message_window_size:
174178
messages = [self.system_message

camel/agents/critic_agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def step(self, messages: Sequence[ChatMessage]) -> ChatMessage:
168168
input_msg = copy.deepcopy(meta_chat_message)
169169
input_msg.content = flatten_options
170170

171-
option = self.get_option(input_msg.set_user_role_at_backend())
171+
option = self.get_option(input_msg)
172172
output_msg = copy.deepcopy(meta_chat_message)
173173
output_msg.content = option
174174

camel/agents/role_playing.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,15 @@ def step(
266266
whether or not the user agent terminated the conversation, and
267267
any additional user information.
268268
"""
269-
assistant_msg_rst = assistant_msg.set_user_role_at_backend()
270-
user_response = self.user_agent.step(assistant_msg_rst)
269+
user_response = self.user_agent.step(assistant_msg)
271270
if user_response.terminated or user_response.msgs is None:
272271
return (ChatAgentResponse([], False, {}),
273272
ChatAgentResponse([], user_response.terminated,
274273
user_response.info))
275274
user_msg = self.process_messages(user_response.msgs)
276275
self.user_agent.update_messages(user_msg)
277276

278-
user_msg_rst = user_msg.set_user_role_at_backend()
279-
assistant_response = self.assistant_agent.step(user_msg_rst)
277+
assistant_response = self.assistant_agent.step(user_msg)
280278
if assistant_response.terminated or assistant_response.msgs is None:
281279
return (ChatAgentResponse([], assistant_response.terminated,
282280
assistant_response.info),

camel/messages/chat_messages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ChatMessage(BaseMessage):
3737
role: str
3838
content: str = ""
3939

40-
def set_user_role_at_backend(self: BaseMessage):
40+
def set_user_role_at_backend(self) -> "ChatMessage":
4141
return self.__class__(
4242
role_name=self.role_name,
4343
role_type=self.role_type,

examples/code/role_playing_multiprocess.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ def generate_data(language_idx: int, language_name: str, domain_idx: int,
125125

126126
while message_counter < max_num_messages:
127127

128-
user_response = user_agent.step(
129-
input_assistant_msg.set_user_role_at_backend())
128+
user_response = user_agent.step(input_assistant_msg)
130129

131130
# Condition 1: User terminates the chat
132131
if user_response.terminated:
@@ -138,8 +137,7 @@ def generate_data(language_idx: int, language_name: str, domain_idx: int,
138137
user_agent.update_messages(user_response.msg)
139138
print(f"User:\n{user_response.msg.content}\n")
140139

141-
assistant_response = assistant_agent.step(
142-
user_response.msg.set_user_role_at_backend())
140+
assistant_response = assistant_agent.step(user_response.msg)
143141

144142
# Condition 2: Assistant terminates the chat
145143
if assistant_response.terminated:

0 commit comments

Comments
 (0)