Skip to content

A programming framework for agentic AI

Add coder, file surfer, common message types in team-one (#142)

gagbaff179776ce7
diff --git a/python/teams/team-one/examples/example_coder.py b/python/teams/team-one/examples/example_coder.py
index a1e604e62..5594d928a 100644
--- a/python/teams/team-one/examples/example_coder.py
+++ b/python/teams/team-one/examples/example_coder.py
@@ -1,35 +1,35 @@
import asyncio
from agnext.application import SingleThreadedAgentRuntime
-from agnext.components.models import OpenAIChatCompletionClient
-from team_one.agents.coder import Coder
-from team_one.messages import LLMResponseMessage, TaskMessage
+from agnext.components.models import OpenAIChatCompletionClient, UserMessage
+from team_one.agents.coder import Coder, Executor
+from team_one.agents.orchestrator import RoundRobinOrchestrator
+from team_one.messages import BroadcastMessage
async def main() -> None:
# Create the runtime.
runtime = SingleThreadedAgentRuntime()
+ client = OpenAIChatCompletionClient(model="gpt-4o")
+
# Register agents.
- coder = runtime.register_and_get(
- "coder",
- lambda: Coder(model_client=OpenAIChatCompletionClient(model="gpt-4o")),
+ coder = runtime.register_and_get_proxy(
+ "Coder",
+ lambda: Coder(model_client=client),
)
+ executor = runtime.register_and_get_proxy("Executor", lambda: Executor("A agent for executing code"))
+
+ runtime.register("orchestrator", lambda: RoundRobinOrchestrator([coder, executor]))
- task = TaskMessage(input(f"Enter a task for {coder.name}: "))
+ task = input("Enter a task: ")
- # Send a task to the tool user.
- result = await runtime.send_message(task, coder)
+ await runtime.publish_message(
+ BroadcastMessage(content=UserMessage(content=task, source="human")), namespace="default"
+ )
# Run the runtime until the task is completed.
- while not result.done():
- await runtime.process_next()
-
- # Print the result.
- final_response = result.result()
- assert isinstance(final_response, LLMResponseMessage)
- print("--------------------")
- print(final_response.content)
+ await runtime.process_until_idle()
if __name__ == "__main__":
diff --git a/python/teams/team-one/examples/example_file_surfer.py b/python/teams/team-one/examples/example_file_surfer.py
index 58103631c..296b4eef2 100644
--- a/python/teams/team-one/examples/example_file_surfer.py
+++ b/python/teams/team-one/examples/example_file_surfer.py
@@ -1,11 +1,9 @@
import asyncio
from agnext.application import SingleThreadedAgentRuntime
-from agnext.components.models import (
- OpenAIChatCompletionClient,
-)
+from agnext.components.models import OpenAIChatCompletionClient, UserMessage
from team_one.agents.file_surfer import FileSurfer
-from team_one.messages import LLMResponseMessage, TaskMessage
+from team_one.messages import BroadcastMessage, RequestReplyMessage
async def main() -> None:
@@ -17,21 +15,15 @@ async def main() -> None:
"file_surfer",
lambda: FileSurfer(model_client=OpenAIChatCompletionClient(model="gpt-4o")),
)
-
- task = TaskMessage(input(f"Enter a task for {file_surfer.name}: "))
+ task = input(f"Enter a task for {file_surfer.name}: ")
+ msg = BroadcastMessage(content=UserMessage(content=task, source="human"))
# Send a task to the tool user.
- result = await runtime.send_message(task, file_surfer)
+ await runtime.publish_message(msg, namespace="default")
+ await runtime.publish_message(RequestReplyMessage(), namespace="default")
# Run the runtime until the task is completed.
- while not result.done():
- await runtime.process_next()
-
- # Print the result.
- final_response = result.result()
- assert isinstance(final_response, LLMResponseMessage)
- print("--------------------")
- print(final_response.content)
+ await runtime.process_until_idle()
if __name__ == "__main__":
diff --git a/python/teams/team-one/examples/example_reflexagents.py b/python/teams/team-one/examples/example_reflexagents.py
index 0198cd699..c12177cc9 100644
--- a/python/teams/team-one/examples/example_reflexagents.py
+++ b/python/teams/team-one/examples/example_reflexagents.py
@@ -1,6 +1,7 @@
import asyncio
from agnext.application import SingleThreadedAgentRuntime
+from agnext.components.models import UserMessage
from team_one.agents.orchestrator import RoundRobinOrchestrator
from team_one.agents.reflex_agents import ReflexAgent
from team_one.messages import BroadcastMessage
@@ -14,7 +15,8 @@ async def main() -> None:
fake3 = runtime.register_and_get_proxy("fake_agent_3", lambda: ReflexAgent("Third reflect agent"))
runtime.register_and_get("orchestrator", lambda: RoundRobinOrchestrator([fake1, fake2, fake3]))
- await runtime.publish_message(BroadcastMessage("Test message"), namespace="default")
+ task_message = UserMessage(content="Test Message", source="User")
+ await runtime.publish_message(BroadcastMessage(task_message), namespace="default")
await runtime.process_until_idle()
diff --git a/python/teams/team-one/src/team_one/agents/coder.py b/python/teams/team-one/src/team_one/agents/coder.py
index a9d846b23..923ce386c 100644
--- a/python/teams/team-one/src/team_one/agents/coder.py
+++ b/python/teams/team-one/src/team_one/agents/coder.py
@@ -1,22 +1,19 @@
import asyncio
-import json
-from typing import List
+import re
+from typing import List, Optional, Union
-from agnext.components import FunctionCall, TypeRoutedAgent, message_handler
-from agnext.components.code_executor import LocalCommandLineCodeExecutor
+from agnext.components import TypeRoutedAgent, message_handler
+from agnext.components.code_executor import CodeBlock, CodeExecutor, LocalCommandLineCodeExecutor
from agnext.components.models import (
AssistantMessage,
ChatCompletionClient,
- FunctionExecutionResult,
- FunctionExecutionResultMessage,
LLMMessage,
SystemMessage,
UserMessage,
)
-from agnext.components.tools import PythonCodeExecutionTool
from agnext.core import CancellationToken
-from ..messages import LLMResponseMessage, TaskMessage, ToolMessage, ToolResultMessage
+from team_one.messages import BroadcastMessage, RequestReplyMessage
class Coder(TypeRoutedAgent):
@@ -25,9 +22,23 @@ class Coder(TypeRoutedAgent):
DEFAULT_DESCRIPTION = "A Python coder assistant."
DEFAULT_SYSTEM_MESSAGES = [
- SystemMessage("""You are a helpful AI Assistant. Use your tools to solve problems.
- If the tool results in an error, use the error trace to improve
- the python code. If the code requires installing packages, use python to install the packages"""),
+ SystemMessage("""You are a helpful AI assistant. Solve tasks using your Python coding skills. The code you output must be formatted in Markdown code blocks demarcated by triple backticks (```). As an example:
+
+```python
+
+def main():
+ print("Hello world.")
+
+if __name__ == "__main__":
+ main()
+```
+
+The user cannot provide any feedback or perform any other action beyond executing the code you suggest. In particular, the user can't modify your code, and can't copy and paste anything, and can't fill in missing values. Thus, do not suggest incomplete code which requires users to perform any of these actions.
+
+Check the execution result returned by the user. If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code or code changes -- code blocks must stand alone and be ready to execute without modification. If the error can't be fixed or if the task is not solved even after the code is executed successfully, analyze the problem, revisit your assumption, and think of a different approach to try.
+
+If the code has executed successfully, and the problem is stolved, reply "TERMINATE".
+""")
]
def __init__(
@@ -39,53 +50,78 @@ class Coder(TypeRoutedAgent):
super().__init__(description)
self._model_client = model_client
self._system_messages = system_messages
- self._tools = [PythonCodeExecutionTool(LocalCommandLineCodeExecutor())]
+ self._chat_history: List[LLMMessage] = []
@message_handler
- async def handle_user_message(
- self, message: TaskMessage, cancellation_token: CancellationToken
- ) -> LLMResponseMessage:
- """Handle a user message, execute the model and tools, and returns the response."""
+ async def handle_broadcast(self, message: BroadcastMessage, cancellation_token: CancellationToken) -> None:
+ """Handle an incoming broadcast message."""
+ assert isinstance(message.content, UserMessage)
+ self._chat_history.append(message.content)
- session: List[LLMMessage] = []
- session.append(UserMessage(content=message.content, source="User"))
-
- response = await self._model_client.create(self._system_messages + session, tools=self._tools)
+ @message_handler
+ async def handle_request_reply(self, message: RequestReplyMessage, cancellation_token: CancellationToken) -> None:
+ """Respond to a reply request."""
- session.append(AssistantMessage(content=response.content, source=self.metadata["name"]))
+ # Make an inference to the model.
+ response = await self._model_client.create(self._system_messages + self._chat_history)
+ assert isinstance(response.content, str)
+ self._chat_history.append(AssistantMessage(content=response.content, source=self.metadata["name"]))
- # Keep executing the tools until the response is not a list of function calls.
- while isinstance(response.content, list) and all(isinstance(item, FunctionCall) for item in response.content):
- # TODO: gather internally too
- results = await asyncio.gather(
- *[await self.send_message(ToolMessage(function_call=call), self.id) for call in response.content]
+ if "TERMINATE" in response.content:
+ return
+ else:
+ await self.publish_message(
+ BroadcastMessage(content=UserMessage(content=response.content, source=self.metadata["name"]))
)
- # Combine the results into a single response.
- result = FunctionExecutionResultMessage(content=[result.result for result in results])
- session.append(result)
- # Execute the model again with the new response.
- response = await self._model_client.create(self._system_messages + session, tools=self._tools)
- session.append(AssistantMessage(content=response.content, source=self.metadata["name"]))
- assert isinstance(response.content, str)
- return LLMResponseMessage(content=response.content)
+
+class Executor(TypeRoutedAgent):
+ def __init__(self, description: str, executor: Optional[CodeExecutor] = None) -> None:
+ super().__init__(description)
+ self._executor = executor or LocalCommandLineCodeExecutor()
+ self._chat_history: List[LLMMessage] = []
+
+ @message_handler
+ async def handle_broadcast(self, message: BroadcastMessage, cancellation_token: CancellationToken) -> None:
+ """Handle an incoming broadcast message."""
+ self._chat_history.append(message.content)
@message_handler
- async def handle_tool_call(self, message: ToolMessage, cancellation_token: CancellationToken) -> ToolResultMessage:
- """Handle a tool execution task. This method executes the tool and publishes the result."""
- # Find the tool
- tool = next((tool for tool in self._tools if tool.name == message.function_call.name), None)
- if tool is None:
- result_as_str = f"Error: Tool not found: {message.function_call.name}"
+ async def handle_request_reply(self, message: RequestReplyMessage, cancellation_token: CancellationToken) -> None:
+ """Respond to a reply request."""
+
+ # Extract code block from the message.
+ assert isinstance(self._chat_history[-1].content, str)
+ code = self._extract_execution_request(self._chat_history[-1].content)
+ if code is not None:
+ execution_requests = [CodeBlock(code=code, language="python")]
+ future = asyncio.get_event_loop().run_in_executor(
+ None, self._executor.execute_code_blocks, execution_requests
+ )
+ cancellation_token.link_future(future)
+ result = await future
+
+ str_result = (
+ f"The script ran, then exited with Unix exit code: {result.exit_code}\nIts output was:\n{result.output}"
+ )
+ await self.publish_message(
+ BroadcastMessage(content=UserMessage(content=str_result, source=self.metadata["name"]))
+ )
else:
- try:
- arguments = json.loads(message.function_call.arguments)
- result = await tool.run_json(args=arguments, cancellation_token=cancellation_token)
- result_as_str = tool.return_value_as_string(result)
- except json.JSONDecodeError:
- result_as_str = f"Error: Invalid arguments: {message.function_call.arguments}"
- except Exception as e:
- result_as_str = f"Error: {e}"
- return ToolResultMessage(
- result=FunctionExecutionResult(content=result_as_str, call_id=message.function_call.id),
- )
+ await self.publish_message(
+ BroadcastMessage(
+ content=UserMessage(
+ content="No code block detected. Please provide a markdown-encoded code block to execute for the original task.",
+ source=self.metadata["name"],
+ )
+ )
+ )
+
+ def _extract_execution_request(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
diff --git a/python/teams/team-one/src/team_one/agents/file_surfer.py b/python/teams/team-one/src/team_one/agents/file_surfer.py
index 331747a16..481b606da 100644
--- a/python/teams/team-one/src/team_one/agents/file_surfer.py
+++ b/python/teams/team-one/src/team_one/agents/file_surfer.py
@@ -1,4 +1,5 @@
import asyncio
+import copy
import json
from pathlib import Path
from typing import List
@@ -9,7 +10,6 @@ from agnext.components.models import (
AssistantMessage,
ChatCompletionClient,
FunctionExecutionResult,
- FunctionExecutionResultMessage,
LLMMessage,
SystemMessage,
UserMessage,
@@ -17,7 +17,7 @@ from agnext.components.models import (
from agnext.components.tools import FunctionTool
from agnext.core import CancellationToken
-from ..messages import LLMResponseMessage, TaskMessage, ToolMessage, ToolResultMessage
+from ..messages import BroadcastMessage, RequestReplyMessage
async def read_local_file(file_path: str) -> str:
@@ -68,52 +68,58 @@ class FileSurfer(TypeRoutedAgent):
name="list_files_in_directory",
),
]
+ self._history: List[LLMMessage] = []
@message_handler
- async def handle_user_message(
- self, message: TaskMessage, cancellation_token: CancellationToken
- ) -> LLMResponseMessage:
+ async def on_broadcast_message(self, message: BroadcastMessage, cancellation_token: CancellationToken) -> None:
"""Handle a user message, execute the model and tools, and returns the response."""
+ assert isinstance(message.content, UserMessage)
+ self._history.append(message.content)
- session: List[LLMMessage] = []
- session.append(UserMessage(content=message.content, source="User"))
+ @message_handler
+ async def on_request_reply_message(
+ self, message: RequestReplyMessage, cancellation_token: CancellationToken
+ ) -> None:
+ session: List[LLMMessage] = copy.deepcopy(self._history)
response = await self._model_client.create(self._system_messages + session, tools=self._tools)
-
session.append(AssistantMessage(content=response.content, source=self.metadata["name"]))
- # Keep executing the tools until the response is not a list of function calls.
- while isinstance(response.content, list) and all(isinstance(item, FunctionCall) for item in response.content):
- # TODO: gather internally too
- results = await asyncio.gather(
- *[await self.send_message(ToolMessage(function_call=call), self.id) for call in response.content]
- )
- # Combine the results into a single response.
- result = FunctionExecutionResultMessage(content=[result.result for result in results])
- session.append(result)
- # Execute the model again with the new response.
- response = await self._model_client.create(self._system_messages + session, tools=self._tools)
- session.append(AssistantMessage(content=response.content, source=self.metadata["name"]))
-
- assert isinstance(response.content, str)
- return LLMResponseMessage(content=response.content)
+ if isinstance(response.content, str):
+ final_result = response.content
+
+ elif isinstance(response.content, list) and all(isinstance(item, FunctionCall) for item in response.content):
+ results = await asyncio.gather(*[await self.send_message(call, self.id) for call in response.content])
+ for result in results:
+ assert isinstance(result, FunctionExecutionResult)
+ final_result = "\n".join(result.content for result in results)
+ else:
+ raise ValueError(f"Unexpected response type: {response.content}")
+
+ assert isinstance(final_result, str)
+
+ session.append(AssistantMessage(content=final_result, source=self.metadata["name"]))
+ await self.publish_message(
+ BroadcastMessage(content=UserMessage(content=final_result, source=self.metadata["name"]))
+ )
@message_handler
- async def handle_tool_call(self, message: ToolMessage, cancellation_token: CancellationToken) -> ToolResultMessage:
+ async def handle_tool_call(
+ self, message: FunctionCall, cancellation_token: CancellationToken
+ ) -> FunctionExecutionResult:
"""Handle a tool execution task. This method executes the tool and publishes the result."""
+ function_call = message
# Find the tool
- tool = next((tool for tool in self._tools if tool.name == message.function_call.name), None)
+ tool = next((tool for tool in self._tools if tool.name == function_call.name), None)
if tool is None:
- result_as_str = f"Error: Tool not found: {message.function_call.name}"
+ result_as_str = f"Error: Tool not found: {function_call.name}"
else:
try:
- arguments = json.loads(message.function_call.arguments)
+ arguments = json.loads(function_call.arguments)
result = await tool.run_json(args=arguments, cancellation_token=cancellation_token)
result_as_str = tool.return_value_as_string(result)
except json.JSONDecodeError:
- result_as_str = f"Error: Invalid arguments: {message.function_call.arguments}"
+ result_as_str = f"Error: Invalid arguments: {function_call.arguments}"
except Exception as e:
result_as_str = f"Error: {e}"
- return ToolResultMessage(
- result=FunctionExecutionResult(content=result_as_str, call_id=message.function_call.id),
- )
+ return FunctionExecutionResult(content=result_as_str, call_id=function_call.id)
diff --git a/python/teams/team-one/src/team_one/agents/orchestrator.py b/python/teams/team-one/src/team_one/agents/orchestrator.py
index 654ea6118..cdb21ef00 100644
--- a/python/teams/team-one/src/team_one/agents/orchestrator.py
+++ b/python/teams/team-one/src/team_one/agents/orchestrator.py
@@ -20,7 +20,7 @@ class RoundRobinOrchestrator(TypeRoutedAgent):
async def handle_incoming_message(self, message: BroadcastMessage, cancellation_token: CancellationToken) -> None:
"""Handle an incoming message."""
- if self._num_rounds > 3:
+ if self._num_rounds > 20:
return
next_agent = self._select_next_agent()
diff --git a/python/teams/team-one/src/team_one/agents/reflex_agents.py b/python/teams/team-one/src/team_one/agents/reflex_agents.py
index b7adbd7da..1f9e0cf7b 100644
--- a/python/teams/team-one/src/team_one/agents/reflex_agents.py
+++ b/python/teams/team-one/src/team_one/agents/reflex_agents.py
@@ -1,4 +1,5 @@
from agnext.components import TypeRoutedAgent, message_handler
+from agnext.components.models import UserMessage
from agnext.core import CancellationToken
from ..messages import BroadcastMessage, RequestReplyMessage
@@ -18,6 +19,10 @@ class ReflexAgent(TypeRoutedAgent):
self, message: RequestReplyMessage, cancellation_token: CancellationToken
) -> None:
name = self.metadata["name"]
- await self.publish_message(
- BroadcastMessage(content=f"Hello, world from {name}!"), cancellation_token=cancellation_token
+
+ response_message = UserMessage(
+ content=f"Hello, world from {name}!",
+ source=name,
)
+
+ await self.publish_message(BroadcastMessage(response_message), cancellation_token=cancellation_token)
diff --git a/python/teams/team-one/src/team_one/messages.py b/python/teams/team-one/src/team_one/messages.py
index 4f82f9cb1..2cbf5a57b 100644
--- a/python/teams/team-one/src/team_one/messages.py
+++ b/python/teams/team-one/src/team_one/messages.py
@@ -1,32 +1,11 @@
from dataclasses import dataclass
-from agnext.components import FunctionCall
-from agnext.components.models import FunctionExecutionResult
-
-
-@dataclass
-class ToolMessage:
- function_call: FunctionCall
-
-
-@dataclass
-class ToolResultMessage:
- result: FunctionExecutionResult
-
-
-@dataclass
-class TaskMessage:
- content: str
-
-
-@dataclass
-class LLMResponseMessage:
- content: str
+from agnext.components.models import LLMMessage
@dataclass
class BroadcastMessage:
- content: str
+ content: LLMMessage
@dataclass