Skip to content

Commit

Permalink
avoid access private variable (#1592)
Browse files Browse the repository at this point in the history
* avoid access private variable

* test chat messages for summary
  • Loading branch information
sonichi authored Feb 8, 2024
1 parent 68337b9 commit 0b4bce2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
8 changes: 6 additions & 2 deletions autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ def chat_messages(self) -> Dict[Agent, List[Dict]]:
"""A dictionary of conversations from agent to list of messages."""
return self._oai_messages

def chat_messages_for_summary(self, agent: Agent) -> List[Dict]:
"""A list of messages as a conversation to summarize."""
return self._oai_messages[agent]

def last_message(self, agent: Optional[Agent] = None) -> Optional[Dict]:
"""The last message exchanged with the agent.
Expand Down Expand Up @@ -449,7 +453,7 @@ def send(
ValueError: if the message can't be converted into a valid ChatCompletion message.
Returns:
ChatResult: an ChatResult object.
ChatResult: a ChatResult object.
"""
# When the agent composes and sends the message, the role of the message is "assistant"
# unless it's "function".
Expand Down Expand Up @@ -826,7 +830,7 @@ def _summarize_chat(
prompt = ConversableAgent.DEFAULT_summary_prompt if prompt is None else prompt
if not isinstance(prompt, str):
raise ValueError("The summary_prompt must be a string.")
msg_list = agent._groupchat.messages if hasattr(agent, "_groupchat") else agent.chat_messages[self]
msg_list = agent.chat_messages_for_summary(self)
try:
summary = self._reflection_with_llm(prompt, msg_list, llm_agent=agent, cache=cache)
except BadRequestError as e:
Expand Down
6 changes: 6 additions & 0 deletions autogen/agentchat/groupchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,12 @@ def __init__(
ignore_async_in_sync_chat=True,
)

def chat_messages_for_summary(self, agent: Agent) -> List[Dict]:
"""The list of messages in the group chat as a conversation to summarize.
The agent is ignored.
"""
return self._groupchat.messages

def _prepare_chat(self, recipient: ConversableAgent, clear_history: bool, prepare_recipient: bool = True) -> None:
super()._prepare_chat(recipient, clear_history, prepare_recipient)

Expand Down
2 changes: 1 addition & 1 deletion autogen/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.2.11"
__version__ = "0.2.12"
24 changes: 21 additions & 3 deletions test/agentchat/test_chats.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,28 @@
from conftest import skip_openai
import autogen
from typing import Literal

from pydantic import BaseModel, Field
from typing_extensions import Annotated


def test_chat_messages_for_summary():
assistant = UserProxyAgent(name="assistant", human_input_mode="NEVER")
user = UserProxyAgent(name="user", human_input_mode="NEVER")
user.send("What is the capital of France?", assistant)
messages = assistant.chat_messages_for_summary(user)
assert len(messages) == 1

groupchat = GroupChat(agents=[user, assistant], messages=[], max_round=2)
manager = GroupChatManager(groupchat=groupchat, name="manager", llm_config=False)
user.initiate_chat(manager, message="What is the capital of France?")
messages = manager.chat_messages_for_summary(user)
assert len(messages) == 2

messages = user.chat_messages_for_summary(manager)
assert len(messages) == 2
messages = assistant.chat_messages_for_summary(manager)
assert len(messages) == 2


@pytest.mark.skipif(skip_openai, reason="requested to skip openai tests")
def test_chats_group():
config_list = autogen.config_list_from_json(
Expand Down Expand Up @@ -266,4 +283,5 @@ def currency_calculator(
if __name__ == "__main__":
# test_chats()
# test_chats_group()
test_chats_w_func()
# test_chats_w_func()
test_chat_messages_for_summary()

0 comments on commit 0b4bce2

Please sign in to comment.