Level 2 - Elemental Agents and Events
This layer is about building Agents and the protocol by which a system of Agents can accomplish work.
This layer is in flux! It will change frequently right now.
Simple Agents
Simple agents provide straightforward, synchronous approaches to problem-solving using LLMs.
-
IterativeProblemSolver: An agent that solves problems iteratively by applying a Local Language Model (LLM) to a problem until a solution is found or a maximum number of iterations is reached.
-
SimpleRecursiveAgent: An agent that solves problems recursively by applying an LLM to a problem until a solution is found or a maximum number of iterations is reached.
API Reference
mojentic.agents.IterativeProblemSolver
An agent that iteratively attempts to solve a problem using available tools.
This solver uses a chat-based approach to break down and solve complex problems. It will continue attempting to solve the problem until it either succeeds, fails explicitly, or reaches the maximum number of iterations.
Attributes:
Name | Type | Description |
---|---|---|
max_iterations |
int
|
Maximum number of attempts to solve the problem |
chat |
ChatSession
|
The chat session used for problem-solving interaction |
Source code in src/mojentic/agents/iterative_problem_solver.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
|
__init__(llm, available_tools=None, max_iterations=3, system_prompt=None)
Initialize the IterativeProblemSolver.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
llm
|
LLMBroker
|
The language model broker to use for generating responses |
required |
available_tools
|
Optional[List[LLMTool]]
|
List of tools that can be used to solve the problem, by default None |
None
|
max_iterations
|
int
|
Maximum number of attempts to solve the problem, by default 3 |
3
|
Source code in src/mojentic/agents/iterative_problem_solver.py
solve(problem)
Execute the problem-solving process.
This method runs the iterative problem-solving process, continuing until one of these conditions is met: - The task is completed successfully ("DONE") - The task fails explicitly ("FAIL") - The maximum number of iterations is reached
Parameters:
Name | Type | Description | Default |
---|---|---|---|
problem
|
str
|
The problem or request to be solved |
required |
Returns:
Type | Description |
---|---|
str
|
A summary of the final result, excluding the process details |
Source code in src/mojentic/agents/iterative_problem_solver.py
mojentic.agents.SimpleRecursiveAgent
An agent that recursively attempts to solve a problem using available tools.
This agent uses an event-driven approach to manage the problem-solving process. It will continue attempting to solve the problem until it either succeeds, fails explicitly, or reaches the maximum number of recursions.
Attributes:
Name | Type | Description |
---|---|---|
max_iterations |
int
|
The maximum number of iterations to perform |
llm |
LLMBroker
|
The language model broker to use for generating responses |
emitter |
EventEmitter
|
The pubsub event emitter used to manage events |
available_tools |
List[LLMTool]
|
List of tools that can be used to solve the problem |
chat |
ChatSession
|
The chat session used for problem-solving interaction |
Source code in src/mojentic/agents/simple_recursive_agent.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
|
__init__(llm, available_tools=None, max_iterations=5, system_prompt=None)
Initialize the SimpleRecursiveAgent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
llm
|
LLMBroker
|
The language model broker to use for generating responses |
required |
max_iterations
|
int
|
The maximum number of iterations to perform |
5
|
available_tools
|
Optional[List[LLMTool]]
|
List of tools that can be used to solve the problem |
None
|
Source code in src/mojentic/agents/simple_recursive_agent.py
solve(problem)
async
Solve a problem asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
problem
|
str
|
The problem to solve |
required |
Returns:
Type | Description |
---|---|
str
|
The solution to the problem |
Source code in src/mojentic/agents/simple_recursive_agent.py
Event Driven Agents
Event-driven agents provide asynchronous, event-based architectures for building complex agent systems that can process multiple events concurrently.
-
BaseAsyncAgent: The foundation for all asynchronous agents, providing the core event processing interface.
-
BaseAsyncLLMAgent: An asynchronous agent that integrates LLM capabilities for generating responses asynchronously.
-
AsyncAggregatorAgent: A specialized agent that collects and aggregates multiple events by correlation ID before processing them together.
-
AsyncDispatcher: The core dispatcher that manages asynchronous execution of tasks and coordinates event routing between agents.
API Reference
mojentic.agents.base_async_agent.BaseAsyncAgent
BaseAsyncAgent class is the base class for all asynchronous agents. It provides an async receive method for event processing.
Source code in src/mojentic/agents/base_async_agent.py
receive_event_async(event)
async
receive_event_async is the method that all async agents must implement. It takes an event as input and returns a list of events as output.
In this way, you can perform work based on the event, and generate whatever subsequent events may need to be processed next.
This keeps the agent decoupled from the specifics of the event routing and processing.
Events are subclasses of the Event class.
:param event: The event to process :return: A list of events to be processed next
Source code in src/mojentic/agents/base_async_agent.py
mojentic.agents.async_llm_agent.BaseAsyncLLMAgent
Bases: BaseAsyncAgent
BaseAsyncLLMAgent is an asynchronous version of the BaseLLMAgent. It uses an LLM to generate responses asynchronously.
Source code in src/mojentic/agents/async_llm_agent.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
|
__init__(llm, behaviour='You are a helpful assistant.', tools=None, response_model=None)
Initialize the BaseAsyncLLMAgent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
llm
|
LLMBroker
|
The LLM broker to use for generating responses |
required |
behaviour
|
str
|
The personality and behavioural traits of the agent |
'You are a helpful assistant.'
|
tools
|
List[LLMTool]
|
The tools available to the agent |
None
|
response_model
|
Type[BaseModel]
|
The model to use for responses |
None
|
Source code in src/mojentic/agents/async_llm_agent.py
add_tool(tool)
Add a tool to the agent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool
|
LLMTool
|
The tool to add |
required |
generate_response(content)
async
Generate a response using the LLM asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content
|
str
|
The content to generate a response for |
required |
Returns:
Type | Description |
---|---|
str or BaseModel
|
The generated response |
Source code in src/mojentic/agents/async_llm_agent.py
receive_event_async(event)
async
Receive an event and process it asynchronously. This method should be overridden by subclasses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event
|
Event
|
The event to process |
required |
Returns:
Type | Description |
---|---|
List[Event]
|
The events to be processed next |
Source code in src/mojentic/agents/async_llm_agent.py
mojentic.agents.async_aggregator_agent.AsyncAggregatorAgent
Bases: BaseAsyncAgent
AsyncAggregatorAgent is an asynchronous version of the BaseAggregatingAgent. It aggregates events based on their correlation_id and processes them when all required events are available.
Source code in src/mojentic/agents/async_aggregator_agent.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
__init__(event_types_needed=None)
Initialize the AsyncAggregatorAgent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event_types_needed
|
list
|
List of event types that need to be captured before processing |
None
|
Source code in src/mojentic/agents/async_aggregator_agent.py
process_events(events)
async
receive_event_async(event)
async
Receive an event and process it if all needed events are available.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event
|
Event
|
The event to process |
required |
Returns:
Type | Description |
---|---|
list
|
The events to be processed next |
Source code in src/mojentic/agents/async_aggregator_agent.py
wait_for_events(correlation_id, timeout=None)
async
Wait for all needed events for a specific correlation_id.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
correlation_id
|
str
|
The correlation_id to wait for |
required |
timeout
|
float
|
The timeout in seconds |
None
|
Returns:
Type | Description |
---|---|
list
|
The events for the correlation_id |
Source code in src/mojentic/agents/async_aggregator_agent.py
mojentic.async_dispatcher.AsyncDispatcher
AsyncDispatcher class is an asynchronous version of the Dispatcher class. It uses asyncio and deque for event processing.
Source code in src/mojentic/async_dispatcher.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
|
__init__(router, shared_working_memory=None, batch_size=5, tracer=None)
Initialize the AsyncDispatcher.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
router
|
Router
|
The router to use for routing events to agents |
required |
shared_working_memory
|
SharedWorkingMemory
|
The shared working memory to use |
None
|
batch_size
|
int
|
The number of events to process in each batch |
5
|
tracer
|
Tracer
|
The tracer to use for tracing events |
None
|
Source code in src/mojentic/async_dispatcher.py
dispatch(event)
Dispatch an event to the event queue.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event
|
Event
|
The event to dispatch |
required |
Source code in src/mojentic/async_dispatcher.py
start()
async
stop()
async
wait_for_empty_queue(timeout=None)
async
Wait for the event queue to be empty.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout
|
float
|
The timeout in seconds |
None
|
Returns:
Type | Description |
---|---|
bool
|
True if the queue is empty, False if the timeout was reached |