recent¶
The module provides core components for the ReCENT memory management Algorithm.
ReCENT Algorithm (Recursive Compressed Episodic Node Tree Algorithm) is an algorithm designed to address issues such as context explosion and goal drift, by employing a recursive memory compression mechanism to compress the memory when necessary.
This module provides an agentic automa and its corresponding memory and task configurations:
ReCentAutoma: The main automaton that implements the ReCENT algorithm.ReCentMemoryConfig: Configuration for ReCENT memory management.ObservationTaskConfig: Configuration for the observation task.ToolTaskConfig: Configuration for the tool selection task.AnswerTaskConfig: Configuration for the answer generation task.StopCondition: Stop condition configuration for ReCentAutoma.
The core data structures are:
EpisodicNodeTree: Tree of episodic nodes which is the core data structure of ReCENT.BaseEpisodicNode: Base class for all episodic nodes. It is inherited by:GoalEpisodicNode: A goal node that represents the goal of the agent.LeafEpisodicNode: A leaf node that represents a sequence of messages.CompressionEpisodicNode: A compression node that summarizes a sequence of episodic nodes.
ReCentAutoma ¶
Bases: GraphAutoma
ReCentAutoma is an automa that implements a ReAct-like process, leveraging the ReCENT memory algorithm to support stronger autonomous next-step planning, thus better achieving the pre-set goal.
This automa extends GraphAutoma to provide a memory-aware agentic automa that: - Maintains episodic memory with compression capabilities - Supports goal-oriented task execution - Dynamically creates tool workers based on LLM decisions - Manages memory compression to prevent context explosion
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm | BaseLlm | The LLM that serves as the default LLM for all tasks (if a dedicated LLM is not configured for a specific task). | required |
tools | Optional[List[Union[Callable, Automa, ToolSpec]]] | List of tools available to the automa. Can be functions, Automa instances, or ToolSpec instances. | None |
tools_builders | Optional[List[ToolSetBuilder]] | List of | None |
stop_condition | Optional[StopCondition] | Stop condition configuration. If None, uses default configuration: - max_iteration: -1 - max_consecutive_no_tool_selected: 3 | None |
memory_config | Optional[ReCentMemoryConfig] | Memory configuration for ReCent memory management. If None, a default config will be created using the provided llm. | None |
observation_task_config | Optional[ObservationTaskConfig] | Configuration for the observation task. If None, uses default config with the provided | None |
tool_task_config | Optional[ToolTaskConfig] | Configuration for the tool selection task. If None, uses default config with the provided | None |
answer_task_config | Optional[AnswerTaskConfig] | Configuration for the answer generation task. If None, uses default config with the provided | None |
name | Optional[str] | The name of the automa instance. | None |
thread_pool | Optional[ThreadPoolExecutor] | The thread pool for parallel execution of I/O-bound or CPU-bound tasks. | None |
running_options | Optional[RunningOptions] | The running options for the automa instance. | None |
Source code in bridgic/core/agentic/recent/_recent_automa.py
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 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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 | |
initialize_task_goal ¶
async Initialize the goal of the task and start the automa.
This worker is the entry point of the automa. It creates a goal node as the first episodic node in the memory sequence and optionally pushes initial user messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
goal | str | The task goal. | required |
guidance | Optional[str] | The guidance for achieving the task goal. | None |
Source code in bridgic/core/agentic/recent/_recent_automa.py
observe ¶
async observe(rtx=System('runtime_context'))
Observe the current state and determine if the goal has been achieved.
This worker builds context from memory, uses LLM (with StructuredOutput protocol) to determine if the goal has been achieved, and routes accordingly.
Source code in bridgic/core/agentic/recent/_recent_automa.py
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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | |
select_tools ¶
async select_tools(
rtx=System("runtime_context"),
*,
messages: List[Message],
tools: List[Tool]
) -> Tuple[List[ToolCall], Optional[str]]
Select tools using LLM's tool selection capability.
This method calls the LLM's aselect_tool method to select appropriate tools based on the conversation context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages | List[Message] | The conversation history and current context. | required |
tools | List[Tool] | Available tools that can be selected for use. | required |
Returns:
| Type | Description |
|---|---|
Tuple[List[ToolCall], Optional[str]] | A tuple containing: - List of selected tool calls with determined parameters - Optional response text from the LLM |
Source code in bridgic/core/agentic/recent/_recent_automa.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |
compress_memory ¶
async Compress memory if necessary.
Returns:
| Type | Description |
|---|---|
Optional[int] | The timestep of the compression node if compression is needed, otherwise None. |
Source code in bridgic/core/agentic/recent/_recent_automa.py
finalize_answer ¶
async Generate the final answer based on memory and goal using LLM.
This worker is the output node of the automa. It builds context from memory, calls LLM to generate a comprehensive final answer based on the goal and conversation history.
Returns:
| Type | Description |
|---|---|
str | The final answer string. |
Source code in bridgic/core/agentic/recent/_recent_automa.py
StopCondition ¶
Bases: BaseModel
Stop condition configuration for ReCentAutoma.
The different stop conditions below are combined with logic "or". In other words, the process will stop if any condition is met.
Attributes:
| Name | Type | Description |
|---|---|---|
max_iteration | int | Maximum number of times to enter the observe node before finalizing the answer. Defaults to -1 which means there is no limit to the number of iterations. |
max_consecutive_no_tool_selected | int | Maximum number of consecutive times no tool is selected before finalizing the answer. Defaults to 3. |
Source code in bridgic/core/agentic/recent/_recent_automa.py
ReCentMemoryConfig ¶
Bases: Serializable
This configuration class defines the memory management strategy that will compress the conversation history when certain conditions are met.
Attributes:
| Name | Type | Description |
|---|---|---|
llm | BaseLlm | The LLM instance used for memory compression operations. |
max_node_size | int | Maximum number of memory nodes before triggering compression. Defaults to 10. |
max_token_size | int | Maximum number of tokens before triggering compression. Defaults to 8192 (1024 * 8). |
system_template | str | Jinja2 prompt template for the system prompt used in memory compression, which accepts parameters: |
instruction_template | str | Jinja2 prompt template for the instruction prompt used in memory compression. |
token_count_callback | Optional[Callable[[str], int]] | Optional callback function to calculate token count from text. If None, defaults to |
Source code in bridgic/core/agentic/recent/_recent_memory_config.py
max_node_size instance-attribute ¶
Threshold for the number of memory nodes to trigger memory compression.
max_token_size instance-attribute ¶
Threshold for the number of tokens to trigger memory compression.
system_template instance-attribute ¶
system_template: EjinjaPromptTemplate = (
EjinjaPromptTemplate(system_template)
)
Template for system prompt used in memory compression.
instruction_template instance-attribute ¶
instruction_template: EjinjaPromptTemplate = (
EjinjaPromptTemplate(instruction_template)
)
Instruction prompt template used in memory compression.
token_count_callback instance-attribute ¶
token_count_callback: Callable[[str], int] = (
token_count_callback
if token_count_callback is not None
else estimate_token_count
)
Callback function to calculate token count from text. Defaults to estimate_token_count.
ObservationTaskConfig ¶
Configuration for the observation task in ReCentAutoma.
This class allows configuring the LLM and prompt templates for the observation task. When system_template or instruction_template is None, the default template will be used.
Attributes:
| Name | Type | Description |
|---|---|---|
llm | BaseLlm | The LLM instance to use for this task. |
system_template | Optional[Union[str, EjinjaPromptTemplate]] | System prompt template. If None, uses DEFAULT_OBSERVE_SYSTEM_TEMPLATE. |
instruction_template | Optional[Union[str, EjinjaPromptTemplate]] | Instruction prompt template. If None, uses DEFAULT_OBSERVE_INSTRUCTION_TEMPLATE. |
Source code in bridgic/core/agentic/recent/_recent_task_configs.py
ToolTaskConfig ¶
Configuration for the tool selection task in ReCentAutoma.
This class allows configuring the LLM and prompt templates for the tool selection task. When system_template or instruction_template is None, the default template will be used.
Attributes:
| Name | Type | Description |
|---|---|---|
llm | BaseLlm | The LLM instance to use for this task. |
system_template | Optional[Union[str, EjinjaPromptTemplate]] | System prompt template. If None, uses DEFAULT_TOOL_SELECTION_SYSTEM_TEMPLATE. |
instruction_template | Optional[Union[str, EjinjaPromptTemplate]] | Instruction prompt template. If None, uses DEFAULT_TOOL_SELECTION_INSTRUCTION_TEMPLATE. |
Source code in bridgic/core/agentic/recent/_recent_task_configs.py
AnswerTaskConfig ¶
Configuration for the answer generation task in ReCentAutoma.
This class allows configuring the LLM and prompt templates for the answer generation task. When system_template or instruction_template is None, the default template will be used.
Attributes:
| Name | Type | Description |
|---|---|---|
llm | BaseLlm | The LLM instance to use for this task. |
system_template | Optional[Union[str, EjinjaPromptTemplate]] | System prompt template. If None, uses DEFAULT_ANSWER_SYSTEM_TEMPLATE. |
instruction_template | Optional[Union[str, EjinjaPromptTemplate]] | Instruction prompt template. If None, uses DEFAULT_ANSWER_INSTRUCTION_TEMPLATE. |
Source code in bridgic/core/agentic/recent/_recent_task_configs.py
EpisodicNodeTree ¶
Bases: Serializable
EpisodicNodeTree is a data structure responsible for managing the sequence of episodic memory nodes, which is the core data structure of the ReCENT Algorithm.
ReCENT Algorithm (Recursive Compressed Episodic Node Tree Algorithm) is an algorithm designed to address issues such as context explosion and goal drift, by employing a recursive memory compression mechanism. In this algorithm, each episodic node will serve as a container of memory and could be tightly organized together to form a more efficient and reliable memory for the higher agentic system.
Notes:
- This data structure only supports appending new nodes; deletion or insertion is not allowed.
- All write operations are protected by a lock to ensure atomicity and preserve ordered nature of the structure.
- The data structure does not and should not perform any computationally expensive operations such as summarization.
Source code in bridgic/core/agentic/recent/_episodic_node_tree.py
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 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 | |
get_node ¶
get_node(timestep: int) -> Optional[BaseEpisodicNode]
Get a node by its timestep.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestep | int | The timestep of the node. | required |
Returns:
| Type | Description |
|---|---|
Optional[BaseEpisodicNode] | The node with the given timestep, or None if not found. |
Source code in bridgic/core/agentic/recent/_episodic_node_tree.py
get_goal_node ¶
get_goal_node() -> Optional[GoalEpisodicNode]
Get the current goal node.
Returns:
| Type | Description |
|---|---|
Optional[GoalEpisodicNode] | The current goal node, or None if no goal node exists. |
Source code in bridgic/core/agentic/recent/_episodic_node_tree.py
get_non_goal_nodes ¶
get_non_goal_nodes() -> List[BaseEpisodicNode]
Get all directly accessible non-goal nodes (sorted by timestep).
Returns:
| Type | Description |
|---|---|
List[BaseEpisodicNode] | List of non-goal nodes sorted by timestep. |
Source code in bridgic/core/agentic/recent/_episodic_node_tree.py
add_goal_node ¶
Add a new goal node.
If a previous goal node exists, its timestep will be linked in the new goal node. The tail appendable leaf node (if exists) will be closed before adding the new goal node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
goal | str | The goal content. | required |
guidance | Optional[str] | Optional execution guidance. | None |
Returns:
| Type | Description |
|---|---|
int | The timestep of the new goal node. |
Source code in bridgic/core/agentic/recent/_episodic_node_tree.py
add_leaf_node ¶
add_leaf_node(messages: List[Message]) -> int
Add a new leaf node that is appendable to new messages.
The tail appendable leaf node will be closed before adding the new node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages | List[Message] | The original message sequence. | required |
Returns:
| Type | Description |
|---|---|
int | The timestep of the new leaf node. |
Source code in bridgic/core/agentic/recent/_episodic_node_tree.py
add_compression_node ¶
Add a new compression node that summarizes the given non-goal nodes.
Before creating the compression node, close the last leaf node if it is still appendable. The compressed_timesteps list tells which nodes to summarize. Those nodes are then removed from the active list, and the new compression node replaces them in the active non-goal node list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compressed_timesteps | List[int] | List of timesteps of the compressed nodes. | required |
summary | Optional[str] | The compression summary content. If not provided, an unset concurrent.futures.Future of summary will be created. | None |
Returns:
| Type | Description |
|---|---|
int | The timestep of the new compression node. |
Source code in bridgic/core/agentic/recent/_episodic_node_tree.py
get_tail_appendable_leaf_node ¶
get_tail_appendable_leaf_node() -> (
Optional[LeafEpisodicNode]
)
Get the tail appendable leaf node if it exists.
Returns:
| Type | Description |
|---|---|
Optional[LeafEpisodicNode] | The tail appendable leaf node, or None if not found. |
Source code in bridgic/core/agentic/recent/_episodic_node_tree.py
BaseEpisodicNode ¶
Bases: Serializable, ABC
BaseEpisodicNode represents a single memory unit in the memory sequence in the ReCENT Algorithm.
Source code in bridgic/core/agentic/recent/_episodic_node.py
GoalEpisodicNode ¶
Bases: BaseEpisodicNode
Source code in bridgic/core/agentic/recent/_episodic_node.py
guidance instance-attribute ¶
The guidance to achieve the goal.
previous_goal_node_timestep instance-attribute ¶
previous_goal_node_timestep: int = (
previous_goal_node_timestep
if previous_goal_node_timestep is not None
else -1
)
The timestep of the previous goal node (the goal node that was replaced by this one).
LeafEpisodicNode ¶
Bases: BaseEpisodicNode
Source code in bridgic/core/agentic/recent/_episodic_node.py
CompressionEpisodicNode ¶
Bases: BaseEpisodicNode
Compression node that compresses a sequence of episodic nodes.
A compression node is created to summarize and compress multiple episodic nodes. The compression node contains a summary of the compressed nodes and records their timesteps.
Source code in bridgic/core/agentic/recent/_episodic_node.py
summary instance-attribute ¶
The summary of the compression node, which is a concurrent.futures.Future for cross-thread/event-loop dependency handling.
compressed_node_timesteps instance-attribute ¶
compressed_node_timesteps: List[int] = (
compressed_timesteps
if compressed_timesteps is not None
else []
)
The timesteps of the compressed nodes (the nodes that were compressed by this compression node).