lionagi 0.2.11__py3-none-any.whl → 0.3.0__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- lionagi/core/action/function_calling.py +13 -6
- lionagi/core/action/tool.py +10 -9
- lionagi/core/action/tool_manager.py +18 -9
- lionagi/core/agent/README.md +1 -1
- lionagi/core/agent/base_agent.py +5 -2
- lionagi/core/agent/eval/README.md +1 -1
- lionagi/core/collections/README.md +1 -1
- lionagi/core/collections/_logger.py +16 -6
- lionagi/core/collections/abc/README.md +1 -1
- lionagi/core/collections/abc/component.py +35 -11
- lionagi/core/collections/abc/concepts.py +5 -3
- lionagi/core/collections/abc/exceptions.py +3 -1
- lionagi/core/collections/flow.py +16 -5
- lionagi/core/collections/model.py +34 -8
- lionagi/core/collections/pile.py +65 -28
- lionagi/core/collections/progression.py +1 -2
- lionagi/core/collections/util.py +11 -2
- lionagi/core/director/README.md +1 -1
- lionagi/core/engine/branch_engine.py +35 -10
- lionagi/core/engine/instruction_map_engine.py +14 -5
- lionagi/core/engine/sandbox_.py +3 -1
- lionagi/core/engine/script_engine.py +6 -2
- lionagi/core/executor/base_executor.py +10 -3
- lionagi/core/executor/graph_executor.py +12 -4
- lionagi/core/executor/neo4j_executor.py +18 -6
- lionagi/core/generic/edge.py +7 -2
- lionagi/core/generic/graph.py +23 -7
- lionagi/core/generic/node.py +14 -5
- lionagi/core/generic/tree_node.py +5 -1
- lionagi/core/mail/mail_manager.py +3 -1
- lionagi/core/mail/package.py +3 -1
- lionagi/core/message/action_request.py +9 -2
- lionagi/core/message/action_response.py +9 -3
- lionagi/core/message/instruction.py +8 -2
- lionagi/core/message/util.py +15 -5
- lionagi/core/report/base.py +12 -7
- lionagi/core/report/form.py +7 -4
- lionagi/core/report/report.py +10 -3
- lionagi/core/report/util.py +3 -1
- lionagi/core/rule/action.py +4 -1
- lionagi/core/rule/base.py +17 -6
- lionagi/core/rule/rulebook.py +8 -4
- lionagi/core/rule/string.py +3 -1
- lionagi/core/session/branch.py +15 -4
- lionagi/core/session/session.py +6 -2
- lionagi/core/unit/parallel_unit.py +9 -3
- lionagi/core/unit/template/action.py +1 -1
- lionagi/core/unit/template/predict.py +3 -1
- lionagi/core/unit/template/select.py +5 -3
- lionagi/core/unit/unit.py +4 -2
- lionagi/core/unit/unit_form.py +13 -15
- lionagi/core/unit/unit_mixin.py +45 -27
- lionagi/core/unit/util.py +7 -3
- lionagi/core/validator/validator.py +28 -15
- lionagi/core/work/work_edge.py +7 -3
- lionagi/core/work/work_task.py +11 -5
- lionagi/core/work/worker.py +20 -5
- lionagi/core/work/worker_engine.py +6 -2
- lionagi/core/work/worklog.py +3 -1
- lionagi/experimental/compressor/llm_compressor.py +20 -5
- lionagi/experimental/directive/README.md +1 -1
- lionagi/experimental/directive/parser/base_parser.py +41 -14
- lionagi/experimental/directive/parser/base_syntax.txt +23 -23
- lionagi/experimental/directive/template/base_template.py +14 -6
- lionagi/experimental/directive/tokenizer.py +3 -1
- lionagi/experimental/evaluator/README.md +1 -1
- lionagi/experimental/evaluator/ast_evaluator.py +6 -2
- lionagi/experimental/evaluator/base_evaluator.py +27 -16
- lionagi/integrations/bridge/autogen_/autogen_.py +7 -3
- lionagi/integrations/bridge/langchain_/documents.py +13 -10
- lionagi/integrations/bridge/llamaindex_/llama_pack.py +36 -12
- lionagi/integrations/bridge/llamaindex_/node_parser.py +8 -3
- lionagi/integrations/bridge/llamaindex_/reader.py +3 -1
- lionagi/integrations/bridge/llamaindex_/textnode.py +9 -3
- lionagi/integrations/bridge/pydantic_/pydantic_bridge.py +7 -1
- lionagi/integrations/bridge/transformers_/install_.py +3 -1
- lionagi/integrations/chunker/chunk.py +5 -2
- lionagi/integrations/loader/load.py +7 -3
- lionagi/integrations/loader/load_util.py +35 -16
- lionagi/integrations/provider/oai.py +13 -4
- lionagi/integrations/provider/openrouter.py +13 -4
- lionagi/integrations/provider/services.py +3 -1
- lionagi/integrations/provider/transformers.py +5 -3
- lionagi/integrations/storage/neo4j.py +23 -7
- lionagi/integrations/storage/storage_util.py +23 -7
- lionagi/integrations/storage/structure_excel.py +7 -2
- lionagi/integrations/storage/to_csv.py +8 -2
- lionagi/integrations/storage/to_excel.py +11 -3
- lionagi/libs/ln_api.py +41 -19
- lionagi/libs/ln_context.py +4 -4
- lionagi/libs/ln_convert.py +35 -14
- lionagi/libs/ln_dataframe.py +9 -3
- lionagi/libs/ln_func_call.py +53 -18
- lionagi/libs/ln_image.py +9 -5
- lionagi/libs/ln_knowledge_graph.py +21 -7
- lionagi/libs/ln_nested.py +57 -16
- lionagi/libs/ln_parse.py +45 -15
- lionagi/libs/ln_queue.py +8 -3
- lionagi/libs/ln_tokenize.py +19 -6
- lionagi/libs/ln_validate.py +14 -3
- lionagi/libs/sys_util.py +44 -12
- lionagi/lions/coder/coder.py +24 -8
- lionagi/lions/coder/util.py +6 -2
- lionagi/lions/researcher/data_source/google_.py +12 -4
- lionagi/lions/researcher/data_source/wiki_.py +3 -1
- lionagi/version.py +1 -1
- {lionagi-0.2.11.dist-info → lionagi-0.3.0.dist-info}/METADATA +6 -7
- lionagi-0.3.0.dist-info/RECORD +226 -0
- lionagi/tests/__init__.py +0 -0
- lionagi/tests/api/__init__.py +0 -0
- lionagi/tests/api/aws/__init__.py +0 -0
- lionagi/tests/api/aws/conftest.py +0 -25
- lionagi/tests/api/aws/test_aws_s3.py +0 -6
- lionagi/tests/integrations/__init__.py +0 -0
- lionagi/tests/libs/__init__.py +0 -0
- lionagi/tests/libs/test_api.py +0 -48
- lionagi/tests/libs/test_convert.py +0 -89
- lionagi/tests/libs/test_field_validators.py +0 -354
- lionagi/tests/libs/test_func_call.py +0 -701
- lionagi/tests/libs/test_nested.py +0 -382
- lionagi/tests/libs/test_parse.py +0 -171
- lionagi/tests/libs/test_queue.py +0 -68
- lionagi/tests/libs/test_sys_util.py +0 -222
- lionagi/tests/test_core/__init__.py +0 -0
- lionagi/tests/test_core/collections/__init__.py +0 -0
- lionagi/tests/test_core/collections/test_component.py +0 -208
- lionagi/tests/test_core/collections/test_exchange.py +0 -139
- lionagi/tests/test_core/collections/test_flow.py +0 -146
- lionagi/tests/test_core/collections/test_pile.py +0 -172
- lionagi/tests/test_core/collections/test_progression.py +0 -130
- lionagi/tests/test_core/generic/__init__.py +0 -0
- lionagi/tests/test_core/generic/test_edge.py +0 -69
- lionagi/tests/test_core/generic/test_graph.py +0 -97
- lionagi/tests/test_core/generic/test_node.py +0 -107
- lionagi/tests/test_core/generic/test_structure.py +0 -194
- lionagi/tests/test_core/generic/test_tree_node.py +0 -74
- lionagi/tests/test_core/graph/__init__.py +0 -0
- lionagi/tests/test_core/graph/test_graph.py +0 -71
- lionagi/tests/test_core/graph/test_tree.py +0 -76
- lionagi/tests/test_core/mail/__init__.py +0 -0
- lionagi/tests/test_core/mail/test_mail.py +0 -98
- lionagi/tests/test_core/test_branch.py +0 -116
- lionagi/tests/test_core/test_form.py +0 -47
- lionagi/tests/test_core/test_report.py +0 -106
- lionagi/tests/test_core/test_structure/__init__.py +0 -0
- lionagi/tests/test_core/test_structure/test_base_structure.py +0 -198
- lionagi/tests/test_core/test_structure/test_graph.py +0 -55
- lionagi/tests/test_core/test_structure/test_tree.py +0 -49
- lionagi/tests/test_core/test_validator.py +0 -112
- lionagi-0.2.11.dist-info/RECORD +0 -267
- {lionagi-0.2.11.dist-info → lionagi-0.3.0.dist-info}/LICENSE +0 -0
- {lionagi-0.2.11.dist-info → lionagi-0.3.0.dist-info}/WHEEL +0 -0
@@ -39,7 +39,9 @@ class SelectTemplate(BaseUnitForm):
|
|
39
39
|
selection: Enum | str | list | None = Field(
|
40
40
|
None, description="selection from given choices"
|
41
41
|
)
|
42
|
-
choices: list = Field(
|
42
|
+
choices: list = Field(
|
43
|
+
default_factory=list, description="the given choices"
|
44
|
+
)
|
43
45
|
|
44
46
|
assignment: str = "task -> selection"
|
45
47
|
|
@@ -78,9 +80,9 @@ class SelectTemplate(BaseUnitForm):
|
|
78
80
|
|
79
81
|
self.choices = choices
|
80
82
|
self.task = f"""
|
81
|
-
select 1 item from the provided choices {choices}.
|
83
|
+
select 1 item from the provided choices {choices}.
|
82
84
|
1. additional objective: {instruction or "N/A"}.
|
83
|
-
2. additional information: {context or "N/A"}.
|
85
|
+
2. additional information: {context or "N/A"}.
|
84
86
|
"""
|
85
87
|
if reason:
|
86
88
|
self.append_to_request("reason")
|
lionagi/core/unit/unit.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
from
|
1
|
+
from collections.abc import Callable
|
2
2
|
|
3
3
|
from lionagi.core.collections import iModel
|
4
4
|
from lionagi.core.collections.abc import Directive
|
@@ -232,7 +232,9 @@ class Unit(Directive, DirectiveMixin):
|
|
232
232
|
)
|
233
233
|
|
234
234
|
if verbose:
|
235
|
-
print(
|
235
|
+
print(
|
236
|
+
"--------------------------------------------------------------"
|
237
|
+
)
|
236
238
|
print(f"Directive successfully completed!")
|
237
239
|
|
238
240
|
return out
|
lionagi/core/unit/unit_form.py
CHANGED
@@ -38,7 +38,9 @@ class UnitForm(BaseUnitForm):
|
|
38
38
|
"number, you should provide a number like 1, 23, or 1.1 if float is "
|
39
39
|
"allowed."
|
40
40
|
),
|
41
|
-
examples=[
|
41
|
+
examples=[
|
42
|
+
"{action_1: {function: 'add', arguments: {num1: 1, num2: 2}}}"
|
43
|
+
],
|
42
44
|
)
|
43
45
|
|
44
46
|
action_required: bool | None = Field(
|
@@ -188,13 +190,13 @@ class UnitForm(BaseUnitForm):
|
|
188
190
|
|
189
191
|
if allow_action:
|
190
192
|
self.append_to_request("actions, action_required, reason")
|
191
|
-
self.task +=
|
193
|
+
self.task += (
|
194
|
+
"- Reason and prepare actions with GIVEN TOOLS ONLY.\n"
|
195
|
+
)
|
192
196
|
|
193
197
|
if allow_extension:
|
194
198
|
self.append_to_request("extension_required")
|
195
|
-
self.task +=
|
196
|
-
f"- Allow auto-extension up to another {max_extension} rounds.\n"
|
197
|
-
)
|
199
|
+
self.task += f"- Allow auto-extension up to another {max_extension} rounds.\n"
|
198
200
|
|
199
201
|
if tool_schema:
|
200
202
|
self.append_to_input("tool_schema")
|
@@ -209,21 +211,15 @@ class UnitForm(BaseUnitForm):
|
|
209
211
|
max_extension = max_extension or plan_num_step
|
210
212
|
allow_extension = True
|
211
213
|
self.append_to_request("plan, extension_required")
|
212
|
-
self.task +=
|
213
|
-
f"- Generate a {plan_num_step}-step plan based on the context.\n"
|
214
|
-
)
|
214
|
+
self.task += f"- Generate a {plan_num_step}-step plan based on the context.\n"
|
215
215
|
|
216
216
|
if predict:
|
217
217
|
self.append_to_request("prediction")
|
218
|
-
self.task += (
|
219
|
-
f"- Predict the next {predict_num_sentences or 1} sentence(s).\n"
|
220
|
-
)
|
218
|
+
self.task += f"- Predict the next {predict_num_sentences or 1} sentence(s).\n"
|
221
219
|
|
222
220
|
if select:
|
223
221
|
self.append_to_request("selection")
|
224
|
-
self.task +=
|
225
|
-
f"- Select 1 item from the provided choices: {select_choices}.\n"
|
226
|
-
)
|
222
|
+
self.task += f"- Select 1 item from the provided choices: {select_choices}.\n"
|
227
223
|
|
228
224
|
if confidence:
|
229
225
|
self.append_to_request("confidence_score")
|
@@ -238,7 +234,9 @@ class UnitForm(BaseUnitForm):
|
|
238
234
|
"upper_bound": score_range[1],
|
239
235
|
"lower_bound": score_range[0],
|
240
236
|
"num_type": int if score_num_digits == 0 else float,
|
241
|
-
"precision":
|
237
|
+
"precision": (
|
238
|
+
score_num_digits if score_num_digits != 0 else None
|
239
|
+
),
|
242
240
|
}
|
243
241
|
|
244
242
|
self.task += (
|
lionagi/core/unit/unit_mixin.py
CHANGED
@@ -22,16 +22,16 @@ class DirectiveMixin(ABC):
|
|
22
22
|
|
23
23
|
def _create_chat_config(
|
24
24
|
self,
|
25
|
-
system:
|
26
|
-
instruction:
|
27
|
-
context:
|
28
|
-
images:
|
29
|
-
sender:
|
30
|
-
recipient:
|
31
|
-
requested_fields:
|
25
|
+
system: str | None = None,
|
26
|
+
instruction: str | None = None,
|
27
|
+
context: str | None = None,
|
28
|
+
images: str | None = None,
|
29
|
+
sender: str | None = None,
|
30
|
+
recipient: str | None = None,
|
31
|
+
requested_fields: list | None = None,
|
32
32
|
form: Form = None,
|
33
33
|
tools: bool = False,
|
34
|
-
branch:
|
34
|
+
branch: Any | None = None,
|
35
35
|
**kwargs,
|
36
36
|
) -> Any:
|
37
37
|
"""
|
@@ -87,7 +87,7 @@ class DirectiveMixin(ABC):
|
|
87
87
|
return config
|
88
88
|
|
89
89
|
async def _call_chatcompletion(
|
90
|
-
self, imodel:
|
90
|
+
self, imodel: Any | None = None, branch: Any | None = None, **kwargs
|
91
91
|
) -> Any:
|
92
92
|
"""
|
93
93
|
Calls the chat completion model.
|
@@ -102,7 +102,9 @@ class DirectiveMixin(ABC):
|
|
102
102
|
"""
|
103
103
|
imodel = imodel or self.imodel
|
104
104
|
branch = branch or self.branch
|
105
|
-
return await imodel.call_chat_completion(
|
105
|
+
return await imodel.call_chat_completion(
|
106
|
+
branch.to_chat_messages(), **kwargs
|
107
|
+
)
|
106
108
|
|
107
109
|
async def _process_chatcompletion(
|
108
110
|
self,
|
@@ -110,8 +112,8 @@ class DirectiveMixin(ABC):
|
|
110
112
|
completion: dict,
|
111
113
|
sender: str,
|
112
114
|
invoke_tool: bool = True,
|
113
|
-
branch:
|
114
|
-
action_request:
|
115
|
+
branch: Any | None = None,
|
116
|
+
action_request: Any | None = None,
|
115
117
|
costs=None,
|
116
118
|
) -> Any:
|
117
119
|
"""
|
@@ -157,7 +159,9 @@ class DirectiveMixin(ABC):
|
|
157
159
|
m = completion.get("model", None)
|
158
160
|
if m:
|
159
161
|
ttl = (a * price[0] + b * price[1]) / 1000000
|
160
|
-
branch.messages[-1]._meta_insert(
|
162
|
+
branch.messages[-1]._meta_insert(
|
163
|
+
["extra", "usage", "expense"], ttl
|
164
|
+
)
|
161
165
|
return msg
|
162
166
|
|
163
167
|
if _choices and not isinstance(_choices, list):
|
@@ -180,10 +184,10 @@ class DirectiveMixin(ABC):
|
|
180
184
|
|
181
185
|
async def _process_action_request(
|
182
186
|
self,
|
183
|
-
_msg:
|
184
|
-
branch:
|
187
|
+
_msg: dict | None = None,
|
188
|
+
branch: Any | None = None,
|
185
189
|
invoke_tool: bool = True,
|
186
|
-
action_request:
|
190
|
+
action_request: Any | None = None,
|
187
191
|
) -> Any:
|
188
192
|
"""
|
189
193
|
Processes an action request from the assistant response.
|
@@ -204,9 +208,13 @@ class DirectiveMixin(ABC):
|
|
204
208
|
if action_request:
|
205
209
|
for i in action_request:
|
206
210
|
if i.function in branch.tool_manager.registry:
|
207
|
-
i.recipient = branch.tool_manager.registry[
|
211
|
+
i.recipient = branch.tool_manager.registry[
|
212
|
+
i.function
|
213
|
+
].ln_id
|
208
214
|
else:
|
209
|
-
raise ActionError(
|
215
|
+
raise ActionError(
|
216
|
+
f"Tool {i.function} not found in registry"
|
217
|
+
)
|
210
218
|
branch.add_message(action_request=i, recipient=i.recipient)
|
211
219
|
|
212
220
|
if invoke_tool:
|
@@ -305,7 +313,7 @@ class DirectiveMixin(ABC):
|
|
305
313
|
requested_fields: dict = None,
|
306
314
|
form: Form = None,
|
307
315
|
tools: Any = False,
|
308
|
-
images:
|
316
|
+
images: str | None = None,
|
309
317
|
invoke_tool: bool = True,
|
310
318
|
return_form: bool = True,
|
311
319
|
strict: bool = False,
|
@@ -396,7 +404,7 @@ class DirectiveMixin(ABC):
|
|
396
404
|
return_form=True,
|
397
405
|
strict=False,
|
398
406
|
imodel=None,
|
399
|
-
images:
|
407
|
+
images: str | None = None,
|
400
408
|
clear_messages=False,
|
401
409
|
use_annotation=True,
|
402
410
|
timeout: float = None,
|
@@ -493,7 +501,7 @@ class DirectiveMixin(ABC):
|
|
493
501
|
predict_num_sentences=None,
|
494
502
|
clear_messages=False,
|
495
503
|
return_branch=False,
|
496
|
-
images:
|
504
|
+
images: str | None = None,
|
497
505
|
verbose=None,
|
498
506
|
**kwargs,
|
499
507
|
):
|
@@ -584,7 +592,7 @@ class DirectiveMixin(ABC):
|
|
584
592
|
predict_num_sentences=None,
|
585
593
|
clear_messages=False,
|
586
594
|
return_branch=False,
|
587
|
-
images:
|
595
|
+
images: str | None = None,
|
588
596
|
verbose=True,
|
589
597
|
formatter=None,
|
590
598
|
format_kwargs=None,
|
@@ -736,10 +744,14 @@ class DirectiveMixin(ABC):
|
|
736
744
|
)
|
737
745
|
|
738
746
|
if verbose:
|
739
|
-
print(
|
747
|
+
print(
|
748
|
+
f"------------------- Extension completed -------------------\n"
|
749
|
+
)
|
740
750
|
|
741
751
|
extension_forms.extend(new_form)
|
742
|
-
last_form =
|
752
|
+
last_form = (
|
753
|
+
new_form[-1] if isinstance(new_form, list) else new_form
|
754
|
+
)
|
743
755
|
ctr += len(form)
|
744
756
|
|
745
757
|
if extension_forms:
|
@@ -977,7 +989,9 @@ class DirectiveMixin(ABC):
|
|
977
989
|
context=context,
|
978
990
|
)
|
979
991
|
|
980
|
-
return await self._chat(
|
992
|
+
return await self._chat(
|
993
|
+
form=form, return_form=True, branch=branch, **kwargs
|
994
|
+
)
|
981
995
|
|
982
996
|
async def _predict(
|
983
997
|
self,
|
@@ -1019,7 +1033,9 @@ class DirectiveMixin(ABC):
|
|
1019
1033
|
reason=reason,
|
1020
1034
|
)
|
1021
1035
|
|
1022
|
-
return await self._chat(
|
1036
|
+
return await self._chat(
|
1037
|
+
form=form, return_form=True, branch=branch, **kwargs
|
1038
|
+
)
|
1023
1039
|
|
1024
1040
|
async def _score(
|
1025
1041
|
self,
|
@@ -1066,7 +1082,9 @@ class DirectiveMixin(ABC):
|
|
1066
1082
|
context=context,
|
1067
1083
|
)
|
1068
1084
|
|
1069
|
-
return await self._chat(
|
1085
|
+
return await self._chat(
|
1086
|
+
form=form, return_form=True, branch=branch, **kwargs
|
1087
|
+
)
|
1070
1088
|
|
1071
1089
|
async def _plan(
|
1072
1090
|
self,
|
lionagi/core/unit/util.py
CHANGED
@@ -21,7 +21,7 @@ choices_fields = ["index", "message", "logprobs", "finish_reason"]
|
|
21
21
|
|
22
22
|
usage_fields = ["prompt_tokens", "completion_tokens", "total_tokens"]
|
23
23
|
|
24
|
-
from
|
24
|
+
from collections.abc import Callable
|
25
25
|
|
26
26
|
from lionagi.core.action.tool import Tool
|
27
27
|
from lionagi.core.action.tool_manager import func_to_tool
|
@@ -40,12 +40,16 @@ def process_tools(tool_obj, branch):
|
|
40
40
|
def _process_tool(tool_obj, branch):
|
41
41
|
if (
|
42
42
|
isinstance(tool_obj, Tool)
|
43
|
-
and tool_obj.schema_["function"]["name"]
|
43
|
+
and tool_obj.schema_["function"]["name"]
|
44
|
+
not in branch.tool_manager.registry
|
44
45
|
):
|
45
46
|
branch.register_tools(tool_obj)
|
46
47
|
if isinstance(tool_obj, Callable):
|
47
48
|
tool = func_to_tool(tool_obj)[0]
|
48
|
-
if
|
49
|
+
if (
|
50
|
+
tool.schema_["function"]["name"]
|
51
|
+
not in branch.tool_manager.registry
|
52
|
+
):
|
49
53
|
branch.register_tools(tool)
|
50
54
|
|
51
55
|
|
@@ -1,5 +1,6 @@
|
|
1
1
|
import asyncio
|
2
|
-
from
|
2
|
+
from collections.abc import Callable
|
3
|
+
from typing import Any, Dict, List, Union
|
3
4
|
|
4
5
|
from lionfuncs import lcall
|
5
6
|
|
@@ -40,10 +41,10 @@ class Validator:
|
|
40
41
|
self,
|
41
42
|
*,
|
42
43
|
rulebook: RuleBook = None,
|
43
|
-
rules:
|
44
|
-
order:
|
45
|
-
init_config:
|
46
|
-
active_rules:
|
44
|
+
rules: dict[str, Rule] = None,
|
45
|
+
order: list[str] = None,
|
46
|
+
init_config: dict[str, dict] = None,
|
47
|
+
active_rules: dict[str, Rule] = None,
|
47
48
|
formatter: Callable = None,
|
48
49
|
format_kwargs: dict = {},
|
49
50
|
):
|
@@ -63,12 +64,14 @@ class Validator:
|
|
63
64
|
self.rulebook = rulebook or RuleBook(
|
64
65
|
rules or _DEFAULT_RULES, order or _DEFAULT_RULEORDER, init_config
|
65
66
|
)
|
66
|
-
self.active_rules:
|
67
|
+
self.active_rules: dict[str, Rule] = (
|
68
|
+
active_rules or self._initiate_rules()
|
69
|
+
)
|
67
70
|
self.validation_log = []
|
68
71
|
self.formatter = formatter
|
69
72
|
self.format_kwargs = format_kwargs
|
70
73
|
|
71
|
-
def _initiate_rules(self) ->
|
74
|
+
def _initiate_rules(self) -> dict[str, Rule]:
|
72
75
|
"""
|
73
76
|
Initialize rules from the rulebook.
|
74
77
|
|
@@ -157,7 +160,7 @@ class Validator:
|
|
157
160
|
raise FieldError(error_message)
|
158
161
|
|
159
162
|
async def validate_report(
|
160
|
-
self, report: Report, forms:
|
163
|
+
self, report: Report, forms: list[Form], strict: bool = True
|
161
164
|
) -> Report:
|
162
165
|
"""
|
163
166
|
Validate a report based on active rules.
|
@@ -176,7 +179,7 @@ class Validator:
|
|
176
179
|
async def validate_response(
|
177
180
|
self,
|
178
181
|
form: Form,
|
179
|
-
response:
|
182
|
+
response: dict | str,
|
180
183
|
strict: bool = True,
|
181
184
|
use_annotation: bool = True,
|
182
185
|
) -> Form:
|
@@ -201,21 +204,29 @@ class Validator:
|
|
201
204
|
else:
|
202
205
|
if self.formatter:
|
203
206
|
if asyncio.iscoroutinefunction(self.formatter):
|
204
|
-
response = await self.formatter(
|
207
|
+
response = await self.formatter(
|
208
|
+
response, **self.format_kwargs
|
209
|
+
)
|
205
210
|
print("formatter used")
|
206
211
|
else:
|
207
|
-
response = self.formatter(
|
212
|
+
response = self.formatter(
|
213
|
+
response, **self.format_kwargs
|
214
|
+
)
|
208
215
|
print("formatter used")
|
209
216
|
|
210
217
|
if not isinstance(response, dict):
|
211
|
-
raise ValueError(
|
218
|
+
raise ValueError(
|
219
|
+
f"The form response format is invalid for filling."
|
220
|
+
)
|
212
221
|
|
213
222
|
dict_ = {}
|
214
223
|
for k, v in response.items():
|
215
224
|
if k in form.requested_fields:
|
216
225
|
kwargs = form.validation_kwargs.get(k, {})
|
217
226
|
_annotation = form._field_annotations[k]
|
218
|
-
if (
|
227
|
+
if (
|
228
|
+
keys := form._get_field_attr(k, "choices", None)
|
229
|
+
) is not None:
|
219
230
|
v = await self.validate_field(
|
220
231
|
field=k,
|
221
232
|
value=v,
|
@@ -227,7 +238,9 @@ class Validator:
|
|
227
238
|
**kwargs,
|
228
239
|
)
|
229
240
|
|
230
|
-
elif (
|
241
|
+
elif (
|
242
|
+
_keys := form._get_field_attr(k, "keys", None)
|
243
|
+
) is not None:
|
231
244
|
|
232
245
|
v = await self.validate_field(
|
233
246
|
field=k,
|
@@ -346,7 +359,7 @@ class Validator:
|
|
346
359
|
}
|
347
360
|
self.validation_log.append(log_entry)
|
348
361
|
|
349
|
-
def get_validation_summary(self) ->
|
362
|
+
def get_validation_summary(self) -> dict[str, Any]:
|
350
363
|
"""
|
351
364
|
Get a summary of validation results.
|
352
365
|
|
lionagi/core/work/work_edge.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import inspect
|
2
|
-
from
|
2
|
+
from collections.abc import Callable
|
3
3
|
|
4
4
|
from pydantic import Field, field_validator
|
5
5
|
|
@@ -54,7 +54,9 @@ class WorkEdge(Edge, Progressable):
|
|
54
54
|
getattr(func, "_worklink_decorator_params")
|
55
55
|
return func
|
56
56
|
except:
|
57
|
-
raise ValueError(
|
57
|
+
raise ValueError(
|
58
|
+
"convert_function must be a worklink decorated function"
|
59
|
+
)
|
58
60
|
|
59
61
|
@property
|
60
62
|
def name(self):
|
@@ -94,5 +96,7 @@ class WorkEdge(Edge, Progressable):
|
|
94
96
|
kwargs = {"from_result": task.current_work.result} | kwargs
|
95
97
|
|
96
98
|
self.convert_function.auto_schedule = True
|
97
|
-
next_work = await self.convert_function(
|
99
|
+
next_work = await self.convert_function(
|
100
|
+
self=self.associated_worker, **kwargs
|
101
|
+
)
|
98
102
|
return next_work
|
lionagi/core/work/work_task.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import inspect
|
2
|
-
from
|
2
|
+
from collections.abc import Callable
|
3
3
|
|
4
4
|
from pydantic import Field, field_validator
|
5
5
|
|
@@ -31,9 +31,13 @@ class WorkTask(Component):
|
|
31
31
|
|
32
32
|
work_history: list[Work] = Field([], description="List of works processed")
|
33
33
|
|
34
|
-
max_steps: int | None = Field(
|
34
|
+
max_steps: int | None = Field(
|
35
|
+
10, description="Maximum number of works allowed"
|
36
|
+
)
|
35
37
|
|
36
|
-
current_work: Work | None = Field(
|
38
|
+
current_work: Work | None = Field(
|
39
|
+
None, description="The current work in progress"
|
40
|
+
)
|
37
41
|
|
38
42
|
post_processing: Callable | None = Field(
|
39
43
|
None,
|
@@ -55,7 +59,9 @@ class WorkTask(Component):
|
|
55
59
|
ValueError: If value is not a positive integer.
|
56
60
|
"""
|
57
61
|
if value <= 0:
|
58
|
-
raise ValueError(
|
62
|
+
raise ValueError(
|
63
|
+
"Invalid value: max_steps must be a positive integer."
|
64
|
+
)
|
59
65
|
return value
|
60
66
|
|
61
67
|
@field_validator("post_processing", mode="before")
|
@@ -72,7 +78,7 @@ class WorkTask(Component):
|
|
72
78
|
Raises:
|
73
79
|
ValueError: If value is not an asynchronous function.
|
74
80
|
"""
|
75
|
-
if value is not None and not inspect.iscoroutinefunction(
|
81
|
+
if value is not None and not inspect.iscoroutinefunction(value):
|
76
82
|
raise ValueError("post_processing must be a async function")
|
77
83
|
return value
|
78
84
|
|
lionagi/core/work/worker.py
CHANGED
@@ -62,7 +62,12 @@ class Worker(ABC):
|
|
62
62
|
"""
|
63
63
|
|
64
64
|
return (
|
65
|
-
any(
|
65
|
+
any(
|
66
|
+
[
|
67
|
+
await i.is_progressable()
|
68
|
+
for i in self.work_functions.values()
|
69
|
+
]
|
70
|
+
)
|
66
71
|
and not self.stopped
|
67
72
|
)
|
68
73
|
|
@@ -261,7 +266,9 @@ def work(
|
|
261
266
|
**kwargs,
|
262
267
|
):
|
263
268
|
if not inspect.iscoroutinefunction(func):
|
264
|
-
raise TypeError(
|
269
|
+
raise TypeError(
|
270
|
+
f"{func.__name__} must be an asynchronous function"
|
271
|
+
)
|
265
272
|
retry_kwargs = retry_kwargs or {}
|
266
273
|
retry_kwargs["timeout"] = retry_kwargs.get("timeout", timeout)
|
267
274
|
return await self._work_wrapper(
|
@@ -309,7 +316,9 @@ def worklink(from_: str, to_: str, auto_schedule: bool = True):
|
|
309
316
|
self: Worker, *args, func=func, from_=from_, to_=to_, **kwargs
|
310
317
|
):
|
311
318
|
if not inspect.iscoroutinefunction(func):
|
312
|
-
raise TypeError(
|
319
|
+
raise TypeError(
|
320
|
+
f"{func.__name__} must be an asynchronous function"
|
321
|
+
)
|
313
322
|
|
314
323
|
work_funcs = self._get_decorated_functions(
|
315
324
|
decorator_attr="_work_decorator_params"
|
@@ -363,7 +372,9 @@ def worklink(from_: str, to_: str, auto_schedule: bool = True):
|
|
363
372
|
next_params[1], dict
|
364
373
|
):
|
365
374
|
if wrapper.auto_schedule:
|
366
|
-
return await to_work_func(
|
375
|
+
return await to_work_func(
|
376
|
+
*next_params[0], **next_params[1]
|
377
|
+
)
|
367
378
|
else:
|
368
379
|
raise TypeError(f"Invalid return type {func.__name__}")
|
369
380
|
else:
|
@@ -372,7 +383,11 @@ def worklink(from_: str, to_: str, auto_schedule: bool = True):
|
|
372
383
|
return next_params
|
373
384
|
|
374
385
|
wrapper.auto_schedule = auto_schedule
|
375
|
-
wrapper._worklink_decorator_params = {
|
386
|
+
wrapper._worklink_decorator_params = {
|
387
|
+
"func": func,
|
388
|
+
"from_": from_,
|
389
|
+
"to_": to_,
|
390
|
+
}
|
376
391
|
|
377
392
|
return wrapper
|
378
393
|
|
@@ -172,8 +172,12 @@ class WorkerEngine:
|
|
172
172
|
)
|
173
173
|
for func_name, func, dec_params in work_decorated_function:
|
174
174
|
if func_name not in self.worker.work_functions:
|
175
|
-
self.worker.work_functions[func_name] = WorkFunctionNode(
|
176
|
-
|
175
|
+
self.worker.work_functions[func_name] = WorkFunctionNode(
|
176
|
+
**dec_params
|
177
|
+
)
|
178
|
+
self.worker_graph.add_node(
|
179
|
+
self.worker.work_functions[func_name]
|
180
|
+
)
|
177
181
|
else:
|
178
182
|
if not isinstance(
|
179
183
|
self.worker.work_functions[func_name], WorkFunctionNode
|
lionagi/core/work/worklog.py
CHANGED
@@ -25,7 +25,9 @@ class WorkLog(Progressable):
|
|
25
25
|
Defaults to 1.
|
26
26
|
"""
|
27
27
|
self.pile = (
|
28
|
-
workpile
|
28
|
+
workpile
|
29
|
+
if workpile and isinstance(workpile, Pile)
|
30
|
+
else pile({}, Work)
|
29
31
|
)
|
30
32
|
self.pending = progression(workpile) if workpile else progression()
|
31
33
|
self.queue = WorkQueue(capacity=capacity, refresh_time=refresh_time)
|
@@ -101,7 +101,12 @@ class LLMCompressor(TokenCompressor):
|
|
101
101
|
return a(text, **kwargs)
|
102
102
|
|
103
103
|
async def rank_by_pplex(
|
104
|
-
self,
|
104
|
+
self,
|
105
|
+
items: list,
|
106
|
+
initial_text=None,
|
107
|
+
cumulative=False,
|
108
|
+
n_samples=None,
|
109
|
+
**kwargs,
|
105
110
|
):
|
106
111
|
"""
|
107
112
|
rank a list of items according to their perplexity
|
@@ -180,17 +185,23 @@ class LLMCompressor(TokenCompressor):
|
|
180
185
|
|
181
186
|
if rank_by == "perplexity":
|
182
187
|
ranked_items = await self.rank_by_pplex(
|
183
|
-
items=items,
|
188
|
+
items=items,
|
189
|
+
initial_text=initial_text,
|
190
|
+
cumulative=cumulative,
|
191
|
+
**kwargs,
|
184
192
|
)
|
185
193
|
|
186
|
-
prompt_tokens = sum(
|
194
|
+
prompt_tokens = sum(
|
195
|
+
[i[1]["num_prompt_tokens"] for i in ranked_items]
|
196
|
+
)
|
187
197
|
|
188
198
|
num_completion_tokens = sum(
|
189
199
|
[i[1]["num_completion_tokens"] for i in ranked_items]
|
190
200
|
)
|
191
201
|
|
192
202
|
price = (
|
193
|
-
prompt_tokens * 0.5 / 1000000
|
203
|
+
prompt_tokens * 0.5 / 1000000
|
204
|
+
+ num_completion_tokens * 1.5 / 1000000
|
194
205
|
)
|
195
206
|
|
196
207
|
selected_items = self.select_by_pplex(
|
@@ -227,7 +238,11 @@ class LLMCompressor(TokenCompressor):
|
|
227
238
|
raise ValueError(f"Ranking method {rank_by} is not supported")
|
228
239
|
|
229
240
|
def select_by_pplex(
|
230
|
-
self,
|
241
|
+
self,
|
242
|
+
ranked_items,
|
243
|
+
target_compression_ratio,
|
244
|
+
original_length,
|
245
|
+
min_pplex=None,
|
231
246
|
):
|
232
247
|
min_pplex = min_pplex or 0
|
233
248
|
|
@@ -1 +1 @@
|
|
1
|
-
TODO
|
1
|
+
TODO
|