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
@@ -1,251 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
This module contains the PolyChat class for performing parallel chat conversations with multiple branches.
|
3
|
-
|
4
|
-
The PolyChat class allows for conducting parallel chat conversations with multiple branches, each processing
|
5
|
-
instructions and context independently. It provides methods for parallel chat execution and manages the
|
6
|
-
created branches within the session.
|
7
|
-
"""
|
8
|
-
|
9
|
-
from typing import Any
|
10
|
-
|
11
|
-
from lionagi.libs import convert, AsyncUtil
|
12
|
-
from lionagi.core.messages.schema import Instruction
|
13
|
-
from lionagi.core.branch.branch import Branch
|
14
|
-
from lionagi.core.flow.baseflow import BasePolyFlow
|
15
|
-
|
16
|
-
|
17
|
-
class PolyChat(BasePolyFlow):
|
18
|
-
"""
|
19
|
-
A class for performing parallel chat conversations with multiple branches.
|
20
|
-
|
21
|
-
Methods:
|
22
|
-
__init__(self, session: Any) -> None:
|
23
|
-
Initializes the PolyChat instance.
|
24
|
-
|
25
|
-
async parallel_chat(self, instruction: Union[Instruction, str], num_instances: int = 1,
|
26
|
-
context: Optional[Any] = None, sender: Optional[str] = None,
|
27
|
-
branch_system: Optional[Any] = None, messages: Optional[Any] = None,
|
28
|
-
tools: bool = False, out: bool = True, invoke: bool = True,
|
29
|
-
output_fields: Optional[Any] = None, persist_path: Optional[str] = None,
|
30
|
-
branch_config: Optional[dict] = None, explode: bool = False, **kwargs) -> Any:
|
31
|
-
Performs parallel chat conversations with multiple branches.
|
32
|
-
|
33
|
-
async _parallel_chat(self, instruction: Union[Instruction, str], num_instances: int = 1,
|
34
|
-
context: Optional[Any] = None, sender: Optional[str] = None,
|
35
|
-
messages: Optional[Any] = None, tools: bool = False, out: bool = True,
|
36
|
-
invoke: bool = True, output_fields: Optional[Any] = None,
|
37
|
-
persist_path: Optional[str] = None, branch_config: dict = {},
|
38
|
-
explode: bool = False, include_mapping: bool = True, default_key: str = "response",
|
39
|
-
**kwargs) -> Any:
|
40
|
-
Internal method for performing parallel chat conversations with multiple branches.
|
41
|
-
"""
|
42
|
-
|
43
|
-
def __init__(self, session) -> None:
|
44
|
-
"""
|
45
|
-
Initializes the PolyChat instance.
|
46
|
-
|
47
|
-
Args:
|
48
|
-
session: The session object.
|
49
|
-
"""
|
50
|
-
super().__init__(session)
|
51
|
-
|
52
|
-
async def parallel_chat(
|
53
|
-
self,
|
54
|
-
instruction: Instruction | str,
|
55
|
-
num_instances=1,
|
56
|
-
context=None,
|
57
|
-
sender=None,
|
58
|
-
branch_system=None,
|
59
|
-
messages=None,
|
60
|
-
tools=False,
|
61
|
-
out=True,
|
62
|
-
invoke: bool = True,
|
63
|
-
output_fields=None,
|
64
|
-
persist_path=None,
|
65
|
-
branch_config=None,
|
66
|
-
explode=False,
|
67
|
-
**kwargs,
|
68
|
-
) -> Any:
|
69
|
-
"""
|
70
|
-
Performs parallel chat conversations with multiple branches.
|
71
|
-
|
72
|
-
Args:
|
73
|
-
instruction (Instruction | str): The instruction for the chat conversation.
|
74
|
-
num_instances (int): The number of branch instances to create (default: 1).
|
75
|
-
context (Optional[Any]): Additional context for the chat conversation.
|
76
|
-
sender (Optional[str]): The sender of the chat message.
|
77
|
-
branch_system (Optional[Any]): The system configuration for the branches.
|
78
|
-
messages (Optional[Any]): Messages to initialize the branches with.
|
79
|
-
tools (bool): Flag indicating whether to use tools in the chat conversation (default: False).
|
80
|
-
out (bool): Flag indicating whether to return the output of the chat conversation (default: True).
|
81
|
-
invoke (bool): Flag indicating whether to invoke tools during the chat conversation (default: True).
|
82
|
-
output_fields (Optional[Any]): The output fields for the chat conversation.
|
83
|
-
persist_path (Optional[str]): The path to persist the branch data.
|
84
|
-
branch_config (Optional[dict]): Additional configuration for the branches.
|
85
|
-
explode (bool): Flag indicating whether to explode the instruction and context combinations (default: False).
|
86
|
-
**kwargs: Additional keyword arguments for the chat conversation.
|
87
|
-
|
88
|
-
Returns:
|
89
|
-
Any: The result of the parallel chat conversation.
|
90
|
-
"""
|
91
|
-
if branch_config is None:
|
92
|
-
branch_config = {}
|
93
|
-
return await self._parallel_chat(
|
94
|
-
instruction,
|
95
|
-
num_instances=num_instances,
|
96
|
-
context=context,
|
97
|
-
sender=sender,
|
98
|
-
branch_system=branch_system,
|
99
|
-
messages=messages,
|
100
|
-
tools=tools,
|
101
|
-
out=out,
|
102
|
-
invoke=invoke,
|
103
|
-
output_fields=output_fields,
|
104
|
-
persist_path=persist_path,
|
105
|
-
branch_config=branch_config,
|
106
|
-
explode=explode,
|
107
|
-
**kwargs,
|
108
|
-
)
|
109
|
-
|
110
|
-
async def _parallel_chat(
|
111
|
-
self,
|
112
|
-
instruction: Instruction | str,
|
113
|
-
num_instances=1,
|
114
|
-
context=None,
|
115
|
-
sender=None,
|
116
|
-
messages=None,
|
117
|
-
tools=False,
|
118
|
-
out=True,
|
119
|
-
invoke: bool = True,
|
120
|
-
output_fields=None,
|
121
|
-
persist_path=None,
|
122
|
-
branch_config={},
|
123
|
-
explode=False,
|
124
|
-
include_mapping=True,
|
125
|
-
default_key="response",
|
126
|
-
**kwargs,
|
127
|
-
) -> Any:
|
128
|
-
"""
|
129
|
-
Internal method for performing parallel chat conversations with multiple branches.
|
130
|
-
|
131
|
-
Args:
|
132
|
-
instruction (Instruction | str): The instruction for the chat conversation.
|
133
|
-
num_instances (int): The number of branch instances to create (default: 1).
|
134
|
-
context (Optional[Any]): Additional context for the chat conversation.
|
135
|
-
sender (Optional[str]): The sender of the chat message.
|
136
|
-
messages (Optional[Any]): Messages to initialize the branches with.
|
137
|
-
tools (bool): Flag indicating whether to use tools in the chat conversation (default: False).
|
138
|
-
out (bool): Flag indicating whether to return the output of the chat conversation (default: True).
|
139
|
-
invoke (bool): Flag indicating whether to invoke tools during the chat conversation (default: True).
|
140
|
-
output_fields (Optional[Any]): The output fields for the chat conversation.
|
141
|
-
persist_path (Optional[str]): The path to persist the branch data.
|
142
|
-
branch_config (dict): Additional configuration for the branches (default: {}).
|
143
|
-
explode (bool): Flag indicating whether to explode the instruction and context combinations (default: False).
|
144
|
-
include_mapping (bool): Flag indicating whether to include mapping information in the output (default: True).
|
145
|
-
default_key (str): The default key for the output mapping (default: "response").
|
146
|
-
**kwargs: Additional keyword arguments for the chat conversation.
|
147
|
-
|
148
|
-
Returns:
|
149
|
-
Any: The result of the parallel chat conversation.
|
150
|
-
"""
|
151
|
-
|
152
|
-
branches = {}
|
153
|
-
|
154
|
-
async def _inner(i, ins_, cxt_):
|
155
|
-
|
156
|
-
branch_ = Branch(
|
157
|
-
messages=messages,
|
158
|
-
service=self.session.default_branch.service,
|
159
|
-
llmconfig=self.session.default_branch.llmconfig,
|
160
|
-
persist_path=persist_path,
|
161
|
-
**branch_config,
|
162
|
-
)
|
163
|
-
|
164
|
-
branch_.branch_name = branch_.id_
|
165
|
-
|
166
|
-
if tools:
|
167
|
-
branch_.tool_manager = self.session.default_branch.tool_manager
|
168
|
-
|
169
|
-
res_ = await branch_.chat(
|
170
|
-
instruction=ins_ or instruction,
|
171
|
-
context=cxt_ or context,
|
172
|
-
sender=sender,
|
173
|
-
tools=tools,
|
174
|
-
invoke=invoke,
|
175
|
-
out=out,
|
176
|
-
output_fields=output_fields,
|
177
|
-
**kwargs,
|
178
|
-
)
|
179
|
-
|
180
|
-
branches[branch_.id_] = branch_
|
181
|
-
if include_mapping:
|
182
|
-
return {
|
183
|
-
"instruction": ins_ or instruction,
|
184
|
-
"context": cxt_ or context,
|
185
|
-
"branch_id": branch_.id_,
|
186
|
-
default_key: res_,
|
187
|
-
}
|
188
|
-
|
189
|
-
else:
|
190
|
-
return res_
|
191
|
-
|
192
|
-
async def _inner_2(i, ins_=None, cxt_=None):
|
193
|
-
"""returns num_instances of branches performing for same task/context"""
|
194
|
-
tasks = [_inner(i, ins_, cxt_) for _ in range(num_instances)]
|
195
|
-
ress = await AsyncUtil.execute_tasks(*tasks)
|
196
|
-
return convert.to_list(ress)
|
197
|
-
|
198
|
-
async def _inner_3(i):
|
199
|
-
"""different instructions but same context"""
|
200
|
-
tasks = [_inner_2(i, ins_=ins_) for ins_ in convert.to_list(instruction)]
|
201
|
-
ress = await AsyncUtil.execute_tasks(*tasks)
|
202
|
-
return convert.to_list(ress)
|
203
|
-
|
204
|
-
async def _inner_3_b(i):
|
205
|
-
"""different context but same instruction"""
|
206
|
-
tasks = [_inner_2(i, cxt_=cxt_) for cxt_ in convert.to_list(context)]
|
207
|
-
ress = await AsyncUtil.execute_tasks(*tasks)
|
208
|
-
return convert.to_list(ress)
|
209
|
-
|
210
|
-
async def _inner_4(i):
|
211
|
-
"""different instructions and different context"""
|
212
|
-
|
213
|
-
tasks = []
|
214
|
-
if explode:
|
215
|
-
tasks = [
|
216
|
-
_inner_2(i, ins_=ins_, cxt_=cxt_)
|
217
|
-
for ins_ in convert.to_list(instruction)
|
218
|
-
for cxt_ in convert.to_list(context)
|
219
|
-
]
|
220
|
-
else:
|
221
|
-
tasks = [
|
222
|
-
_inner_2(i, ins_=ins_, cxt_=cxt_)
|
223
|
-
for ins_, cxt_ in zip(
|
224
|
-
convert.to_list(instruction), convert.to_list(context)
|
225
|
-
)
|
226
|
-
]
|
227
|
-
|
228
|
-
ress = await AsyncUtil.execute_tasks(*tasks)
|
229
|
-
return convert.to_list(ress)
|
230
|
-
|
231
|
-
if len(convert.to_list(instruction)) == 1:
|
232
|
-
if len(convert.to_list(context)) == 1:
|
233
|
-
out_ = await _inner_2(0)
|
234
|
-
self.session.branches.update(branches)
|
235
|
-
return out_
|
236
|
-
|
237
|
-
elif len(convert.to_list(context)) > 1:
|
238
|
-
out_ = await _inner_3_b(0)
|
239
|
-
self.session.branches.update(branches)
|
240
|
-
return out_
|
241
|
-
|
242
|
-
elif len(convert.to_list(instruction)) > 1:
|
243
|
-
if len(convert.to_list(context)) == 1:
|
244
|
-
out_ = await _inner_3(0)
|
245
|
-
self.session.branches.update(branches)
|
246
|
-
return out_
|
247
|
-
|
248
|
-
elif len(convert.to_list(context)) > 1:
|
249
|
-
out_ = await _inner_4(0)
|
250
|
-
self.session.branches.update(branches)
|
251
|
-
return out_
|
lionagi/core/form/action_form.py
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
from typing import Any
|
2
|
-
from lionagi.integrations.bridge.pydantic_.pydantic_bridge import Field
|
3
|
-
|
4
|
-
from lionagi.core.form.scored_form import ScoredForm
|
5
|
-
|
6
|
-
|
7
|
-
class ActionRequest: ...
|
8
|
-
|
9
|
-
|
10
|
-
class ActionForm(ScoredForm):
|
11
|
-
|
12
|
-
action_needed: bool | None = Field(
|
13
|
-
False, description="true if actions are needed else false"
|
14
|
-
)
|
15
|
-
|
16
|
-
actions: list[dict | ActionRequest | Any] | None = Field(
|
17
|
-
default_factory=list,
|
18
|
-
description="""provide The list of action(s) to take, each action in {"function": function_name, "arguments": {param1:..., param2:..., ...}}. Leave blank if no further actions are needed, you must use provided parameters for each action, DO NOT MAKE UP KWARG NAME!!!""",
|
19
|
-
)
|
20
|
-
|
21
|
-
answer: str | dict | Any | None = Field(
|
22
|
-
default_factory=str,
|
23
|
-
description="output answer to the questions asked if further actions are not needed, leave blank if an accurate answer cannot be provided from context during this step",
|
24
|
-
)
|
25
|
-
|
26
|
-
signature: str = "sentence -> reason, action_needed, actions, answer"
|
@@ -1,287 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
This module provides functions for validating and fixing field values based on their data types.
|
3
|
-
|
4
|
-
The module defines several functions for checking and fixing field values of different data types,
|
5
|
-
including numeric, boolean, string, and enum. It also provides a dictionary `validation_funcs` that
|
6
|
-
maps data types to their corresponding validation functions.
|
7
|
-
"""
|
8
|
-
|
9
|
-
from lionagi.libs import convert, StringMatch, ParseUtil
|
10
|
-
|
11
|
-
|
12
|
-
def check_dict_field(x, keys: list[str] | dict, fix_=True, **kwargs):
|
13
|
-
if isinstance(x, dict):
|
14
|
-
return x
|
15
|
-
if fix_:
|
16
|
-
try:
|
17
|
-
x = convert.to_str(x)
|
18
|
-
return StringMatch.force_validate_dict(x, keys=keys, **kwargs)
|
19
|
-
except Exception as e:
|
20
|
-
raise ValueError("Invalid dict field type.") from e
|
21
|
-
raise ValueError(f"Default value for DICT must be a dict, got {type(x).__name__}")
|
22
|
-
|
23
|
-
|
24
|
-
def check_action_field(x, fix_=True, **kwargs):
|
25
|
-
if (
|
26
|
-
isinstance(x, list)
|
27
|
-
and convert.is_same_dtype(x, dict)
|
28
|
-
and all(_has_action_keys(y) for y in x)
|
29
|
-
):
|
30
|
-
return x
|
31
|
-
try:
|
32
|
-
x = _fix_action_field(x, fix_)
|
33
|
-
return x
|
34
|
-
except Exception as e:
|
35
|
-
raise ValueError("Invalid action field type.") from e
|
36
|
-
|
37
|
-
|
38
|
-
def check_number_field(x, fix_=True, **kwargs):
|
39
|
-
"""
|
40
|
-
Checks if the given value is a valid numeric field.
|
41
|
-
|
42
|
-
Args:
|
43
|
-
x: The value to check.
|
44
|
-
fix_ (bool): Flag indicating whether to attempt fixing the value if it's invalid (default: True).
|
45
|
-
**kwargs: Additional keyword arguments for fixing the value.
|
46
|
-
|
47
|
-
Returns:
|
48
|
-
The original value if it's valid, or the fixed value if `fix_` is True.
|
49
|
-
|
50
|
-
Raises:
|
51
|
-
ValueError: If the value is not a valid numeric field and cannot be fixed.
|
52
|
-
"""
|
53
|
-
if not isinstance(x, (int, float)):
|
54
|
-
if fix_:
|
55
|
-
try:
|
56
|
-
return _fix_number_field(x, **kwargs)
|
57
|
-
except Exception as e:
|
58
|
-
raise e
|
59
|
-
|
60
|
-
raise ValueError(
|
61
|
-
f"Default value for NUMERIC must be an int or float, got {type(x).__name__}"
|
62
|
-
)
|
63
|
-
return x
|
64
|
-
|
65
|
-
|
66
|
-
def check_bool_field(x, fix_=True):
|
67
|
-
"""
|
68
|
-
Checks if the given value is a valid boolean field.
|
69
|
-
|
70
|
-
Args:
|
71
|
-
x: The value to check.
|
72
|
-
fix_ (bool): Flag indicating whether to attempt fixing the value if it's invalid (default: True).
|
73
|
-
|
74
|
-
Returns:
|
75
|
-
The original value if it's valid, or the fixed value if `fix_` is True.
|
76
|
-
|
77
|
-
Raises:
|
78
|
-
ValueError: If the value is not a valid boolean field and cannot be fixed.
|
79
|
-
"""
|
80
|
-
if not isinstance(x, bool):
|
81
|
-
if fix_:
|
82
|
-
try:
|
83
|
-
return _fix_bool_field(x)
|
84
|
-
except Exception as e:
|
85
|
-
raise e
|
86
|
-
|
87
|
-
raise ValueError(
|
88
|
-
f"Default value for BOOLEAN must be a bool, got {type(x).__name__}"
|
89
|
-
)
|
90
|
-
return x
|
91
|
-
|
92
|
-
|
93
|
-
def check_str_field(x, *args, fix_=True, **kwargs):
|
94
|
-
"""
|
95
|
-
Checks if the given value is a valid string field.
|
96
|
-
|
97
|
-
Args:
|
98
|
-
x: The value to check.
|
99
|
-
*args: Additional positional arguments for fixing the value.
|
100
|
-
fix_ (bool): Flag indicating whether to attempt fixing the value if it's invalid (default: True).
|
101
|
-
**kwargs: Additional keyword arguments for fixing the value.
|
102
|
-
|
103
|
-
Returns:
|
104
|
-
The original value if it's valid, or the fixed value if `fix_` is True.
|
105
|
-
|
106
|
-
Raises:
|
107
|
-
ValueError: If the value is not a valid string field and cannot be fixed.
|
108
|
-
"""
|
109
|
-
if not isinstance(x, str):
|
110
|
-
if fix_:
|
111
|
-
try:
|
112
|
-
return _fix_str_field(x, *args, **kwargs)
|
113
|
-
except Exception as e:
|
114
|
-
raise e
|
115
|
-
|
116
|
-
raise ValueError(
|
117
|
-
f"Default value for STRING must be a str, got {type(x).__name__}"
|
118
|
-
)
|
119
|
-
return x
|
120
|
-
|
121
|
-
|
122
|
-
def check_enum_field(x, choices, fix_=True, **kwargs):
|
123
|
-
"""
|
124
|
-
Checks if the given value is a valid enum field.
|
125
|
-
|
126
|
-
Args:
|
127
|
-
x: The value to check.
|
128
|
-
choices: The list of valid choices for the enum field.
|
129
|
-
fix_ (bool): Flag indicating whether to attempt fixing the value if it's invalid (default: True).
|
130
|
-
**kwargs: Additional keyword arguments for fixing the value.
|
131
|
-
|
132
|
-
Returns:
|
133
|
-
The original value if it's valid, or the fixed value if `fix_` is True.
|
134
|
-
|
135
|
-
Raises:
|
136
|
-
ValueError: If the value is not a valid enum field and cannot be fixed.
|
137
|
-
"""
|
138
|
-
same_dtype, dtype_ = convert.is_same_dtype(choices, return_dtype=True)
|
139
|
-
if not same_dtype:
|
140
|
-
raise ValueError(
|
141
|
-
f"Field type ENUM requires all choices to be of the same type, got {choices}"
|
142
|
-
)
|
143
|
-
|
144
|
-
if not isinstance(x, dtype_):
|
145
|
-
raise ValueError(
|
146
|
-
f"Default value for ENUM must be an instance of the {dtype_.__name__}, got {type(x).__name__}"
|
147
|
-
)
|
148
|
-
|
149
|
-
if x not in choices:
|
150
|
-
if fix_:
|
151
|
-
try:
|
152
|
-
return _fix_enum_field(x, choices, **kwargs)
|
153
|
-
except Exception as e:
|
154
|
-
raise e
|
155
|
-
raise ValueError(
|
156
|
-
f"Default value for ENUM must be one of the {choices}, got {x}"
|
157
|
-
)
|
158
|
-
|
159
|
-
return x
|
160
|
-
|
161
|
-
|
162
|
-
def _has_action_keys(dict_):
|
163
|
-
return list(dict_.keys()) >= ["function", "arguments"]
|
164
|
-
|
165
|
-
|
166
|
-
def _fix_action_field(x, discard_=True):
|
167
|
-
corrected = []
|
168
|
-
if isinstance(x, str):
|
169
|
-
x = ParseUtil.fuzzy_parse_json(x)
|
170
|
-
|
171
|
-
try:
|
172
|
-
x = convert.to_list(x)
|
173
|
-
|
174
|
-
for i in x:
|
175
|
-
i = convert.to_dict(i)
|
176
|
-
if _has_action_keys(i):
|
177
|
-
corrected.append(i)
|
178
|
-
elif not discard_:
|
179
|
-
raise ValueError(f"Invalid action field: {i}")
|
180
|
-
except Exception as e:
|
181
|
-
raise ValueError(f"Invalid action field: {e}") from e
|
182
|
-
|
183
|
-
return corrected
|
184
|
-
|
185
|
-
|
186
|
-
def _fix_number_field(x, *args, **kwargs):
|
187
|
-
"""
|
188
|
-
Attempts to fix an invalid numeric field value.
|
189
|
-
|
190
|
-
Args:
|
191
|
-
x: The value to fix.
|
192
|
-
*args: Additional positional arguments for fixing the value.
|
193
|
-
**kwargs: Additional keyword arguments for fixing the value.
|
194
|
-
|
195
|
-
Returns:
|
196
|
-
The fixed numeric value.
|
197
|
-
|
198
|
-
Raises:
|
199
|
-
ValueError: If the value cannot be converted into a valid numeric value.
|
200
|
-
"""
|
201
|
-
try:
|
202
|
-
x = convert.to_num(x, *args, **kwargs)
|
203
|
-
if isinstance(x, (int, float)):
|
204
|
-
return x
|
205
|
-
raise ValueError(f"Failed to convert {x} into a numeric value")
|
206
|
-
except Exception as e:
|
207
|
-
raise ValueError(f"Failed to convert {x} into a numeric value") from e
|
208
|
-
|
209
|
-
|
210
|
-
def _fix_bool_field(x):
|
211
|
-
"""
|
212
|
-
Attempts to fix an invalid boolean field value.
|
213
|
-
|
214
|
-
Args:
|
215
|
-
x: The value to fix.
|
216
|
-
|
217
|
-
Returns:
|
218
|
-
The fixed boolean value.
|
219
|
-
|
220
|
-
Raises:
|
221
|
-
ValueError: If the value cannot be converted into a valid boolean value.
|
222
|
-
"""
|
223
|
-
try:
|
224
|
-
x = convert.strip_lower(convert.to_str(x))
|
225
|
-
if x in ["true", "1", "correct", "yes"]:
|
226
|
-
return True
|
227
|
-
|
228
|
-
elif x in ["false", "0", "incorrect", "no", "none", "n/a"]:
|
229
|
-
return False
|
230
|
-
|
231
|
-
raise ValueError(f"Failed to convert {x} into a boolean value")
|
232
|
-
except Exception as e:
|
233
|
-
raise ValueError(f"Failed to convert {x} into a boolean value") from e
|
234
|
-
|
235
|
-
|
236
|
-
def _fix_str_field(x):
|
237
|
-
"""
|
238
|
-
Attempts to fix an invalid string field value.
|
239
|
-
|
240
|
-
Args:
|
241
|
-
x: The value to fix.
|
242
|
-
|
243
|
-
Returns:
|
244
|
-
The fixed string value.
|
245
|
-
|
246
|
-
Raises:
|
247
|
-
ValueError: If the value cannot be converted into a valid string value.
|
248
|
-
"""
|
249
|
-
try:
|
250
|
-
x = convert.to_str(x)
|
251
|
-
if isinstance(x, str):
|
252
|
-
return x
|
253
|
-
raise ValueError(f"Failed to convert {x} into a string value")
|
254
|
-
except Exception as e:
|
255
|
-
raise ValueError(f"Failed to convert {x} into a string value") from e
|
256
|
-
|
257
|
-
|
258
|
-
def _fix_enum_field(x, choices, **kwargs):
|
259
|
-
"""
|
260
|
-
Attempts to fix an invalid enum field value.
|
261
|
-
|
262
|
-
Args:
|
263
|
-
x: The value to fix.
|
264
|
-
choices: The list of valid choices for the enum field.
|
265
|
-
**kwargs: Additional keyword arguments for fixing the value.
|
266
|
-
|
267
|
-
Returns:
|
268
|
-
The fixed enum value.
|
269
|
-
|
270
|
-
Raises:
|
271
|
-
ValueError: If the value cannot be converted into a valid enum value.
|
272
|
-
"""
|
273
|
-
try:
|
274
|
-
x = convert.to_str(x)
|
275
|
-
return StringMatch.choose_most_similar(x, choices, **kwargs)
|
276
|
-
except Exception as e:
|
277
|
-
raise ValueError(f"Failed to convert {x} into one of the choices") from e
|
278
|
-
|
279
|
-
|
280
|
-
validation_funcs = {
|
281
|
-
"number": check_number_field,
|
282
|
-
"bool": check_bool_field,
|
283
|
-
"str": check_str_field,
|
284
|
-
"enum": check_enum_field,
|
285
|
-
"action": check_action_field,
|
286
|
-
"dict": check_dict_field,
|
287
|
-
}
|