lionagi 0.0.316__py3-none-any.whl → 0.1.1__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
Files changed (157) hide show
  1. lionagi/core/__init__.py +19 -8
  2. lionagi/core/agent/__init__.py +0 -3
  3. lionagi/core/agent/base_agent.py +25 -30
  4. lionagi/core/branch/__init__.py +0 -4
  5. lionagi/core/branch/{base_branch.py → base.py} +12 -13
  6. lionagi/core/branch/branch.py +22 -19
  7. lionagi/core/branch/executable_branch.py +0 -347
  8. lionagi/core/branch/{branch_flow_mixin.py → flow_mixin.py} +5 -5
  9. lionagi/core/direct/__init__.py +10 -1
  10. lionagi/core/direct/cot.py +61 -26
  11. lionagi/core/direct/plan.py +10 -8
  12. lionagi/core/direct/predict.py +5 -5
  13. lionagi/core/direct/react.py +8 -8
  14. lionagi/core/direct/score.py +4 -4
  15. lionagi/core/direct/select.py +4 -4
  16. lionagi/core/direct/utils.py +7 -4
  17. lionagi/core/direct/vote.py +2 -2
  18. lionagi/core/execute/base_executor.py +47 -0
  19. lionagi/core/execute/branch_executor.py +296 -0
  20. lionagi/core/execute/instruction_map_executor.py +179 -0
  21. lionagi/core/execute/neo4j_executor.py +381 -0
  22. lionagi/core/execute/structure_executor.py +314 -0
  23. lionagi/core/flow/monoflow/ReAct.py +20 -20
  24. lionagi/core/flow/monoflow/chat.py +6 -6
  25. lionagi/core/flow/monoflow/chat_mixin.py +23 -33
  26. lionagi/core/flow/monoflow/followup.py +14 -15
  27. lionagi/core/flow/polyflow/chat.py +15 -12
  28. lionagi/core/{prompt/action_template.py → form/action_form.py} +2 -2
  29. lionagi/core/{prompt → form}/field_validator.py +40 -31
  30. lionagi/core/form/form.py +302 -0
  31. lionagi/core/form/mixin.py +214 -0
  32. lionagi/core/{prompt/scored_template.py → form/scored_form.py} +2 -2
  33. lionagi/core/generic/__init__.py +37 -0
  34. lionagi/core/generic/action.py +26 -0
  35. lionagi/core/generic/component.py +455 -0
  36. lionagi/core/generic/condition.py +44 -0
  37. lionagi/core/generic/data_logger.py +305 -0
  38. lionagi/core/generic/edge.py +162 -0
  39. lionagi/core/generic/mail.py +90 -0
  40. lionagi/core/generic/mailbox.py +36 -0
  41. lionagi/core/generic/node.py +285 -0
  42. lionagi/core/generic/relation.py +70 -0
  43. lionagi/core/generic/signal.py +22 -0
  44. lionagi/core/generic/structure.py +362 -0
  45. lionagi/core/generic/transfer.py +20 -0
  46. lionagi/core/generic/work.py +40 -0
  47. lionagi/core/graph/graph.py +126 -0
  48. lionagi/core/graph/tree.py +190 -0
  49. lionagi/core/mail/__init__.py +0 -8
  50. lionagi/core/mail/mail_manager.py +15 -12
  51. lionagi/core/mail/schema.py +9 -2
  52. lionagi/core/messages/__init__.py +0 -3
  53. lionagi/core/messages/schema.py +17 -225
  54. lionagi/core/session/__init__.py +0 -3
  55. lionagi/core/session/session.py +24 -22
  56. lionagi/core/tool/__init__.py +3 -1
  57. lionagi/core/tool/tool.py +28 -0
  58. lionagi/core/tool/tool_manager.py +75 -75
  59. lionagi/experimental/directive/evaluator/__init__.py +0 -0
  60. lionagi/experimental/directive/evaluator/ast_evaluator.py +115 -0
  61. lionagi/experimental/directive/evaluator/base_evaluator.py +202 -0
  62. lionagi/experimental/directive/evaluator/sandbox_.py +14 -0
  63. lionagi/experimental/directive/evaluator/script_engine.py +83 -0
  64. lionagi/experimental/directive/parser/__init__.py +0 -0
  65. lionagi/experimental/directive/parser/base_parser.py +215 -0
  66. lionagi/experimental/directive/schema.py +36 -0
  67. lionagi/experimental/directive/template_/__init__.py +0 -0
  68. lionagi/experimental/directive/template_/base_template.py +63 -0
  69. lionagi/experimental/tool/__init__.py +0 -0
  70. lionagi/experimental/tool/function_calling.py +43 -0
  71. lionagi/experimental/tool/manual.py +66 -0
  72. lionagi/experimental/tool/schema.py +59 -0
  73. lionagi/experimental/tool/tool_manager.py +138 -0
  74. lionagi/experimental/tool/util.py +16 -0
  75. lionagi/experimental/work/__init__.py +0 -0
  76. lionagi/experimental/work/_logger.py +25 -0
  77. lionagi/experimental/work/exchange.py +0 -0
  78. lionagi/experimental/work/schema.py +30 -0
  79. lionagi/experimental/work/tests.py +72 -0
  80. lionagi/experimental/work/util.py +0 -0
  81. lionagi/experimental/work/work_function.py +89 -0
  82. lionagi/experimental/work/worker.py +12 -0
  83. lionagi/integrations/bridge/autogen_/__init__.py +0 -0
  84. lionagi/integrations/bridge/autogen_/autogen_.py +124 -0
  85. lionagi/integrations/bridge/llamaindex_/get_index.py +294 -0
  86. lionagi/integrations/bridge/llamaindex_/llama_pack.py +227 -0
  87. lionagi/integrations/bridge/transformers_/__init__.py +0 -0
  88. lionagi/integrations/bridge/transformers_/install_.py +36 -0
  89. lionagi/integrations/chunker/chunk.py +7 -7
  90. lionagi/integrations/config/oai_configs.py +5 -5
  91. lionagi/integrations/config/ollama_configs.py +1 -1
  92. lionagi/integrations/config/openrouter_configs.py +1 -1
  93. lionagi/integrations/loader/load.py +6 -6
  94. lionagi/integrations/loader/load_util.py +8 -8
  95. lionagi/integrations/storage/__init__.py +3 -0
  96. lionagi/integrations/storage/neo4j.py +673 -0
  97. lionagi/integrations/storage/storage_util.py +289 -0
  98. lionagi/integrations/storage/to_csv.py +63 -0
  99. lionagi/integrations/storage/to_excel.py +67 -0
  100. lionagi/libs/ln_api.py +3 -3
  101. lionagi/libs/ln_knowledge_graph.py +405 -0
  102. lionagi/libs/ln_parse.py +43 -6
  103. lionagi/libs/ln_queue.py +101 -0
  104. lionagi/libs/ln_tokenizer.py +57 -0
  105. lionagi/libs/ln_validate.py +288 -0
  106. lionagi/libs/sys_util.py +29 -7
  107. lionagi/lions/__init__.py +0 -0
  108. lionagi/lions/coder/__init__.py +0 -0
  109. lionagi/lions/coder/add_feature.py +20 -0
  110. lionagi/lions/coder/base_prompts.py +22 -0
  111. lionagi/lions/coder/coder.py +121 -0
  112. lionagi/lions/coder/util.py +91 -0
  113. lionagi/lions/researcher/__init__.py +0 -0
  114. lionagi/lions/researcher/data_source/__init__.py +0 -0
  115. lionagi/lions/researcher/data_source/finhub_.py +191 -0
  116. lionagi/lions/researcher/data_source/google_.py +199 -0
  117. lionagi/lions/researcher/data_source/wiki_.py +96 -0
  118. lionagi/lions/researcher/data_source/yfinance_.py +21 -0
  119. lionagi/tests/integrations/__init__.py +0 -0
  120. lionagi/tests/libs/__init__.py +0 -0
  121. lionagi/tests/libs/test_async.py +0 -0
  122. lionagi/tests/libs/test_field_validators.py +353 -0
  123. lionagi/tests/libs/test_queue.py +67 -0
  124. lionagi/tests/test_core/test_base_branch.py +0 -1
  125. lionagi/tests/test_core/test_branch.py +2 -0
  126. lionagi/tests/test_core/test_session_base_util.py +1 -0
  127. lionagi/version.py +1 -1
  128. {lionagi-0.0.316.dist-info → lionagi-0.1.1.dist-info}/METADATA +1 -1
  129. lionagi-0.1.1.dist-info/RECORD +190 -0
  130. lionagi/core/prompt/prompt_template.py +0 -312
  131. lionagi/core/schema/__init__.py +0 -22
  132. lionagi/core/schema/action_node.py +0 -29
  133. lionagi/core/schema/base_mixin.py +0 -296
  134. lionagi/core/schema/base_node.py +0 -199
  135. lionagi/core/schema/condition.py +0 -24
  136. lionagi/core/schema/data_logger.py +0 -354
  137. lionagi/core/schema/data_node.py +0 -93
  138. lionagi/core/schema/prompt_template.py +0 -67
  139. lionagi/core/schema/structure.py +0 -912
  140. lionagi/core/tool/manual.py +0 -1
  141. lionagi-0.0.316.dist-info/RECORD +0 -121
  142. /lionagi/core/{branch/base → execute}/__init__.py +0 -0
  143. /lionagi/core/flow/{base/baseflow.py → baseflow.py} +0 -0
  144. /lionagi/core/flow/{base/__init__.py → mono_chat_mixin.py} +0 -0
  145. /lionagi/core/{prompt → form}/__init__.py +0 -0
  146. /lionagi/{tests/test_integrations → core/graph}/__init__.py +0 -0
  147. /lionagi/{tests/test_libs → experimental}/__init__.py +0 -0
  148. /lionagi/{tests/test_libs/test_async.py → experimental/directive/__init__.py} +0 -0
  149. /lionagi/tests/{test_libs → libs}/test_api.py +0 -0
  150. /lionagi/tests/{test_libs → libs}/test_convert.py +0 -0
  151. /lionagi/tests/{test_libs → libs}/test_func_call.py +0 -0
  152. /lionagi/tests/{test_libs → libs}/test_nested.py +0 -0
  153. /lionagi/tests/{test_libs → libs}/test_parse.py +0 -0
  154. /lionagi/tests/{test_libs → libs}/test_sys_util.py +0 -0
  155. {lionagi-0.0.316.dist-info → lionagi-0.1.1.dist-info}/LICENSE +0 -0
  156. {lionagi-0.0.316.dist-info → lionagi-0.1.1.dist-info}/WHEEL +0 -0
  157. {lionagi-0.0.316.dist-info → lionagi-0.1.1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,296 @@
1
+ import contextlib
2
+ from lionagi.libs import convert, AsyncUtil, ParseUtil
3
+ from lionagi.core.generic import ActionNode, Edge
4
+ from lionagi.core.mail.schema import BaseMail
5
+ from lionagi.core.messages.schema import System, Instruction
6
+
7
+ from lionagi.core.branch.branch import Branch
8
+ from lionagi.core.execute.base_executor import BaseExecutor
9
+
10
+
11
+ class BranchExecutor(Branch, BaseExecutor):
12
+
13
+ async def forward(self) -> None:
14
+ """
15
+ Forwards the execution by processing all pending incoming mails in each branch. Depending on the category of the mail,
16
+ it processes starts, nodes, node lists, conditions, or ends, accordingly executing different functions.
17
+ """
18
+ for key in list(self.pending_ins.keys()):
19
+ while self.pending_ins[key]:
20
+ mail = self.pending_ins[key].popleft()
21
+ if mail.category == "start":
22
+ self._process_start(mail)
23
+ elif mail.category == "node":
24
+ await self._process_node(mail)
25
+ elif mail.category == "node_list":
26
+ self._process_node_list(mail)
27
+ elif mail.category == "condition":
28
+ self._process_condition(mail)
29
+ elif mail.category == "end":
30
+ self._process_end(mail)
31
+
32
+ async def execute(self, refresh_time=1) -> None:
33
+ """
34
+ Executes the forward process repeatedly at specified time intervals until execution is instructed to stop.
35
+
36
+ Args:
37
+ refresh_time (int): The interval, in seconds, at which the forward method is called repeatedly.
38
+ """
39
+ while not self.execute_stop:
40
+ await self.forward()
41
+ await AsyncUtil.sleep(refresh_time)
42
+
43
+ async def _process_node(self, mail: BaseMail):
44
+ """
45
+ Processes a single node based on the node type specified in the mail's package. It handles different types of nodes such as System,
46
+ Instruction, ActionNode, and generic nodes through separate processes.
47
+
48
+ Args:
49
+ mail (BaseMail): The mail containing the node to be processed along with associated details.
50
+
51
+ Raises:
52
+ ValueError: If an invalid mail is encountered or the process encounters errors.
53
+ """
54
+ if isinstance(mail.package["package"], System):
55
+ self._system_process(mail.package["package"], verbose=self.verbose)
56
+ self.send(
57
+ mail.sender_id,
58
+ "node_id",
59
+ {"request_source": self.id_, "package": mail.package["package"].id_},
60
+ )
61
+
62
+ elif isinstance(mail.package["package"], Instruction):
63
+ await self._instruction_process(
64
+ mail.package["package"], verbose=self.verbose
65
+ )
66
+ self.send(
67
+ mail.sender_id,
68
+ "node_id",
69
+ {"request_source": self.id_, "package": mail.package["package"].id_},
70
+ )
71
+
72
+ elif isinstance(mail.package["package"], ActionNode):
73
+ await self._action_process(mail.package["package"], verbose=self.verbose)
74
+ self.send(
75
+ mail.sender_id,
76
+ "node_id",
77
+ {
78
+ "request_source": self.id_,
79
+ "package": mail.package["package"].instruction.id_,
80
+ },
81
+ )
82
+ else:
83
+ try:
84
+ await self._agent_process(mail.package["package"], verbose=self.verbose)
85
+ self.send(
86
+ mail.sender_id,
87
+ "node_id",
88
+ {
89
+ "request_source": self.id_,
90
+ "package": mail.package["package"].id_,
91
+ },
92
+ )
93
+ except:
94
+ raise ValueError(f"Invalid mail to process. Mail:{mail}")
95
+
96
+ def _process_node_list(self, mail: BaseMail):
97
+ """
98
+ Processes a list of nodes provided in the mail, but currently only sends an end signal as multiple path selection is not supported.
99
+
100
+ Args:
101
+ mail (BaseMail): The mail containing a list of nodes to be processed.
102
+
103
+ Raises:
104
+ ValueError: When trying to process multiple paths which is currently unsupported.
105
+ """
106
+ self.send(mail.sender_id, "end", {"request_source": self.id_, "package": "end"})
107
+ self.execute_stop = True
108
+ raise ValueError("Multiple path selection is currently not supported")
109
+
110
+ def _process_condition(self, mail: BaseMail):
111
+ """
112
+ Processes a condition associated with an edge based on the mail's package, setting up the result of the condition check.
113
+
114
+ Args:
115
+ mail (BaseMail): The mail containing the condition to be processed.
116
+ """
117
+ relationship: Edge = mail.package["package"]
118
+ check_result = relationship.condition(self)
119
+ back_mail = {
120
+ "from": self.id_,
121
+ "edge_id": mail.package["package"].id_,
122
+ "check_result": check_result,
123
+ }
124
+ self.send(
125
+ mail.sender_id,
126
+ "condition",
127
+ {"request_source": self.id_, "package": back_mail},
128
+ )
129
+
130
+ def _system_process(self, system: System, verbose=True, context_verbose=False):
131
+ """
132
+ Processes a system node, possibly displaying its content and context if verbose is enabled.
133
+
134
+ Args:
135
+ system (System): The system node to process.
136
+ verbose (bool): Flag to enable verbose output.
137
+ context_verbose (bool): Flag to enable verbose output specifically for context.
138
+ """
139
+ from lionagi.libs import SysUtil
140
+
141
+ SysUtil.check_import("IPython")
142
+ from IPython.display import Markdown, display
143
+
144
+ if verbose:
145
+ print(f"------------------Welcome: {system.sender}--------------------")
146
+ with contextlib.suppress(Exception):
147
+ system.content = ParseUtil.fuzzy_parse_json(system.content)
148
+ display(Markdown(f"system: {convert.to_str(system.system_info)}"))
149
+ if self.context and context_verbose:
150
+ display(Markdown(f"context: {convert.to_str(self.context)}"))
151
+
152
+ self.add_message(system=system)
153
+
154
+ async def _instruction_process(
155
+ self, instruction: Instruction, verbose=True, **kwargs
156
+ ):
157
+ """
158
+ Processes an instruction node, possibly displaying its content if verbose is enabled, and handling any additional keyword arguments.
159
+
160
+ Args:
161
+ instruction (Instruction): The instruction node to process.
162
+ verbose (bool): Flag to enable verbose output.
163
+ **kwargs: Additional keyword arguments that might affect how instructions are processed.
164
+ """
165
+ from lionagi.libs import SysUtil
166
+
167
+ SysUtil.check_import("IPython")
168
+ from IPython.display import Markdown, display
169
+
170
+ if verbose:
171
+ with contextlib.suppress(Exception):
172
+ instruction.content = ParseUtil.fuzzy_parse_json(instruction.content)
173
+ display(
174
+ Markdown(
175
+ f"{instruction.sender}: {convert.to_str(instruction.instruct)}"
176
+ )
177
+ )
178
+
179
+ if self.context:
180
+ instruction.content.update({"context": self.context})
181
+ self.context_log.append(self.context)
182
+ self.context = None
183
+
184
+ result = await self.chat(instruction, **kwargs)
185
+ with contextlib.suppress(Exception):
186
+ result = ParseUtil.fuzzy_parse_json(result)
187
+ if "response" in result.keys():
188
+ result = result["response"]
189
+ if verbose and len(self.assistant_responses) != 0:
190
+ display(
191
+ Markdown(
192
+ f"{self.last_assistant_response.sender}: {convert.to_str(result)}"
193
+ )
194
+ )
195
+ print("-----------------------------------------------------")
196
+
197
+ self.execution_responses.append(result)
198
+
199
+ async def _action_process(self, action: ActionNode, verbose=True):
200
+ """
201
+ Processes an action node, executing the defined action along with any tools specified within the node.
202
+
203
+ Args:
204
+ action (ActionNode): The action node to process.
205
+ verbose (bool): Flag to enable verbose output of the action results.
206
+ """
207
+ from lionagi.libs import SysUtil
208
+
209
+ SysUtil.check_import("IPython")
210
+ from IPython.display import Markdown, display
211
+
212
+ try:
213
+ func = getattr(self, action.action)
214
+ except:
215
+ raise ValueError(f"{action.action} is not a valid action")
216
+
217
+ if verbose:
218
+ display(
219
+ Markdown(
220
+ f"{action.instruction.sender}: {convert.to_str(action.instruction.instruct)}"
221
+ )
222
+ )
223
+
224
+ if action.tools:
225
+ self.register_tools(action.tools)
226
+ if self.context:
227
+ result = await func(
228
+ action.instruction.content["instruction"],
229
+ context=self.context,
230
+ tools=action.tools,
231
+ **action.action_kwargs,
232
+ )
233
+ self.context = None
234
+ else:
235
+ result = await func(
236
+ action.instruction.content, tools=action.tools, **action.action_kwargs
237
+ )
238
+
239
+ if verbose and len(self.assistant_responses) != 0:
240
+ display(
241
+ Markdown(
242
+ f"{self.last_assistant_response.sender}: {convert.to_str(result)}"
243
+ )
244
+ )
245
+ print("-----------------------------------------------------")
246
+
247
+ self.execution_responses.append(result)
248
+
249
+ async def _agent_process(self, agent, verbose=True):
250
+ """
251
+ Processes an agent.
252
+
253
+ Args:
254
+ agent: The agent to process.
255
+ verbose (bool): A flag indicating whether to provide verbose output (default: True).
256
+ """
257
+ context = list(self.messages["content"])
258
+ if verbose:
259
+ print("*****************************************************")
260
+ result = await agent.execute(context)
261
+
262
+ if verbose:
263
+ print("*****************************************************")
264
+
265
+ from pandas import DataFrame
266
+
267
+ if isinstance(result, DataFrame):
268
+ self.context = list(result["content"])
269
+ else:
270
+ self.context = result
271
+ self.execution_responses.append(result)
272
+
273
+ def _process_start(self, mail):
274
+ """
275
+ Processes a start mail.
276
+
277
+ Args:
278
+ mail (BaseMail): The start mail to process.
279
+ """
280
+ start_mail_content = mail.package
281
+ self.context = start_mail_content["context"]
282
+ self.send(
283
+ start_mail_content["structure_id"],
284
+ "start",
285
+ {"request_source": self.id_, "package": "start"},
286
+ )
287
+
288
+ def _process_end(self, mail: BaseMail):
289
+ """
290
+ Processes an end mail.
291
+
292
+ Args:
293
+ mail (BaseMail): The end mail to process.
294
+ """
295
+ self.execute_stop = True
296
+ self.send(mail.sender_id, "end", {"request_source": self.id_, "package": "end"})
@@ -0,0 +1,179 @@
1
+ import asyncio
2
+ from pydantic import Field
3
+
4
+ from lionagi.libs import AsyncUtil
5
+
6
+ from lionagi.core.mail.schema import BaseMail, MailTransfer
7
+ from lionagi.core.mail.mail_manager import MailManager
8
+ from lionagi.core.execute.base_executor import BaseExecutor
9
+ from lionagi.core.execute.branch_executor import BranchExecutor
10
+
11
+
12
+ class InstructionMapExecutor(BaseExecutor):
13
+ """
14
+ Manages the execution of a mapped set of instructions across multiple branches within an executable structure.
15
+
16
+ Attributes:
17
+ branches (dict[str, BranchExecutor]): A dictionary of branch executors managing individual instruction flows.
18
+ structure_id (str): The identifier for the structure within which these branches operate.
19
+ mail_transfer (MailTransfer): Handles the transfer of mail between branches and other components.
20
+ branch_kwargs (dict): Keyword arguments used for initializing branches.
21
+ num_end_branches (int): Tracks the number of branches that have completed execution.
22
+ mail_manager (MailManager): Manages the distribution and collection of mails across branches.
23
+ """
24
+
25
+ branches: dict[str, BranchExecutor] = Field(
26
+ default_factory=dict, description="The branches of the instruction mapping."
27
+ )
28
+ structure_id: str = Field("", description="The ID of the executable structure.")
29
+ mail_transfer: MailTransfer = Field(
30
+ default_factory=MailTransfer, description="The mail transfer."
31
+ )
32
+ branch_kwargs: dict = Field(
33
+ default_factory=dict,
34
+ description="The keyword arguments for the initializing the branches.",
35
+ )
36
+ num_end_branches: int = Field(0, description="The number of end branches.")
37
+ mail_manager: MailManager = Field(
38
+ default_factory=MailManager, description="The mail manager."
39
+ )
40
+
41
+ def __init__(self, **kwargs):
42
+ """
43
+ Initializes an InstructionMapExecutor with the given parameters.
44
+
45
+ Args:
46
+ **kwargs: Arbitrary keyword arguments passed to the base executor and used for initializing branch executors.
47
+ """
48
+ super().__init__(**kwargs)
49
+ self.mail_manager = MailManager([self.mail_transfer])
50
+
51
+ def transfer_ins(self):
52
+ """
53
+ Processes incoming mails, directing them appropriately based on their categories, and handles the initial setup
54
+ of branches or the routing of node and condition mails.
55
+ """
56
+ for key in list(self.pending_ins.keys()):
57
+ while self.pending_ins[key]:
58
+ mail: BaseMail = self.pending_ins[key].popleft()
59
+ if mail.category == "start":
60
+ self._process_start(mail)
61
+ elif mail.category == "node_list":
62
+ self._process_node_list(mail)
63
+ elif (
64
+ (mail.category == "node")
65
+ or (mail.category == "condition")
66
+ or (mail.category == "end")
67
+ ):
68
+ mail.sender_id = self.mail_transfer.id_
69
+ mail.recipient_id = mail.package["request_source"]
70
+ self.mail_transfer.pending_outs.append(mail)
71
+
72
+ def transfer_outs(self):
73
+ """
74
+ Processes outgoing mails from the central mail transfer, handling end-of-execution notifications and routing
75
+ other mails to appropriate recipients.
76
+ """
77
+ for key in list(self.mail_transfer.pending_ins.keys()):
78
+ while self.mail_transfer.pending_ins[key]:
79
+ mail: BaseMail = self.mail_transfer.pending_ins[key].popleft()
80
+ if mail.category == "end":
81
+ self.num_end_branches += 1
82
+ if self.num_end_branches == len(
83
+ self.branches
84
+ ): # tell when structure should stop
85
+ mail.sender_id = self.id_
86
+ mail.recipient_id = self.structure_id
87
+ self.pending_outs.append(mail)
88
+ self.execute_stop = True
89
+ else:
90
+ mail.sender_id = self.id_
91
+ mail.recipient_id = self.structure_id
92
+ self.pending_outs.append(mail)
93
+
94
+ def _process_start(self, start_mail: BaseMail):
95
+ """
96
+ Processes a start mail to initialize a new branch executor and configures it based on the mail's package content.
97
+
98
+ Args:
99
+ start_mail (BaseMail): The mail initiating the start of a new branch execution.
100
+ """
101
+ branch = BranchExecutor(verbose=self.verbose, **self.branch_kwargs)
102
+ branch.context = start_mail.package["context"]
103
+ self.branches[branch.id_] = branch
104
+ self.mail_manager.add_sources([branch])
105
+ self.structure_id = start_mail.package["structure_id"]
106
+ mail = BaseMail(
107
+ sender_id=self.id_,
108
+ recipient_id=self.structure_id,
109
+ category="start",
110
+ package={"request_source": branch.id_, "package": "start"},
111
+ )
112
+ self.pending_outs.append(mail)
113
+
114
+ def _process_node_list(self, nl_mail: BaseMail):
115
+ """
116
+ Processes a node list mail, setting up new branches or propagating the execution context based on the node list
117
+ provided in the mail.
118
+
119
+ Args:
120
+ nl_mail (BaseMail): The mail containing a list of nodes to be processed in subsequent branches.
121
+ """
122
+ source_branch_id = nl_mail.package["request_source"]
123
+ node_list = nl_mail.package["package"]
124
+ shared_context = self.branches[source_branch_id].context
125
+ shared_context_log = self.branches[source_branch_id].context_log
126
+ base_branch = self.branches[source_branch_id]
127
+
128
+ first_node_mail = BaseMail(
129
+ sender_id=self.mail_transfer.id_,
130
+ recipient_id=source_branch_id,
131
+ category="node",
132
+ package={"request_source": source_branch_id, "package": node_list[0]},
133
+ )
134
+ self.mail_transfer.pending_outs.append(first_node_mail)
135
+
136
+ for i in range(1, len(node_list)):
137
+ branch = BranchExecutor(
138
+ verbose=self.verbose,
139
+ messages=base_branch.messages.copy(),
140
+ service=base_branch.service,
141
+ llmconfig=base_branch.llmconfig,
142
+ datalogger=base_branch.datalogger,
143
+ )
144
+ branch.context = shared_context
145
+ branch.context_log = shared_context_log
146
+ self.branches[branch.id_] = branch
147
+ self.mail_manager.add_sources([branch])
148
+ node_mail = BaseMail(
149
+ sender_id=self.mail_transfer.id_,
150
+ recipient_id=branch.id_,
151
+ category="node",
152
+ package={"request_source": source_branch_id, "package": node_list[i]},
153
+ )
154
+ self.mail_transfer.pending_outs.append(node_mail)
155
+
156
+ async def forward(self):
157
+ """
158
+ Forwards the execution by processing all incoming and outgoing mails and advancing the state of all active branches.
159
+ """
160
+ self.transfer_ins()
161
+ self.transfer_outs()
162
+ self.mail_manager.collect_all()
163
+ self.mail_manager.send_all()
164
+ tasks = [
165
+ branch.forward() for branch in self.branches.values() if branch.pending_ins
166
+ ]
167
+ await asyncio.gather(*tasks)
168
+ return
169
+
170
+ async def execute(self, refresh_time=1):
171
+ """
172
+ Continuously executes the forward process at specified intervals until instructed to stop.
173
+
174
+ Args:
175
+ refresh_time (int): The time in seconds between execution cycles.
176
+ """
177
+ while not self.execute_stop:
178
+ await self.forward()
179
+ await asyncio.sleep(refresh_time)