lionagi 0.1.2__py3-none-any.whl → 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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} +62 -66
- lionagi/core/{execute → executor}/neo4j_executor.py +70 -67
- lionagi/core/generic/__init__.py +3 -33
- lionagi/core/generic/edge.py +29 -79
- 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/base.py +10 -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 +44 -46
- lionagi/integrations/storage/structure_excel.py +43 -26
- lionagi/integrations/storage/to_excel.py +11 -4
- lionagi/libs/__init__.py +22 -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/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 -240
- 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 -215
- 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 -532
- lionagi/core/generic/condition.py +0 -46
- 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 -283
- lionagi/experimental/report/form.py +0 -64
- lionagi/experimental/report/report.py +0 -138
- lionagi/experimental/report/util.py +0 -47
- 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/validator/rule.py +0 -139
- lionagi/experimental/validator/validator.py +0 -56
- lionagi/experimental/work/__init__.py +0 -10
- lionagi/experimental/work/async_queue.py +0 -54
- lionagi/experimental/work/schema.py +0 -73
- lionagi/experimental/work/work_function.py +0 -67
- lionagi/experimental/work/worker.py +0 -56
- lionagi/experimental/work2/form.py +0 -371
- lionagi/experimental/work2/report.py +0 -289
- lionagi/experimental/work2/schema.py +0 -30
- lionagi/experimental/work2/tests.py +0 -72
- lionagi/experimental/work2/work_function.py +0 -89
- lionagi/experimental/work2/worker.py +0 -12
- lionagi/integrations/bridge/llamaindex_/get_index.py +0 -294
- lionagi/tests/test_core/generic/test_component.py +0 -89
- 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.2.dist-info/LICENSE +0 -9
- lionagi-0.1.2.dist-info/METADATA +0 -174
- lionagi-0.1.2.dist-info/RECORD +0 -206
- /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/report → core/unit/template}/__init__.py +0 -0
- /lionagi/{experimental/tool → core/validator}/__init__.py +0 -0
- /lionagi/{experimental/validator → core/work}/__init__.py +0 -0
- /lionagi/experimental/{work2 → compressor}/__init__.py +0 -0
- /lionagi/{core/flow/mono_chat_mixin.py → experimental/directive/template/__init__.py} +0 -0
- /lionagi/experimental/directive/{schema.py → template/schema.py} +0 -0
- /lionagi/experimental/{work2/util.py → evaluator/__init__.py} +0 -0
- /lionagi/experimental/{work2/work.py → knowledge/__init__.py} +0 -0
- /lionagi/{tests/libs/test_async.py → experimental/knowledge/graph.py} +0 -0
- {lionagi-0.1.2.dist-info → lionagi-0.2.0.dist-info}/WHEEL +0 -0
- {lionagi-0.1.2.dist-info → lionagi-0.2.0.dist-info}/top_level.txt +0 -0
lionagi/core/graph/tree.py
DELETED
@@ -1,190 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
This module provides classes for representing and working with tree structures.
|
3
|
-
|
4
|
-
The module includes the following classes:
|
5
|
-
- TreeLabel: An enumeration representing tree relationships (parent and child).
|
6
|
-
- TreeNode: A specialized node representing a node within a tree structure,
|
7
|
-
extending the basic node functionality with parent-child relationships.
|
8
|
-
- Tree: Represents a tree structure, extending the graph with tree-specific
|
9
|
-
functionalities like managing parent-child relationships.
|
10
|
-
"""
|
11
|
-
|
12
|
-
from enum import Enum
|
13
|
-
from pydantic import Field
|
14
|
-
|
15
|
-
from lionagi.core.generic import Node, Condition
|
16
|
-
from lionagi.core.graph.graph import Graph
|
17
|
-
|
18
|
-
|
19
|
-
class TreeLabel(str, Enum):
|
20
|
-
"""
|
21
|
-
Enumeration representing tree relationships.
|
22
|
-
|
23
|
-
Attributes:
|
24
|
-
PARENT (str): Represents the parent edge.
|
25
|
-
CHILD (str): Represents the child edge.
|
26
|
-
"""
|
27
|
-
|
28
|
-
PARENT = "parent"
|
29
|
-
CHILD = "child"
|
30
|
-
|
31
|
-
|
32
|
-
class TreeNode(Node):
|
33
|
-
"""
|
34
|
-
A specialized node representing a node within a tree structure.
|
35
|
-
|
36
|
-
Extends the basic node functionality with parent-child relationships.
|
37
|
-
|
38
|
-
Attributes:
|
39
|
-
parent (Node | None): The parent node. Defaults to None if the node has
|
40
|
-
no parent.
|
41
|
-
"""
|
42
|
-
|
43
|
-
parent: Node | None = Field(
|
44
|
-
default=None,
|
45
|
-
description="The parent node, as an instance of Node.",
|
46
|
-
)
|
47
|
-
|
48
|
-
@property
|
49
|
-
def children(self) -> list[str]:
|
50
|
-
"""
|
51
|
-
Retrieves the IDs of all child nodes.
|
52
|
-
|
53
|
-
Returns:
|
54
|
-
list[str]: A list containing the IDs of the child nodes.
|
55
|
-
"""
|
56
|
-
if not self.parent:
|
57
|
-
return list(self.related_nodes)
|
58
|
-
else:
|
59
|
-
return [
|
60
|
-
node_id for node_id in self.related_nodes if node_id != self.parent.id_
|
61
|
-
]
|
62
|
-
|
63
|
-
def relate_child(
|
64
|
-
self,
|
65
|
-
child: Node | list[Node],
|
66
|
-
condition: Condition | None = None,
|
67
|
-
bundle: bool = False,
|
68
|
-
) -> None:
|
69
|
-
"""
|
70
|
-
Establishes a parent-child relationship between this node and the given
|
71
|
-
child node(s).
|
72
|
-
|
73
|
-
Args:
|
74
|
-
child (Node | list[Node]): The child node or list of child nodes to
|
75
|
-
be related.
|
76
|
-
condition (Condition | None): The condition associated with the
|
77
|
-
relationship, if any.
|
78
|
-
bundle (bool): Indicates whether to bundle the relation into a
|
79
|
-
single transaction. Defaults to False.
|
80
|
-
"""
|
81
|
-
children = [child] if isinstance(child, Node) else child
|
82
|
-
for _child in children:
|
83
|
-
self.relate(
|
84
|
-
_child,
|
85
|
-
node_as="head",
|
86
|
-
label=TreeLabel.PARENT,
|
87
|
-
condition=condition,
|
88
|
-
bundle=bundle,
|
89
|
-
)
|
90
|
-
if isinstance(_child, TreeNode):
|
91
|
-
_child.parent = self
|
92
|
-
|
93
|
-
def relate_parent(
|
94
|
-
self,
|
95
|
-
parent: Node,
|
96
|
-
condition: Condition | None = None,
|
97
|
-
bundle: bool = False,
|
98
|
-
) -> None:
|
99
|
-
"""
|
100
|
-
Establishes a parent-child relationship between the given parent node
|
101
|
-
and this node.
|
102
|
-
|
103
|
-
Args:
|
104
|
-
parent (Node): The parent node to be related.
|
105
|
-
condition (Condition | None): The condition associated with the
|
106
|
-
relationship, if any.
|
107
|
-
bundle (bool): Indicates whether to bundle the relation into a
|
108
|
-
single transaction. Defaults to False.
|
109
|
-
"""
|
110
|
-
if self.parent:
|
111
|
-
self.unrelate(self.parent)
|
112
|
-
self.relate(
|
113
|
-
parent,
|
114
|
-
node_as="tail",
|
115
|
-
label=TreeLabel.PARENT,
|
116
|
-
condition=condition,
|
117
|
-
bundle=bundle,
|
118
|
-
)
|
119
|
-
self.parent = parent
|
120
|
-
|
121
|
-
def unrelate_parent(self):
|
122
|
-
"""
|
123
|
-
Removes the parent relationship of this node.
|
124
|
-
"""
|
125
|
-
self.unrelate(self.parent)
|
126
|
-
self.parent = None
|
127
|
-
|
128
|
-
def unrelate_child(self, child: Node | list[Node]):
|
129
|
-
"""
|
130
|
-
Removes the child relationship between this node and the given child
|
131
|
-
node(s).
|
132
|
-
|
133
|
-
Args:
|
134
|
-
child (Node | list[Node]): The child node or list of child nodes to
|
135
|
-
be unrelated.
|
136
|
-
"""
|
137
|
-
children: list[Node] = [child] if isinstance(child, Node) else child
|
138
|
-
for _child in children:
|
139
|
-
self.unrelate(_child)
|
140
|
-
if isinstance(_child, TreeNode):
|
141
|
-
_child.parent = None
|
142
|
-
|
143
|
-
|
144
|
-
class Tree(Graph):
|
145
|
-
"""
|
146
|
-
Represents a tree structure, extending the graph with tree-specific
|
147
|
-
functionalities.
|
148
|
-
|
149
|
-
Manages parent-child relationships within the tree.
|
150
|
-
|
151
|
-
Attributes:
|
152
|
-
root (TreeNode | None): The root node of the tree. Defaults to None.
|
153
|
-
"""
|
154
|
-
|
155
|
-
root: TreeNode | None = Field(
|
156
|
-
default=None, description="The root node of the tree graph."
|
157
|
-
)
|
158
|
-
|
159
|
-
def relate_parent_child(
|
160
|
-
self,
|
161
|
-
parent: TreeNode,
|
162
|
-
children: list[TreeNode],
|
163
|
-
condition: Condition | None = None,
|
164
|
-
bundle: bool = False,
|
165
|
-
) -> None:
|
166
|
-
"""
|
167
|
-
Establishes parent-child relationships between the given parent node and
|
168
|
-
child node(s).
|
169
|
-
|
170
|
-
Args:
|
171
|
-
parent (TreeNode): The parent node.
|
172
|
-
children (list[TreeNode]): A list of child nodes.
|
173
|
-
condition (Condition | None): The condition associated with the
|
174
|
-
relationships, if any.
|
175
|
-
bundle (bool): Indicates whether to bundle the relations into a
|
176
|
-
single transaction. Defaults to False.
|
177
|
-
"""
|
178
|
-
children = [children] if isinstance(children, TreeNode) else children
|
179
|
-
for child in children:
|
180
|
-
if child.id_ not in self.internal_nodes:
|
181
|
-
self.add_node(child)
|
182
|
-
|
183
|
-
for child in children:
|
184
|
-
parent.relate_child(child, condition=condition, bundle=bundle)
|
185
|
-
|
186
|
-
if parent.id_ not in self.internal_nodes:
|
187
|
-
self.add_node(parent)
|
188
|
-
|
189
|
-
if self.root is None:
|
190
|
-
self.root = parent
|
lionagi/core/mail/schema.py
DELETED
@@ -1,63 +0,0 @@
|
|
1
|
-
from collections import deque
|
2
|
-
from enum import Enum
|
3
|
-
|
4
|
-
from lionagi.core.generic import Node
|
5
|
-
|
6
|
-
|
7
|
-
class MailCategory(str, Enum):
|
8
|
-
MESSAGES = "messages"
|
9
|
-
TOOL = "tool"
|
10
|
-
SERVICE = "service"
|
11
|
-
MODEL = "model"
|
12
|
-
NODE = "node"
|
13
|
-
NODE_LIST = "node_list"
|
14
|
-
NODE_ID = "node_id"
|
15
|
-
START = "start"
|
16
|
-
END = "end"
|
17
|
-
CONDITION = "condition"
|
18
|
-
|
19
|
-
|
20
|
-
class BaseMail:
|
21
|
-
|
22
|
-
def __init__(self, sender_id, recipient_id, category, package):
|
23
|
-
self.sender_id = sender_id
|
24
|
-
self.recipient_id = recipient_id
|
25
|
-
try:
|
26
|
-
if isinstance(category, str):
|
27
|
-
category = MailCategory(category)
|
28
|
-
if isinstance(category, MailCategory):
|
29
|
-
self.category = category
|
30
|
-
else:
|
31
|
-
raise ValueError(
|
32
|
-
f"Invalid request title. Valid titles are" f" {list(MailCategory)}"
|
33
|
-
)
|
34
|
-
except Exception as e:
|
35
|
-
raise ValueError(
|
36
|
-
f"Invalid request title. Valid titles are "
|
37
|
-
f"{list(MailCategory)}, Error: {e}"
|
38
|
-
) from e
|
39
|
-
self.package = package
|
40
|
-
|
41
|
-
|
42
|
-
class StartMail(Node):
|
43
|
-
|
44
|
-
def __init__(self, **kwargs):
|
45
|
-
super().__init__(**kwargs)
|
46
|
-
self.pending_outs = deque()
|
47
|
-
|
48
|
-
def trigger(self, context, structure_id, executable_id):
|
49
|
-
start_mail_content = {"context": context, "structure_id": structure_id}
|
50
|
-
start_mail = BaseMail(
|
51
|
-
sender_id=self.id_,
|
52
|
-
recipient_id=executable_id,
|
53
|
-
category="start",
|
54
|
-
package=start_mail_content,
|
55
|
-
)
|
56
|
-
self.pending_outs.append(start_mail)
|
57
|
-
|
58
|
-
|
59
|
-
class MailTransfer(Node):
|
60
|
-
def __init__(self):
|
61
|
-
super().__init__()
|
62
|
-
self.pending_ins = {}
|
63
|
-
self.pending_outs = deque()
|
lionagi/core/messages/schema.py
DELETED
@@ -1,325 +0,0 @@
|
|
1
|
-
from enum import Enum
|
2
|
-
|
3
|
-
from lionagi.libs import nested, convert
|
4
|
-
from lionagi.core.generic import Node
|
5
|
-
from lionagi.core.form.form import Form
|
6
|
-
|
7
|
-
_message_fields = ["node_id", "timestamp", "role", "sender", "recipient", "content"]
|
8
|
-
|
9
|
-
|
10
|
-
class BranchColumns(list[str], Enum):
|
11
|
-
COLUMNS = _message_fields
|
12
|
-
|
13
|
-
|
14
|
-
class MessageField(str, Enum):
|
15
|
-
NODE_ID = "node_id"
|
16
|
-
TIMESTAMP = "timestamp"
|
17
|
-
ROLE = "role"
|
18
|
-
SENDER = "sender"
|
19
|
-
RECIPIENT = "recipient"
|
20
|
-
CONTENT = "content"
|
21
|
-
METADATA = "metadata"
|
22
|
-
RELATION = "relation"
|
23
|
-
|
24
|
-
|
25
|
-
class MessageRoleType(str, Enum):
|
26
|
-
SYSTEM = "system"
|
27
|
-
USER = "user"
|
28
|
-
ASSISTANT = "assistant"
|
29
|
-
|
30
|
-
|
31
|
-
class MessageContentKey(str, Enum):
|
32
|
-
INSTRUCTION = "instruction"
|
33
|
-
CONTEXT = "context"
|
34
|
-
SYSTEM = "system_info"
|
35
|
-
ACTION_REQUEST = "action_request"
|
36
|
-
ACTION_RESPONSE = "action_response"
|
37
|
-
RESPONSE = "response"
|
38
|
-
|
39
|
-
|
40
|
-
class MessageType(dict, Enum):
|
41
|
-
SYSTEM = (
|
42
|
-
{
|
43
|
-
MessageField.ROLE.value: MessageRoleType.SYSTEM.value,
|
44
|
-
MessageField.SENDER.value: MessageRoleType.SYSTEM.value,
|
45
|
-
MessageField.RECIPIENT.value: "null",
|
46
|
-
"content_key": MessageContentKey.SYSTEM.value,
|
47
|
-
},
|
48
|
-
)
|
49
|
-
|
50
|
-
INSTRUCTION = (
|
51
|
-
{
|
52
|
-
MessageField.ROLE.value: MessageRoleType.USER.value,
|
53
|
-
MessageField.SENDER.value: MessageRoleType.USER.value,
|
54
|
-
MessageField.RECIPIENT.value: "null",
|
55
|
-
"content_key": MessageContentKey.INSTRUCTION.value,
|
56
|
-
},
|
57
|
-
)
|
58
|
-
|
59
|
-
CONTEXT = (
|
60
|
-
{
|
61
|
-
MessageField.ROLE.value: MessageRoleType.USER.value,
|
62
|
-
MessageField.SENDER.value: MessageRoleType.USER.value,
|
63
|
-
MessageField.RECIPIENT.value: "null",
|
64
|
-
"content_key": MessageContentKey.CONTEXT.value,
|
65
|
-
},
|
66
|
-
)
|
67
|
-
|
68
|
-
ACTION_REQUEST = (
|
69
|
-
{
|
70
|
-
MessageField.ROLE.value: MessageRoleType.ASSISTANT.value,
|
71
|
-
MessageField.SENDER.value: MessageRoleType.ASSISTANT.value,
|
72
|
-
MessageField.RECIPIENT.value: "null",
|
73
|
-
"content_key": MessageContentKey.ACTION_REQUEST.value,
|
74
|
-
},
|
75
|
-
)
|
76
|
-
|
77
|
-
ACTION_RESPONSE = (
|
78
|
-
{
|
79
|
-
MessageField.ROLE.value: MessageRoleType.ASSISTANT.value,
|
80
|
-
MessageField.SENDER.value: MessageRoleType.ASSISTANT.value,
|
81
|
-
MessageField.RECIPIENT.value: "null",
|
82
|
-
"content_key": MessageContentKey.ACTION_RESPONSE.value,
|
83
|
-
},
|
84
|
-
)
|
85
|
-
|
86
|
-
RESPONSE = {
|
87
|
-
MessageField.ROLE.value: MessageRoleType.ASSISTANT.value,
|
88
|
-
MessageField.SENDER.value: MessageRoleType.ASSISTANT.value,
|
89
|
-
MessageField.RECIPIENT.value: "null",
|
90
|
-
"content_key": MessageContentKey.RESPONSE.value,
|
91
|
-
}
|
92
|
-
|
93
|
-
|
94
|
-
class BaseMessage(Node):
|
95
|
-
"""
|
96
|
-
Represents a message in a chatbot-like system, inheriting from BaseNode.
|
97
|
-
|
98
|
-
Attributes:
|
99
|
-
role (str | None): The role of the entity sending the message, e.g., 'user', 'system'.
|
100
|
-
sender (str | None): The identifier of the sender of the message.
|
101
|
-
content (Any): The actual content of the message.
|
102
|
-
"""
|
103
|
-
|
104
|
-
role: str | None = None
|
105
|
-
sender: str | None = None
|
106
|
-
recipient: str | None = None
|
107
|
-
|
108
|
-
@property
|
109
|
-
def msg(self) -> dict:
|
110
|
-
"""
|
111
|
-
Constructs and returns a dictionary representation of the message.
|
112
|
-
|
113
|
-
Returns:
|
114
|
-
A dictionary representation of the message with 'role' and 'content' keys.
|
115
|
-
"""
|
116
|
-
return self._to_message()
|
117
|
-
|
118
|
-
@property
|
119
|
-
def msg_content(self) -> str | dict:
|
120
|
-
"""
|
121
|
-
Gets the 'content' field of the message.
|
122
|
-
|
123
|
-
Returns:
|
124
|
-
The 'content' part of the message.
|
125
|
-
"""
|
126
|
-
return self.msg["content"]
|
127
|
-
|
128
|
-
def _to_message(self):
|
129
|
-
"""
|
130
|
-
Constructs and returns a dictionary representation of the message.
|
131
|
-
|
132
|
-
Returns:
|
133
|
-
dict: A dictionary representation of the message with 'role' and 'content' keys.
|
134
|
-
"""
|
135
|
-
return {"role": self.role, "content": convert.to_str(self.content)}
|
136
|
-
|
137
|
-
def __str__(self):
|
138
|
-
content_preview = (
|
139
|
-
f"{str(self.content)[:75]}..."
|
140
|
-
if self.content and len(self.content) > 75
|
141
|
-
else str(self.content)
|
142
|
-
)
|
143
|
-
return f"Message(role={self.role}, sender={self.sender}, content='{content_preview}')"
|
144
|
-
|
145
|
-
|
146
|
-
class Instruction(BaseMessage):
|
147
|
-
"""
|
148
|
-
Represents an instruction message, a specialized subclass of Message.
|
149
|
-
|
150
|
-
This class is specifically used for creating messages that are instructions from the user,
|
151
|
-
including any associated context. It sets the message role to 'user'.
|
152
|
-
"""
|
153
|
-
|
154
|
-
def __init__(
|
155
|
-
self,
|
156
|
-
instruction: dict | list | str,
|
157
|
-
context=None,
|
158
|
-
sender: str | None = None,
|
159
|
-
output_fields=None,
|
160
|
-
recipient=None,
|
161
|
-
):
|
162
|
-
super().__init__(
|
163
|
-
role="user",
|
164
|
-
sender=sender or "user",
|
165
|
-
content={"instruction": instruction},
|
166
|
-
recipient=recipient or "assistant",
|
167
|
-
)
|
168
|
-
if context:
|
169
|
-
self.content.update({"context": context})
|
170
|
-
|
171
|
-
if output_fields:
|
172
|
-
format_ = f"""
|
173
|
-
MUST EXACTLY Follow the following response format. NO ADDITIONAL COMMENTS ALLOWED!
|
174
|
-
```json
|
175
|
-
{output_fields}
|
176
|
-
```
|
177
|
-
"""
|
178
|
-
self.content.update(
|
179
|
-
{"response_format": format_.replace(" ", "")}
|
180
|
-
)
|
181
|
-
|
182
|
-
@property
|
183
|
-
def instruct(self):
|
184
|
-
return self.content["instruction"]
|
185
|
-
|
186
|
-
@classmethod
|
187
|
-
def from_form(
|
188
|
-
cls,
|
189
|
-
form: Form,
|
190
|
-
sender: str | None = None,
|
191
|
-
recipient=None,
|
192
|
-
):
|
193
|
-
return cls(
|
194
|
-
instruction=form.instruction,
|
195
|
-
context=form.instruction_context,
|
196
|
-
output_fields=form.instruction_output_fields,
|
197
|
-
sender=sender,
|
198
|
-
recipient=recipient,
|
199
|
-
)
|
200
|
-
|
201
|
-
|
202
|
-
class System(BaseMessage):
|
203
|
-
"""
|
204
|
-
Represents a system-related message, a specialized subclass of Message.
|
205
|
-
|
206
|
-
Designed for messages containing system information, this class sets the message role to 'system'.
|
207
|
-
"""
|
208
|
-
|
209
|
-
def __init__(
|
210
|
-
self, system: dict | list | str, sender: str | None = None, recipient=None
|
211
|
-
):
|
212
|
-
super().__init__(
|
213
|
-
role="system",
|
214
|
-
sender=sender or "system",
|
215
|
-
content={"system_info": system},
|
216
|
-
recipient=recipient or "assistant",
|
217
|
-
)
|
218
|
-
|
219
|
-
@property
|
220
|
-
def system_info(self):
|
221
|
-
return self.content["system_info"]
|
222
|
-
|
223
|
-
|
224
|
-
class Response(BaseMessage):
|
225
|
-
"""
|
226
|
-
Represents a response message, a specialized subclass of Message.
|
227
|
-
|
228
|
-
Used for various types of response messages including regular responses, action requests,
|
229
|
-
and action responses. It sets the message role to 'assistant'.
|
230
|
-
|
231
|
-
"""
|
232
|
-
|
233
|
-
def __init__(
|
234
|
-
self, response: dict | list | str, sender: str | None = None, recipient=None
|
235
|
-
) -> None:
|
236
|
-
content_key = ""
|
237
|
-
try:
|
238
|
-
response = response["message"]
|
239
|
-
if convert.strip_lower(response["content"]) == "none":
|
240
|
-
content_ = self._handle_action_request(response)
|
241
|
-
sender = sender or "action_request"
|
242
|
-
content_key = content_key or "action_request"
|
243
|
-
recipient = recipient or "action"
|
244
|
-
|
245
|
-
else:
|
246
|
-
try:
|
247
|
-
if "tool_uses" in convert.to_dict(response["content"]):
|
248
|
-
content_ = convert.to_dict(response["content"])["tool_uses"]
|
249
|
-
content_key = content_key or "action_request"
|
250
|
-
sender = sender or "action_request"
|
251
|
-
recipient = recipient or "action"
|
252
|
-
|
253
|
-
elif "response" in convert.to_dict(response["content"]):
|
254
|
-
sender = sender or "assistant"
|
255
|
-
content_key = content_key or "response"
|
256
|
-
content_ = convert.to_dict(response["content"])["response"]
|
257
|
-
recipient = recipient or "user"
|
258
|
-
|
259
|
-
elif "action_request" in convert.to_dict(response["content"]):
|
260
|
-
sender = sender or "action_request"
|
261
|
-
content_key = content_key or "action_request"
|
262
|
-
content_ = convert.to_dict(response["content"])[
|
263
|
-
"action_request"
|
264
|
-
]
|
265
|
-
recipient = recipient or "action"
|
266
|
-
|
267
|
-
else:
|
268
|
-
content_ = response["content"]
|
269
|
-
content_key = content_key or "response"
|
270
|
-
sender = sender or "assistant"
|
271
|
-
recipient = recipient or "user"
|
272
|
-
except Exception:
|
273
|
-
content_ = response["content"]
|
274
|
-
content_key = content_key or "response"
|
275
|
-
sender = sender or "assistant"
|
276
|
-
recipient = recipient or "user"
|
277
|
-
|
278
|
-
except Exception:
|
279
|
-
sender = sender or "action_response"
|
280
|
-
content_ = response
|
281
|
-
content_key = content_key or "action_response"
|
282
|
-
recipient = recipient or "assistant"
|
283
|
-
|
284
|
-
super().__init__(
|
285
|
-
role="assistant",
|
286
|
-
sender=sender,
|
287
|
-
content={content_key: content_},
|
288
|
-
recipient=recipient,
|
289
|
-
)
|
290
|
-
|
291
|
-
@staticmethod
|
292
|
-
def _handle_action_request(response):
|
293
|
-
"""
|
294
|
-
Processes an action request response and extracts relevant information.
|
295
|
-
|
296
|
-
Args:
|
297
|
-
response (dict): The response dictionary containing tool calls and other information.
|
298
|
-
|
299
|
-
Returns:
|
300
|
-
list: A list of dictionaries, each representing a function call with action and arguments.
|
301
|
-
|
302
|
-
Raises:
|
303
|
-
ValueError: If the response does not conform to the expected format for action requests.
|
304
|
-
"""
|
305
|
-
try:
|
306
|
-
tool_count = 0
|
307
|
-
func_list = []
|
308
|
-
while tool_count < len(response["tool_calls"]):
|
309
|
-
_path = ["tool_calls", tool_count, "type"]
|
310
|
-
|
311
|
-
if nested.nget(response, _path) == "function":
|
312
|
-
_path1 = ["tool_calls", tool_count, "function", "name"]
|
313
|
-
_path2 = ["tool_calls", tool_count, "function", "arguments"]
|
314
|
-
|
315
|
-
func_content = {
|
316
|
-
"action": f"action_{nested.nget(response, _path1)}",
|
317
|
-
"arguments": nested.nget(response, _path2),
|
318
|
-
}
|
319
|
-
func_list.append(func_content)
|
320
|
-
tool_count += 1
|
321
|
-
return func_list
|
322
|
-
except:
|
323
|
-
raise ValueError(
|
324
|
-
"Response message must be one of regular response or function calling"
|
325
|
-
)
|
lionagi/core/tool/__init__.py
DELETED
lionagi/core/tool/tool.py
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
from typing import Any
|
2
|
-
|
3
|
-
from pydantic import field_serializer
|
4
|
-
from lionagi.core.generic import Node
|
5
|
-
|
6
|
-
|
7
|
-
class Tool(Node):
|
8
|
-
"""
|
9
|
-
Represents a tool, extending BaseNode with specific functionalities and configurations.
|
10
|
-
|
11
|
-
Attributes:
|
12
|
-
func: The main function or capability of the tool.
|
13
|
-
schema_: An optional schema defining the structure and constraints of data the tool works with.
|
14
|
-
manual: Optional documentation or manual for using the tool.
|
15
|
-
parser: An optional parser associated with the tool for data processing or interpretation.
|
16
|
-
"""
|
17
|
-
|
18
|
-
func: Any
|
19
|
-
schema_: dict | None = None
|
20
|
-
manual: Any | None = None
|
21
|
-
parser: Any | None = None
|
22
|
-
|
23
|
-
@field_serializer("func")
|
24
|
-
def serialize_func(self, func):
|
25
|
-
return func.__name__
|
26
|
-
|
27
|
-
|
28
|
-
TOOL_TYPE = bool | Tool | str | list[Tool | str | dict] | dict
|