Skip to content

A programming framework for agentic AI

add coder reviewer core examples; renames (#110)

Eric Zhu4e1e4c2e33da
diff --git a/python/examples/README.md b/python/examples/README.md
index eeda1d11d..1a0effbc8 100644
--- a/python/examples/README.md
+++ b/python/examples/README.md
@@ -1,52 +1,36 @@
# Examples
-This directory contains examples of how to use AGNext.
+This directory contains examples and demos of how to use AGNext.
-## `chat` layer examples
+## Core examples
-We provide examples that use pre-built agents and message types in the `chat` layer.
-These examples are intended for users who want to quickly create
-demos and experimenting with multi-agent design paterns.
+We provide examples to illustrate the core concepts of AGNext:
+agents, runtime, and message passing APIs.
-### Single agent examples
+- [`inner_outer.py`](inner_outer.py): A simple example of how to create custom agent and message type.
+- [`mixture_of_agents_direct.py`](mixture_of_agents_direct.py): An example of how to create a [mixture of agents](https://github.com/togethercomputer/moa) that communicate using async direct messaging API.
+- [`mixture_of_agents_pub_sub.py`](mixture_of_agents_pub_sub.py): An example of how to create a [mixture of agents](https://github.com/togethercomputer/moa) that communicate using publish-subscribe API.
+- [`coder_reviewer_direct.py`](coder_reviewer_direct.py): An example of how to create a coder-reviewer reflection pattern using async direct messaging API.
+- [`coder_reviewer_pub_sub.py`](coder_reviewer_pub_sub.py): An example of how to create a coder-reviewer reflection pattern using publish-subscribe API.
-- `assistant.py`: a demonstration of how to use the OpenAI Assistant API to create
- a ChatGPT agent.
+## Demos
-### Reflection pattern examples
+We provide interactive demos that showcase the capabilities of AGNext:
-- `coder_reviewer.py`: using a coder and reviewer agents to implement the
- reflection pattern for code generation.
-- `illustrator_critics.py`: using an illustrator, critics and descriptor agent
+- `assistant.py`: a demonstration of how to use the OpenAI Assistant API to create
+ a ChatGPT agent.
+- `chat_room.py`: An example of how to create a chat room of custom agents without
+ a centralized orchestrator.
+- `illustrator_critics.py`: a demo that uses an illustrator, critics and descriptor agent
to implement the reflection pattern for image generation.
-- `chest_game.py`: using two chess player agents to demonstrate tool use and reflection
+- `chest_game.py`: a demo that two chess player agents to demonstrate tool use and reflection
on tool use.
-
-### Group chat pattern examples
-
- `software_consultancy.py`: a demonstration of multi-agent interaction using
the group chat pattern.
-
-### Orchestrator pattern examples
-
- `orchestrator.py`: a demonstration of multi-agent problem solving using
the orchestrator pattern.
-## Advanced examples
-
-We also provide examples that use only the `core`, `application`, and `components` layers.
-These examples are intended for advanced users who want to create
-custom agents and message types for building applications.
-
-- `inner_outer.py`: An example of how to create an inner and outer custom agent.
-- `chat_room.py`: An example of how to create a chat room of custom agents without
- a centralized orchestrator.
-- `mixture_of_agents_pub_sub.py`: An example of how to create [a mixture of agents](https://github.com/togethercomputer/moa)
- that communicate using a publish-subscribe pattern.
-- `mixture_of_agents_gather.py`: An example of how to create [a mixture of agents](https://github.com/togethercomputer/moa)
- that communicate using an async distribute-gather pattern.
-
-## Running the examples
+## Running the examples and demos
First, you need a shell with AGNext and the examples dependencies installed. To do this, run:
diff --git a/python/examples/coder_reviewer.py b/python/examples/coder_reviewer.py
deleted file mode 100644
index 2af239dfa..000000000
--- a/python/examples/coder_reviewer.py
+++ /dev/null
@@ -1,97 +0,0 @@
-import argparse
-import asyncio
-import logging
-import logging.handlers
-import os
-import sys
-
-from agnext.application import SingleThreadedAgentRuntime
-from agnext.chat.agents import ChatCompletionAgent
-from agnext.chat.memory import BufferedChatMemory
-from agnext.chat.patterns import GroupChatManager
-from agnext.components.models import OpenAI, SystemMessage
-from agnext.core import AgentRuntime
-
-sys.path.append(os.path.abspath(os.path.dirname(__file__)))
-
-from utils import TextualChatApp, TextualUserAgent, start_runtime
-
-
-def coder_reviewer(runtime: AgentRuntime, app: TextualChatApp) -> None:
- runtime.register(
- "Human",
- lambda: TextualUserAgent(
- description="A human user that provides a problem statement.",
- app=app,
- ),
- )
- coder = runtime.register_and_get_proxy(
- "Coder",
- lambda: ChatCompletionAgent(
- description="An agent that writes code",
- system_messages=[
- SystemMessage(
- "You are a coder. You can write code to solve problems.\n"
- "Work with the reviewer to improve your code."
- )
- ],
- model_client=OpenAI(model="gpt-4-turbo"),
- memory=BufferedChatMemory(buffer_size=10),
- ),
- )
- reviewer = runtime.register_and_get_proxy(
- "Reviewer",
- lambda: ChatCompletionAgent(
- description="An agent that reviews code",
- system_messages=[
- SystemMessage(
- "You are a code reviewer. You focus on correctness, efficiency and safety of the code.\n"
- "Respond using the following format:\n"
- "Code Review:\n"
- "Correctness: <Your comments>\n"
- "Efficiency: <Your comments>\n"
- "Safety: <Your comments>\n"
- "Approval: <APPROVE or REVISE>\n"
- "Suggested Changes: <Your comments>"
- )
- ],
- model_client=OpenAI(model="gpt-4-turbo"),
- memory=BufferedChatMemory(buffer_size=10),
- ),
- )
- runtime.register(
- "Manager",
- lambda: GroupChatManager(
- description="A manager that orchestrates a back-and-forth converation between a coder and a reviewer.",
- participants=[coder.id, reviewer.id], # The order of the participants indicates the order of speaking.
- memory=BufferedChatMemory(buffer_size=10),
- termination_word="APPROVE",
- ),
- )
- app.welcoming_notice = f"""Welcome to the coder-reviewer demo with the following roles:
-1. πŸ€– {coder.metadata['name']}: {coder.metadata['description']}
-2. 🧐 {reviewer.metadata['name']}: {reviewer.metadata['description']}
-The coder will write code to solve a problem, and the reviewer will review the code.
-The conversation will end when the reviewer approves the code.
-Let's get started by providing a problem statement.
-"""
-
-
-async def main() -> None:
- runtime = SingleThreadedAgentRuntime()
- app = TextualChatApp(runtime, user_name="You")
- coder_reviewer(runtime, app)
- asyncio.create_task(start_runtime(runtime))
- await app.run_async()
-
-
-if __name__ == "__main__":
- parser = argparse.ArgumentParser(description="Coder-reviewer pattern for code writing and review.")
- parser.add_argument("--verbose", action="store_true", help="Enable verbose logging.")
- args = parser.parse_args()
- if args.verbose:
- logging.basicConfig(level=logging.WARNING)
- logging.getLogger("agnext").setLevel(logging.DEBUG)
- handler = logging.FileHandler("coder_reviewer.log")
- logging.getLogger("agnext").addHandler(handler)
- asyncio.run(main())
diff --git a/python/examples/coder_reviewer_direct.py b/python/examples/coder_reviewer_direct.py
new file mode 100644
index 000000000..0e018a810
--- /dev/null
+++ b/python/examples/coder_reviewer_direct.py
@@ -0,0 +1,235 @@
+import asyncio
+import json
+import re
+from dataclasses import dataclass
+from typing import List, Union
+
+from agnext.application import SingleThreadedAgentRuntime
+from agnext.components import TypeRoutedAgent, message_handler
+from agnext.components.models import (
+ AssistantMessage,
+ ChatCompletionClient,
+ LLMMessage,
+ OpenAI,
+ SystemMessage,
+ UserMessage,
+)
+from agnext.core import AgentId, CancellationToken
+
+
+@dataclass
+class CodeWritingTask:
+ task: str
+
+
+@dataclass
+class CodeWritingResult:
+ task: str
+ code: str
+ review: str
+
+
+@dataclass
+class CodeReviewTask:
+ code_writing_task: str
+ code_writing_scratchpad: str
+ code: str
+
+
+@dataclass
+class CodeReviewResult:
+ review: str
+ approved: bool
+
+
+class ReviewerAgent(TypeRoutedAgent):
+ """An agent that performs code review tasks."""
+
+ def __init__(
+ self,
+ description: str,
+ model_client: ChatCompletionClient,
+ ) -> None:
+ super().__init__(description)
+ self._system_messages = [
+ SystemMessage(
+ content="""You are a code reviewer. You focus on correctness, efficiency and safety of the code.
+Respond using the following JSON format:
+{
+ "correctness": "<Your comments>",
+ "efficiency": "<Your comments>",
+ "safety": "<Your comments>",
+ "approval": "<APPROVE or REVISE>",
+ "suggested_changes": "<Your comments>"
+}
+""",
+ )
+ ]
+ self._model_client = model_client
+
+ @message_handler
+ async def handle_code_review_task(
+ self, message: CodeReviewTask, cancellation_token: CancellationToken
+ ) -> CodeReviewResult:
+ # Format the prompt for the code review.
+ prompt = f"""The problem statement is: {message.code_writing_task}
+The code is:
+```
+{message.code}
+```
+Please review the code and provide feedback.
+"""
+ # Generate a response using the chat completion API.
+ response = await self._model_client.create(
+ self._system_messages + [UserMessage(content=prompt, source=self.metadata["name"])]
+ )
+ assert isinstance(response.content, str)
+ # TODO: use structured generation library e.g. guidance to ensure the response is in the expected format.
+ # Parse the response JSON.
+ review = json.loads(response.content)
+ # Construct the review text.
+ review_text = "Code review:\n" + "\n".join([f"{k}: {v}" for k, v in review.items()])
+ approved = review["approval"].lower().strip() == "approve"
+ # Return the review result.
+ return CodeReviewResult(
+ review=review_text,
+ approved=approved,
+ )
+
+
+class CoderAgent(TypeRoutedAgent):
+ """An agent that performs code writing tasks."""
+
+ def __init__(
+ self,
+ description: str,
+ model_client: ChatCompletionClient,
+ reviewer: AgentId,
+ ) -> None:
+ super().__init__(
+ description,
+ )
+ self._system_messages = [
+ SystemMessage(
+ content="""You are a proficient coder. You write code to solve problems.
+Work with the reviewer to improve your code.
+Always put all finished code in a single Markdown code block.
+For example:
+```python
+def hello_world():
+ print("Hello, World!")
+```
+
+Respond using the following format:
+
+Thoughts: <Your comments>
+Code: <Your code>
+""",
+ )
+ ]
+ self._model_client = model_client
+ self._reviewer = reviewer
+
+ @message_handler
+ async def handle_code_writing_task(
+ self,
+ message: CodeWritingTask,
+ cancellation_token: CancellationToken,
+ ) -> CodeWritingResult:
+ # Store the messages in a temporary memory for this request only.
+ memory: List[CodeWritingTask | CodeReviewTask | CodeReviewResult] = []
+ memory.append(message)
+ # Keep generating responses until the code is approved.
+ while not (isinstance(memory[-1], CodeReviewResult) and memory[-1].approved):
+ # Create a list of LLM messages to send to the model.
+ messages: List[LLMMessage] = [*self._system_messages]
+ for m in memory:
+ if isinstance(m, CodeReviewResult):
+ messages.append(UserMessage(content=m.review, source="Reviewer"))
+ elif isinstance(m, CodeReviewTask):
+ messages.append(AssistantMessage(content=m.code_writing_scratchpad, source="Coder"))
+ elif isinstance(m, CodeWritingTask):
+ messages.append(UserMessage(content=m.task, source="User"))
+ else:
+ raise ValueError(f"Unexpected message type: {m}")
+ # Generate a revision using the chat completion API.
+ response = await self._model_client.create(messages)
+ assert isinstance(response.content, str)
+ # Extract the code block from the response.
+ code_block = self._extract_code_block(response.content)
+ if code_block is None:
+ raise ValueError("Code block not found.")
+ # Create a code review task.
+ code_review_task = CodeReviewTask(
+ code_writing_task=message.task,
+ code_writing_scratchpad=response.content,
+ code=code_block,
+ )
+ # Store the code review task in the session memory.
+ memory.append(code_review_task)
+ # Send the code review task to the reviewer.
+ result = await self.send_message(code_review_task, self._reviewer)
+ # Store the review result in the session memory.
+ memory.append(result)
+ # Obtain the request from previous messages.
+ review_request = next(m for m in reversed(memory) if isinstance(m, CodeReviewTask))
+ assert review_request is not None
+ # Publish the code writing result.
+ return CodeWritingResult(
+ task=message.task,
+ code=review_request.code,
+ review=memory[-1].review,
+ )
+
+ def _extract_code_block(self, markdown_text: str) -> Union[str, None]:
+ pattern = r"```(\w+)\n(.*?)\n```"
+ # Search for the pattern in the markdown text
+ match = re.search(pattern, markdown_text, re.DOTALL)
+ # Extract the language and code block if a match is found
+ if match:
+ return match.group(2)
+ return None
+
+
+async def main() -> None:
+ runtime = SingleThreadedAgentRuntime()
+ reviewer = runtime.register_and_get(
+ "ReviewerAgent",
+ lambda: ReviewerAgent(
+ description="Code Reviewer",
+ model_client=OpenAI(model="gpt-3.5-turbo"),
+ ),
+ )
+ coder = runtime.register_and_get(
+ "CoderAgent",
+ lambda: CoderAgent(
+ description="Coder",
+ model_client=OpenAI(model="gpt-3.5-turbo"),
+ reviewer=reviewer,
+ ),
+ )
+ result = runtime.send_message(
+ message=CodeWritingTask(
+ task="Write a function to find the directory with the largest number of files using multi-processing."
+ ),
+ recipient=coder,
+ )
+ while not result.done():
+ await runtime.process_next()
+ code_writing_result = result.result()
+ assert isinstance(code_writing_result, CodeWritingResult)
+ print("Code Writing Result:")
+ print("-" * 80)
+ print(f"Task:\n{code_writing_result.task}")
+ print("-" * 80)
+ print(f"Code:\n{code_writing_result.code}")
+ print("-" * 80)
+ print(f"Review:\n{code_writing_result.review}")
+
+
+if __name__ == "__main__":
+ import logging
+
+ logging.basicConfig(level=logging.WARNING)
+ logging.getLogger("agnext").setLevel(logging.DEBUG)
+ asyncio.run(main())
diff --git a/python/examples/coder_reviewer_pub_sub.py b/python/examples/coder_reviewer_pub_sub.py
new file mode 100644
index 000000000..c9815fde9
--- /dev/null
+++ b/python/examples/coder_reviewer_pub_sub.py
@@ -0,0 +1,306 @@
+import asyncio
+import json
+import re
+import uuid
+from dataclasses import dataclass
+from typing import Any, Dict, List, Union
+
+from agnext.application import SingleThreadedAgentRuntime
+from agnext.components import TypeRoutedAgent, message_handler
+from agnext.components.models import (
+ AssistantMessage,
+ ChatCompletionClient,
+ LLMMessage,
+ OpenAI,
+ SystemMessage,
+ UserMessage,
+)
+from agnext.core import AgentId, CancellationToken
+from agnext.core.intervention import DefaultInterventionHandler
+
+
+@dataclass
+class CodeWritingTask:
+ task: str
+
+
+@dataclass
+class CodeWritingResult:
+ task: str
+ code: str
+ review: str
+
+
+@dataclass
+class CodeReviewTask:
+ session_id: str
+ code_writing_task: str
+ code_writing_scratchpad: str
+ code: str
+
+
+@dataclass
+class CodeReviewResult:
+ review: str
+ session_id: str
+ approved: bool
+
+
+@dataclass
+class Termination:
+ pass
+
+
+class ReviewerAgent(TypeRoutedAgent):
+ """An agent that performs code review tasks."""
+
+ def __init__(
+ self,
+ description: str,
+ model_client: ChatCompletionClient,
+ ) -> None:
+ super().__init__(description)
+ self._system_messages = [
+ SystemMessage(
+ content="""You are a code reviewer. You focus on correctness, efficiency and safety of the code.
+Respond using the following JSON format:
+{
+ "correctness": "<Your comments>",
+ "efficiency": "<Your comments>",
+ "safety": "<Your comments>",
+ "approval": "<APPROVE or REVISE>",
+ "suggested_changes": "<Your comments>"
+}
+""",
+ )
+ ]
+ self._model_client = model_client
+
+ @message_handler
+ async def handle_code_review_task(self, message: CodeReviewTask, cancellation_token: CancellationToken) -> None:
+ # Format the prompt for the code review.
+ prompt = f"""The problem statement is: {message.code_writing_task}
+The code is:
+```
+{message.code}
+```
+Please review the code and provide feedback.
+"""
+ # Generate a response using the chat completion API.
+ response = await self._model_client.create(
+ self._system_messages + [UserMessage(content=prompt, source=self.metadata["name"])]
+ )
+ assert isinstance(response.content, str)
+ # TODO: use structured generation library e.g. guidance to ensure the response is in the expected format.
+ # Parse the response JSON.
+ review = json.loads(response.content)
+ # Construct the review text.
+ review_text = "Code review:\n" + "\n".join([f"{k}: {v}" for k, v in review.items()])
+ approved = review["approval"].lower().strip() == "approve"
+ # Publish the review result.
+ await self.publish_message(
+ CodeReviewResult(
+ review=review_text,
+ approved=approved,
+ session_id=message.session_id,
+ )
+ )
+
+
+class CoderAgent(TypeRoutedAgent):
+ """An agent that performs code writing tasks."""
+
+ def __init__(
+ self,
+ description: str,
+ model_client: ChatCompletionClient,
+ ) -> None:
+ super().__init__(
+ description,
+ )
+ self._system_messages = [
+ SystemMessage(
+ content="""You are a proficient coder. You write code to solve problems.
+Work with the reviewer to improve your code.
+Always put all finished code in a single Markdown code block.
+For example:
+```python
+def hello_world():
+ print("Hello, World!")
+```
+
+Respond using the following format:
+
+Thoughts: <Your comments>
+Code: <Your code>
+""",
+ )
+ ]
+ self._model_client = model_client
+ self._session_memory: Dict[str, List[CodeWritingTask | CodeReviewTask | CodeReviewResult]] = {}
+
+ @message_handler
+ async def handle_code_writing_task(
+ self,
+ message: CodeWritingTask,
+ cancellation_token: CancellationToken,
+ ) -> None:
+ # Store the messages in a temporary memory for this request only.
+ session_id = str(uuid.uuid4())
+ self._session_memory.setdefault(session_id, []).append(message)
+ # Generate a response using the chat completion API.
+ response = await self._model_client.create(
+ self._system_messages + [UserMessage(content=message.task, source=self.metadata["name"])]
+ )
+ assert isinstance(response.content, str)
+ # Extract the code block from the response.
+ code_block = self._extract_code_block(response.content)
+ if code_block is None:
+ raise ValueError("Code block not found.")
+ # Create a code review task.
+ code_review_task = CodeReviewTask(
+ session_id=session_id,
+ code_writing_task=message.task,
+ code_writing_scratchpad=response.content,
+ code=code_block,
+ )
+ # Store the code review task in the session memory.
+ self._session_memory[session_id].append(code_review_task)
+ # Publish a code review task.
+ await self.publish_message(code_review_task)
+
+ @message_handler
+ async def handle_code_review_result(self, message: CodeReviewResult, cancellation_token: CancellationToken) -> None:
+ # Store the review result in the session memory.
+ self._session_memory[message.session_id].append(message)
+ # Obtain the request from previous messages.
+ review_request = next(
+ m for m in reversed(self._session_memory[message.session_id]) if isinstance(m, CodeReviewTask)
+ )
+ assert review_request is not None
+ # Check if the code is approved.
+ if message.approved:
+ # Publish the code writing result.
+ await self.publish_message(
+ CodeWritingResult(
+ code=review_request.code,
+ task=review_request.code_writing_task,
+ review=message.review,
+ )
+ )
+ else:
+ # Create a list of LLM messages to send to the model.
+ messages: List[LLMMessage] = [*self._system_messages]
+ for m in self._session_memory[message.session_id]:
+ if isinstance(m, CodeReviewResult):
+ messages.append(UserMessage(content=m.review, source="Reviewer"))
+ elif isinstance(m, CodeReviewTask):
+ messages.append(AssistantMessage(content=m.code_writing_scratchpad, source="Coder"))
+ elif isinstance(m, CodeWritingTask):
+ messages.append(UserMessage(content=m.task, source="User"))
+ else:
+ raise ValueError(f"Unexpected message type: {m}")
+ # Generate a revision using the chat completion API.
+ response = await self._model_client.create(messages)
+ assert isinstance(response.content, str)
+ # Extract the code block from the response.
+ code_block = self._extract_code_block(response.content)
+ if code_block is None:
+ raise ValueError("Code block not found.")
+ # Create a new code review task.
+ code_review_task = CodeReviewTask(
+ session_id=message.session_id,
+ code_writing_task=review_request.code_writing_task,
+ code_writing_scratchpad=response.content,
+ code=code_block,
+ )
+ # Store the code review task in the session memory.
+ self._session_memory[message.session_id].append(code_review_task)
+ # Publish a new code review task.
+ await self.publish_message(code_review_task)
+
+ def _extract_code_block(self, markdown_text: str) -> Union[str, None]:
+ pattern = r"```(\w+)\n(.*?)\n```"
+ # Search for the pattern in the markdown text
+ match = re.search(pattern, markdown_text, re.DOTALL)
+ # Extract the language and code block if a match is found
+ if match:
+ return match.group(2)
+ return None
+
+
+class TerminationHandler(DefaultInterventionHandler):
+ """A handler that listens for termination messages."""
+
+ def __init__(self) -> None:
+ self._terminated = False
+
+ async def on_publish(self, message: Any, *, sender: AgentId | None) -> Any:
+ if isinstance(message, Termination):
+ self._terminated = True
+ return message
+
+ @property
+ def terminated(self) -> bool:
+ return self._terminated
+
+
+class DisplayAgent(TypeRoutedAgent):
+ """An agent that displays code writing result to the console and
+ publishes termination message."""
+
+ @message_handler
+ async def handle_code_writing_result(
+ self, message: CodeWritingResult, cancellation_token: CancellationToken
+ ) -> None:
+ print("Code Writing Result:")
+ print("-" * 80)
+ print(f"Task:\n{message.task}")
+ print("-" * 80)
+ print(f"Code:\n{message.code}")
+ print("-" * 80)
+ print(f"Review:\n{message.review}")
+ print("-" * 80)
+ # Publish a termination message to the runtime.
+ self.publish_message(Termination())
+
+
+async def main() -> None:
+ termination_handler = TerminationHandler()
+ runtime = SingleThreadedAgentRuntime(before_send=termination_handler)
+ runtime.register(
+ "ReviewerAgent",
+ lambda: ReviewerAgent(
+ description="Code Reviewer",
+ model_client=OpenAI(model="gpt-3.5-turbo"),
+ ),
+ )
+ runtime.register(
+ "CoderAgent",
+ lambda: CoderAgent(
+ description="Coder",
+ model_client=OpenAI(model="gpt-3.5-turbo"),
+ ),
+ )
+ runtime.register(
+ "DisplayAgent",
+ lambda: DisplayAgent(description="Display Agent"),
+ )
+ runtime.publish_message(
+ message=CodeWritingTask(
+ task="Write a function to find the directory with the largest number of files using multi-processing."
+ ),
+ namespace="default",
+ )
+
+ # Keep processing messages until termination.
+ while not termination_handler.terminated:
+ await runtime.process_next()
+
+
+if __name__ == "__main__":
+ import logging
+
+ logging.basicConfig(level=logging.WARNING)
+ logging.getLogger("agnext").setLevel(logging.DEBUG)
+ asyncio.run(main())
diff --git a/python/examples/mixture_of_agents_gather.py b/python/examples/mixture_of_agents_direct.py
similarity index 100%
rename from python/examples/mixture_of_agents_gather.py
rename to python/examples/mixture_of_agents_direct.py
diff --git a/python/examples/mixture_of_agents_pub_sub.py b/python/examples/mixture_of_agents_pub_sub.py
index 8292a31f7..6eff0ec43 100644
--- a/python/examples/mixture_of_agents_pub_sub.py
+++ b/python/examples/mixture_of_agents_pub_sub.py
@@ -4,12 +4,13 @@ Mixture of agents: https://github.com/togethercomputer/moa"""
import asyncio
import uuid
from dataclasses import dataclass
-from typing import Dict, List
+from typing import Any, Dict, List
from agnext.application import SingleThreadedAgentRuntime
from agnext.components import TypeRoutedAgent, message_handler
from agnext.components.models import ChatCompletionClient, OpenAI, SystemMessage, UserMessage
-from agnext.core import CancellationToken
+from agnext.core import AgentId, CancellationToken
+from agnext.core.intervention import DefaultInterventionHandler
@dataclass
@@ -34,6 +35,11 @@ class AggregatorTaskResult:
result: str
+@dataclass
+class Termination:
+ pass
+
+
class ReferenceAgent(TypeRoutedAgent):
"""The reference agent that handles each task independently."""
@@ -96,8 +102,39 @@ class AggregatorAgent(TypeRoutedAgent):
self._session_results.pop(message.session_id)
+class TerminationHandler(DefaultInterventionHandler):
+ """A handler that listens for termination messages."""
+
+ def __init__(self) -> None:
+ self._terminated = False
+
+ async def on_publish(self, message: Any, *, sender: AgentId | None) -> Any:
+ if isinstance(message, Termination):
+ self._terminated = True
+ return message
+
+ @property
+ def terminated(self) -> bool:
+ return self._terminated
+
+
+class DisplayAgent(TypeRoutedAgent):
+ """An agent that displays code writing result to the console and
+ publishes a termination message to the runtime."""
+
+ @message_handler
+ async def handle_code_writing_result(
+ self, message: AggregatorTaskResult, cancellation_token: CancellationToken
+ ) -> None:
+ print("Aggregator Task Result:", message.result)
+ # Terminate the runtime.
+ await self.publish_message(Termination())
+
+
async def main() -> None:
- runtime = SingleThreadedAgentRuntime()
+ termination_handler = TerminationHandler()
+ runtime = SingleThreadedAgentRuntime(before_send=termination_handler)
+ # TODO: use different models for each agent.
runtime.register(
"ReferenceAgent1",
lambda: ReferenceAgent(
@@ -135,8 +172,14 @@ async def main() -> None:
num_references=3,
),
)
+ runtime.register(
+ "DisplayAgent",
+ lambda: DisplayAgent(description="Display Agent"),
+ )
await runtime.publish_message(AggregatorTask(task="What are something fun to do in SF?"), namespace="default")
- while True:
+
+ # Keep processing messages until termination.
+ while not termination_handler.terminated:
await runtime.process_next()