lionagi 0.1.1__py3-none-any.whl → 0.2.0__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- lionagi/__init__.py +60 -5
- lionagi/core/__init__.py +0 -25
- lionagi/core/_setting/_setting.py +59 -0
- lionagi/core/action/__init__.py +14 -0
- lionagi/core/action/function_calling.py +136 -0
- lionagi/core/action/manual.py +1 -0
- lionagi/core/action/node.py +109 -0
- lionagi/core/action/tool.py +114 -0
- lionagi/core/action/tool_manager.py +356 -0
- lionagi/core/agent/base_agent.py +27 -13
- lionagi/core/agent/eval/evaluator.py +1 -0
- lionagi/core/agent/eval/vote.py +40 -0
- lionagi/core/agent/learn/learner.py +59 -0
- lionagi/core/agent/plan/unit_template.py +1 -0
- lionagi/core/collections/__init__.py +17 -0
- lionagi/core/{generic/data_logger.py → collections/_logger.py} +69 -55
- lionagi/core/collections/abc/__init__.py +53 -0
- lionagi/core/collections/abc/component.py +615 -0
- lionagi/core/collections/abc/concepts.py +297 -0
- lionagi/core/collections/abc/exceptions.py +150 -0
- lionagi/core/collections/abc/util.py +45 -0
- lionagi/core/collections/exchange.py +161 -0
- lionagi/core/collections/flow.py +426 -0
- lionagi/core/collections/model.py +419 -0
- lionagi/core/collections/pile.py +913 -0
- lionagi/core/collections/progression.py +236 -0
- lionagi/core/collections/util.py +64 -0
- lionagi/core/director/direct.py +314 -0
- lionagi/core/director/director.py +2 -0
- lionagi/core/{execute/branch_executor.py → engine/branch_engine.py} +134 -97
- lionagi/core/{execute/instruction_map_executor.py → engine/instruction_map_engine.py} +80 -55
- lionagi/{experimental/directive/evaluator → core/engine}/script_engine.py +17 -1
- lionagi/core/executor/base_executor.py +90 -0
- lionagi/core/{execute/structure_executor.py → executor/graph_executor.py} +83 -67
- lionagi/core/{execute → executor}/neo4j_executor.py +70 -67
- lionagi/core/generic/__init__.py +3 -33
- lionagi/core/generic/edge.py +42 -92
- lionagi/core/generic/edge_condition.py +16 -0
- lionagi/core/generic/graph.py +236 -0
- lionagi/core/generic/hyperedge.py +1 -0
- lionagi/core/generic/node.py +156 -221
- lionagi/core/generic/tree.py +48 -0
- lionagi/core/generic/tree_node.py +79 -0
- lionagi/core/mail/__init__.py +12 -0
- lionagi/core/mail/mail.py +25 -0
- lionagi/core/mail/mail_manager.py +139 -58
- lionagi/core/mail/package.py +45 -0
- lionagi/core/mail/start_mail.py +36 -0
- lionagi/core/message/__init__.py +19 -0
- lionagi/core/message/action_request.py +133 -0
- lionagi/core/message/action_response.py +135 -0
- lionagi/core/message/assistant_response.py +95 -0
- lionagi/core/message/instruction.py +234 -0
- lionagi/core/message/message.py +101 -0
- lionagi/core/message/system.py +86 -0
- lionagi/core/message/util.py +283 -0
- lionagi/core/report/__init__.py +4 -0
- lionagi/core/report/base.py +217 -0
- lionagi/core/report/form.py +231 -0
- lionagi/core/report/report.py +166 -0
- lionagi/core/report/util.py +28 -0
- lionagi/core/rule/_default.py +16 -0
- lionagi/core/rule/action.py +99 -0
- lionagi/core/rule/base.py +238 -0
- lionagi/core/rule/boolean.py +56 -0
- lionagi/core/rule/choice.py +47 -0
- lionagi/core/rule/mapping.py +96 -0
- lionagi/core/rule/number.py +71 -0
- lionagi/core/rule/rulebook.py +109 -0
- lionagi/core/rule/string.py +52 -0
- lionagi/core/rule/util.py +35 -0
- lionagi/core/session/branch.py +431 -0
- lionagi/core/session/directive_mixin.py +287 -0
- lionagi/core/session/session.py +229 -903
- lionagi/core/structure/__init__.py +1 -0
- lionagi/core/structure/chain.py +1 -0
- lionagi/core/structure/forest.py +1 -0
- lionagi/core/structure/graph.py +1 -0
- lionagi/core/structure/tree.py +1 -0
- lionagi/core/unit/__init__.py +5 -0
- lionagi/core/unit/parallel_unit.py +245 -0
- lionagi/core/unit/template/action.py +81 -0
- lionagi/core/unit/template/base.py +51 -0
- lionagi/core/unit/template/plan.py +84 -0
- lionagi/core/unit/template/predict.py +109 -0
- lionagi/core/unit/template/score.py +124 -0
- lionagi/core/unit/template/select.py +104 -0
- lionagi/core/unit/unit.py +362 -0
- lionagi/core/unit/unit_form.py +305 -0
- lionagi/core/unit/unit_mixin.py +1168 -0
- lionagi/core/unit/util.py +71 -0
- lionagi/core/validator/validator.py +364 -0
- lionagi/core/work/work.py +74 -0
- lionagi/core/work/work_function.py +92 -0
- lionagi/core/work/work_queue.py +81 -0
- lionagi/core/work/worker.py +195 -0
- lionagi/core/work/worklog.py +124 -0
- lionagi/experimental/compressor/base.py +46 -0
- lionagi/experimental/compressor/llm_compressor.py +247 -0
- lionagi/experimental/compressor/llm_summarizer.py +61 -0
- lionagi/experimental/compressor/util.py +70 -0
- lionagi/experimental/directive/__init__.py +19 -0
- lionagi/experimental/directive/parser/base_parser.py +69 -2
- lionagi/experimental/directive/{template_ → template}/base_template.py +17 -1
- lionagi/{libs/ln_tokenizer.py → experimental/directive/tokenizer.py} +16 -0
- lionagi/experimental/{directive/evaluator → evaluator}/ast_evaluator.py +16 -0
- lionagi/experimental/{directive/evaluator → evaluator}/base_evaluator.py +16 -0
- lionagi/experimental/knowledge/__init__.py +0 -0
- lionagi/experimental/knowledge/base.py +10 -0
- lionagi/experimental/knowledge/graph.py +0 -0
- lionagi/experimental/memory/__init__.py +0 -0
- lionagi/experimental/strategies/__init__.py +0 -0
- lionagi/experimental/strategies/base.py +1 -0
- lionagi/integrations/bridge/langchain_/documents.py +4 -0
- lionagi/integrations/bridge/llamaindex_/index.py +30 -0
- lionagi/integrations/bridge/llamaindex_/llama_index_bridge.py +6 -0
- lionagi/integrations/chunker/chunk.py +161 -24
- lionagi/integrations/config/oai_configs.py +34 -3
- lionagi/integrations/config/openrouter_configs.py +14 -2
- lionagi/integrations/loader/load.py +122 -21
- lionagi/integrations/loader/load_util.py +6 -77
- lionagi/integrations/provider/_mapping.py +46 -0
- lionagi/integrations/provider/litellm.py +2 -1
- lionagi/integrations/provider/mlx_service.py +16 -9
- lionagi/integrations/provider/oai.py +91 -4
- lionagi/integrations/provider/ollama.py +6 -5
- lionagi/integrations/provider/openrouter.py +115 -8
- lionagi/integrations/provider/services.py +2 -2
- lionagi/integrations/provider/transformers.py +18 -22
- lionagi/integrations/storage/__init__.py +3 -3
- lionagi/integrations/storage/neo4j.py +52 -60
- lionagi/integrations/storage/storage_util.py +45 -47
- lionagi/integrations/storage/structure_excel.py +285 -0
- lionagi/integrations/storage/to_excel.py +23 -7
- lionagi/libs/__init__.py +26 -1
- lionagi/libs/ln_api.py +75 -20
- lionagi/libs/ln_context.py +37 -0
- lionagi/libs/ln_convert.py +21 -9
- lionagi/libs/ln_func_call.py +69 -28
- lionagi/libs/ln_image.py +107 -0
- lionagi/libs/ln_nested.py +26 -11
- lionagi/libs/ln_parse.py +82 -23
- lionagi/libs/ln_queue.py +16 -0
- lionagi/libs/ln_tokenize.py +164 -0
- lionagi/libs/ln_validate.py +16 -0
- lionagi/libs/special_tokens.py +172 -0
- lionagi/libs/sys_util.py +95 -24
- lionagi/lions/coder/code_form.py +13 -0
- lionagi/lions/coder/coder.py +50 -3
- lionagi/lions/coder/util.py +30 -25
- lionagi/tests/libs/test_func_call.py +23 -21
- lionagi/tests/libs/test_nested.py +36 -21
- lionagi/tests/libs/test_parse.py +1 -1
- lionagi/tests/test_core/collections/__init__.py +0 -0
- lionagi/tests/test_core/collections/test_component.py +206 -0
- lionagi/tests/test_core/collections/test_exchange.py +138 -0
- lionagi/tests/test_core/collections/test_flow.py +145 -0
- lionagi/tests/test_core/collections/test_pile.py +171 -0
- lionagi/tests/test_core/collections/test_progression.py +129 -0
- lionagi/tests/test_core/generic/__init__.py +0 -0
- lionagi/tests/test_core/generic/test_edge.py +67 -0
- lionagi/tests/test_core/generic/test_graph.py +96 -0
- lionagi/tests/test_core/generic/test_node.py +106 -0
- lionagi/tests/test_core/generic/test_tree_node.py +73 -0
- lionagi/tests/test_core/test_branch.py +115 -294
- lionagi/tests/test_core/test_form.py +46 -0
- lionagi/tests/test_core/test_report.py +105 -0
- lionagi/tests/test_core/test_validator.py +111 -0
- lionagi/version.py +1 -1
- lionagi-0.2.0.dist-info/LICENSE +202 -0
- lionagi-0.2.0.dist-info/METADATA +272 -0
- lionagi-0.2.0.dist-info/RECORD +240 -0
- lionagi/core/branch/base.py +0 -653
- lionagi/core/branch/branch.py +0 -474
- lionagi/core/branch/flow_mixin.py +0 -96
- lionagi/core/branch/util.py +0 -323
- lionagi/core/direct/__init__.py +0 -19
- lionagi/core/direct/cot.py +0 -123
- lionagi/core/direct/plan.py +0 -164
- lionagi/core/direct/predict.py +0 -166
- lionagi/core/direct/react.py +0 -171
- lionagi/core/direct/score.py +0 -279
- lionagi/core/direct/select.py +0 -170
- lionagi/core/direct/sentiment.py +0 -1
- lionagi/core/direct/utils.py +0 -110
- lionagi/core/direct/vote.py +0 -64
- lionagi/core/execute/base_executor.py +0 -47
- lionagi/core/flow/baseflow.py +0 -23
- lionagi/core/flow/monoflow/ReAct.py +0 -238
- lionagi/core/flow/monoflow/__init__.py +0 -9
- lionagi/core/flow/monoflow/chat.py +0 -95
- lionagi/core/flow/monoflow/chat_mixin.py +0 -253
- lionagi/core/flow/monoflow/followup.py +0 -213
- lionagi/core/flow/polyflow/__init__.py +0 -1
- lionagi/core/flow/polyflow/chat.py +0 -251
- lionagi/core/form/action_form.py +0 -26
- lionagi/core/form/field_validator.py +0 -287
- lionagi/core/form/form.py +0 -302
- lionagi/core/form/mixin.py +0 -214
- lionagi/core/form/scored_form.py +0 -13
- lionagi/core/generic/action.py +0 -26
- lionagi/core/generic/component.py +0 -455
- lionagi/core/generic/condition.py +0 -44
- lionagi/core/generic/mail.py +0 -90
- lionagi/core/generic/mailbox.py +0 -36
- lionagi/core/generic/relation.py +0 -70
- lionagi/core/generic/signal.py +0 -22
- lionagi/core/generic/structure.py +0 -362
- lionagi/core/generic/transfer.py +0 -20
- lionagi/core/generic/work.py +0 -40
- lionagi/core/graph/graph.py +0 -126
- lionagi/core/graph/tree.py +0 -190
- lionagi/core/mail/schema.py +0 -63
- lionagi/core/messages/schema.py +0 -325
- lionagi/core/tool/__init__.py +0 -5
- lionagi/core/tool/tool.py +0 -28
- lionagi/core/tool/tool_manager.py +0 -282
- lionagi/experimental/tool/function_calling.py +0 -43
- lionagi/experimental/tool/manual.py +0 -66
- lionagi/experimental/tool/schema.py +0 -59
- lionagi/experimental/tool/tool_manager.py +0 -138
- lionagi/experimental/tool/util.py +0 -16
- lionagi/experimental/work/_logger.py +0 -25
- lionagi/experimental/work/schema.py +0 -30
- lionagi/experimental/work/tests.py +0 -72
- lionagi/experimental/work/work_function.py +0 -89
- lionagi/experimental/work/worker.py +0 -12
- lionagi/integrations/bridge/llamaindex_/get_index.py +0 -294
- lionagi/tests/test_core/test_base_branch.py +0 -426
- lionagi/tests/test_core/test_chat_flow.py +0 -63
- lionagi/tests/test_core/test_mail_manager.py +0 -75
- lionagi/tests/test_core/test_prompts.py +0 -51
- lionagi/tests/test_core/test_session.py +0 -254
- lionagi/tests/test_core/test_session_base_util.py +0 -313
- lionagi/tests/test_core/test_tool_manager.py +0 -95
- lionagi-0.1.1.dist-info/LICENSE +0 -9
- lionagi-0.1.1.dist-info/METADATA +0 -174
- lionagi-0.1.1.dist-info/RECORD +0 -190
- /lionagi/core/{branch → _setting}/__init__.py +0 -0
- /lionagi/core/{execute → agent/eval}/__init__.py +0 -0
- /lionagi/core/{flow → agent/learn}/__init__.py +0 -0
- /lionagi/core/{form → agent/plan}/__init__.py +0 -0
- /lionagi/core/{branch/executable_branch.py → agent/plan/plan.py} +0 -0
- /lionagi/core/{graph → director}/__init__.py +0 -0
- /lionagi/core/{messages → engine}/__init__.py +0 -0
- /lionagi/{experimental/directive/evaluator → core/engine}/sandbox_.py +0 -0
- /lionagi/{experimental/directive/evaluator → core/executor}/__init__.py +0 -0
- /lionagi/{experimental/directive/template_ → core/rule}/__init__.py +0 -0
- /lionagi/{experimental/tool → core/unit/template}/__init__.py +0 -0
- /lionagi/{experimental/work → core/validator}/__init__.py +0 -0
- /lionagi/core/{flow/mono_chat_mixin.py → work/__init__.py} +0 -0
- /lionagi/experimental/{work/exchange.py → compressor/__init__.py} +0 -0
- /lionagi/experimental/{work/util.py → directive/template/__init__.py} +0 -0
- /lionagi/experimental/directive/{schema.py → template/schema.py} +0 -0
- /lionagi/{tests/libs/test_async.py → experimental/evaluator/__init__.py} +0 -0
- {lionagi-0.1.1.dist-info → lionagi-0.2.0.dist-info}/WHEEL +0 -0
- {lionagi-0.1.1.dist-info → lionagi-0.2.0.dist-info}/top_level.txt +0 -0
lionagi/core/direct/select.py
DELETED
@@ -1,170 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
This module contains the SelectTemplate class for selecting an item from given choices based on a given context.
|
3
|
-
|
4
|
-
The SelectTemplate class is a subclass of ScoredTemplate and provides functionality for selecting an item from
|
5
|
-
given choices using a language model. It includes fields for the input sentence, choices, selected answer,
|
6
|
-
confidence score, and reason for the selection.
|
7
|
-
|
8
|
-
The module also includes a function for selecting an item from given choices using the SelectTemplate class
|
9
|
-
and a language model.
|
10
|
-
"""
|
11
|
-
|
12
|
-
from enum import Enum
|
13
|
-
from pydantic import Field
|
14
|
-
|
15
|
-
from lionagi.libs import func_call, StringMatch
|
16
|
-
from lionagi.core.form.scored_form import ScoredForm
|
17
|
-
from lionagi.core.branch.branch import Branch
|
18
|
-
|
19
|
-
|
20
|
-
class SelectTemplate(ScoredForm):
|
21
|
-
"""
|
22
|
-
A class for selecting an item from given choices based on a given context.
|
23
|
-
|
24
|
-
Attributes:
|
25
|
-
template_name (str): The name of the select template (default: "default_select").
|
26
|
-
sentence (str | list | dict): The given context.
|
27
|
-
answer (Enum | str): The selected item from the given choices.
|
28
|
-
signature (str): The signature indicating the input and output fields (default: "sentence -> answer").
|
29
|
-
|
30
|
-
Methods:
|
31
|
-
__init__(self, sentence=None, choices=None, instruction=None, reason=False, confidence_score=False, **kwargs):
|
32
|
-
Initializes a new instance of the SelectTemplate class.
|
33
|
-
"""
|
34
|
-
|
35
|
-
template_name: str = "default_select"
|
36
|
-
sentence: str | list | dict = Field(
|
37
|
-
default_factory=str, description="the given context"
|
38
|
-
)
|
39
|
-
answer: Enum | str = Field(
|
40
|
-
default_factory=str, description="selection from given choices"
|
41
|
-
)
|
42
|
-
choices: list = Field(default_factory=list, description="the given choices")
|
43
|
-
|
44
|
-
signature: str = "sentence -> answer"
|
45
|
-
|
46
|
-
def __init__(
|
47
|
-
self,
|
48
|
-
sentence=None,
|
49
|
-
choices=None,
|
50
|
-
instruction=None,
|
51
|
-
reason=False,
|
52
|
-
confidence_score=False,
|
53
|
-
**kwargs,
|
54
|
-
):
|
55
|
-
"""
|
56
|
-
Initializes a new instance of the SelectTemplate class.
|
57
|
-
|
58
|
-
Args:
|
59
|
-
sentence (Optional[str | list | dict]): The given context.
|
60
|
-
choices (Optional[list]): The list of choices to select from.
|
61
|
-
instruction (Optional[str]): The instruction for selection.
|
62
|
-
reason (bool): Whether to include the reason for the selection in the output (default: False).
|
63
|
-
confidence_score (bool): Whether to include the confidence score in the output (default: False).
|
64
|
-
**kwargs: Additional keyword arguments.
|
65
|
-
"""
|
66
|
-
super().__init__(**kwargs)
|
67
|
-
|
68
|
-
self.sentence = sentence
|
69
|
-
self.choices = choices
|
70
|
-
self.task = f"select 1 item, from provided choices {choices}."
|
71
|
-
if instruction:
|
72
|
-
self.task += f"objetive {instruction}."
|
73
|
-
|
74
|
-
if reason:
|
75
|
-
self.output_fields.append("reason")
|
76
|
-
|
77
|
-
if confidence_score:
|
78
|
-
self.output_fields.append("confidence_score")
|
79
|
-
|
80
|
-
|
81
|
-
async def select(
|
82
|
-
sentence,
|
83
|
-
choices=None,
|
84
|
-
instruction=None,
|
85
|
-
confidence_score=False,
|
86
|
-
reason=False,
|
87
|
-
retries=2,
|
88
|
-
delay=0.5,
|
89
|
-
backoff_factor=2,
|
90
|
-
default_value=None,
|
91
|
-
timeout=None,
|
92
|
-
branch_name=None,
|
93
|
-
system=None,
|
94
|
-
messages=None,
|
95
|
-
service=None,
|
96
|
-
sender=None,
|
97
|
-
llmconfig=None,
|
98
|
-
tools=None,
|
99
|
-
datalogger=None,
|
100
|
-
persist_path=None,
|
101
|
-
tool_manager=None,
|
102
|
-
**kwargs,
|
103
|
-
):
|
104
|
-
"""
|
105
|
-
Selects an item from given choices based on a given context using a language model.
|
106
|
-
|
107
|
-
Args:
|
108
|
-
sentence (str | list | dict): The given context.
|
109
|
-
choices (Optional[list]): The list of choices to select from.
|
110
|
-
instruction (Optional[str]): The instruction for selection.
|
111
|
-
confidence_score (bool): Whether to include the confidence score in the output (default: False).
|
112
|
-
reason (bool): Whether to include the reason for the selection in the output (default: False).
|
113
|
-
retries (int): The number of retries for the API call (default: 2).
|
114
|
-
delay (float): The initial delay between retries in seconds (default: 0.5).
|
115
|
-
backoff_factor (float): The backoff factor for exponential delay between retries (default: 2).
|
116
|
-
default_value (Optional[Any]): The default value to return if the API call fails (default: None).
|
117
|
-
timeout (Optional[float]): The timeout for the API call in seconds (default: None).
|
118
|
-
branch_name (Optional[str]): The name of the branch to use for selection.
|
119
|
-
system (Optional[Any]): The system configuration for the branch.
|
120
|
-
messages (Optional[Any]): The messages to initialize the branch with.
|
121
|
-
service (Optional[Any]): The service to use for selection.
|
122
|
-
sender (Optional[str]): The sender of the selection request.
|
123
|
-
llmconfig (Optional[Any]): The configuration for the language model.
|
124
|
-
tools (Optional[Any]): The tools to use for selection.
|
125
|
-
datalogger (Optional[Any]): The data logger for the branch.
|
126
|
-
persist_path (Optional[str]): The path to persist the branch data.
|
127
|
-
tool_manager (Optional[Any]): The tool manager for the branch.
|
128
|
-
**kwargs: Additional keyword arguments for the API call.
|
129
|
-
|
130
|
-
Returns:
|
131
|
-
SelectTemplate: The select template with the selected item.
|
132
|
-
"""
|
133
|
-
branch = Branch(
|
134
|
-
name=branch_name,
|
135
|
-
system=system,
|
136
|
-
messages=messages,
|
137
|
-
service=service,
|
138
|
-
sender=sender,
|
139
|
-
llmconfig=llmconfig,
|
140
|
-
tools=tools,
|
141
|
-
datalogger=datalogger,
|
142
|
-
persist_path=persist_path,
|
143
|
-
tool_manager=tool_manager,
|
144
|
-
)
|
145
|
-
|
146
|
-
_template = SelectTemplate(
|
147
|
-
sentence=sentence,
|
148
|
-
choices=choices,
|
149
|
-
instruction=instruction,
|
150
|
-
confidence_score=confidence_score,
|
151
|
-
reason=reason,
|
152
|
-
)
|
153
|
-
|
154
|
-
await func_call.rcall(
|
155
|
-
branch.chat,
|
156
|
-
form=_template,
|
157
|
-
retries=retries,
|
158
|
-
delay=delay,
|
159
|
-
backoff_factor=backoff_factor,
|
160
|
-
default=default_value,
|
161
|
-
timeout=timeout,
|
162
|
-
**kwargs,
|
163
|
-
)
|
164
|
-
|
165
|
-
ans = _template.answer
|
166
|
-
|
167
|
-
if ans not in _template.choices:
|
168
|
-
_template.answer = StringMatch.choose_most_similar(ans, _template.choices)
|
169
|
-
|
170
|
-
return _template
|
lionagi/core/direct/sentiment.py
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
# TODO: sentiment analysis
|
lionagi/core/direct/utils.py
DELETED
@@ -1,110 +0,0 @@
|
|
1
|
-
from typing import Callable
|
2
|
-
from lionagi.core.tool import func_to_tool, Tool
|
3
|
-
|
4
|
-
# import contextlib
|
5
|
-
# from lionagi.libs import ParseUtil, StringMatch, convert, func_call
|
6
|
-
|
7
|
-
|
8
|
-
# def _parse_out(out_):
|
9
|
-
# if isinstance(out_, str):
|
10
|
-
# try:
|
11
|
-
# out_ = ParseUtil.md_to_json(out_)
|
12
|
-
# except Exception:
|
13
|
-
# with contextlib.suppress(Exception):
|
14
|
-
# out_ = ParseUtil.fuzzy_parse_json(out_.strip("```json").strip("```"))
|
15
|
-
# return out_
|
16
|
-
|
17
|
-
|
18
|
-
# def _handle_single_out(
|
19
|
-
# out_,
|
20
|
-
# default_key="answer",
|
21
|
-
# choices=None,
|
22
|
-
# to_type="dict",
|
23
|
-
# to_type_kwargs=None,
|
24
|
-
# to_default=True,
|
25
|
-
# ):
|
26
|
-
|
27
|
-
# if to_type_kwargs is None:
|
28
|
-
# to_type_kwargs = {}
|
29
|
-
# out_ = _parse_out(out_)
|
30
|
-
|
31
|
-
# if default_key not in out_:
|
32
|
-
# raise ValueError(f"Key {default_key} not found in output")
|
33
|
-
|
34
|
-
# answer = out_[default_key]
|
35
|
-
|
36
|
-
# if (
|
37
|
-
# choices is not None
|
38
|
-
# and answer not in choices
|
39
|
-
# and convert.strip_lower(out_) in ["", "none", "null", "na", "n/a"]
|
40
|
-
# ):
|
41
|
-
# raise ValueError(f"Answer {answer} not in choices {choices}")
|
42
|
-
|
43
|
-
# if to_type == "str":
|
44
|
-
# answer = convert.to_str(answer, **to_type_kwargs)
|
45
|
-
|
46
|
-
# elif to_type == "num":
|
47
|
-
# answer = convert.to_num(answer, **to_type_kwargs)
|
48
|
-
|
49
|
-
# out_[default_key] = answer
|
50
|
-
|
51
|
-
# return answer if to_default else out_
|
52
|
-
|
53
|
-
|
54
|
-
# def _handle_multi_out(
|
55
|
-
# out_,
|
56
|
-
# default_key="answer",
|
57
|
-
# choices=None,
|
58
|
-
# to_type="dict",
|
59
|
-
# to_type_kwargs=None,
|
60
|
-
# to_default=True,
|
61
|
-
# include_mapping=False,
|
62
|
-
# ):
|
63
|
-
# if to_type_kwargs is None:
|
64
|
-
# to_type_kwargs = {}
|
65
|
-
|
66
|
-
# if include_mapping:
|
67
|
-
# for i in out_:
|
68
|
-
# i[default_key] = _handle_single_out(
|
69
|
-
# i[default_key],
|
70
|
-
# choices=choices,
|
71
|
-
# default_key=default_key,
|
72
|
-
# to_type=to_type,
|
73
|
-
# to_type_kwargs=to_type_kwargs,
|
74
|
-
# to_default=to_default,
|
75
|
-
# )
|
76
|
-
# else:
|
77
|
-
# _out = []
|
78
|
-
# for i in out_:
|
79
|
-
# i = _handle_single_out(
|
80
|
-
# i,
|
81
|
-
# choices=choices,
|
82
|
-
# default_key=default_key,
|
83
|
-
# to_type=to_type,
|
84
|
-
# to_type_kwargs=to_type_kwargs,
|
85
|
-
# to_default=to_default,
|
86
|
-
# )
|
87
|
-
# _out.append(i)
|
88
|
-
# return _out
|
89
|
-
|
90
|
-
# return out_ if len(out_) > 1 else out_[0]
|
91
|
-
|
92
|
-
|
93
|
-
def _process_tools(tool_obj, branch):
|
94
|
-
if isinstance(tool_obj, Callable):
|
95
|
-
_process_tool(tool_obj, branch)
|
96
|
-
else:
|
97
|
-
for i in tool_obj:
|
98
|
-
_process_tool(i, branch)
|
99
|
-
|
100
|
-
|
101
|
-
def _process_tool(tool_obj, branch):
|
102
|
-
if (
|
103
|
-
isinstance(tool_obj, Tool)
|
104
|
-
and tool_obj.schema_["function"]["name"] not in branch.tool_manager.registry
|
105
|
-
):
|
106
|
-
branch.register_tools(tool_obj)
|
107
|
-
if isinstance(tool_obj, Callable):
|
108
|
-
tool = func_to_tool(tool_obj)[0]
|
109
|
-
if tool.schema_["function"]["name"] not in branch.tool_manager.registry:
|
110
|
-
branch.register_tools(tool)
|
lionagi/core/direct/vote.py
DELETED
@@ -1,64 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
This module contains the vote function for generating and scoring multiple outputs and selecting the top-ranked ones.
|
3
|
-
|
4
|
-
The vote function generates multiple outputs using a specified directive function (default: predict), scores each output
|
5
|
-
using the score function, and returns the top-ranked output(s) based on the scores. It allows for customization of the
|
6
|
-
number of generations, number of outputs to return, number of scorers, score range, and scorer instruction.
|
7
|
-
"""
|
8
|
-
|
9
|
-
from lionagi.libs import func_call
|
10
|
-
import numpy as np
|
11
|
-
from lionagi.core.direct.predict import predict
|
12
|
-
from lionagi.core.direct.score import score
|
13
|
-
|
14
|
-
|
15
|
-
async def vote(
|
16
|
-
sentence,
|
17
|
-
directive=predict,
|
18
|
-
num_generations=5,
|
19
|
-
num_output=1,
|
20
|
-
num_scorer=5,
|
21
|
-
score_range=(0, 100),
|
22
|
-
num_digit=2,
|
23
|
-
scorer_instruction=None,
|
24
|
-
**kwargs,
|
25
|
-
):
|
26
|
-
"""
|
27
|
-
Generates and scores multiple outputs and returns the top-ranked output(s).
|
28
|
-
|
29
|
-
Args:
|
30
|
-
sentence (str): The input sentence or context.
|
31
|
-
directive (function): The function used to generate outputs (default: predict).
|
32
|
-
num_generations (int): The number of outputs to generate (default: 5).
|
33
|
-
num_output (int): The number of top-ranked outputs to return (default: 1).
|
34
|
-
num_scorer (int): The number of scorers to use for scoring each output (default: 5).
|
35
|
-
score_range (tuple): The range of scores to assign (default: (0, 100)).
|
36
|
-
num_digit (int): The number of digits after the decimal point for scores (default: 2).
|
37
|
-
scorer_instruction (str): The instruction for the scorers (default: None).
|
38
|
-
**kwargs: Additional keyword arguments to pass to the directive function.
|
39
|
-
|
40
|
-
Returns:
|
41
|
-
The top-ranked output if num_output is 1, or a list of top-ranked outputs if num_output is greater than 1.
|
42
|
-
"""
|
43
|
-
|
44
|
-
async def _inner(i):
|
45
|
-
out_ = await directive(sentence, **kwargs)
|
46
|
-
score_ = await score(
|
47
|
-
out_.answer,
|
48
|
-
context=sentence,
|
49
|
-
instruction=scorer_instruction,
|
50
|
-
score_range=score_range,
|
51
|
-
num_digit=num_digit,
|
52
|
-
num_instances=num_scorer,
|
53
|
-
return_template=False,
|
54
|
-
)
|
55
|
-
|
56
|
-
out_.__setattr__("score", score_)
|
57
|
-
return out_
|
58
|
-
|
59
|
-
_outs = await func_call.alcall(list(range(num_generations)), _inner)
|
60
|
-
|
61
|
-
top_index = np.argsort([i.score for i in _outs])[-num_output:]
|
62
|
-
final_output = list(np.array(_outs)[top_index])
|
63
|
-
|
64
|
-
return final_output[0] if len(final_output) == 1 else final_output
|
@@ -1,47 +0,0 @@
|
|
1
|
-
from collections import deque
|
2
|
-
from abc import ABC, abstractmethod
|
3
|
-
from typing import Any
|
4
|
-
|
5
|
-
from pydantic import Field
|
6
|
-
|
7
|
-
from lionagi.core.generic import BaseComponent
|
8
|
-
from lionagi.core.mail.schema import BaseMail
|
9
|
-
|
10
|
-
|
11
|
-
class BaseExecutor(BaseComponent, ABC):
|
12
|
-
pending_ins: dict = Field(
|
13
|
-
default_factory=dict, description="The pending incoming mails."
|
14
|
-
)
|
15
|
-
pending_outs: deque = Field(
|
16
|
-
default_factory=deque, description="The pending outgoing mails."
|
17
|
-
)
|
18
|
-
execute_stop: bool = Field(
|
19
|
-
False, description="A flag indicating whether to stop execution."
|
20
|
-
)
|
21
|
-
context: dict | str | list | None = Field(
|
22
|
-
None, description="The context buffer for the next instruction."
|
23
|
-
)
|
24
|
-
execution_responses: list = Field(
|
25
|
-
default_factory=list, description="The list of responses."
|
26
|
-
)
|
27
|
-
context_log: list = Field(default_factory=list, description="The context log.")
|
28
|
-
verbose: bool = Field(
|
29
|
-
True, description="A flag indicating whether to provide verbose output."
|
30
|
-
)
|
31
|
-
|
32
|
-
def send(self, recipient_id: str, category: str, package: Any) -> None:
|
33
|
-
"""
|
34
|
-
Sends a mail to a recipient.
|
35
|
-
|
36
|
-
Args:
|
37
|
-
recipient_id (str): The ID of the recipient.
|
38
|
-
category (str): The category of the mail.
|
39
|
-
package (Any): The package to send in the mail.
|
40
|
-
"""
|
41
|
-
mail = BaseMail(
|
42
|
-
sender_id=self.id_,
|
43
|
-
recipient_id=recipient_id,
|
44
|
-
category=category,
|
45
|
-
package=package,
|
46
|
-
)
|
47
|
-
self.pending_outs.append(mail)
|
lionagi/core/flow/baseflow.py
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
from abc import ABC
|
2
|
-
|
3
|
-
|
4
|
-
class BaseFlow(ABC):
|
5
|
-
|
6
|
-
@classmethod
|
7
|
-
def class_name(cls) -> str:
|
8
|
-
"""
|
9
|
-
Returns the class name of the flow.
|
10
|
-
"""
|
11
|
-
return cls.__name__
|
12
|
-
|
13
|
-
|
14
|
-
class BaseMonoFlow(BaseFlow):
|
15
|
-
|
16
|
-
def __init__(self, branch) -> None:
|
17
|
-
self.branch = branch
|
18
|
-
|
19
|
-
|
20
|
-
class BasePolyFlow(BaseFlow):
|
21
|
-
|
22
|
-
def __init__(self, session) -> None:
|
23
|
-
self.session = session
|
@@ -1,238 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
This module contains the MonoReAct class for performing reasoning and action tasks with an LLM.
|
3
|
-
|
4
|
-
The MonoReAct class allows for conducting a series of reasoning and action steps with an LLM, with the ability to
|
5
|
-
process instructions, system messages, and invoke tools during the conversation. It extends the MonoChat class.
|
6
|
-
"""
|
7
|
-
|
8
|
-
from typing import Callable
|
9
|
-
from lionagi.core.flow.monoflow.chat import MonoChat
|
10
|
-
from lionagi.core.tool import Tool
|
11
|
-
from lionagi.core.messages.schema import Instruction
|
12
|
-
|
13
|
-
|
14
|
-
class MonoReAct(MonoChat):
|
15
|
-
"""
|
16
|
-
A class for performing reasoning and action tasks with an LLM, processing instructions and system messages,
|
17
|
-
and optionally invoking tools.
|
18
|
-
|
19
|
-
Attributes:
|
20
|
-
REASON_PROMPT (str): The default prompt for reasoning steps.
|
21
|
-
ACTION_PROMPT (str): The default prompt for action steps.
|
22
|
-
OUTPUT_PROMPT (str): The default prompt for presenting the final output to the user.
|
23
|
-
|
24
|
-
Methods:
|
25
|
-
async ReAct(self, instruction, context=None, sender=None, system=None, tools=None,
|
26
|
-
num_rounds=1, reason_prompt=None, action_prompt=None, output_prompt=None, **kwargs):
|
27
|
-
Performs a series of reasoning and action steps with an LLM, processing instructions and system messages,
|
28
|
-
and optionally invoking tools.
|
29
|
-
|
30
|
-
_get_prompt(prompt=None, default=None, num_steps=None, instruction=None) -> str:
|
31
|
-
Retrieves the appropriate prompt for the reasoning or action step based on the provided parameters.
|
32
|
-
|
33
|
-
_create_followup_config(self, tools, tool_choice="auto", **kwargs) -> dict:
|
34
|
-
Creates the configuration for the followup steps based on the provided tools and parameters.
|
35
|
-
|
36
|
-
async _ReAct(self, instruction, context=None, sender=None, system=None, tools=None,
|
37
|
-
num_rounds=1, auto=False, reason_prompt=None, action_prompt=None,
|
38
|
-
output_prompt=None, out=True, **kwargs):
|
39
|
-
Performs the actual reasoning and action steps with the LLM, processing instructions and system messages,
|
40
|
-
and optionally invoking tools.
|
41
|
-
"""
|
42
|
-
|
43
|
-
REASON_PROMPT = """
|
44
|
-
You have {num_steps} steps left in the current task. If available, integrate previous tool responses.
|
45
|
-
Perform reasoning and prepare an action plan according to available tools only. Apply divide and conquer technique.
|
46
|
-
"""
|
47
|
-
|
48
|
-
ACTION_PROMPT = """
|
49
|
-
You have {num_steps} steps left in the current task. invoke tool usage.
|
50
|
-
"""
|
51
|
-
|
52
|
-
OUTPUT_PROMPT = "Notice: Present the final output to the user. Original user instruction: {instruction}"
|
53
|
-
|
54
|
-
async def ReAct(
|
55
|
-
self,
|
56
|
-
instruction: Instruction | str | dict[str, dict | str],
|
57
|
-
context=None,
|
58
|
-
sender=None,
|
59
|
-
system=None,
|
60
|
-
tools=None,
|
61
|
-
num_rounds: int = 1,
|
62
|
-
reason_prompt=None,
|
63
|
-
action_prompt=None,
|
64
|
-
output_prompt=None,
|
65
|
-
**kwargs,
|
66
|
-
):
|
67
|
-
"""
|
68
|
-
Performs a series of reasoning and action steps with an LLM, processing instructions and system messages,
|
69
|
-
and optionally invoking tools.
|
70
|
-
|
71
|
-
Args:
|
72
|
-
instruction (Instruction | str | dict[str, dict | str]): The instruction for the task.
|
73
|
-
context (Optional[Any]): Additional context for the task.
|
74
|
-
sender (Optional[str]): The sender of the task message.
|
75
|
-
system (Optional[Any]): System message to be processed during the task.
|
76
|
-
tools (Optional[Any]): Specifies tools to be invoked during the task.
|
77
|
-
num_rounds (int): The number of reasoning and action rounds to perform (default: 1).
|
78
|
-
reason_prompt (Optional[str]): The prompt to use for reasoning steps.
|
79
|
-
action_prompt (Optional[str]): The prompt to use for action steps.
|
80
|
-
output_prompt (Optional[str]): The prompt to use for presenting the final output to the user.
|
81
|
-
**kwargs: Additional keyword arguments for the task.
|
82
|
-
|
83
|
-
Returns:
|
84
|
-
The result of the reasoning and action steps.
|
85
|
-
"""
|
86
|
-
return await self._ReAct(
|
87
|
-
instruction,
|
88
|
-
context=context,
|
89
|
-
sender=sender,
|
90
|
-
system=system,
|
91
|
-
tools=tools,
|
92
|
-
num_rounds=num_rounds,
|
93
|
-
reason_prompt=reason_prompt,
|
94
|
-
action_prompt=action_prompt,
|
95
|
-
output_prompt=output_prompt,
|
96
|
-
**kwargs,
|
97
|
-
)
|
98
|
-
|
99
|
-
@staticmethod
|
100
|
-
def _get_prompt(prompt=None, default=None, num_steps=None, instruction=None):
|
101
|
-
"""
|
102
|
-
Retrieves the appropriate prompt for the reasoning or action step based on the provided parameters.
|
103
|
-
|
104
|
-
Args:
|
105
|
-
prompt (Optional[str]): The prompt to use for the step.
|
106
|
-
default (Optional[str]): The default prompt to use if no specific prompt is provided.
|
107
|
-
num_steps (Optional[int]): The number of remaining steps in the task.
|
108
|
-
instruction (Optional[Any]): The original user instruction.
|
109
|
-
|
110
|
-
Returns:
|
111
|
-
str: The appropriate prompt for the reasoning or action step.
|
112
|
-
"""
|
113
|
-
|
114
|
-
if prompt is not None:
|
115
|
-
return prompt
|
116
|
-
|
117
|
-
try:
|
118
|
-
if num_steps is not None:
|
119
|
-
return default.format(num_steps=num_steps)
|
120
|
-
elif instruction is not None:
|
121
|
-
return default.format(instruction=instruction)
|
122
|
-
except (KeyError, ValueError):
|
123
|
-
pass
|
124
|
-
|
125
|
-
return default
|
126
|
-
|
127
|
-
def _create_followup_config(self, tools, tool_choice="auto", **kwargs):
|
128
|
-
"""
|
129
|
-
Creates the configuration for the followup chat based on the provided tools and parameters.
|
130
|
-
|
131
|
-
Args:
|
132
|
-
tools (Optional[Any]): Specifies tools to be invoked during the followup chat.
|
133
|
-
tool_choice (str): The choice of tools to use (default: "auto").
|
134
|
-
**kwargs: Additional keyword arguments for the followup chat configuration.
|
135
|
-
|
136
|
-
Returns:
|
137
|
-
dict: The configuration for the followup chat.
|
138
|
-
|
139
|
-
Raises:
|
140
|
-
ValueError: If no tools are found and registered.
|
141
|
-
"""
|
142
|
-
if tools and isinstance(tools, list) and isinstance(tools[0], (Callable, Tool)):
|
143
|
-
self.branch.tool_manager.register_tools(tools)
|
144
|
-
|
145
|
-
if not self.branch.tool_manager.has_tools:
|
146
|
-
raise ValueError("No tools found. You need to register tools.")
|
147
|
-
|
148
|
-
config = self.branch.tool_manager.parse_tool(tools=True, **kwargs)
|
149
|
-
config["tool_parsed"] = True
|
150
|
-
config["tool_choice"] = tool_choice
|
151
|
-
return config
|
152
|
-
|
153
|
-
async def _ReAct(
|
154
|
-
self,
|
155
|
-
instruction: Instruction | str | dict[str, dict | str],
|
156
|
-
context=None,
|
157
|
-
sender=None,
|
158
|
-
system=None,
|
159
|
-
tools=None,
|
160
|
-
num_rounds: int = 1,
|
161
|
-
auto=True,
|
162
|
-
reason_prompt=None,
|
163
|
-
action_prompt=None,
|
164
|
-
output_prompt=None,
|
165
|
-
out=True,
|
166
|
-
**kwargs,
|
167
|
-
):
|
168
|
-
"""
|
169
|
-
Performs the actual reasoning and action steps with the LLM, processing instructions and system messages,
|
170
|
-
and optionally invoking tools.
|
171
|
-
|
172
|
-
Args:
|
173
|
-
instruction (Instruction | str | dict[str, dict | str]): The instruction for the task.
|
174
|
-
context (Optional[Any]): Additional context for the task.
|
175
|
-
sender (Optional[str]): The sender of the task message.
|
176
|
-
system (Optional[Any]): System message to be processed during the task.
|
177
|
-
tools (Optional[Any]): Specifies tools to be invoked during the task.
|
178
|
-
num_rounds (int): The number of reasoning and action rounds to perform (default: 1).
|
179
|
-
auto (bool): Flag indicating whether to automatically determine if the task is finished (default: False).
|
180
|
-
reason_prompt (Optional[str]): The prompt to use for reasoning steps.
|
181
|
-
action_prompt (Optional[str]): The prompt to use for action steps.
|
182
|
-
output_prompt (Optional[str]): The prompt to use for presenting the final output to the user.
|
183
|
-
out (bool): Flag indicating whether to return the output of the task (default: True).
|
184
|
-
**kwargs: Additional keyword arguments for the task.
|
185
|
-
|
186
|
-
Returns:
|
187
|
-
The result of the reasoning and action steps.
|
188
|
-
"""
|
189
|
-
config = self._create_followup_config(tools, **kwargs)
|
190
|
-
kwargs.pop("tools", None)
|
191
|
-
|
192
|
-
i = 0
|
193
|
-
_out = ""
|
194
|
-
|
195
|
-
while i < num_rounds:
|
196
|
-
_prompt = self._get_prompt(
|
197
|
-
prompt=reason_prompt,
|
198
|
-
default=self.REASON_PROMPT,
|
199
|
-
num_steps=(num_rounds - i) * 2,
|
200
|
-
)
|
201
|
-
_instruct = {"NOTICE": _prompt}
|
202
|
-
|
203
|
-
# reason
|
204
|
-
if i == 0:
|
205
|
-
_instruct["TASK"] = instruction
|
206
|
-
|
207
|
-
await self.chat(
|
208
|
-
_instruct, context=context, sender=sender, system=system, **kwargs
|
209
|
-
)
|
210
|
-
|
211
|
-
elif i > 0:
|
212
|
-
await self.chat(_instruct, sender=sender, **kwargs)
|
213
|
-
|
214
|
-
# action
|
215
|
-
_prompt = self._get_prompt(
|
216
|
-
prompt=action_prompt,
|
217
|
-
default=self.ACTION_PROMPT,
|
218
|
-
num_steps=(num_rounds - i) * 2 - 1,
|
219
|
-
)
|
220
|
-
|
221
|
-
_out = await self.chat(_prompt, sender=sender, **config)
|
222
|
-
|
223
|
-
if not self.branch._is_invoked():
|
224
|
-
return _out if out else None
|
225
|
-
|
226
|
-
i += 1
|
227
|
-
|
228
|
-
|
229
|
-
if not self.branch._is_invoked():
|
230
|
-
return _out if out else None
|
231
|
-
|
232
|
-
_prompt = self._get_prompt(
|
233
|
-
prompt=output_prompt,
|
234
|
-
default=self.OUTPUT_PROMPT,
|
235
|
-
instruction=instruction,
|
236
|
-
)
|
237
|
-
_out = await self.chat(_prompt, sender=sender, **kwargs)
|
238
|
-
return _out if out else None
|