From 4998ab539884f9ead29e90ed04c3ef65722fc6e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aaron=20G=C3=A4tje?= <44442602+aarongae@users.noreply.github.com> Date: Wed, 18 Sep 2024 16:49:13 +0200 Subject: [PATCH] Fix issues in python core samples (#549) * fix issues * remove sender from send_message call * Fix agent type --------- Co-authored-by: Aaron Gaetje Co-authored-by: Jack Gerrits --- .../samples/core/inner_outer_direct.py | 2 +- .../samples/core/two_agents_pub_sub.py | 20 ++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/python/packages/autogen-core/samples/core/inner_outer_direct.py b/python/packages/autogen-core/samples/core/inner_outer_direct.py index 4d2a1f9331d..bb669b25679 100644 --- a/python/packages/autogen-core/samples/core/inner_outer_direct.py +++ b/python/packages/autogen-core/samples/core/inner_outer_direct.py @@ -46,7 +46,7 @@ async def on_new_message(self, message: MessageType, ctx: MessageContext) -> Mes async def main() -> None: runtime = SingleThreadedAgentRuntime() await runtime.register("inner", Inner) - await runtime.register("outer", lambda: Outer(AgentId("outer", AgentInstantiationContext.current_agent_id().key))) + await runtime.register("outer", lambda: Outer(AgentId("inner", AgentInstantiationContext.current_agent_id().key))) outer = AgentId("outer", "default") runtime.start() diff --git a/python/packages/autogen-core/samples/core/two_agents_pub_sub.py b/python/packages/autogen-core/samples/core/two_agents_pub_sub.py index 70fcca88c91..306a310c149 100644 --- a/python/packages/autogen-core/samples/core/two_agents_pub_sub.py +++ b/python/packages/autogen-core/samples/core/two_agents_pub_sub.py @@ -17,7 +17,7 @@ from typing import List from autogen_core.application import SingleThreadedAgentRuntime -from autogen_core.base import AgentId +from autogen_core.base import AgentId, MessageContext from autogen_core.components import DefaultSubscription, DefaultTopicId, RoutedAgent, message_handler from autogen_core.components.models import ( AssistantMessage, @@ -29,7 +29,6 @@ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) -from autogen_core.base import MessageContext from common.utils import get_chat_completion_client_from_envs @@ -71,10 +70,9 @@ async def handle_message(self, message: Message, ctx: MessageContext) -> None: response = await self._model_client.create(self._system_messages + llm_messages) assert isinstance(response.content, str) - if ctx.topic_id is not None: - await self.publish_message( - Message(content=response.content, source=self.metadata["type"]), topic_id=DefaultTopicId() - ) + await self.publish_message( + Message(content=response.content, source=self.metadata["type"]), topic_id=DefaultTopicId() + ) async def main() -> None: @@ -88,7 +86,9 @@ async def main() -> None: description="Jack a comedian", model_client=get_chat_completion_client_from_envs(model="gpt-4o-mini"), system_messages=[ - SystemMessage("You are a comedian likes to make jokes. " "When you are done talking, say 'TERMINATE'.") + SystemMessage( + "You are a comedian that likes to make jokes. " "After multiple turns, respond with 'TERMINATE'" + ) ], termination_word="TERMINATE", ), @@ -100,7 +100,9 @@ async def main() -> None: description="Cathy a poet", model_client=get_chat_completion_client_from_envs(model="gpt-4o-mini"), system_messages=[ - SystemMessage("You are a poet likes to write poems. " "When you are done talking, say 'TERMINATE'.") + SystemMessage( + "You are a poet likes that to write poems. " "After multiple turns, respond with 'TERMINATE'" + ) ], termination_word="TERMINATE", ), @@ -111,7 +113,7 @@ async def main() -> None: # Send a message to Jack to start the conversation. message = Message(content="Can you tell me something fun about SF?", source="User") - await runtime.send_message(message, AgentId("jack", "default")) + await runtime.send_message(message, AgentId("Jack", "default")) # Process messages. await runtime.stop_when_idle()