lionagi 0.1.2__py3-none-any.whl → 0.2.1__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} +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 +76 -0
- lionagi/core/work/work_function.py +101 -0
- lionagi/core/work/work_queue.py +103 -0
- lionagi/core/work/worker.py +258 -0
- lionagi/core/work/worklog.py +120 -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.1.dist-info/LICENSE +202 -0
- lionagi-0.2.1.dist-info/METADATA +272 -0
- lionagi-0.2.1.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.1.dist-info}/WHEEL +0 -0
- {lionagi-0.1.2.dist-info → lionagi-0.2.1.dist-info}/top_level.txt +0 -0
lionagi/core/direct/predict.py
DELETED
@@ -1,166 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
This module contains the PredictTemplate class for predicting the next sentence(s) based on a given sentence.
|
3
|
-
|
4
|
-
The PredictTemplate class is a subclass of ScoredTemplate and provides functionality for predicting the next sentence(s)
|
5
|
-
using a language model. It includes fields for the input sentence, number of sentences to predict, predicted answer,
|
6
|
-
confidence score, and reason for the prediction.
|
7
|
-
"""
|
8
|
-
|
9
|
-
from lionagi.libs import func_call
|
10
|
-
from lionagi.integrations.bridge.pydantic_.pydantic_bridge import Field
|
11
|
-
|
12
|
-
from lionagi.core.form.scored_form import ScoredForm
|
13
|
-
from lionagi.core.branch.branch import Branch
|
14
|
-
|
15
|
-
|
16
|
-
class PredictTemplate(ScoredForm):
|
17
|
-
"""
|
18
|
-
A class for predicting the next sentence(s) based on a given sentence.
|
19
|
-
|
20
|
-
Attributes:
|
21
|
-
template_name (str): The name of the predict template (default: "default_predict_template").
|
22
|
-
sentence (str | list | dict): The given sentence(s) to predict.
|
23
|
-
num_sentences (int): The number of sentences to predict.
|
24
|
-
answer (str | list): The predicted sentence(s).
|
25
|
-
signature (str): The signature indicating the input and output fields (default: "sentence -> answer").
|
26
|
-
|
27
|
-
Methods:
|
28
|
-
__init__(self, sentence=None, num_sentences=None, confidence_score=False, reason=False, **kwargs):
|
29
|
-
Initializes a new instance of the PredictTemplate class.
|
30
|
-
|
31
|
-
async predict(sentence=None, num_sentences=1, confidence_score=False, reason=False, retries=2,
|
32
|
-
delay=0.5, backoff_factor=2, default_value=None, timeout=None, branch_name=None,
|
33
|
-
system=None, messages=None, service=None, sender=None, llmconfig=None, tools=None,
|
34
|
-
datalogger=None, persist_path=None, tool_manager=None, **kwargs) -> PredictTemplate:
|
35
|
-
Predicts the next sentence(s) based on the given sentence using a language model.
|
36
|
-
"""
|
37
|
-
|
38
|
-
template_name: str = "default_predict"
|
39
|
-
sentence: str | list | dict = Field(
|
40
|
-
default_factory=str, description="the given sentence(s) to predict"
|
41
|
-
)
|
42
|
-
num_sentences: int = Field(
|
43
|
-
default_factory=int, description="the number of sentences to predict"
|
44
|
-
)
|
45
|
-
answer: str | list = Field(
|
46
|
-
default_factory=str, description="the predicted sentence(s) or desired output"
|
47
|
-
)
|
48
|
-
signature: str = "sentence -> answer"
|
49
|
-
|
50
|
-
def __init__(
|
51
|
-
self,
|
52
|
-
sentence=None,
|
53
|
-
instruction=None,
|
54
|
-
num_sentences=1,
|
55
|
-
confidence_score=False,
|
56
|
-
reason=False,
|
57
|
-
**kwargs,
|
58
|
-
):
|
59
|
-
"""
|
60
|
-
Initializes a new instance of the PredictTemplate class.
|
61
|
-
|
62
|
-
Args:
|
63
|
-
sentence (Optional[str | list | dict]): The given sentence(s) to predict.
|
64
|
-
num_sentences (Optional[int]): The number of sentences to predict.
|
65
|
-
confidence_score (bool): Whether to include the confidence score in the output (default: False).
|
66
|
-
reason (bool): Whether to include the reason for the prediction in the output (default: False).
|
67
|
-
**kwargs: Additional keyword arguments.
|
68
|
-
"""
|
69
|
-
super().__init__(**kwargs)
|
70
|
-
|
71
|
-
self.sentence = sentence or ""
|
72
|
-
self.num_sentences = num_sentences
|
73
|
-
self.task = f"follow instruction to predict the next {self.num_sentences} sentence(s). Instruction: {instruction}."
|
74
|
-
|
75
|
-
if reason:
|
76
|
-
self.output_fields.append("reason")
|
77
|
-
|
78
|
-
if confidence_score:
|
79
|
-
self.output_fields.append("confidence_score")
|
80
|
-
|
81
|
-
|
82
|
-
async def predict(
|
83
|
-
sentence=None,
|
84
|
-
num_sentences=1,
|
85
|
-
confidence_score=False,
|
86
|
-
instruction=None,
|
87
|
-
branch=None,
|
88
|
-
reason=False,
|
89
|
-
retries=2,
|
90
|
-
delay=0.5,
|
91
|
-
backoff_factor=2,
|
92
|
-
default_value=None,
|
93
|
-
timeout=None,
|
94
|
-
branch_name=None,
|
95
|
-
system=None,
|
96
|
-
messages=None,
|
97
|
-
service=None,
|
98
|
-
sender=None,
|
99
|
-
llmconfig=None,
|
100
|
-
tools=None,
|
101
|
-
datalogger=None,
|
102
|
-
persist_path=None,
|
103
|
-
tool_manager=None,
|
104
|
-
**kwargs,
|
105
|
-
) -> "PredictTemplate":
|
106
|
-
"""
|
107
|
-
Predicts the next sentence(s) based on the given sentence using a language model.
|
108
|
-
|
109
|
-
Args:
|
110
|
-
sentence (Optional[str | list | dict]): The given sentence(s) to predict.
|
111
|
-
num_sentences (int): The number of sentences to predict (default: 1).
|
112
|
-
confidence_score (bool): Whether to include the confidence score in the output (default: False).
|
113
|
-
reason (bool): Whether to include the reason for the prediction in the output (default: False).
|
114
|
-
retries (int): The number of retries for the API call (default: 2).
|
115
|
-
delay (float): The initial delay between retries in seconds (default: 0.5).
|
116
|
-
backoff_factor (float): The backoff factor for exponential delay between retries (default: 2).
|
117
|
-
default_value (Optional[Any]): The default value to return if the API call fails (default: None).
|
118
|
-
timeout (Optional[float]): The timeout for the API call in seconds (default: None).
|
119
|
-
branch_name (Optional[str]): The name of the branch to use for prediction.
|
120
|
-
system (Optional[Any]): The system configuration for the branch.
|
121
|
-
messages (Optional[Any]): The messages to initialize the branch with.
|
122
|
-
service (Optional[Any]): The service to use for prediction.
|
123
|
-
sender (Optional[str]): The sender of the prediction request.
|
124
|
-
llmconfig (Optional[Any]): The configuration for the language model.
|
125
|
-
tools (Optional[Any]): The tools to use for prediction.
|
126
|
-
datalogger (Optional[Any]): The data logger for the branch.
|
127
|
-
persist_path (Optional[str]): The path to persist the branch data.
|
128
|
-
tool_manager (Optional[Any]): The tool manager for the branch.
|
129
|
-
**kwargs: Additional keyword arguments for the API call.
|
130
|
-
|
131
|
-
Returns:
|
132
|
-
PredictTemplate: The predict template with the predicted sentence(s).
|
133
|
-
"""
|
134
|
-
branch = branch or Branch(
|
135
|
-
name=branch_name,
|
136
|
-
system=system,
|
137
|
-
messages=messages,
|
138
|
-
service=service,
|
139
|
-
sender=sender,
|
140
|
-
llmconfig=llmconfig,
|
141
|
-
tools=tools,
|
142
|
-
datalogger=datalogger,
|
143
|
-
persist_path=persist_path,
|
144
|
-
tool_manager=tool_manager,
|
145
|
-
)
|
146
|
-
|
147
|
-
predict_template = PredictTemplate(
|
148
|
-
instruction=instruction,
|
149
|
-
sentence=sentence,
|
150
|
-
num_sentences=num_sentences,
|
151
|
-
confidence_score=confidence_score,
|
152
|
-
reason=reason,
|
153
|
-
)
|
154
|
-
|
155
|
-
await func_call.rcall(
|
156
|
-
branch.chat,
|
157
|
-
form=predict_template,
|
158
|
-
retries=retries,
|
159
|
-
delay=delay,
|
160
|
-
backoff_factor=backoff_factor,
|
161
|
-
default=default_value,
|
162
|
-
timeout=timeout,
|
163
|
-
**kwargs,
|
164
|
-
)
|
165
|
-
|
166
|
-
return predict_template
|
lionagi/core/direct/react.py
DELETED
@@ -1,171 +0,0 @@
|
|
1
|
-
from lionagi.libs import func_call, AsyncUtil
|
2
|
-
|
3
|
-
from lionagi.integrations.bridge.pydantic_.pydantic_bridge import Field
|
4
|
-
from lionagi.core.form.action_form import ActionForm
|
5
|
-
from lionagi.core.branch.branch import Branch
|
6
|
-
from lionagi.core.direct.utils import _process_tools
|
7
|
-
|
8
|
-
|
9
|
-
class ReactTemplate(ActionForm):
|
10
|
-
template_name: str = "default_react"
|
11
|
-
sentence: str | list | dict | None = Field(
|
12
|
-
default_factory=str,
|
13
|
-
description="the given sentence(s) to reason and take actions on",
|
14
|
-
)
|
15
|
-
|
16
|
-
def __init__(
|
17
|
-
self,
|
18
|
-
sentence=None,
|
19
|
-
instruction=None,
|
20
|
-
confidence_score=False,
|
21
|
-
**kwargs,
|
22
|
-
):
|
23
|
-
super().__init__(**kwargs)
|
24
|
-
|
25
|
-
self.sentence = sentence
|
26
|
-
self.task = f"Think step by step. Perform reasoning and prepare actions with given tools only.Instruction: {instruction}. Absolutely DO NOT MAKE UP FUNCTIONS !!!"
|
27
|
-
|
28
|
-
if confidence_score:
|
29
|
-
self.output_fields.append("confidence_score")
|
30
|
-
|
31
|
-
|
32
|
-
async def _react(
|
33
|
-
sentence=None,
|
34
|
-
*,
|
35
|
-
instruction=None,
|
36
|
-
branch=None,
|
37
|
-
confidence_score=False,
|
38
|
-
retries=2,
|
39
|
-
delay=0.5,
|
40
|
-
backoff_factor=2,
|
41
|
-
default_value=None,
|
42
|
-
timeout=None,
|
43
|
-
branch_name=None,
|
44
|
-
system=None,
|
45
|
-
messages=None,
|
46
|
-
service=None,
|
47
|
-
sender=None,
|
48
|
-
llmconfig=None,
|
49
|
-
tools=None,
|
50
|
-
datalogger=None,
|
51
|
-
persist_path=None,
|
52
|
-
tool_manager=None,
|
53
|
-
return_branch=False,
|
54
|
-
**kwargs,
|
55
|
-
):
|
56
|
-
|
57
|
-
if "temperature" not in kwargs:
|
58
|
-
kwargs["temperature"] = 0.1
|
59
|
-
|
60
|
-
instruction = instruction or ""
|
61
|
-
|
62
|
-
if branch and tools:
|
63
|
-
_process_tools(tools, branch)
|
64
|
-
|
65
|
-
branch = branch or Branch(
|
66
|
-
name=branch_name,
|
67
|
-
system=system,
|
68
|
-
messages=messages,
|
69
|
-
service=service,
|
70
|
-
sender=sender,
|
71
|
-
llmconfig=llmconfig,
|
72
|
-
tools=tools,
|
73
|
-
datalogger=datalogger,
|
74
|
-
persist_path=persist_path,
|
75
|
-
tool_manager=tool_manager,
|
76
|
-
)
|
77
|
-
|
78
|
-
_template = ReactTemplate(
|
79
|
-
sentence=sentence,
|
80
|
-
instruction=instruction,
|
81
|
-
confidence_score=confidence_score,
|
82
|
-
)
|
83
|
-
|
84
|
-
await func_call.rcall(
|
85
|
-
branch.chat,
|
86
|
-
form=_template,
|
87
|
-
retries=retries,
|
88
|
-
delay=delay,
|
89
|
-
backoff_factor=backoff_factor,
|
90
|
-
default=default_value,
|
91
|
-
timeout=timeout,
|
92
|
-
**kwargs,
|
93
|
-
)
|
94
|
-
|
95
|
-
if _template.action_needed:
|
96
|
-
actions = _template.actions
|
97
|
-
tasks = [branch.tool_manager.invoke(i.values()) for i in actions]
|
98
|
-
results = await AsyncUtil.execute_tasks(*tasks)
|
99
|
-
|
100
|
-
a = []
|
101
|
-
for idx, item in enumerate(actions):
|
102
|
-
res = {
|
103
|
-
"function": item["function"],
|
104
|
-
"arguments": item["arguments"],
|
105
|
-
"output": results[idx],
|
106
|
-
}
|
107
|
-
branch.add_message(response=res)
|
108
|
-
a.append(res)
|
109
|
-
|
110
|
-
_template.__setattr__("action_response", a)
|
111
|
-
|
112
|
-
return (_template, branch) if return_branch else _template
|
113
|
-
|
114
|
-
|
115
|
-
async def react(
|
116
|
-
sentence=None,
|
117
|
-
*,
|
118
|
-
instruction=None,
|
119
|
-
num_instances=1,
|
120
|
-
branch=None,
|
121
|
-
confidence_score=False,
|
122
|
-
retries=2,
|
123
|
-
delay=0.5,
|
124
|
-
backoff_factor=2,
|
125
|
-
default_value=None,
|
126
|
-
timeout=None,
|
127
|
-
branch_name=None,
|
128
|
-
system=None,
|
129
|
-
messages=None,
|
130
|
-
service=None,
|
131
|
-
sender=None,
|
132
|
-
llmconfig=None,
|
133
|
-
tools=None,
|
134
|
-
datalogger=None,
|
135
|
-
persist_path=None,
|
136
|
-
tool_manager=None,
|
137
|
-
return_branch=False,
|
138
|
-
**kwargs,
|
139
|
-
):
|
140
|
-
|
141
|
-
async def _inner(i=0):
|
142
|
-
return await _react(
|
143
|
-
sentence=sentence,
|
144
|
-
instruction=instruction,
|
145
|
-
num_instances=num_instances,
|
146
|
-
branch=branch,
|
147
|
-
confidence_score=confidence_score,
|
148
|
-
retries=retries,
|
149
|
-
delay=delay,
|
150
|
-
backoff_factor=backoff_factor,
|
151
|
-
default_value=default_value,
|
152
|
-
timeout=timeout,
|
153
|
-
branch_name=branch_name,
|
154
|
-
system=system,
|
155
|
-
messages=messages,
|
156
|
-
service=service,
|
157
|
-
sender=sender,
|
158
|
-
llmconfig=llmconfig,
|
159
|
-
tools=tools,
|
160
|
-
datalogger=datalogger,
|
161
|
-
persist_path=persist_path,
|
162
|
-
tool_manager=tool_manager,
|
163
|
-
return_branch=return_branch,
|
164
|
-
**kwargs,
|
165
|
-
)
|
166
|
-
|
167
|
-
if num_instances == 1:
|
168
|
-
return await _inner()
|
169
|
-
|
170
|
-
elif num_instances > 1:
|
171
|
-
return await func_call.alcall(range(num_instances), _inner)
|
lionagi/core/direct/score.py
DELETED
@@ -1,279 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
This module contains the ScoreTemplate class and related functions for scoring a given context using a language model.
|
3
|
-
|
4
|
-
The ScoreTemplate class is a subclass of ScoredTemplate and provides functionality for scoring a given context
|
5
|
-
based on specified instructions, score range, and other parameters. It includes fields for the input sentence,
|
6
|
-
score range, inclusive flag, number of digits, confidence score, and reason for the score.
|
7
|
-
|
8
|
-
The module also includes functions for scoring a single instance or multiple instances of the context using the
|
9
|
-
ScoreTemplate class and a language model.
|
10
|
-
"""
|
11
|
-
|
12
|
-
from pydantic import Field
|
13
|
-
import numpy as np
|
14
|
-
from lionagi.libs import func_call, convert
|
15
|
-
from lionagi.core.form.scored_form import ScoredForm
|
16
|
-
from lionagi.core.branch.branch import Branch
|
17
|
-
|
18
|
-
|
19
|
-
class ScoreTemplate(ScoredForm):
|
20
|
-
"""
|
21
|
-
A class for scoring a given context using a language model.
|
22
|
-
|
23
|
-
Attributes:
|
24
|
-
template_name (str): The name of the score template (default: "default_score").
|
25
|
-
sentence (str | list | dict): The given context to score.
|
26
|
-
answer (float): The numeric score for the context.
|
27
|
-
signature (str): The signature indicating the input and output fields (default: "sentence -> answer").
|
28
|
-
|
29
|
-
Methods:
|
30
|
-
__init__(self, sentence=None, instruction=None, score_range=(1, 10), inclusive=True, num_digit=0,
|
31
|
-
confidence_score=False, reason=False, **kwargs):
|
32
|
-
Initializes a new instance of the ScoreTemplate class.
|
33
|
-
"""
|
34
|
-
|
35
|
-
template_name: str = "default_score"
|
36
|
-
sentence: str | list | dict = Field(
|
37
|
-
default_factory=str, description="the given context to score"
|
38
|
-
)
|
39
|
-
answer: float = Field(default_factory=float, description=f"a numeric score")
|
40
|
-
signature: str = "sentence -> answer"
|
41
|
-
|
42
|
-
def __init__(
|
43
|
-
self,
|
44
|
-
sentence=None,
|
45
|
-
instruction=None,
|
46
|
-
score_range=(1, 10),
|
47
|
-
inclusive=True,
|
48
|
-
num_digit=0,
|
49
|
-
confidence_score=False,
|
50
|
-
reason=False,
|
51
|
-
**kwargs,
|
52
|
-
):
|
53
|
-
super().__init__(**kwargs)
|
54
|
-
|
55
|
-
self.sentence = sentence
|
56
|
-
|
57
|
-
return_precision = ""
|
58
|
-
if num_digit == 0:
|
59
|
-
return_precision = "integer"
|
60
|
-
else:
|
61
|
-
return_precision = f"num:{convert.to_str(num_digit)}f"
|
62
|
-
|
63
|
-
self.task = f"""
|
64
|
-
score context according to the following constraints
|
65
|
-
1. objective, {convert.to_str(instruction)}
|
66
|
-
2. score range, {convert.to_str(score_range)}
|
67
|
-
3. include_endpoints, {"yes" if inclusive else "no"}
|
68
|
-
4. format the score in {return_precision}
|
69
|
-
"""
|
70
|
-
|
71
|
-
if reason:
|
72
|
-
self.output_fields.append("reason")
|
73
|
-
|
74
|
-
if confidence_score:
|
75
|
-
self.output_fields.append("confidence_score")
|
76
|
-
|
77
|
-
self.out_validation_kwargs["answer"] = {
|
78
|
-
"upper_bound": score_range[1],
|
79
|
-
"lower_bound": score_range[0],
|
80
|
-
"num_type": int if num_digit == 0 else float,
|
81
|
-
"precision": num_digit if num_digit != 0 else None,
|
82
|
-
}
|
83
|
-
|
84
|
-
|
85
|
-
async def _score(
|
86
|
-
sentence,
|
87
|
-
instruction=None,
|
88
|
-
score_range=(1, 10),
|
89
|
-
inclusive=True,
|
90
|
-
num_digit=0,
|
91
|
-
confidence_score=False,
|
92
|
-
reason=False,
|
93
|
-
retries=2,
|
94
|
-
delay=0.5,
|
95
|
-
backoff_factor=2,
|
96
|
-
default_value=None,
|
97
|
-
timeout=None,
|
98
|
-
branch_name=None,
|
99
|
-
system=None,
|
100
|
-
messages=None,
|
101
|
-
service=None,
|
102
|
-
sender=None,
|
103
|
-
llmconfig=None,
|
104
|
-
tools=None,
|
105
|
-
datalogger=None,
|
106
|
-
persist_path=None,
|
107
|
-
tool_manager=None,
|
108
|
-
**kwargs,
|
109
|
-
):
|
110
|
-
"""
|
111
|
-
Scores a given context using a language model.
|
112
|
-
|
113
|
-
Args:
|
114
|
-
sentence (str | list | dict): The given context to score.
|
115
|
-
instruction (Optional[str]): The instruction for scoring the context.
|
116
|
-
score_range (tuple): The range of valid scores (default: (1, 10)).
|
117
|
-
inclusive (bool): Whether to include the endpoints of the score range (default: True).
|
118
|
-
num_digit (int): The number of digits after the decimal point for the score (default: 0).
|
119
|
-
confidence_score (bool): Whether to include the confidence score in the output (default: False).
|
120
|
-
reason (bool): Whether to include the reason for the score in the output (default: False).
|
121
|
-
retries (int): The number of retries for the API call (default: 2).
|
122
|
-
delay (float): The initial delay between retries in seconds (default: 0.5).
|
123
|
-
backoff_factor (float): The backoff factor for exponential delay between retries (default: 2).
|
124
|
-
default_value (Optional[Any]): The default value to return if the API call fails (default: None).
|
125
|
-
timeout (Optional[float]): The timeout for the API call in seconds (default: None).
|
126
|
-
branch_name (Optional[str]): The name of the branch to use for scoring.
|
127
|
-
system (Optional[Any]): The system configuration for the branch.
|
128
|
-
messages (Optional[Any]): The messages to initialize the branch with.
|
129
|
-
service (Optional[Any]): The service to use for scoring.
|
130
|
-
sender (Optional[str]): The sender of the scoring request.
|
131
|
-
llmconfig (Optional[Any]): The configuration for the language model.
|
132
|
-
tools (Optional[Any]): The tools to use for scoring.
|
133
|
-
datalogger (Optional[Any]): The data logger for the branch.
|
134
|
-
persist_path (Optional[str]): The path to persist the branch data.
|
135
|
-
tool_manager (Optional[Any]): The tool manager for the branch.
|
136
|
-
**kwargs: Additional keyword arguments for the API call.
|
137
|
-
|
138
|
-
Returns:
|
139
|
-
ScoreTemplate: The score template with the scored context.
|
140
|
-
"""
|
141
|
-
|
142
|
-
if "temperature" not in kwargs:
|
143
|
-
kwargs["temperature"] = 0.1
|
144
|
-
|
145
|
-
instruction = instruction or ""
|
146
|
-
|
147
|
-
branch = Branch(
|
148
|
-
name=branch_name,
|
149
|
-
system=system,
|
150
|
-
messages=messages,
|
151
|
-
service=service,
|
152
|
-
sender=sender,
|
153
|
-
llmconfig=llmconfig,
|
154
|
-
tools=tools,
|
155
|
-
datalogger=datalogger,
|
156
|
-
persist_path=persist_path,
|
157
|
-
tool_manager=tool_manager,
|
158
|
-
)
|
159
|
-
|
160
|
-
_template = ScoreTemplate(
|
161
|
-
sentence=sentence,
|
162
|
-
instruction=instruction,
|
163
|
-
score_range=score_range,
|
164
|
-
inclusive=inclusive,
|
165
|
-
num_digit=num_digit,
|
166
|
-
confidence_score=confidence_score,
|
167
|
-
reason=reason,
|
168
|
-
)
|
169
|
-
|
170
|
-
await func_call.rcall(
|
171
|
-
branch.chat,
|
172
|
-
form=_template,
|
173
|
-
retries=retries,
|
174
|
-
delay=delay,
|
175
|
-
backoff_factor=backoff_factor,
|
176
|
-
default=default_value,
|
177
|
-
timeout=timeout,
|
178
|
-
**kwargs,
|
179
|
-
)
|
180
|
-
|
181
|
-
return _template
|
182
|
-
|
183
|
-
|
184
|
-
async def score(
|
185
|
-
sentence,
|
186
|
-
*,
|
187
|
-
num_instances=1,
|
188
|
-
instruction=None,
|
189
|
-
score_range=(1, 10),
|
190
|
-
inclusive=True,
|
191
|
-
num_digit=0,
|
192
|
-
confidence_score=False,
|
193
|
-
reason=False,
|
194
|
-
retries=2,
|
195
|
-
delay=0.5,
|
196
|
-
backoff_factor=2,
|
197
|
-
default_value=None,
|
198
|
-
timeout=None,
|
199
|
-
branch_name=None,
|
200
|
-
system=None,
|
201
|
-
messages=None,
|
202
|
-
service=None,
|
203
|
-
sender=None,
|
204
|
-
llmconfig=None,
|
205
|
-
tools=None,
|
206
|
-
datalogger=None,
|
207
|
-
persist_path=None,
|
208
|
-
tool_manager=None,
|
209
|
-
return_template=True,
|
210
|
-
**kwargs,
|
211
|
-
) -> ScoreTemplate | float:
|
212
|
-
"""
|
213
|
-
Scores a given context using a language model, with the option to score multiple instances.
|
214
|
-
|
215
|
-
Args:
|
216
|
-
sentence (str | list | dict): The given context to score.
|
217
|
-
num_instances (int): The number of instances to score (default: 1).
|
218
|
-
instruction (Optional[str]): The instruction for scoring the context.
|
219
|
-
score_range (tuple): The range of valid scores (default: (1, 10)).
|
220
|
-
inclusive (bool): Whether to include the endpoints of the score range (default: True).
|
221
|
-
num_digit (int): The number of digits after the decimal point for the score (default: 0).
|
222
|
-
confidence_score (bool): Whether to include the confidence score in the output (default: False).
|
223
|
-
reason (bool): Whether to include the reason for the score in the output (default: False).
|
224
|
-
retries (int): The number of retries for the API call (default: 2).
|
225
|
-
delay (float): The initial delay between retries in seconds (default: 0.5).
|
226
|
-
backoff_factor (float): The backoff factor for exponential delay between retries (default: 2).
|
227
|
-
default_value (Optional[Any]): The default value to return if the API call fails (default: None).
|
228
|
-
timeout (Optional[float]): The timeout for the API call in seconds (default: None).
|
229
|
-
branch_name (Optional[str]): The name of the branch to use for scoring.
|
230
|
-
system (Optional[Any]): The system configuration for the branch.
|
231
|
-
messages (Optional[Any]): The messages to initialize the branch with.
|
232
|
-
service (Optional[Any]): The service to use for scoring.
|
233
|
-
sender (Optional[str]): The sender of the scoring request.
|
234
|
-
llmconfig (Optional[Any]): The configuration for the language model.
|
235
|
-
tools (Optional[Any]): The tools to use for scoring.
|
236
|
-
datalogger (Optional[Any]): The data logger for the branch.
|
237
|
-
persist_path (Optional[str]): The path to persist the branch data.
|
238
|
-
tool_manager (Optional[Any]): The tool manager for the branch.
|
239
|
-
return_template (bool): Whether to return the score template or only the score (default: True).
|
240
|
-
**kwargs: Additional keyword arguments for the API call.
|
241
|
-
|
242
|
-
Returns:
|
243
|
-
ScoreTemplate | float: The score template with the scored context or the average score if `return_template` is False.
|
244
|
-
"""
|
245
|
-
|
246
|
-
async def _inner(i=0):
|
247
|
-
return await _score(
|
248
|
-
sentence=sentence,
|
249
|
-
instruction=instruction,
|
250
|
-
score_range=score_range,
|
251
|
-
inclusive=inclusive,
|
252
|
-
num_digit=num_digit,
|
253
|
-
confidence_score=confidence_score,
|
254
|
-
reason=reason,
|
255
|
-
retries=retries,
|
256
|
-
delay=delay,
|
257
|
-
backoff_factor=backoff_factor,
|
258
|
-
default_value=default_value,
|
259
|
-
timeout=timeout,
|
260
|
-
branch_name=branch_name,
|
261
|
-
system=system,
|
262
|
-
messages=messages,
|
263
|
-
service=service,
|
264
|
-
sender=sender,
|
265
|
-
llmconfig=llmconfig,
|
266
|
-
tools=tools,
|
267
|
-
datalogger=datalogger,
|
268
|
-
persist_path=persist_path,
|
269
|
-
tool_manager=tool_manager,
|
270
|
-
**kwargs,
|
271
|
-
)
|
272
|
-
|
273
|
-
if num_instances == 1:
|
274
|
-
_out = await _inner()
|
275
|
-
return _out if return_template else _out.answer
|
276
|
-
|
277
|
-
elif num_instances > 1:
|
278
|
-
_outs = await func_call.alcall(range(num_instances), _inner)
|
279
|
-
return _outs if return_template else np.mean([i.answer for i in _outs])
|