Create a Loop. Args: name: Loop name. max_iterations: Maximum iterations before forced termination. model: Optional model used by controller termination prompt. terminate_condition_prompt: Optional natural-language terminate condition.
(self,
name:str,
max_iterations:int=10,
model:Model|None=None,
terminate_condition_prompt:str|None=None,
terminate_condition_function:Callable|None=None,
pull_keys:dict[str,dict|str]|None=None,
push_keys:dict[str,dict|str]|None=None,
attributes:dict[str,object] | None = None,
initial_messages:dict[str,object] | None = None,
edges: list[tuple[str, str] | tuple[str, str, dict[str, dict|str] ]]|None=None,
nodes:list[tuple]|None=None,
build_func:Callable|None = None,
)
| 32 | internal nodes should feed back by `edge_to_controller`, not by direct inner cycles. |
| 33 | """ |
| 34 | def __init__(self, |
| 35 | name:str, |
| 36 | max_iterations:int=10, |
| 37 | model:Model|None=None, |
| 38 | terminate_condition_prompt:str|None=None, |
| 39 | terminate_condition_function:Callable|None=None, |
| 40 | pull_keys:dict[str,dict|str]|None=None, |
| 41 | push_keys:dict[str,dict|str]|None=None, |
| 42 | attributes:dict[str,object] | None = None, |
| 43 | initial_messages:dict[str,object] | None = None, |
| 44 | edges: list[tuple[str, str] | tuple[str, str, dict[str, dict|str] ]]|None=None, |
| 45 | nodes:list[tuple]|None=None, |
| 46 | build_func:Callable|None = None, |
| 47 | ): |
| 48 | """Create a Loop. |
| 49 | |
| 50 | Args: |
| 51 | name: Loop name. |
| 52 | max_iterations: Maximum iterations before forced termination. |
| 53 | model: Optional model used by controller termination prompt. |
| 54 | terminate_condition_prompt: Optional natural-language terminate condition. |
| 55 | terminate_condition_function: Optional callable terminate condition. Supported signatures are: |
| 56 | - `(input) -> terminate` |
| 57 | - `(input, attributes) -> terminate` |
| 58 | - `(input, attributes, controller) -> terminate` |
| 59 | - `(input, attributes, controller, memories) -> terminate` |
| 60 | - `(input, attributes, controller, memories, tools) -> terminate` |
| 61 | - `(input, attributes, controller, memories, tools, retrievers) -> terminate` |
| 62 | |
| 63 | Where: |
| 64 | - `input`: `dict[str, object]` |
| 65 | - `attributes`: `dict[str, object]` (loop-local attribute store) |
| 66 | - `controller`: internal controller node instance |
| 67 | - `memories`: list of memories attached to the controller (may be empty) |
| 68 | - `tools`: list of tools attached to the controller (may be empty) |
| 69 | - `retrievers`: list of retrievers attached to the controller (may be empty) |
| 70 | - `terminate`: `bool` |
| 71 | pull_keys: Keys pulled from outer attributes. |
| 72 | push_keys: Keys pushed to outer attributes. |
| 73 | attributes: Initial loop attributes. |
| 74 | initial_messages: Initial cached controller messages. |
| 75 | edges: Optional declarative edge definitions for inner nodes. |
| 76 | nodes: Optional declarative node definitions for inner nodes as `(name, NodeTemplate)` entries. |
| 77 | build_func: Optional custom build function. |
| 78 | """ |
| 79 | self._init_nodes = nodes |
| 80 | self._init_edges = edges |
| 81 | attributes = {} if attributes is None else dict(attributes) |
| 82 | # Copy to avoid sharing mutable defaults and to avoid mutating caller-provided dicts. |
| 83 | initial_messages = {} if initial_messages is None else dict(initial_messages) |
| 84 | initial_messages_template = initial_messages.copy() |
| 85 | |
| 86 | # By default, expose loop iteration counters to the outer environment. |
| 87 | if push_keys is None: |
| 88 | push_keys = { |
| 89 | 'current_iteration': 'Current iteration of the loop.', |
| 90 | 'max_iterations': 'Maximum iterations of the loop.', |
| 91 | } |
no test coverage detected