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
@@ -0,0 +1,48 @@
|
|
1
|
+
"""This module provides tree structure."""
|
2
|
+
|
3
|
+
from pydantic import Field
|
4
|
+
from lionagi.core.collections.abc import Condition
|
5
|
+
from lionagi.core.collections.util import to_list_type
|
6
|
+
from lionagi.core.generic.tree_node import TreeNode
|
7
|
+
from lionagi.core.generic.graph import Graph
|
8
|
+
|
9
|
+
|
10
|
+
class Tree(Graph):
|
11
|
+
"""
|
12
|
+
Represents a tree structure, extending the graph with tree-specific functionalities.
|
13
|
+
|
14
|
+
Manages parent-child relationships within the tree.
|
15
|
+
|
16
|
+
Attributes:
|
17
|
+
root (TreeNode | None): The root node of the tree. Defaults to None.
|
18
|
+
"""
|
19
|
+
|
20
|
+
root: TreeNode | None = Field(
|
21
|
+
default=None, description="The root node of the tree graph."
|
22
|
+
)
|
23
|
+
|
24
|
+
def relate_parent_child(
|
25
|
+
self,
|
26
|
+
parent: TreeNode,
|
27
|
+
children,
|
28
|
+
condition: Condition | None = None,
|
29
|
+
bundle: bool = False,
|
30
|
+
) -> None:
|
31
|
+
"""
|
32
|
+
Establishes parent-child relationships between the given parent and child node(s).
|
33
|
+
|
34
|
+
Args:
|
35
|
+
parent (TreeNode): The parent node.
|
36
|
+
children (list[TreeNode]): A list of child nodes.
|
37
|
+
condition (Condition | None): The condition associated with the relationships, if any.
|
38
|
+
bundle (bool): Indicates whether to bundle the relations into a single
|
39
|
+
transaction. Defaults to False.
|
40
|
+
"""
|
41
|
+
|
42
|
+
for i in to_list_type(children):
|
43
|
+
i.relate_parent(parent, condition=condition, bundle=bundle)
|
44
|
+
|
45
|
+
if self.root is None:
|
46
|
+
self.root = parent
|
47
|
+
|
48
|
+
self.add_node([parent, *children])
|
@@ -0,0 +1,79 @@
|
|
1
|
+
from enum import Enum
|
2
|
+
from pydantic import Field
|
3
|
+
from lionagi.core.collections.abc import Condition
|
4
|
+
from lionagi.core.collections.util import to_list_type
|
5
|
+
from lionagi.core.generic.node import Node
|
6
|
+
|
7
|
+
|
8
|
+
class TreeLabel(str, Enum):
|
9
|
+
"""Enumeration representing tree relationships."""
|
10
|
+
|
11
|
+
PARENT = "parent"
|
12
|
+
CHILD = "child"
|
13
|
+
|
14
|
+
|
15
|
+
class TreeNode(Node):
|
16
|
+
"""Represents a node in a tree structure."""
|
17
|
+
|
18
|
+
parent: Node | None = Field(
|
19
|
+
default=None,
|
20
|
+
description="The parent node, as an instance of Node.",
|
21
|
+
)
|
22
|
+
|
23
|
+
@property
|
24
|
+
def children(self) -> list[str]:
|
25
|
+
"""Return a list of child node ids."""
|
26
|
+
if not self.parent:
|
27
|
+
return list(self.related_nodes)
|
28
|
+
else:
|
29
|
+
return [node for node in self.related_nodes if node != self.parent.ln_id]
|
30
|
+
|
31
|
+
def relate_child(
|
32
|
+
self,
|
33
|
+
node: Node | list[Node],
|
34
|
+
condition: Condition | None = None,
|
35
|
+
bundle: bool = False,
|
36
|
+
) -> None:
|
37
|
+
"""Establish a parent-child relationship with the given node(s)."""
|
38
|
+
children = to_list_type(node)
|
39
|
+
for _child in children:
|
40
|
+
self.relate(
|
41
|
+
_child,
|
42
|
+
direction="out",
|
43
|
+
# label=TreeLabel.PARENT,
|
44
|
+
condition=condition,
|
45
|
+
bundle=bundle,
|
46
|
+
)
|
47
|
+
if isinstance(_child, TreeNode):
|
48
|
+
_child.parent = self
|
49
|
+
|
50
|
+
def relate_parent(
|
51
|
+
self,
|
52
|
+
node: Node,
|
53
|
+
condition: Condition | None = None,
|
54
|
+
bundle: bool = False,
|
55
|
+
) -> None:
|
56
|
+
"""Establish a parent-child relationship with the given parent node."""
|
57
|
+
if self.parent:
|
58
|
+
self.unrelate(self.parent)
|
59
|
+
self.relate(
|
60
|
+
node,
|
61
|
+
direction="in",
|
62
|
+
# label=TreeLabel.PARENT,
|
63
|
+
condition=condition,
|
64
|
+
bundle=bundle,
|
65
|
+
)
|
66
|
+
self.parent = node
|
67
|
+
|
68
|
+
def unrelate_parent(self):
|
69
|
+
"""Remove the parent-child relationship with the parent node."""
|
70
|
+
self.unrelate(self.parent)
|
71
|
+
self.parent = None
|
72
|
+
|
73
|
+
def unrelate_child(self, child: Node | list[Node]):
|
74
|
+
"""Remove the parent-child relationship with the given child node(s)."""
|
75
|
+
children: list[Node] = [child] if isinstance(child, Node) else child
|
76
|
+
for _child in children:
|
77
|
+
self.unrelate(_child)
|
78
|
+
if isinstance(_child, TreeNode):
|
79
|
+
_child.parent = None
|
lionagi/core/mail/__init__.py
CHANGED
@@ -0,0 +1,25 @@
|
|
1
|
+
from lionagi.core.collections.abc import Element, Field, Sendable
|
2
|
+
from .package import PackageCategory, Package
|
3
|
+
|
4
|
+
|
5
|
+
class Mail(Element, Sendable):
|
6
|
+
"""Represents a mail component with sender and recipient information."""
|
7
|
+
|
8
|
+
package: Package | None = Field(
|
9
|
+
None,
|
10
|
+
title="Package",
|
11
|
+
description="The package to be delivered.",
|
12
|
+
)
|
13
|
+
|
14
|
+
@property
|
15
|
+
def category(self) -> PackageCategory:
|
16
|
+
"""Return the category of the package."""
|
17
|
+
return self.package.category
|
18
|
+
|
19
|
+
def to_dict(self):
|
20
|
+
return {
|
21
|
+
"ln_id": self.ln_id,
|
22
|
+
"created": self.timestamp,
|
23
|
+
"package_category": self.package.category,
|
24
|
+
"package_id": self.package.ln_id,
|
25
|
+
}
|
@@ -1,10 +1,14 @@
|
|
1
1
|
from collections import deque
|
2
|
+
from pydantic import Field
|
2
3
|
from lionagi.libs import AsyncUtil
|
3
|
-
from lionagi.core.
|
4
|
-
from lionagi.core.
|
4
|
+
from lionagi.core.collections.abc import Executable, Element
|
5
|
+
from lionagi.core.collections import Exchange
|
6
|
+
from lionagi.core.collections.util import to_list_type, get_lion_id
|
7
|
+
from .mail import Mail, Package
|
8
|
+
from lionagi.core.collections import Pile, pile
|
5
9
|
|
6
10
|
|
7
|
-
class MailManager:
|
11
|
+
class MailManager(Element, Executable):
|
8
12
|
"""
|
9
13
|
Manages the sending, receiving, and storage of mail items between various sources.
|
10
14
|
|
@@ -15,85 +19,162 @@ class MailManager:
|
|
15
19
|
sources (Dict[str, Any]): A dictionary mapping source identifiers to their attributes.
|
16
20
|
mails (Dict[str, Dict[str, deque]]): A nested dictionary storing queued mail items, organized by recipient
|
17
21
|
and sender.
|
22
|
+
execute_stop (bool): A flag indicating whether to stop execution.
|
18
23
|
"""
|
19
24
|
|
25
|
+
sources: Pile[Element] = Field(
|
26
|
+
default_factory=lambda: pile(),
|
27
|
+
description="The pile of managed sources",
|
28
|
+
)
|
29
|
+
|
30
|
+
mails: dict[str, dict[str, deque]] = Field(
|
31
|
+
default_factory=dict,
|
32
|
+
description="The mails waiting to be sent",
|
33
|
+
examples=["{'recipient_id': {'sender_id': deque()}}"],
|
34
|
+
)
|
35
|
+
|
36
|
+
execute_stop: bool = Field(
|
37
|
+
False, description="A flag indicating whether to stop execution."
|
38
|
+
)
|
39
|
+
|
20
40
|
def __init__(self, sources=None):
|
21
|
-
|
22
|
-
|
41
|
+
"""
|
42
|
+
Initializes the MailManager with optional sources.
|
43
|
+
|
44
|
+
Args:
|
45
|
+
sources (Optional[list]): A list of sources to be managed by the MailManager.
|
46
|
+
"""
|
47
|
+
super().__init__()
|
23
48
|
if sources:
|
24
49
|
self.add_sources(sources)
|
25
|
-
self.execute_stop = False
|
26
50
|
|
27
51
|
def add_sources(self, sources):
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
52
|
+
"""
|
53
|
+
Adds new sources to the MailManager.
|
54
|
+
|
55
|
+
Args:
|
56
|
+
sources (list): A list of sources to be added.
|
57
|
+
|
58
|
+
Raises:
|
59
|
+
ValueError: If failed to add sources.
|
60
|
+
"""
|
61
|
+
try:
|
62
|
+
sources = to_list_type(sources)
|
63
|
+
self.sources.include(sources)
|
64
|
+
for item in sources:
|
65
|
+
self.mails[item.ln_id] = {}
|
66
|
+
except Exception as e:
|
67
|
+
raise ValueError(f"Failed to add source. Error {e}")
|
40
68
|
|
41
69
|
@staticmethod
|
42
|
-
def create_mail(
|
43
|
-
|
70
|
+
def create_mail(sender, recipient, category, package):
|
71
|
+
"""
|
72
|
+
Creates a mail item.
|
44
73
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
74
|
+
Args:
|
75
|
+
sender (str): The sender of the mail.
|
76
|
+
recipient (str): The recipient of the mail.
|
77
|
+
category (str): The category of the mail.
|
78
|
+
package (Any): The content of the package.
|
79
|
+
|
80
|
+
Returns:
|
81
|
+
Mail: The created mail object.
|
82
|
+
"""
|
83
|
+
pack = Package(category=category, package=package)
|
84
|
+
mail = Mail(
|
85
|
+
sender=sender,
|
86
|
+
recipient=recipient,
|
87
|
+
package=pack,
|
88
|
+
)
|
89
|
+
return mail
|
52
90
|
|
53
91
|
def delete_source(self, source_id):
|
92
|
+
"""
|
93
|
+
Deletes a source from the MailManager.
|
94
|
+
|
95
|
+
Args:
|
96
|
+
source_id (str): The ID of the source to be deleted.
|
97
|
+
|
98
|
+
Raises:
|
99
|
+
ValueError: If the source does not exist.
|
100
|
+
"""
|
54
101
|
if source_id not in self.sources:
|
55
102
|
raise ValueError(f"Source {source_id} does not exist.")
|
56
|
-
# if self.mails[source_id]:
|
57
|
-
# raise ValueError(f"None empty pending mails in source {source_id}")
|
58
103
|
self.sources.pop(source_id)
|
59
104
|
self.mails.pop(source_id)
|
60
105
|
|
61
|
-
def collect(self,
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
106
|
+
def collect(self, sender):
|
107
|
+
"""
|
108
|
+
Collects mails from a sender's outbox and queues them for the recipient.
|
109
|
+
|
110
|
+
Args:
|
111
|
+
sender (str): The ID of the sender.
|
112
|
+
|
113
|
+
Raises:
|
114
|
+
ValueError: If the sender or recipient source does not exist.
|
115
|
+
"""
|
116
|
+
if sender not in self.sources:
|
117
|
+
raise ValueError(f"Sender source {sender} does not exist.")
|
118
|
+
mailbox = (
|
119
|
+
self.sources[sender]
|
120
|
+
if isinstance(self.sources[sender], Exchange)
|
121
|
+
else self.sources[sender].mailbox
|
122
|
+
)
|
123
|
+
while mailbox.pending_outs.size() > 0:
|
124
|
+
mail_id = mailbox.pending_outs.popleft()
|
125
|
+
mail = mailbox.pile.pop(mail_id)
|
126
|
+
if mail.recipient not in self.sources:
|
127
|
+
raise ValueError(f"Recipient source {mail.recipient} does not exist")
|
128
|
+
if mail.sender not in self.mails[mail.recipient]:
|
129
|
+
self.mails[mail.recipient].update({mail.sender: deque()})
|
130
|
+
self.mails[mail.recipient][mail.sender].append(mail)
|
131
|
+
|
132
|
+
def send(self, recipient):
|
133
|
+
"""
|
134
|
+
Sends mails to a recipient's inbox.
|
135
|
+
|
136
|
+
Args:
|
137
|
+
recipient (str): The ID of the recipient.
|
138
|
+
|
139
|
+
Raises:
|
140
|
+
ValueError: If the recipient source does not exist.
|
141
|
+
"""
|
142
|
+
if recipient not in self.sources:
|
143
|
+
raise ValueError(f"Recipient source {recipient} does not exist.")
|
144
|
+
if not self.mails[recipient]:
|
78
145
|
return
|
79
|
-
for key in list(self.mails[
|
80
|
-
|
81
|
-
|
82
|
-
self.sources[
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
146
|
+
for key in list(self.mails[recipient].keys()):
|
147
|
+
pending_mails = self.mails[recipient].pop(key)
|
148
|
+
mailbox = (
|
149
|
+
self.sources[recipient]
|
150
|
+
if isinstance(self.sources[recipient], Exchange)
|
151
|
+
else self.sources[recipient].mailbox
|
152
|
+
)
|
153
|
+
while pending_mails:
|
154
|
+
mail = pending_mails.popleft()
|
155
|
+
mailbox.include(mail, "in")
|
87
156
|
|
88
157
|
def collect_all(self):
|
89
|
-
|
90
|
-
|
158
|
+
"""
|
159
|
+
Collects mails from all sources.
|
160
|
+
"""
|
161
|
+
for source in self.sources:
|
162
|
+
self.collect(get_lion_id(source))
|
91
163
|
|
92
164
|
def send_all(self):
|
93
|
-
|
94
|
-
|
165
|
+
"""
|
166
|
+
Sends mails to all sources.
|
167
|
+
"""
|
168
|
+
for source in self.sources:
|
169
|
+
self.send(get_lion_id(source))
|
95
170
|
|
96
171
|
async def execute(self, refresh_time=1):
|
172
|
+
"""
|
173
|
+
Continuously collects and sends mails until execution is stopped.
|
174
|
+
|
175
|
+
Args:
|
176
|
+
refresh_time (int): The time in seconds to wait between each cycle. Defaults to 1.
|
177
|
+
"""
|
97
178
|
while not self.execute_stop:
|
98
179
|
self.collect_all()
|
99
180
|
self.send_all()
|
@@ -0,0 +1,45 @@
|
|
1
|
+
from enum import Enum
|
2
|
+
from typing import Any
|
3
|
+
from pydantic import field_validator
|
4
|
+
from lionagi.core.collections.abc import Element, Field
|
5
|
+
|
6
|
+
|
7
|
+
class PackageCategory(str, Enum):
|
8
|
+
MESSAGE = "message"
|
9
|
+
TOOL = "tool"
|
10
|
+
IMODEL = "imodel"
|
11
|
+
NODE = "node"
|
12
|
+
NODE_LIST = "node_list"
|
13
|
+
NODE_ID = "node_id"
|
14
|
+
START = "start"
|
15
|
+
END = "end"
|
16
|
+
CONDITION = "condition"
|
17
|
+
|
18
|
+
|
19
|
+
class Package(Element):
|
20
|
+
|
21
|
+
request_source: str | None = None
|
22
|
+
|
23
|
+
category: PackageCategory = Field(
|
24
|
+
None,
|
25
|
+
title="Category",
|
26
|
+
description="The category of the package.",
|
27
|
+
)
|
28
|
+
|
29
|
+
package: Any = Field(
|
30
|
+
None,
|
31
|
+
title="Package",
|
32
|
+
description="The package to be delivered.",
|
33
|
+
)
|
34
|
+
|
35
|
+
@field_validator("category", mode="before")
|
36
|
+
def validate_category(cls, value: Any):
|
37
|
+
if value is None:
|
38
|
+
raise ValueError("Package category cannot be None.")
|
39
|
+
if isinstance(value, PackageCategory):
|
40
|
+
return value
|
41
|
+
else:
|
42
|
+
try:
|
43
|
+
return PackageCategory(value)
|
44
|
+
except Exception as e:
|
45
|
+
raise ValueError(f"Invalid value for category: {value}.") from e
|
@@ -0,0 +1,36 @@
|
|
1
|
+
from collections import deque
|
2
|
+
from pydantic import Field
|
3
|
+
from lionagi.core.generic.node import Node
|
4
|
+
from lionagi.core.mail.mail import Mail, Package
|
5
|
+
from lionagi.core.collections import Exchange
|
6
|
+
|
7
|
+
|
8
|
+
class StartMail(Node):
|
9
|
+
"""
|
10
|
+
Represents a start mail node that triggers the initiation of a process.
|
11
|
+
|
12
|
+
Attributes:
|
13
|
+
mailbox (Exchange): The exchange object that holds pending start mails.
|
14
|
+
"""
|
15
|
+
|
16
|
+
mailbox: Exchange = Field(
|
17
|
+
default_factory=Exchange[Mail], description="The pending start mail"
|
18
|
+
)
|
19
|
+
|
20
|
+
def trigger(self, context, structure_id, executable_id):
|
21
|
+
"""
|
22
|
+
Triggers the start mail by including it in the mailbox.
|
23
|
+
|
24
|
+
Args:
|
25
|
+
context (Any): The context to be included in the start mail.
|
26
|
+
structure_id (str): The ID of the structure to be initiated.
|
27
|
+
executable_id (str): The ID of the executable to receive the start mail.
|
28
|
+
"""
|
29
|
+
start_mail_content = {"context": context, "structure_id": structure_id}
|
30
|
+
pack = Package(category="start", package=start_mail_content)
|
31
|
+
start_mail = Mail(
|
32
|
+
sender=self.ln_id,
|
33
|
+
recipient=executable_id,
|
34
|
+
package=pack,
|
35
|
+
)
|
36
|
+
self.mailbox.include(start_mail, "out")
|
@@ -0,0 +1,19 @@
|
|
1
|
+
from .message import RoledMessage, MessageRole
|
2
|
+
from .system import System
|
3
|
+
from .instruction import Instruction
|
4
|
+
from .assistant_response import AssistantResponse
|
5
|
+
from .action_request import ActionRequest
|
6
|
+
from .action_response import ActionResponse
|
7
|
+
from .util import create_message
|
8
|
+
|
9
|
+
|
10
|
+
__all__ = [
|
11
|
+
"RoledMessage",
|
12
|
+
"MessageRole",
|
13
|
+
"System",
|
14
|
+
"Instruction",
|
15
|
+
"AssistantResponse",
|
16
|
+
"ActionRequest",
|
17
|
+
"ActionResponse",
|
18
|
+
"create_message",
|
19
|
+
]
|
@@ -0,0 +1,133 @@
|
|
1
|
+
"""
|
2
|
+
Copyright 2024 HaiyangLi
|
3
|
+
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
you may not use this file except in compliance with the License.
|
6
|
+
You may obtain a copy of the License at
|
7
|
+
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
See the License for the specific language governing permissions and
|
14
|
+
limitations under the License.
|
15
|
+
"""
|
16
|
+
|
17
|
+
import inspect
|
18
|
+
from pydantic import Field
|
19
|
+
from lionagi.libs import convert, ParseUtil
|
20
|
+
from .message import RoledMessage, MessageRole
|
21
|
+
|
22
|
+
|
23
|
+
class ActionRequest(RoledMessage):
|
24
|
+
"""
|
25
|
+
Represents a request for an action with function and arguments.
|
26
|
+
|
27
|
+
Inherits from `RoledMessage` and provides attributes specific to action requests.
|
28
|
+
|
29
|
+
Attributes:
|
30
|
+
function (str): The name of the function to be called.
|
31
|
+
arguments (dict): The keyword arguments to be passed to the function.
|
32
|
+
action_response (str): The ID of the action response that this request corresponds to.
|
33
|
+
"""
|
34
|
+
|
35
|
+
function: str | None = Field(
|
36
|
+
None, description="The name of the function to be called"
|
37
|
+
)
|
38
|
+
|
39
|
+
arguments: dict | None = Field(
|
40
|
+
None, description="The keyword arguments to be passed to the function"
|
41
|
+
)
|
42
|
+
|
43
|
+
action_response: str | None = Field(
|
44
|
+
None,
|
45
|
+
description="The id of the action response that this request corresponds to",
|
46
|
+
)
|
47
|
+
|
48
|
+
def __init__(
|
49
|
+
self,
|
50
|
+
function=None,
|
51
|
+
arguments=None,
|
52
|
+
sender=None, # sender is the assistant who made the request
|
53
|
+
recipient=None, # recipient is the actionable component
|
54
|
+
**kwargs,
|
55
|
+
):
|
56
|
+
"""
|
57
|
+
Initializes the ActionRequest.
|
58
|
+
|
59
|
+
Args:
|
60
|
+
function (str or function, optional): The function to be called.
|
61
|
+
arguments (dict, optional): The keyword arguments for the function.
|
62
|
+
sender (str, optional): The sender of the request.
|
63
|
+
recipient (str, optional): The recipient of the request.
|
64
|
+
"""
|
65
|
+
function = function.__name__ if inspect.isfunction(function) else function
|
66
|
+
arguments = _prepare_arguments(arguments)
|
67
|
+
|
68
|
+
super().__init__(
|
69
|
+
role=MessageRole.ASSISTANT,
|
70
|
+
sender=sender,
|
71
|
+
recipient=recipient,
|
72
|
+
content={"action_request": {"function": function, "arguments": arguments}},
|
73
|
+
**kwargs,
|
74
|
+
)
|
75
|
+
self.function = function
|
76
|
+
self.arguments = arguments
|
77
|
+
|
78
|
+
def is_responded(self):
|
79
|
+
"""
|
80
|
+
Checks if the action request has been responded to.
|
81
|
+
|
82
|
+
Returns:
|
83
|
+
bool: True if the action request has a response, otherwise False.
|
84
|
+
"""
|
85
|
+
return self.action_response is not None
|
86
|
+
|
87
|
+
def clone(self, **kwargs):
|
88
|
+
"""
|
89
|
+
Creates a copy of the current ActionRequest object with optional additional arguments.
|
90
|
+
|
91
|
+
This method clones the current object, preserving its function and arguments.
|
92
|
+
It also retains the original `action_response` and metadata, while allowing
|
93
|
+
for the addition of new attributes through keyword arguments.
|
94
|
+
|
95
|
+
Args:
|
96
|
+
**kwargs: Optional keyword arguments to be included in the cloned object.
|
97
|
+
|
98
|
+
Returns:
|
99
|
+
ActionRequest: A new instance of the object with the same function, arguments,
|
100
|
+
and additional keyword arguments.
|
101
|
+
"""
|
102
|
+
import json
|
103
|
+
|
104
|
+
arguments = json.dumps(self.arguments)
|
105
|
+
request_copy = ActionRequest(
|
106
|
+
function=self.function, arguments=json.loads(arguments), **kwargs
|
107
|
+
)
|
108
|
+
request_copy.action_response = self.action_response
|
109
|
+
request_copy.metadata["origin_ln_id"] = self.ln_id
|
110
|
+
return request_copy
|
111
|
+
|
112
|
+
|
113
|
+
def _prepare_arguments(arguments):
|
114
|
+
"""
|
115
|
+
Prepares the arguments for the action request.
|
116
|
+
|
117
|
+
Args:
|
118
|
+
arguments (Any): The arguments to be prepared.
|
119
|
+
|
120
|
+
Returns:
|
121
|
+
dict: The prepared arguments.
|
122
|
+
|
123
|
+
Raises:
|
124
|
+
ValueError: If the arguments are invalid.
|
125
|
+
"""
|
126
|
+
if not isinstance(arguments, dict):
|
127
|
+
try:
|
128
|
+
arguments = ParseUtil.fuzzy_parse_json(convert.to_str(arguments))
|
129
|
+
except Exception as e:
|
130
|
+
raise ValueError(f"Invalid arguments: {e}") from e
|
131
|
+
if isinstance(arguments, dict):
|
132
|
+
return arguments
|
133
|
+
raise ValueError(f"Invalid arguments: {arguments}")
|