Diving deep into Magentic-One...
I did a deep dive into the Magentic-One(SingleThreadedAgentRuntime) code today. I wanted to understand how it has been designed. The framework looked to me has a message queue at the core. All interactions between the orchestrator and the agents are orchestrated via the message queue. Below I have explained, what I have explored so far on this framework
The runtime of the framework is created by instantiating SingleThreadedAgentRuntime() as below.
runtime = SingleThreadedAgentRuntime()
This run time creates a RunContext that starts a process in a continuous loop. That process continues to look for any new message in the message queue to process. The exact code looks as below
async def _run(self) -> None:
while True:
async with self._lock:
if self._end_condition():
return
await self._runtime.process_next()
The process_next() function continues to poll the message queue to process and hand over the message to the appropriate components(both agents and orchestrator) through a message handler. I have explained the flow through the below diagram
领英推荐
A user query is wrapped into the SendMessageEnvelope and dropped in the message queue which is nothing but a Python List data structure.
self._message_queue.append(
SendMessageEnvelope(
message=message,
recipient=recipient,
future=future,
cancellation_token=cancellation_token,
sender=sender,
metadata=get_telemetry_envelope_metadata(),
)
)
The process that is continuously polling the queue, takes the message and based on the Message protocol(SendMessageEnvelope) uses a message handler to send he query to the User Proxy. The user proxy then takes the message and converts it to a PublishMessageEnvelope(one of the three message protocols that the message queue can take) and puts into the message queue. Since this is now a PublishMessageEnvelope, the message handler now broadcasts this message to all the agents and the orchestrator. The agents do not process the broadcasted message, the broadcasted message is processed only by the orchestrator. The orchestrator synthesizes a prompt to find out which agent can handle the use query, it also creates some instructions for the next agent. It then wraps the message in SendMessageEnvelope protocol and inserts into the message queue. The looping process retrieves the new message and sends to the recipient agent to get a response to the user query.
The Magentic-One is a open-source package and the code can be found here.