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
@@ -0,0 +1,236 @@
|
|
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 contextlib
|
18
|
+
from lionagi.libs import SysUtil
|
19
|
+
from pydantic import Field, field_validator
|
20
|
+
from .abc import Ordering, get_lion_id, ItemNotFoundError, LionIDable, Element
|
21
|
+
from .util import _validate_order
|
22
|
+
|
23
|
+
|
24
|
+
class Progression(Element, Ordering):
|
25
|
+
|
26
|
+
name: str | None = Field(
|
27
|
+
None,
|
28
|
+
title="Name",
|
29
|
+
description="The name of the progression.",
|
30
|
+
)
|
31
|
+
order: list = Field(
|
32
|
+
default_factory=list,
|
33
|
+
title="Order",
|
34
|
+
description="The order of the progression.",
|
35
|
+
)
|
36
|
+
|
37
|
+
@field_validator("order", mode="before")
|
38
|
+
def _validate_order(cls, value) -> list[str]:
|
39
|
+
"""Validate and convert the order field."""
|
40
|
+
return _validate_order(value)
|
41
|
+
|
42
|
+
def __contains__(self, item):
|
43
|
+
"""Check if an item or items are in the progression."""
|
44
|
+
if not item:
|
45
|
+
return False
|
46
|
+
if isinstance(item, Progression):
|
47
|
+
return all([i in self.order for i in item.order])
|
48
|
+
if isinstance(item, LionIDable) and len(a := get_lion_id(item)) == 32:
|
49
|
+
return a in self.order
|
50
|
+
item = self._validate_order(item)
|
51
|
+
return all([i in self.order for i in item])
|
52
|
+
|
53
|
+
def __len__(self):
|
54
|
+
return len(self.order)
|
55
|
+
|
56
|
+
def keys(self):
|
57
|
+
yield from range(len(self))
|
58
|
+
|
59
|
+
def values(self):
|
60
|
+
yield from self.order
|
61
|
+
|
62
|
+
def items(self):
|
63
|
+
for idx, item in enumerate(self.order):
|
64
|
+
yield idx, item
|
65
|
+
|
66
|
+
def size(self):
|
67
|
+
return len(self)
|
68
|
+
|
69
|
+
def copy(self):
|
70
|
+
"""create a deep copy"""
|
71
|
+
return self.model_copy()
|
72
|
+
|
73
|
+
def append(self, item):
|
74
|
+
"""Append an item to the end of the progression."""
|
75
|
+
id_ = get_lion_id(item)
|
76
|
+
self.order.append(id_)
|
77
|
+
|
78
|
+
def extend(self, item):
|
79
|
+
"""Extend the progression from the right with item(s)"""
|
80
|
+
if isinstance(item, Progression):
|
81
|
+
self.order.extend(item.order)
|
82
|
+
return
|
83
|
+
order = self._validate_order(item)
|
84
|
+
self.order.extend(order)
|
85
|
+
|
86
|
+
def include(self, item) -> bool:
|
87
|
+
"""Include item(s) in the progression. return true if the item is included."""
|
88
|
+
if item not in self:
|
89
|
+
self.extend(item)
|
90
|
+
return item in self
|
91
|
+
|
92
|
+
def __getitem__(self, key):
|
93
|
+
with contextlib.suppress(IndexError):
|
94
|
+
a = self.order[key]
|
95
|
+
if isinstance(a, list) and len(a) > 1:
|
96
|
+
return Progression(order=a)
|
97
|
+
elif isinstance(a, list) and len(a) == 1:
|
98
|
+
return a[0]
|
99
|
+
return a
|
100
|
+
|
101
|
+
raise ItemNotFoundError(f"index {key}")
|
102
|
+
|
103
|
+
def remove(self, item: LionIDable):
|
104
|
+
"""Remove the next occurrence of an item from the progression."""
|
105
|
+
if item in self:
|
106
|
+
item = self._validate_order(item)
|
107
|
+
l_ = SysUtil.create_copy(self.order)
|
108
|
+
|
109
|
+
with contextlib.suppress(Exception):
|
110
|
+
for i in item:
|
111
|
+
l_.remove(i)
|
112
|
+
self.order = l_
|
113
|
+
return
|
114
|
+
|
115
|
+
raise ItemNotFoundError(f"{item}")
|
116
|
+
|
117
|
+
def __list__(self):
|
118
|
+
return self.order
|
119
|
+
|
120
|
+
def popleft(self):
|
121
|
+
"""Remove and return the leftmost item from the progression."""
|
122
|
+
try:
|
123
|
+
return self.order.pop(0)
|
124
|
+
except IndexError as e:
|
125
|
+
raise ItemNotFoundError("None") from e
|
126
|
+
|
127
|
+
def pop(self, index=None):
|
128
|
+
"""Remove and return an item from the progression."""
|
129
|
+
try:
|
130
|
+
return self.order.pop(index)
|
131
|
+
except IndexError as e:
|
132
|
+
raise ItemNotFoundError("None") from e
|
133
|
+
|
134
|
+
def exclude(self, item) -> bool:
|
135
|
+
"""Exclude an item or items from the progression."""
|
136
|
+
if isinstance(item, int) and item > 0:
|
137
|
+
if item > len(self):
|
138
|
+
raise IndexError("Cannot remove more items than available.")
|
139
|
+
for _ in range(item):
|
140
|
+
self.popleft()
|
141
|
+
return True
|
142
|
+
if isinstance(item, Progression):
|
143
|
+
for i in item:
|
144
|
+
while i in self:
|
145
|
+
self.remove(i)
|
146
|
+
for i in (a := self._validate_order(item)):
|
147
|
+
while i in self:
|
148
|
+
self.remove(i)
|
149
|
+
return a not in self
|
150
|
+
|
151
|
+
def __add__(self, other):
|
152
|
+
"""Add an item or items to the end of the progression."""
|
153
|
+
_copy = self.copy()
|
154
|
+
_copy.include(other)
|
155
|
+
return _copy
|
156
|
+
|
157
|
+
def __radd__(self, other):
|
158
|
+
if not isinstance(other, Progression):
|
159
|
+
_copy = self.copy()
|
160
|
+
l_ = SysUtil.create_copy(_copy.order)
|
161
|
+
l_.insert(0, get_lion_id(other))
|
162
|
+
_copy.order = l_
|
163
|
+
return _copy
|
164
|
+
|
165
|
+
return other + self
|
166
|
+
|
167
|
+
def __setitem__(self, key, value):
|
168
|
+
a = self._validate_order(value)
|
169
|
+
self.order[key] = a
|
170
|
+
|
171
|
+
def __iadd__(self, other):
|
172
|
+
"""Add an item or items to the end of the progression."""
|
173
|
+
return self + other
|
174
|
+
|
175
|
+
def __isub__(self, other):
|
176
|
+
"""Remove an item or items from the beginning of the progression."""
|
177
|
+
return self - other
|
178
|
+
|
179
|
+
def __sub__(self, other):
|
180
|
+
"""Remove an item or items from the progression."""
|
181
|
+
_copy = self.copy()
|
182
|
+
_copy.remove(other)
|
183
|
+
return _copy
|
184
|
+
|
185
|
+
def __len__(self):
|
186
|
+
"""Return the length of the progression."""
|
187
|
+
return len(self.order)
|
188
|
+
|
189
|
+
def __iter__(self):
|
190
|
+
"""Iterate over the items in the progression."""
|
191
|
+
return iter(self.order)
|
192
|
+
|
193
|
+
def __next__(self):
|
194
|
+
"""Return the next item in the progression."""
|
195
|
+
return self.popleft()
|
196
|
+
|
197
|
+
def __repr__(self):
|
198
|
+
"""Return a string representation of the progression."""
|
199
|
+
return f"Progression({self.order})"
|
200
|
+
|
201
|
+
def __str__(self):
|
202
|
+
"""Return a string representation of the progression."""
|
203
|
+
return f"Progression({self.order})"
|
204
|
+
|
205
|
+
def __list__(self):
|
206
|
+
"""Return a list representation of the progression."""
|
207
|
+
return SysUtil.create_copy(self.order)
|
208
|
+
|
209
|
+
def __reversed__(self):
|
210
|
+
"""Return a reversed progression."""
|
211
|
+
return reversed(self.order)
|
212
|
+
|
213
|
+
def clear(self):
|
214
|
+
"""Clear the progression."""
|
215
|
+
self.order = []
|
216
|
+
|
217
|
+
def to_dict(self):
|
218
|
+
"""Return a dictionary representation of the progression."""
|
219
|
+
return {"order": self.order, "name": self.name}
|
220
|
+
|
221
|
+
def __bool__(self):
|
222
|
+
return True
|
223
|
+
|
224
|
+
def __eq__(self, other):
|
225
|
+
"""Compare two Progression instances for equality."""
|
226
|
+
if not isinstance(other, Progression):
|
227
|
+
return NotImplemented
|
228
|
+
return self.order == other.order and self.name == other.name
|
229
|
+
|
230
|
+
def __hash__(self):
|
231
|
+
"""Return a hash value for the Progression instance."""
|
232
|
+
return hash((tuple(self.order), self.name))
|
233
|
+
|
234
|
+
|
235
|
+
def progression(order=None, name=None) -> Progression:
|
236
|
+
return Progression(order=order, name=name)
|
@@ -0,0 +1,64 @@
|
|
1
|
+
from collections.abc import Mapping, Generator
|
2
|
+
from collections import deque
|
3
|
+
|
4
|
+
from .abc import LionTypeError, Record, Ordering, Component, get_lion_id, Element
|
5
|
+
|
6
|
+
|
7
|
+
def to_list_type(value):
|
8
|
+
"""
|
9
|
+
Convert the provided value to a list.
|
10
|
+
|
11
|
+
This function ensures that the input value is converted to a list,
|
12
|
+
regardless of its original type. It handles various types including
|
13
|
+
Component, Mapping, Record, tuple, list, set, Generator, and deque.
|
14
|
+
|
15
|
+
Args:
|
16
|
+
value: The value to convert to a list.
|
17
|
+
|
18
|
+
Returns:
|
19
|
+
list: The converted list.
|
20
|
+
|
21
|
+
Raises:
|
22
|
+
TypeError: If the value cannot be converted to a list.
|
23
|
+
"""
|
24
|
+
if isinstance(value, Component) and not isinstance(value, (Record, Ordering)):
|
25
|
+
return [value]
|
26
|
+
if isinstance(value, (Mapping, Record)):
|
27
|
+
return list(value.values())
|
28
|
+
# if isinstance(value, Ordering):
|
29
|
+
# return list(value.order)
|
30
|
+
if isinstance(value, (tuple, list, set, Generator, deque)):
|
31
|
+
return list(value)
|
32
|
+
return [value]
|
33
|
+
|
34
|
+
|
35
|
+
def _validate_order(value) -> list[str]:
|
36
|
+
"""
|
37
|
+
Validate and convert the order field to a list of strings.
|
38
|
+
|
39
|
+
This function ensures that the input value is a valid order and converts it to a list of strings.
|
40
|
+
It handles various input types including string, Ordering, and Element.
|
41
|
+
|
42
|
+
Args:
|
43
|
+
value: The value to validate and convert.
|
44
|
+
|
45
|
+
Returns:
|
46
|
+
list[str]: The validated and converted order list.
|
47
|
+
|
48
|
+
Raises:
|
49
|
+
LionTypeError: If the value contains invalid types.
|
50
|
+
"""
|
51
|
+
if value is None:
|
52
|
+
return []
|
53
|
+
if isinstance(value, str) and len(value) == 32:
|
54
|
+
return [value]
|
55
|
+
elif isinstance(value, Ordering):
|
56
|
+
return value.order
|
57
|
+
|
58
|
+
elif isinstance(value, Element):
|
59
|
+
return [value.ln_id]
|
60
|
+
|
61
|
+
try:
|
62
|
+
return [i for item in to_list_type(value) if (i := get_lion_id(item))]
|
63
|
+
except Exception as e:
|
64
|
+
raise LionTypeError("Progression must only contain lion ids.") from e
|
@@ -0,0 +1,314 @@
|
|
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
|
+
from lionagi.core.unit.unit import Unit
|
18
|
+
from lionagi.core.session.branch import Branch
|
19
|
+
|
20
|
+
|
21
|
+
async def chat(
|
22
|
+
instruction=None,
|
23
|
+
context=None,
|
24
|
+
system=None,
|
25
|
+
sender=None,
|
26
|
+
recipient=None,
|
27
|
+
branch=None,
|
28
|
+
form=None,
|
29
|
+
confidence_score=None,
|
30
|
+
reason=False,
|
31
|
+
**kwargs,
|
32
|
+
):
|
33
|
+
"""
|
34
|
+
Performs a chat operation using the specified parameters.
|
35
|
+
|
36
|
+
Args:
|
37
|
+
instruction (str, optional): The instruction for the chat.
|
38
|
+
context (Any, optional): The context to perform the instruction on.
|
39
|
+
system (Any, optional): The system context for the chat.
|
40
|
+
sender (str, optional): The sender of the instruction.
|
41
|
+
recipient (str, optional): The recipient of the instruction.
|
42
|
+
branch (Branch, optional): The branch to use for the chat.
|
43
|
+
form (Any, optional): The form to create instruction from.
|
44
|
+
confidence_score (float, optional): The confidence score for the operation.
|
45
|
+
reason (bool, optional): Whether to include a reason for the operation.
|
46
|
+
**kwargs: Additional keyword arguments for the chat operation.
|
47
|
+
|
48
|
+
Returns:
|
49
|
+
Any: The result of the chat operation.
|
50
|
+
"""
|
51
|
+
branch = branch or Branch()
|
52
|
+
unit = Unit(branch)
|
53
|
+
|
54
|
+
return await unit.chat(
|
55
|
+
instruction=instruction,
|
56
|
+
context=context,
|
57
|
+
system=system,
|
58
|
+
sender=sender,
|
59
|
+
recipient=recipient,
|
60
|
+
form=form,
|
61
|
+
confidence_score=confidence_score,
|
62
|
+
reason=reason,
|
63
|
+
branch=branch,
|
64
|
+
**kwargs,
|
65
|
+
)
|
66
|
+
|
67
|
+
|
68
|
+
async def select(
|
69
|
+
instruction=None,
|
70
|
+
context=None,
|
71
|
+
system=None,
|
72
|
+
sender=None,
|
73
|
+
recipient=None,
|
74
|
+
choices=None,
|
75
|
+
branch=None,
|
76
|
+
form=None,
|
77
|
+
confidence_score=None,
|
78
|
+
reason=False,
|
79
|
+
**kwargs,
|
80
|
+
):
|
81
|
+
"""
|
82
|
+
Performs a select operation using the specified parameters.
|
83
|
+
|
84
|
+
Args:
|
85
|
+
instruction (str, optional): The instruction for the selection.
|
86
|
+
context (Any, optional): The context to perform the instruction on.
|
87
|
+
system (Any, optional): The system context for the selection.
|
88
|
+
sender (str, optional): The sender of the instruction.
|
89
|
+
recipient (str, optional): The recipient of the instruction.
|
90
|
+
choices (list, optional): The choices for the selection.
|
91
|
+
branch (Branch, optional): The branch to use for the selection.
|
92
|
+
form (Any, optional): The form to create instruction from.
|
93
|
+
confidence_score (float, optional): The confidence score for the operation.
|
94
|
+
reason (bool, optional): Whether to include a reason for the operation.
|
95
|
+
**kwargs: Additional keyword arguments for the selection operation.
|
96
|
+
|
97
|
+
Returns:
|
98
|
+
Any: The result of the selection operation.
|
99
|
+
"""
|
100
|
+
branch = branch or Branch()
|
101
|
+
unit = Unit(branch)
|
102
|
+
|
103
|
+
return await unit.select(
|
104
|
+
instruction=instruction,
|
105
|
+
context=context,
|
106
|
+
system=system,
|
107
|
+
sender=sender,
|
108
|
+
recipient=recipient,
|
109
|
+
choices=choices,
|
110
|
+
form=form,
|
111
|
+
confidence_score=confidence_score,
|
112
|
+
reason=reason,
|
113
|
+
**kwargs,
|
114
|
+
)
|
115
|
+
|
116
|
+
|
117
|
+
async def predict(
|
118
|
+
instruction=None,
|
119
|
+
context=None,
|
120
|
+
system=None,
|
121
|
+
sender=None,
|
122
|
+
recipient=None,
|
123
|
+
branch=None,
|
124
|
+
form=None,
|
125
|
+
confidence_score=None,
|
126
|
+
reason=False,
|
127
|
+
num_sentences=1,
|
128
|
+
**kwargs,
|
129
|
+
):
|
130
|
+
"""
|
131
|
+
Performs a predict operation using the specified parameters.
|
132
|
+
|
133
|
+
Args:
|
134
|
+
instruction (str, optional): The instruction for the prediction.
|
135
|
+
context (Any, optional): The context to perform the instruction on.
|
136
|
+
system (Any, optional): The system context for the prediction.
|
137
|
+
sender (str, optional): The sender of the instruction.
|
138
|
+
recipient (str, optional): The recipient of the instruction.
|
139
|
+
branch (Branch, optional): The branch to use for the prediction.
|
140
|
+
form (Any, optional): The form to create instruction from.
|
141
|
+
confidence_score (float, optional): The confidence score for the operation.
|
142
|
+
reason (bool, optional): Whether to include a reason for the operation.
|
143
|
+
**kwargs: Additional keyword arguments for the prediction operation.
|
144
|
+
|
145
|
+
Returns:
|
146
|
+
Any: The result of the prediction operation.
|
147
|
+
"""
|
148
|
+
branch = branch or Branch()
|
149
|
+
unit = Unit(branch)
|
150
|
+
|
151
|
+
return await unit.predict(
|
152
|
+
instruction=instruction,
|
153
|
+
context=context,
|
154
|
+
system=system,
|
155
|
+
sender=sender,
|
156
|
+
recipient=recipient,
|
157
|
+
form=form,
|
158
|
+
confidence_score=confidence_score,
|
159
|
+
reason=reason,
|
160
|
+
num_sentences=num_sentences,
|
161
|
+
**kwargs,
|
162
|
+
)
|
163
|
+
|
164
|
+
|
165
|
+
async def act(
|
166
|
+
instruction=None,
|
167
|
+
context=None,
|
168
|
+
system=None,
|
169
|
+
sender=None,
|
170
|
+
recipient=None,
|
171
|
+
branch=None,
|
172
|
+
form=None,
|
173
|
+
confidence_score=None,
|
174
|
+
reason=False,
|
175
|
+
**kwargs,
|
176
|
+
):
|
177
|
+
"""
|
178
|
+
Performs an act operation using the specified parameters.
|
179
|
+
|
180
|
+
Args:
|
181
|
+
instruction (str, optional): The instruction for the action.
|
182
|
+
context (Any, optional): The context to perform the instruction on.
|
183
|
+
system (Any, optional): The system context for the action.
|
184
|
+
sender (str, optional): The sender of the instruction.
|
185
|
+
recipient (str, optional): The recipient of the instruction.
|
186
|
+
branch (Branch, optional): The branch to use for the action.
|
187
|
+
form (Any, optional): The form to create instruction from.
|
188
|
+
confidence_score (float, optional): The confidence score for the operation.
|
189
|
+
reason (bool, optional): Whether to include a reason for the operation.
|
190
|
+
**kwargs: Additional keyword arguments for the act operation.
|
191
|
+
|
192
|
+
Returns:
|
193
|
+
Any: The result of the act operation.
|
194
|
+
"""
|
195
|
+
branch = branch or Branch()
|
196
|
+
unit = Unit(branch)
|
197
|
+
|
198
|
+
return await unit.act(
|
199
|
+
instruction=instruction,
|
200
|
+
context=context,
|
201
|
+
system=system,
|
202
|
+
sender=sender,
|
203
|
+
recipient=recipient,
|
204
|
+
form=form,
|
205
|
+
confidence_score=confidence_score,
|
206
|
+
reason=reason,
|
207
|
+
**kwargs,
|
208
|
+
)
|
209
|
+
|
210
|
+
|
211
|
+
async def score(
|
212
|
+
instruction=None,
|
213
|
+
context=None,
|
214
|
+
system=None,
|
215
|
+
sender=None,
|
216
|
+
recipient=None,
|
217
|
+
branch=None,
|
218
|
+
form=None,
|
219
|
+
confidence_score=None,
|
220
|
+
reason=False,
|
221
|
+
score_range=None,
|
222
|
+
include_endpoints=None,
|
223
|
+
num_digit=None,
|
224
|
+
**kwargs,
|
225
|
+
):
|
226
|
+
"""
|
227
|
+
Asynchronously performs a scoring task within a given context.
|
228
|
+
|
229
|
+
Args:
|
230
|
+
instruction (str, optional): Additional instruction for the scoring task.
|
231
|
+
context (Any, optional): Context to perform the scoring task on.
|
232
|
+
system (str, optional): System message to use for the scoring task.
|
233
|
+
sender (str, optional): Sender of the instruction. Defaults to None.
|
234
|
+
recipient (str, optional): Recipient of the instruction. Defaults to None.
|
235
|
+
branch (Branch, optional): Branch to perform the task within. Defaults to a new Branch.
|
236
|
+
form (Form, optional): Form to create the instruction from. Defaults to None.
|
237
|
+
confidence_score (bool, optional): Flag to include a confidence score. Defaults to None.
|
238
|
+
reason (bool, optional): Flag to include a reason for the scoring. Defaults to False.
|
239
|
+
score_range (tuple, optional): Range for the score. Defaults to None.
|
240
|
+
include_endpoints (bool, optional): Flag to include endpoints in the score range. Defaults to None.
|
241
|
+
num_digit (int, optional): Number of decimal places for the score. Defaults to None.
|
242
|
+
**kwargs: Additional keyword arguments for further customization.
|
243
|
+
|
244
|
+
Returns:
|
245
|
+
Any: The result of the scoring task.
|
246
|
+
"""
|
247
|
+
|
248
|
+
branch = branch or Branch()
|
249
|
+
unit = Unit(branch)
|
250
|
+
|
251
|
+
return await unit.score(
|
252
|
+
instruction=instruction,
|
253
|
+
context=context,
|
254
|
+
system=system,
|
255
|
+
sender=sender,
|
256
|
+
recipient=recipient,
|
257
|
+
form=form,
|
258
|
+
confidence_score=confidence_score,
|
259
|
+
reason=reason,
|
260
|
+
score_range=score_range,
|
261
|
+
include_endpoints=include_endpoints,
|
262
|
+
num_digit=num_digit,
|
263
|
+
**kwargs,
|
264
|
+
)
|
265
|
+
|
266
|
+
|
267
|
+
async def plan(
|
268
|
+
instruction=None,
|
269
|
+
context=None,
|
270
|
+
system=None,
|
271
|
+
sender=None,
|
272
|
+
recipient=None,
|
273
|
+
branch=None,
|
274
|
+
form=None,
|
275
|
+
confidence_score=None,
|
276
|
+
reason=False,
|
277
|
+
num_step=3,
|
278
|
+
**kwargs,
|
279
|
+
):
|
280
|
+
"""
|
281
|
+
Asynchronously generates a step-by-step plan within a given context.
|
282
|
+
|
283
|
+
Args:
|
284
|
+
instruction (str, optional): Additional instruction for the planning task.
|
285
|
+
context (Any, optional): Context to perform the planning task on.
|
286
|
+
system (str, optional): System message to use for the planning task.
|
287
|
+
sender (str, optional): Sender of the instruction. Defaults to None.
|
288
|
+
recipient (str, optional): Recipient of the instruction. Defaults to None.
|
289
|
+
branch (Branch, optional): Branch to perform the task within. Defaults to a new Branch.
|
290
|
+
form (Form, optional): Form to create the instruction from. Defaults to None.
|
291
|
+
confidence_score (bool, optional): Flag to include a confidence score. Defaults to None.
|
292
|
+
reason (bool, optional): Flag to include a reason for the plan. Defaults to False.
|
293
|
+
num_step (int, optional): Number of steps in the plan. Defaults to 3.
|
294
|
+
**kwargs: Additional keyword arguments for further customization.
|
295
|
+
|
296
|
+
Returns:
|
297
|
+
Any: The result of the planning task.
|
298
|
+
"""
|
299
|
+
|
300
|
+
branch = branch or Branch()
|
301
|
+
unit = Unit(branch)
|
302
|
+
|
303
|
+
return await unit.plan(
|
304
|
+
instruction=instruction,
|
305
|
+
context=context,
|
306
|
+
system=system,
|
307
|
+
sender=sender,
|
308
|
+
recipient=recipient,
|
309
|
+
form=form,
|
310
|
+
confidence_score=confidence_score,
|
311
|
+
reason=reason,
|
312
|
+
num_step=num_step,
|
313
|
+
**kwargs,
|
314
|
+
)
|