Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure 'name' on initial message #2635

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ def _assert_valid_name(name):
raise ValueError(f"Invalid name: {name}. Name must be less than 64 characters.")
return name

def _append_oai_message(self, message: Union[Dict, str], role, conversation_id: Agent) -> bool:
def _append_oai_message(self, message: Union[Dict, str], role, conversation_id: Agent, is_sending: bool) -> bool:
"""Append a message to the ChatCompletion conversation.

If the message received is a string, it will be put in the "content" field of the new dictionary.
Expand All @@ -567,6 +567,7 @@ def _append_oai_message(self, message: Union[Dict, str], role, conversation_id:
message (dict or str): message to be appended to the ChatCompletion conversation.
role (str): role of the message, can be "assistant" or "function".
conversation_id (Agent): id of the conversation, should be the recipient or sender.
is_sending (bool): If the agent (aka self) is sending to the conversation_id agent, otherwise receiving.

Returns:
bool: whether the message is appended to the ChatCompletion conversation.
Expand Down Expand Up @@ -596,7 +597,15 @@ def _append_oai_message(self, message: Union[Dict, str], role, conversation_id:

if oai_message.get("function_call", False) or oai_message.get("tool_calls", False):
oai_message["role"] = "assistant" # only messages with role 'assistant' can have a function call.
elif "name" not in oai_message:
# If we don't have a name field, append it
if is_sending:
oai_message["name"] = self.name
else:
oai_message["name"] = conversation_id.name

self._oai_messages[conversation_id].append(oai_message)

return True

def _process_message_before_send(
Expand Down Expand Up @@ -650,7 +659,7 @@ def send(
message = self._process_message_before_send(message, recipient, silent)
# When the agent composes and sends the message, the role of the message is "assistant"
# unless it's "function".
valid = self._append_oai_message(message, "assistant", recipient)
valid = self._append_oai_message(message, "assistant", recipient, is_sending=True)
if valid:
recipient.receive(message, self, request_reply, silent)
else:
Expand Down Expand Up @@ -700,7 +709,7 @@ async def a_send(
message = self._process_message_before_send(message, recipient, silent)
# When the agent composes and sends the message, the role of the message is "assistant"
# unless it's "function".
valid = self._append_oai_message(message, "assistant", recipient)
valid = self._append_oai_message(message, "assistant", recipient, is_sending=True)
if valid:
await recipient.a_receive(message, self, request_reply, silent)
else:
Expand Down Expand Up @@ -771,7 +780,7 @@ def _print_received_message(self, message: Union[Dict, str], sender: Agent):

def _process_received_message(self, message: Union[Dict, str], sender: Agent, silent: bool):
# When the agent receives a message, the role of the message is "user". (If 'role' exists and is 'function', it will remain unchanged.)
valid = self._append_oai_message(message, "user", sender)
valid = self._append_oai_message(message, "user", sender, is_sending=False)
if logging_enabled():
log_event(self, "received_message", message=message, sender=sender.name, valid=valid)

Expand Down
15 changes: 8 additions & 7 deletions test/agentchat/test_groupchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ def test_clear_agents_history():
agent1_history = list(agent1._oai_messages.values())[0]
agent2_history = list(agent2._oai_messages.values())[0]
assert agent1_history == [
{"content": "hello", "role": "assistant"},
{"content": "hello", "role": "assistant", "name": "alice"},
{"content": "This is bob speaking.", "name": "bob", "role": "user"},
{"content": "How you doing?", "name": "sam", "role": "user"},
]
Expand All @@ -742,7 +742,7 @@ def test_clear_agents_history():
{"content": "How you doing?", "name": "sam", "role": "user"},
]
assert agent2_history == [
{"content": "This is bob speaking.", "role": "assistant"},
{"content": "This is bob speaking.", "role": "assistant", "name": "bob"},
{"content": "How you doing?", "name": "sam", "role": "user"},
]
assert groupchat.messages == [
Expand All @@ -756,12 +756,12 @@ def test_clear_agents_history():
agent1_history = list(agent1._oai_messages.values())[0]
agent2_history = list(agent2._oai_messages.values())[0]
assert agent1_history == [
{"content": "hello", "role": "assistant"},
{"content": "hello", "role": "assistant", "name": "alice"},
{"content": "This is bob speaking.", "name": "bob", "role": "user"},
{"content": "How you doing?", "name": "sam", "role": "user"},
]
assert agent2_history == [
{"content": "This is bob speaking.", "role": "assistant"},
{"content": "This is bob speaking.", "role": "assistant", "name": "bob"},
{"content": "How you doing?", "name": "sam", "role": "user"},
]
assert groupchat.messages == [
Expand Down Expand Up @@ -819,6 +819,7 @@ def test_clear_agents_history():
"content": "example tool response",
"tool_responses": [{"tool_call_id": "call_emulated", "role": "tool", "content": "example tool response"}],
"role": "tool",
"name": "alice",
},
]

Expand Down Expand Up @@ -1215,7 +1216,7 @@ def test_role_for_select_speaker_messages():
# into a message attribute called 'override_role'. This is evaluated in Conversable Agent's _append_oai_message function
# e.g.: message={'content':self.select_speaker_prompt(agents),'override_role':self.role_for_select_speaker_messages},
message = {"content": "A prompt goes here.", "override_role": groupchat.role_for_select_speaker_messages}
checking_agent._append_oai_message(message, "assistant", speaker_selection_agent)
checking_agent._append_oai_message(message, "assistant", speaker_selection_agent, is_sending=True)

# Test default is "system"
assert len(checking_agent.chat_messages) == 1
Expand All @@ -1224,15 +1225,15 @@ def test_role_for_select_speaker_messages():
# Test as "user"
groupchat.role_for_select_speaker_messages = "user"
message = {"content": "A prompt goes here.", "override_role": groupchat.role_for_select_speaker_messages}
checking_agent._append_oai_message(message, "assistant", speaker_selection_agent)
checking_agent._append_oai_message(message, "assistant", speaker_selection_agent, is_sending=True)

assert len(checking_agent.chat_messages) == 1
assert checking_agent.chat_messages[speaker_selection_agent][-1]["role"] == "user"

# Test as something unusual
groupchat.role_for_select_speaker_messages = "SockS"
message = {"content": "A prompt goes here.", "override_role": groupchat.role_for_select_speaker_messages}
checking_agent._append_oai_message(message, "assistant", speaker_selection_agent)
checking_agent._append_oai_message(message, "assistant", speaker_selection_agent, is_sending=True)

assert len(checking_agent.chat_messages) == 1
assert checking_agent.chat_messages[speaker_selection_agent][-1]["role"] == "SockS"
Expand Down