diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index a82b4c82a96..388e67b1c60 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -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. @@ -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". @@ -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: diff --git a/autogen/agentchat/groupchat.py b/autogen/agentchat/groupchat.py index f615a1a5300..01c6ad709ad 100644 --- a/autogen/agentchat/groupchat.py +++ b/autogen/agentchat/groupchat.py @@ -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) diff --git a/autogen/version.py b/autogen/version.py index 5635676f6b4..b5c9b6cb711 100644 --- a/autogen/version.py +++ b/autogen/version.py @@ -1 +1 @@ -__version__ = "0.2.11" +__version__ = "0.2.12" diff --git a/test/agentchat/test_chats.py b/test/agentchat/test_chats.py index 36de8d120e3..9b578c00c70 100644 --- a/test/agentchat/test_chats.py +++ b/test/agentchat/test_chats.py @@ -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( @@ -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()