A programming framework for agentic AI
avoid access private variable (#1592)
Chi Wang0b4bce28cf4a
diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.pyindex a82b4c82a..388e67b1c 100644--- a/autogen/agentchat/conversable_agent.py+++ b/autogen/agentchat/conversable_agent.py@@ -303,6 +303,10 @@ class ConversableAgent(Agent):"""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 @@ class ConversableAgent(Agent):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 @@ class ConversableAgent(Agent):prompt = ConversableAgent.DEFAULT_summary_prompt if prompt is None else promptif 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.pyindex f615a1a53..01c6ad709 100644--- a/autogen/agentchat/groupchat.py+++ b/autogen/agentchat/groupchat.py@@ -478,6 +478,12 @@ class GroupChatManager(ConversableAgent):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.pyindex 5635676f6..b5c9b6cb7 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.pyindex 36de8d120..9b578c00c 100644--- a/test/agentchat/test_chats.py+++ b/test/agentchat/test_chats.py@@ -5,11 +5,28 @@ import pytestfrom conftest import skip_openaiimport autogenfrom typing import Literal--from pydantic import BaseModel, Fieldfrom 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 test_chats_w_func():if __name__ == "__main__":# test_chats()# test_chats_group()- test_chats_w_func()+ # test_chats_w_func()+ test_chat_messages_for_summary()