lionagi 0.1.1__py3-none-any.whl → 0.2.0__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- lionagi/__init__.py +60 -5
- lionagi/core/__init__.py +0 -25
- lionagi/core/_setting/_setting.py +59 -0
- lionagi/core/action/__init__.py +14 -0
- lionagi/core/action/function_calling.py +136 -0
- lionagi/core/action/manual.py +1 -0
- lionagi/core/action/node.py +109 -0
- lionagi/core/action/tool.py +114 -0
- lionagi/core/action/tool_manager.py +356 -0
- lionagi/core/agent/base_agent.py +27 -13
- lionagi/core/agent/eval/evaluator.py +1 -0
- lionagi/core/agent/eval/vote.py +40 -0
- lionagi/core/agent/learn/learner.py +59 -0
- lionagi/core/agent/plan/unit_template.py +1 -0
- lionagi/core/collections/__init__.py +17 -0
- lionagi/core/{generic/data_logger.py → collections/_logger.py} +69 -55
- lionagi/core/collections/abc/__init__.py +53 -0
- lionagi/core/collections/abc/component.py +615 -0
- lionagi/core/collections/abc/concepts.py +297 -0
- lionagi/core/collections/abc/exceptions.py +150 -0
- lionagi/core/collections/abc/util.py +45 -0
- lionagi/core/collections/exchange.py +161 -0
- lionagi/core/collections/flow.py +426 -0
- lionagi/core/collections/model.py +419 -0
- lionagi/core/collections/pile.py +913 -0
- lionagi/core/collections/progression.py +236 -0
- lionagi/core/collections/util.py +64 -0
- lionagi/core/director/direct.py +314 -0
- lionagi/core/director/director.py +2 -0
- lionagi/core/{execute/branch_executor.py → engine/branch_engine.py} +134 -97
- lionagi/core/{execute/instruction_map_executor.py → engine/instruction_map_engine.py} +80 -55
- lionagi/{experimental/directive/evaluator → core/engine}/script_engine.py +17 -1
- lionagi/core/executor/base_executor.py +90 -0
- lionagi/core/{execute/structure_executor.py → executor/graph_executor.py} +83 -67
- lionagi/core/{execute → executor}/neo4j_executor.py +70 -67
- lionagi/core/generic/__init__.py +3 -33
- lionagi/core/generic/edge.py +42 -92
- lionagi/core/generic/edge_condition.py +16 -0
- lionagi/core/generic/graph.py +236 -0
- lionagi/core/generic/hyperedge.py +1 -0
- lionagi/core/generic/node.py +156 -221
- lionagi/core/generic/tree.py +48 -0
- lionagi/core/generic/tree_node.py +79 -0
- lionagi/core/mail/__init__.py +12 -0
- lionagi/core/mail/mail.py +25 -0
- lionagi/core/mail/mail_manager.py +139 -58
- lionagi/core/mail/package.py +45 -0
- lionagi/core/mail/start_mail.py +36 -0
- lionagi/core/message/__init__.py +19 -0
- lionagi/core/message/action_request.py +133 -0
- lionagi/core/message/action_response.py +135 -0
- lionagi/core/message/assistant_response.py +95 -0
- lionagi/core/message/instruction.py +234 -0
- lionagi/core/message/message.py +101 -0
- lionagi/core/message/system.py +86 -0
- lionagi/core/message/util.py +283 -0
- lionagi/core/report/__init__.py +4 -0
- lionagi/core/report/base.py +217 -0
- lionagi/core/report/form.py +231 -0
- lionagi/core/report/report.py +166 -0
- lionagi/core/report/util.py +28 -0
- lionagi/core/rule/_default.py +16 -0
- lionagi/core/rule/action.py +99 -0
- lionagi/core/rule/base.py +238 -0
- lionagi/core/rule/boolean.py +56 -0
- lionagi/core/rule/choice.py +47 -0
- lionagi/core/rule/mapping.py +96 -0
- lionagi/core/rule/number.py +71 -0
- lionagi/core/rule/rulebook.py +109 -0
- lionagi/core/rule/string.py +52 -0
- lionagi/core/rule/util.py +35 -0
- lionagi/core/session/branch.py +431 -0
- lionagi/core/session/directive_mixin.py +287 -0
- lionagi/core/session/session.py +229 -903
- lionagi/core/structure/__init__.py +1 -0
- lionagi/core/structure/chain.py +1 -0
- lionagi/core/structure/forest.py +1 -0
- lionagi/core/structure/graph.py +1 -0
- lionagi/core/structure/tree.py +1 -0
- lionagi/core/unit/__init__.py +5 -0
- lionagi/core/unit/parallel_unit.py +245 -0
- lionagi/core/unit/template/action.py +81 -0
- lionagi/core/unit/template/base.py +51 -0
- lionagi/core/unit/template/plan.py +84 -0
- lionagi/core/unit/template/predict.py +109 -0
- lionagi/core/unit/template/score.py +124 -0
- lionagi/core/unit/template/select.py +104 -0
- lionagi/core/unit/unit.py +362 -0
- lionagi/core/unit/unit_form.py +305 -0
- lionagi/core/unit/unit_mixin.py +1168 -0
- lionagi/core/unit/util.py +71 -0
- lionagi/core/validator/validator.py +364 -0
- lionagi/core/work/work.py +74 -0
- lionagi/core/work/work_function.py +92 -0
- lionagi/core/work/work_queue.py +81 -0
- lionagi/core/work/worker.py +195 -0
- lionagi/core/work/worklog.py +124 -0
- lionagi/experimental/compressor/base.py +46 -0
- lionagi/experimental/compressor/llm_compressor.py +247 -0
- lionagi/experimental/compressor/llm_summarizer.py +61 -0
- lionagi/experimental/compressor/util.py +70 -0
- lionagi/experimental/directive/__init__.py +19 -0
- lionagi/experimental/directive/parser/base_parser.py +69 -2
- lionagi/experimental/directive/{template_ → template}/base_template.py +17 -1
- lionagi/{libs/ln_tokenizer.py → experimental/directive/tokenizer.py} +16 -0
- lionagi/experimental/{directive/evaluator → evaluator}/ast_evaluator.py +16 -0
- lionagi/experimental/{directive/evaluator → evaluator}/base_evaluator.py +16 -0
- lionagi/experimental/knowledge/__init__.py +0 -0
- lionagi/experimental/knowledge/base.py +10 -0
- lionagi/experimental/knowledge/graph.py +0 -0
- lionagi/experimental/memory/__init__.py +0 -0
- lionagi/experimental/strategies/__init__.py +0 -0
- lionagi/experimental/strategies/base.py +1 -0
- lionagi/integrations/bridge/langchain_/documents.py +4 -0
- lionagi/integrations/bridge/llamaindex_/index.py +30 -0
- lionagi/integrations/bridge/llamaindex_/llama_index_bridge.py +6 -0
- lionagi/integrations/chunker/chunk.py +161 -24
- lionagi/integrations/config/oai_configs.py +34 -3
- lionagi/integrations/config/openrouter_configs.py +14 -2
- lionagi/integrations/loader/load.py +122 -21
- lionagi/integrations/loader/load_util.py +6 -77
- lionagi/integrations/provider/_mapping.py +46 -0
- lionagi/integrations/provider/litellm.py +2 -1
- lionagi/integrations/provider/mlx_service.py +16 -9
- lionagi/integrations/provider/oai.py +91 -4
- lionagi/integrations/provider/ollama.py +6 -5
- lionagi/integrations/provider/openrouter.py +115 -8
- lionagi/integrations/provider/services.py +2 -2
- lionagi/integrations/provider/transformers.py +18 -22
- lionagi/integrations/storage/__init__.py +3 -3
- lionagi/integrations/storage/neo4j.py +52 -60
- lionagi/integrations/storage/storage_util.py +45 -47
- lionagi/integrations/storage/structure_excel.py +285 -0
- lionagi/integrations/storage/to_excel.py +23 -7
- lionagi/libs/__init__.py +26 -1
- lionagi/libs/ln_api.py +75 -20
- lionagi/libs/ln_context.py +37 -0
- lionagi/libs/ln_convert.py +21 -9
- lionagi/libs/ln_func_call.py +69 -28
- lionagi/libs/ln_image.py +107 -0
- lionagi/libs/ln_nested.py +26 -11
- lionagi/libs/ln_parse.py +82 -23
- lionagi/libs/ln_queue.py +16 -0
- lionagi/libs/ln_tokenize.py +164 -0
- lionagi/libs/ln_validate.py +16 -0
- lionagi/libs/special_tokens.py +172 -0
- lionagi/libs/sys_util.py +95 -24
- lionagi/lions/coder/code_form.py +13 -0
- lionagi/lions/coder/coder.py +50 -3
- lionagi/lions/coder/util.py +30 -25
- lionagi/tests/libs/test_func_call.py +23 -21
- lionagi/tests/libs/test_nested.py +36 -21
- lionagi/tests/libs/test_parse.py +1 -1
- lionagi/tests/test_core/collections/__init__.py +0 -0
- lionagi/tests/test_core/collections/test_component.py +206 -0
- lionagi/tests/test_core/collections/test_exchange.py +138 -0
- lionagi/tests/test_core/collections/test_flow.py +145 -0
- lionagi/tests/test_core/collections/test_pile.py +171 -0
- lionagi/tests/test_core/collections/test_progression.py +129 -0
- lionagi/tests/test_core/generic/__init__.py +0 -0
- lionagi/tests/test_core/generic/test_edge.py +67 -0
- lionagi/tests/test_core/generic/test_graph.py +96 -0
- lionagi/tests/test_core/generic/test_node.py +106 -0
- lionagi/tests/test_core/generic/test_tree_node.py +73 -0
- lionagi/tests/test_core/test_branch.py +115 -294
- lionagi/tests/test_core/test_form.py +46 -0
- lionagi/tests/test_core/test_report.py +105 -0
- lionagi/tests/test_core/test_validator.py +111 -0
- lionagi/version.py +1 -1
- lionagi-0.2.0.dist-info/LICENSE +202 -0
- lionagi-0.2.0.dist-info/METADATA +272 -0
- lionagi-0.2.0.dist-info/RECORD +240 -0
- lionagi/core/branch/base.py +0 -653
- lionagi/core/branch/branch.py +0 -474
- lionagi/core/branch/flow_mixin.py +0 -96
- lionagi/core/branch/util.py +0 -323
- lionagi/core/direct/__init__.py +0 -19
- lionagi/core/direct/cot.py +0 -123
- lionagi/core/direct/plan.py +0 -164
- lionagi/core/direct/predict.py +0 -166
- lionagi/core/direct/react.py +0 -171
- lionagi/core/direct/score.py +0 -279
- lionagi/core/direct/select.py +0 -170
- lionagi/core/direct/sentiment.py +0 -1
- lionagi/core/direct/utils.py +0 -110
- lionagi/core/direct/vote.py +0 -64
- lionagi/core/execute/base_executor.py +0 -47
- lionagi/core/flow/baseflow.py +0 -23
- lionagi/core/flow/monoflow/ReAct.py +0 -238
- lionagi/core/flow/monoflow/__init__.py +0 -9
- lionagi/core/flow/monoflow/chat.py +0 -95
- lionagi/core/flow/monoflow/chat_mixin.py +0 -253
- lionagi/core/flow/monoflow/followup.py +0 -213
- lionagi/core/flow/polyflow/__init__.py +0 -1
- lionagi/core/flow/polyflow/chat.py +0 -251
- lionagi/core/form/action_form.py +0 -26
- lionagi/core/form/field_validator.py +0 -287
- lionagi/core/form/form.py +0 -302
- lionagi/core/form/mixin.py +0 -214
- lionagi/core/form/scored_form.py +0 -13
- lionagi/core/generic/action.py +0 -26
- lionagi/core/generic/component.py +0 -455
- lionagi/core/generic/condition.py +0 -44
- lionagi/core/generic/mail.py +0 -90
- lionagi/core/generic/mailbox.py +0 -36
- lionagi/core/generic/relation.py +0 -70
- lionagi/core/generic/signal.py +0 -22
- lionagi/core/generic/structure.py +0 -362
- lionagi/core/generic/transfer.py +0 -20
- lionagi/core/generic/work.py +0 -40
- lionagi/core/graph/graph.py +0 -126
- lionagi/core/graph/tree.py +0 -190
- lionagi/core/mail/schema.py +0 -63
- lionagi/core/messages/schema.py +0 -325
- lionagi/core/tool/__init__.py +0 -5
- lionagi/core/tool/tool.py +0 -28
- lionagi/core/tool/tool_manager.py +0 -282
- lionagi/experimental/tool/function_calling.py +0 -43
- lionagi/experimental/tool/manual.py +0 -66
- lionagi/experimental/tool/schema.py +0 -59
- lionagi/experimental/tool/tool_manager.py +0 -138
- lionagi/experimental/tool/util.py +0 -16
- lionagi/experimental/work/_logger.py +0 -25
- lionagi/experimental/work/schema.py +0 -30
- lionagi/experimental/work/tests.py +0 -72
- lionagi/experimental/work/work_function.py +0 -89
- lionagi/experimental/work/worker.py +0 -12
- lionagi/integrations/bridge/llamaindex_/get_index.py +0 -294
- lionagi/tests/test_core/test_base_branch.py +0 -426
- lionagi/tests/test_core/test_chat_flow.py +0 -63
- lionagi/tests/test_core/test_mail_manager.py +0 -75
- lionagi/tests/test_core/test_prompts.py +0 -51
- lionagi/tests/test_core/test_session.py +0 -254
- lionagi/tests/test_core/test_session_base_util.py +0 -313
- lionagi/tests/test_core/test_tool_manager.py +0 -95
- lionagi-0.1.1.dist-info/LICENSE +0 -9
- lionagi-0.1.1.dist-info/METADATA +0 -174
- lionagi-0.1.1.dist-info/RECORD +0 -190
- /lionagi/core/{branch → _setting}/__init__.py +0 -0
- /lionagi/core/{execute → agent/eval}/__init__.py +0 -0
- /lionagi/core/{flow → agent/learn}/__init__.py +0 -0
- /lionagi/core/{form → agent/plan}/__init__.py +0 -0
- /lionagi/core/{branch/executable_branch.py → agent/plan/plan.py} +0 -0
- /lionagi/core/{graph → director}/__init__.py +0 -0
- /lionagi/core/{messages → engine}/__init__.py +0 -0
- /lionagi/{experimental/directive/evaluator → core/engine}/sandbox_.py +0 -0
- /lionagi/{experimental/directive/evaluator → core/executor}/__init__.py +0 -0
- /lionagi/{experimental/directive/template_ → core/rule}/__init__.py +0 -0
- /lionagi/{experimental/tool → core/unit/template}/__init__.py +0 -0
- /lionagi/{experimental/work → core/validator}/__init__.py +0 -0
- /lionagi/core/{flow/mono_chat_mixin.py → work/__init__.py} +0 -0
- /lionagi/experimental/{work/exchange.py → compressor/__init__.py} +0 -0
- /lionagi/experimental/{work/util.py → directive/template/__init__.py} +0 -0
- /lionagi/experimental/directive/{schema.py → template/schema.py} +0 -0
- /lionagi/{tests/libs/test_async.py → experimental/evaluator/__init__.py} +0 -0
- {lionagi-0.1.1.dist-info → lionagi-0.2.0.dist-info}/WHEEL +0 -0
- {lionagi-0.1.1.dist-info → lionagi-0.2.0.dist-info}/top_level.txt +0 -0
@@ -2,16 +2,16 @@ from collections import deque
|
|
2
2
|
import json
|
3
3
|
from typing import Callable
|
4
4
|
|
5
|
-
from lionagi.core.
|
5
|
+
from lionagi.core.executor.base_executor import BaseExecutor
|
6
6
|
from lionagi.integrations.storage.neo4j import Neo4j
|
7
7
|
from lionagi.integrations.storage.storage_util import ParseNode
|
8
|
-
from lionagi.core.generic import ActionNode
|
9
8
|
from lionagi.core.agent.base_agent import BaseAgent
|
10
|
-
from lionagi.core.
|
9
|
+
from lionagi.core.engine.instruction_map_engine import InstructionMapEngine
|
11
10
|
|
12
|
-
from lionagi.core.mail
|
13
|
-
from lionagi.core.
|
14
|
-
from lionagi.core.generic import
|
11
|
+
from lionagi.core.mail import Mail
|
12
|
+
from lionagi.core.action import Tool, DirectiveSelection, ActionNode
|
13
|
+
from lionagi.core.generic.edge import Edge
|
14
|
+
from lionagi.core.collections.progression import progression
|
15
15
|
|
16
16
|
from lionagi.libs import AsyncUtil
|
17
17
|
|
@@ -33,9 +33,12 @@ class Neo4jExecutor(BaseExecutor):
|
|
33
33
|
structure_id: str = None
|
34
34
|
structure_name: str = None
|
35
35
|
middle_agents: list | None = None
|
36
|
-
default_agent_executable: BaseExecutor =
|
36
|
+
default_agent_executable: BaseExecutor = InstructionMapEngine()
|
37
37
|
condition_check_result: bool | None = None
|
38
38
|
|
39
|
+
class Config:
|
40
|
+
arbitrary_types_allowed = True
|
41
|
+
|
39
42
|
async def check_edge_condition(
|
40
43
|
self, condition, executable_id, request_source, head, tail
|
41
44
|
):
|
@@ -52,9 +55,9 @@ class Neo4jExecutor(BaseExecutor):
|
|
52
55
|
Returns:
|
53
56
|
bool: Result of the condition check.
|
54
57
|
"""
|
55
|
-
if condition.
|
56
|
-
return condition(self)
|
57
|
-
elif condition.
|
58
|
+
if condition.source == "structure":
|
59
|
+
return condition.applies(self)
|
60
|
+
elif condition.source == "executable":
|
58
61
|
return await self._check_executable_condition(
|
59
62
|
condition, executable_id, head, tail, request_source
|
60
63
|
)
|
@@ -66,20 +69,20 @@ class Neo4jExecutor(BaseExecutor):
|
|
66
69
|
Args:
|
67
70
|
edge_id (str): The ID of the edge.
|
68
71
|
"""
|
69
|
-
for key in list(self.pending_ins.keys()):
|
70
|
-
skipped_requests =
|
71
|
-
while self.pending_ins[key]:
|
72
|
-
|
72
|
+
for key in list(self.mailbox.pending_ins.keys()):
|
73
|
+
skipped_requests = progression()
|
74
|
+
while self.mailbox.pending_ins[key].size() > 0:
|
75
|
+
mail_id = self.mailbox.pending_ins[key].popleft()
|
76
|
+
mail = self.mailbox.pile[mail_id]
|
73
77
|
if (
|
74
78
|
mail.category == "condition"
|
75
|
-
and mail.package
|
79
|
+
and mail.package.package["edge_id"] == edge_id
|
76
80
|
):
|
77
|
-
self.
|
78
|
-
|
79
|
-
]
|
81
|
+
self.mailbox.pile.pop(mail_id)
|
82
|
+
self.condition_check_result = mail.package.package["check_result"]
|
80
83
|
else:
|
81
84
|
skipped_requests.append(mail)
|
82
|
-
self.pending_ins[key] = skipped_requests
|
85
|
+
self.mailbox.pending_ins[key] = skipped_requests
|
83
86
|
|
84
87
|
async def _check_executable_condition(
|
85
88
|
self, condition, executable_id, head, tail, request_source
|
@@ -99,13 +102,14 @@ class Neo4jExecutor(BaseExecutor):
|
|
99
102
|
"""
|
100
103
|
edge = Edge(head=head, tail=tail, condition=condition)
|
101
104
|
self.send(
|
102
|
-
|
105
|
+
recipient=executable_id,
|
103
106
|
category="condition",
|
104
|
-
package=
|
107
|
+
package=edge,
|
108
|
+
request_source=request_source,
|
105
109
|
)
|
106
110
|
while self.condition_check_result is None:
|
107
111
|
await AsyncUtil.sleep(0.1)
|
108
|
-
self._process_edge_condition(edge.
|
112
|
+
self._process_edge_condition(edge.ln_id)
|
109
113
|
continue
|
110
114
|
check_result = self.condition_check_result
|
111
115
|
self.condition_check_result = None
|
@@ -126,27 +130,27 @@ class Neo4jExecutor(BaseExecutor):
|
|
126
130
|
bundled_nodes = deque()
|
127
131
|
for node_labels, node_properties in bundle_list:
|
128
132
|
try:
|
129
|
-
if "
|
130
|
-
node = ParseNode.
|
133
|
+
if "DirectiveSelection" in node_labels:
|
134
|
+
node = ParseNode.parse_directiveSelection(node_properties)
|
131
135
|
bundled_nodes.append(node)
|
132
136
|
elif "Tool" in node_labels:
|
133
137
|
node = ParseNode.parse_tool(node_properties)
|
134
138
|
bundled_nodes.append(node)
|
135
139
|
else:
|
136
140
|
raise ValueError(
|
137
|
-
f"Invalid bundle node {node_properties.
|
141
|
+
f"Invalid bundle node {node_properties.ln_id}. Valid nodes are ActionSelection or Tool"
|
138
142
|
)
|
139
143
|
except Exception as e:
|
140
144
|
raise ValueError(
|
141
|
-
f"Failed to parse ActionSelection or Tool node {node_properties.
|
145
|
+
f"Failed to parse ActionSelection or Tool node {node_properties.ln_id}. Error: {e}"
|
142
146
|
)
|
143
147
|
|
144
148
|
action_node = ActionNode(instruction=instruction)
|
145
149
|
while bundled_nodes:
|
146
150
|
node = bundled_nodes.popleft()
|
147
|
-
if isinstance(node,
|
148
|
-
action_node.
|
149
|
-
action_node.
|
151
|
+
if isinstance(node, DirectiveSelection):
|
152
|
+
action_node.directive = node.directive
|
153
|
+
action_node.directive_kwargs = node.directive_kwargs
|
150
154
|
elif isinstance(node, Tool):
|
151
155
|
action_node.tools.append(node)
|
152
156
|
return action_node
|
@@ -161,7 +165,11 @@ class Neo4jExecutor(BaseExecutor):
|
|
161
165
|
Returns:
|
162
166
|
BaseAgent: An agent executor configured with the given properties.
|
163
167
|
"""
|
164
|
-
output_parser =
|
168
|
+
output_parser = (
|
169
|
+
ParseNode.convert_to_def(node_properties["outputParser"])
|
170
|
+
if "outputParser" in node_properties
|
171
|
+
else None
|
172
|
+
)
|
165
173
|
|
166
174
|
structure = Neo4jExecutor(
|
167
175
|
driver=self.driver, structure_id=node_properties["structureId"]
|
@@ -170,9 +178,9 @@ class Neo4jExecutor(BaseExecutor):
|
|
170
178
|
structure=structure,
|
171
179
|
executable=self.default_agent_executable,
|
172
180
|
output_parser=output_parser,
|
181
|
+
ln_id=node_properties["ln_id"],
|
182
|
+
timestamp=node_properties["timestamp"],
|
173
183
|
)
|
174
|
-
agent.id_ = node_properties["id"]
|
175
|
-
agent.timestamp = node_properties["timestamp"]
|
176
184
|
return agent
|
177
185
|
|
178
186
|
async def _next_node(
|
@@ -202,7 +210,7 @@ class Neo4jExecutor(BaseExecutor):
|
|
202
210
|
condition_obj = ParseNode.parse_condition(condition, condition_cls)
|
203
211
|
|
204
212
|
head = node_id
|
205
|
-
tail = node_properties["
|
213
|
+
tail = node_properties["ln_id"]
|
206
214
|
check = await self.check_edge_condition(
|
207
215
|
condition_obj, executable_id, request_source, head, tail
|
208
216
|
)
|
@@ -210,7 +218,7 @@ class Neo4jExecutor(BaseExecutor):
|
|
210
218
|
continue
|
211
219
|
except Exception as e:
|
212
220
|
raise ValueError(
|
213
|
-
f"Failed to use condition {edge_properties['condition']} from {node_id} to {node_properties['
|
221
|
+
f"Failed to use condition {edge_properties['condition']} from {node_id} to {node_properties['ln_id']}, Error: {e}"
|
214
222
|
)
|
215
223
|
|
216
224
|
try:
|
@@ -223,14 +231,14 @@ class Neo4jExecutor(BaseExecutor):
|
|
223
231
|
|
224
232
|
else:
|
225
233
|
raise ValueError(
|
226
|
-
f"Invalid start node {node_properties.
|
234
|
+
f"Invalid start node {node_properties.ln_id}. Valid nodes are System or Instruction"
|
227
235
|
)
|
228
236
|
except Exception as e:
|
229
237
|
raise ValueError(
|
230
|
-
f"Failed to parse System or Instruction node {node_properties.
|
238
|
+
f"Failed to parse System or Instruction node {node_properties.ln_id}. Error: {e}"
|
231
239
|
)
|
232
240
|
|
233
|
-
bundle_list = await self.driver.get_bundle(node.
|
241
|
+
bundle_list = await self.driver.get_bundle(node.ln_id)
|
234
242
|
|
235
243
|
if bundle_list and "System" in node_labels:
|
236
244
|
raise ValueError("System node does not support bundle edge")
|
@@ -247,10 +255,10 @@ class Neo4jExecutor(BaseExecutor):
|
|
247
255
|
ValueError: If there is an issue with finding or starting the structure.
|
248
256
|
"""
|
249
257
|
try:
|
250
|
-
|
258
|
+
id_, head_list = await self.driver.get_heads(
|
251
259
|
self.structure_name, self.structure_id
|
252
260
|
)
|
253
|
-
self.structure_id =
|
261
|
+
self.structure_id = id_
|
254
262
|
return await self._next_node(head_list)
|
255
263
|
except Exception as e:
|
256
264
|
raise ValueError(f"Error in searching for structure in Neo4j. Error: {e}")
|
@@ -273,12 +281,12 @@ class Neo4jExecutor(BaseExecutor):
|
|
273
281
|
node_list = await self.driver.get_forwards(node_id)
|
274
282
|
return await self._next_node(node_list, node_id, executable_id, request_source)
|
275
283
|
|
276
|
-
async def _handle_mail(self, mail:
|
284
|
+
async def _handle_mail(self, mail: Mail):
|
277
285
|
"""
|
278
286
|
Processes incoming mail, determining the next action based on the mail's category and content.
|
279
287
|
|
280
288
|
Args:
|
281
|
-
mail (
|
289
|
+
mail (Mail): The incoming mail to be processed.
|
282
290
|
|
283
291
|
Raises:
|
284
292
|
ValueError: If there is an error processing the mail.
|
@@ -295,9 +303,9 @@ class Neo4jExecutor(BaseExecutor):
|
|
295
303
|
|
296
304
|
elif mail.category == "node_id":
|
297
305
|
try:
|
298
|
-
node_id = mail.package
|
299
|
-
executable_id = mail.
|
300
|
-
request_source = mail.package
|
306
|
+
node_id = mail.package.package
|
307
|
+
executable_id = mail.sender
|
308
|
+
request_source = mail.package.request_source
|
301
309
|
return await self._handle_node_id(
|
302
310
|
node_id, executable_id, request_source
|
303
311
|
)
|
@@ -305,9 +313,9 @@ class Neo4jExecutor(BaseExecutor):
|
|
305
313
|
raise ValueError(f"Error in handling node_id: {e}")
|
306
314
|
elif mail.category == "node":
|
307
315
|
try:
|
308
|
-
node_id = mail.package
|
309
|
-
executable_id = mail.
|
310
|
-
request_source = mail.package
|
316
|
+
node_id = mail.package.package.ln_id
|
317
|
+
executable_id = mail.sender
|
318
|
+
request_source = mail.package.request_source
|
311
319
|
return await self._handle_node_id(
|
312
320
|
node_id, executable_id, request_source
|
313
321
|
)
|
@@ -316,50 +324,45 @@ class Neo4jExecutor(BaseExecutor):
|
|
316
324
|
else:
|
317
325
|
raise ValueError(f"Invalid mail type for structure")
|
318
326
|
|
319
|
-
def _send_mail(self, next_nodes: list | None, mail:
|
327
|
+
def _send_mail(self, next_nodes: list | None, mail: Mail):
|
320
328
|
"""
|
321
329
|
Sends out mail to the next nodes or marks the execution as ended if there are no next nodes.
|
322
330
|
|
323
331
|
Args:
|
324
332
|
next_nodes (list | None): List of next nodes to which mail should be sent.
|
325
|
-
mail (
|
333
|
+
mail (Mail): The current mail being processed.
|
326
334
|
"""
|
327
335
|
if not next_nodes: # tail
|
328
336
|
self.send(
|
329
|
-
|
337
|
+
recipient=mail.sender,
|
330
338
|
category="end",
|
331
|
-
package=
|
332
|
-
|
333
|
-
"package": "end",
|
334
|
-
},
|
339
|
+
package="end",
|
340
|
+
request_source=mail.package.request_source,
|
335
341
|
)
|
336
342
|
else:
|
337
343
|
if len(next_nodes) == 1:
|
338
344
|
self.send(
|
339
|
-
|
345
|
+
recipient=mail.sender,
|
340
346
|
category="node",
|
341
|
-
package=
|
342
|
-
|
343
|
-
"package": next_nodes[0],
|
344
|
-
},
|
347
|
+
package=next_nodes[0],
|
348
|
+
request_source=mail.package.request_source,
|
345
349
|
)
|
346
350
|
else:
|
347
351
|
self.send(
|
348
|
-
|
352
|
+
recipient=mail.sender,
|
349
353
|
category="node_list",
|
350
|
-
package=
|
351
|
-
|
352
|
-
"package": next_nodes,
|
353
|
-
},
|
354
|
+
package=next_nodes,
|
355
|
+
request_source=mail.package.request_source,
|
354
356
|
)
|
355
357
|
|
356
358
|
async def forward(self) -> None:
|
357
359
|
"""
|
358
360
|
Forwards execution by processing all pending mails and advancing to next nodes or actions.
|
359
361
|
"""
|
360
|
-
for key in list(self.pending_ins.keys()):
|
361
|
-
while self.pending_ins[key]:
|
362
|
-
|
362
|
+
for key in list(self.mailbox.pending_ins.keys()):
|
363
|
+
while self.mailbox.pending_ins[key].size() > 0:
|
364
|
+
mail_id = self.mailbox.pending_ins[key].popleft()
|
365
|
+
mail = self.mailbox.pile.pop(mail_id)
|
363
366
|
try:
|
364
367
|
if mail == "end":
|
365
368
|
self.execute_stop = True
|
lionagi/core/generic/__init__.py
CHANGED
@@ -1,37 +1,7 @@
|
|
1
|
-
from .component import BaseComponent, BaseNode
|
2
|
-
from .condition import Condition
|
3
|
-
from .data_logger import DataLogger, DLog
|
4
|
-
from .signal import Signal, Start
|
5
|
-
from .mail import Mail, Package
|
6
|
-
from .mailbox import MailBox
|
7
1
|
from .edge import Edge
|
8
|
-
from .relation import Relations
|
9
|
-
from .transfer import Transfer
|
10
|
-
from .work import Work, Worker
|
11
2
|
from .node import Node
|
12
|
-
from .
|
13
|
-
from .
|
3
|
+
from .graph import Graph
|
4
|
+
from .tree import Tree
|
14
5
|
|
15
6
|
|
16
|
-
__all__ = [
|
17
|
-
"BaseComponent",
|
18
|
-
"BaseNode",
|
19
|
-
"BaseStructure",
|
20
|
-
"BaseWork",
|
21
|
-
"Condition",
|
22
|
-
"Edge",
|
23
|
-
"Mail",
|
24
|
-
"MailBox",
|
25
|
-
"Start",
|
26
|
-
"Package",
|
27
|
-
"Relations",
|
28
|
-
"Signal",
|
29
|
-
"Transfer",
|
30
|
-
"Node",
|
31
|
-
"Work",
|
32
|
-
"Worker",
|
33
|
-
"ActionNode",
|
34
|
-
"ActionSelection",
|
35
|
-
"DataLogger",
|
36
|
-
"DLog",
|
37
|
-
]
|
7
|
+
__all__ = ["Edge", "Node", "Graph", "Tree"]
|
lionagi/core/generic/edge.py
CHANGED
@@ -1,105 +1,71 @@
|
|
1
|
-
"""
|
2
|
-
Module for representing conditions and edges between nodes in a graph structure.
|
3
|
-
|
4
|
-
This module provides the base for creating and managing edges that connect nodes
|
5
|
-
within a graph. It includes support for conditional edges, allowing the dynamic
|
6
|
-
evaluation of connections based on custom logic.
|
7
|
-
"""
|
8
|
-
|
9
|
-
from typing import Any
|
10
1
|
from pydantic import Field, field_validator
|
11
|
-
from
|
12
|
-
from lionagi.core.
|
13
|
-
|
14
|
-
|
15
|
-
class Edge(BaseComponent):
|
16
|
-
"""
|
17
|
-
Represents an edge between two nodes, potentially with a condition.
|
2
|
+
from typing import Any
|
3
|
+
from lionagi.core.collections.abc import Component, get_lion_id, LionIDable, Condition
|
4
|
+
from lionagi.core.generic.edge_condition import EdgeCondition
|
18
5
|
|
19
|
-
Attributes:
|
20
|
-
head (str): The identifier of the head node of the edge.
|
21
|
-
tail (str): The identifier of the tail node of the edge.
|
22
|
-
condition (Optional[Condition]): An optional condition that must be met
|
23
|
-
for the edge to be considered active.
|
24
|
-
label (Optional[str]): An optional label for the edge.
|
25
6
|
|
26
|
-
|
27
|
-
|
28
|
-
"""
|
7
|
+
class Edge(Component):
|
8
|
+
"""Represents a directed edge between two nodes in a graph."""
|
29
9
|
|
30
10
|
head: str = Field(
|
11
|
+
...,
|
31
12
|
title="Head",
|
32
13
|
description="The identifier of the head node of the edge.",
|
33
14
|
)
|
15
|
+
|
34
16
|
tail: str = Field(
|
35
|
-
|
17
|
+
...,
|
18
|
+
title="Out",
|
36
19
|
description="The identifier of the tail node of the edge.",
|
37
20
|
)
|
38
|
-
|
21
|
+
|
22
|
+
condition: Condition | EdgeCondition | None = Field(
|
39
23
|
default=None,
|
40
|
-
description="
|
24
|
+
description="Optional condition that must be met for the edge "
|
25
|
+
"to be considered active.",
|
41
26
|
)
|
27
|
+
|
42
28
|
label: str | None = Field(
|
43
29
|
default=None,
|
44
30
|
description="An optional label for the edge.",
|
45
31
|
)
|
32
|
+
|
46
33
|
bundle: bool = Field(
|
47
34
|
default=False,
|
48
35
|
description="A flag indicating if the edge is bundled.",
|
49
36
|
)
|
50
37
|
|
51
|
-
|
52
|
-
|
53
|
-
"""
|
54
|
-
Validates the head and tail fields to ensure they are valid node identifiers.
|
55
|
-
|
56
|
-
Args:
|
57
|
-
value: The value of the field being validated.
|
58
|
-
values: A dictionary of all other values on the model.
|
59
|
-
field: The model field being validated.
|
60
|
-
|
61
|
-
Returns:
|
62
|
-
The validated value, ensuring it is a valid identifier.
|
63
|
-
|
64
|
-
Raises:
|
65
|
-
ValueError: If the validation fails.
|
66
|
-
"""
|
67
|
-
if isinstance(value, BaseNode):
|
68
|
-
return value.id_
|
69
|
-
return value
|
70
|
-
|
71
|
-
def check_condition(self, obj: dict[str, Any]) -> bool:
|
72
|
-
"""
|
73
|
-
Evaluates if the condition associated with the edge is met.
|
74
|
-
|
75
|
-
Args:
|
76
|
-
obj (dict[str, Any]): The context object used for condition evaluation.
|
77
|
-
|
78
|
-
Returns:
|
79
|
-
bool: True if the condition is met, False otherwise.
|
80
|
-
|
81
|
-
Raises:
|
82
|
-
ValueError: If the condition is not set.
|
83
|
-
"""
|
38
|
+
async def check_condition(self, obj: Any) -> bool:
|
39
|
+
"""Check if the edge condition is met for the given object."""
|
84
40
|
if not self.condition:
|
85
41
|
raise ValueError("The condition for the edge is not set.")
|
86
|
-
|
42
|
+
check = await self.condition.applies(obj)
|
43
|
+
return check
|
44
|
+
|
45
|
+
@field_validator("head", "tail", mode="before")
|
46
|
+
def _validate_head_tail(cls, value):
|
47
|
+
"""Validate the head and tail fields."""
|
48
|
+
return get_lion_id(value)
|
87
49
|
|
88
50
|
def string_condition(self):
|
89
51
|
"""
|
90
|
-
Retrieves the
|
52
|
+
Retrieves the condition class source code as a string.
|
91
53
|
|
92
|
-
This method is useful for serialization and debugging, allowing
|
93
|
-
|
94
|
-
|
54
|
+
This method is useful for serialization and debugging, allowing
|
55
|
+
the condition logic to be inspected or stored in a human-readable
|
56
|
+
format. It employs advanced introspection techniques to locate and
|
57
|
+
extract the exact class definition, handling edge cases like
|
58
|
+
dynamically defined classes or classes defined interactively.
|
95
59
|
|
96
60
|
Returns:
|
97
|
-
str: The source code
|
98
|
-
|
61
|
+
str | None: The condition class source code if available.
|
62
|
+
If the condition is None or the source code cannot be
|
63
|
+
located, this method returns None.
|
99
64
|
|
100
65
|
Raises:
|
101
|
-
TypeError: If the source code
|
102
|
-
|
66
|
+
TypeError: If the condition class source code cannot be found
|
67
|
+
due to the class being defined in a non-standard manner or
|
68
|
+
in the interactive interpreter (__main__ context).
|
103
69
|
"""
|
104
70
|
if self.condition is None:
|
105
71
|
return
|
@@ -137,26 +103,10 @@ class Edge(BaseComponent):
|
|
137
103
|
class_code = extract_symbols(cell_code, obj.__name__)[0][0]
|
138
104
|
return class_code
|
139
105
|
|
140
|
-
def
|
141
|
-
"""
|
142
|
-
|
143
|
-
"""
|
106
|
+
def __len__(self):
|
107
|
+
"""Return the length of the edge (always 1)."""
|
108
|
+
return 1
|
144
109
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
)
|
149
|
-
|
150
|
-
def __repr__(self) -> str:
|
151
|
-
"""
|
152
|
-
Returns a detailed string representation of the Relationship.
|
153
|
-
|
154
|
-
Examples:
|
155
|
-
>>> edge = Relationship(source_node_id="node1", target_node_id="node2")
|
156
|
-
>>> repr(edge)
|
157
|
-
'Relationship(id_=None, from=node1, to=node2, content=None, metadata=None, label=None)'
|
158
|
-
"""
|
159
|
-
return (
|
160
|
-
f"Edge(id_={self.id_}, from={self.head}, to={self.tail}, "
|
161
|
-
f"label={self.label})"
|
162
|
-
)
|
110
|
+
def __contains__(self, item: LionIDable) -> bool:
|
111
|
+
"""Check if the given item is the head or tail of the edge."""
|
112
|
+
return get_lion_id(item) in (self.head, self.tail)
|
@@ -0,0 +1,16 @@
|
|
1
|
+
from typing import Any
|
2
|
+
from pydantic import Field
|
3
|
+
from lionagi.core.collections.abc import Condition
|
4
|
+
from pydantic import BaseModel
|
5
|
+
|
6
|
+
|
7
|
+
class EdgeCondition(Condition, BaseModel):
|
8
|
+
source: Any = Field(
|
9
|
+
title="Source",
|
10
|
+
description="The source for condition check",
|
11
|
+
)
|
12
|
+
|
13
|
+
class Config:
|
14
|
+
"""Model configuration settings."""
|
15
|
+
|
16
|
+
extra = "allow"
|