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
lionagi/core/branch/branch.py
DELETED
@@ -1,474 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
This module contains the Branch class, which represents a branch in a conversation tree.
|
3
|
-
"""
|
4
|
-
|
5
|
-
from collections import deque
|
6
|
-
from typing import Any, Union, TypeVar, Callable
|
7
|
-
from pathlib import Path
|
8
|
-
|
9
|
-
from lionagi.libs import StatusTracker, BaseService, convert, dataframe
|
10
|
-
|
11
|
-
from lionagi.core.generic import DataLogger
|
12
|
-
from lionagi.core.tool import ToolManager, func_to_tool, Tool, TOOL_TYPE
|
13
|
-
from lionagi.core.messages.schema import System
|
14
|
-
from lionagi.core.mail.schema import BaseMail
|
15
|
-
|
16
|
-
from lionagi.core.branch.util import MessageUtil
|
17
|
-
from lionagi.core.branch.base import BaseBranch
|
18
|
-
from lionagi.core.branch.flow_mixin import BranchFlowMixin
|
19
|
-
|
20
|
-
from dotenv import load_dotenv
|
21
|
-
|
22
|
-
load_dotenv()
|
23
|
-
|
24
|
-
T = TypeVar("T", bound=Tool)
|
25
|
-
|
26
|
-
|
27
|
-
class Branch(BaseBranch, BranchFlowMixin):
|
28
|
-
"""
|
29
|
-
Represents a branch in a conversation tree.
|
30
|
-
|
31
|
-
Attributes:
|
32
|
-
sender (str): The sender of the branch (default: "system").
|
33
|
-
tool_manager (ToolManager): The tool manager for the branch.
|
34
|
-
service (BaseService): The service associated with the branch.
|
35
|
-
llmconfig (dict): The configuration for the language model.
|
36
|
-
status_tracker (StatusTracker): The status tracker for the branch.
|
37
|
-
pending_ins (dict): The pending incoming mails for the branch.
|
38
|
-
pending_outs (deque): The pending outgoing mails for the branch.
|
39
|
-
|
40
|
-
Methods:
|
41
|
-
__init__(self, name=None, system=None, messages=None, service=None, sender=None,
|
42
|
-
llmconfig=None, tools=None, datalogger=None, persist_path=None,
|
43
|
-
tool_manager=None, **kwargs) -> None:
|
44
|
-
Initializes the Branch instance.
|
45
|
-
|
46
|
-
from_csv(cls, filepath, name=None, service=None, llmconfig=None, tools=None,
|
47
|
-
datalogger=None, persist_path=None, tool_manager=None, read_kwargs=None,
|
48
|
-
**kwargs) -> Branch:
|
49
|
-
Creates a Branch instance from a CSV file.
|
50
|
-
|
51
|
-
from_json_string(cls, filepath, name=None, service=None, llmconfig=None, tools=None,
|
52
|
-
datalogger=None, persist_path=None, tool_manager=None, read_kwargs=None,
|
53
|
-
**kwargs) -> Branch:
|
54
|
-
Creates a Branch instance from a JSON string file.
|
55
|
-
|
56
|
-
messages_describe(self) -> dict[str, Any]:
|
57
|
-
Describes the messages in the branch.
|
58
|
-
|
59
|
-
has_tools(self) -> bool:
|
60
|
-
Checks if the branch has any registered tools.
|
61
|
-
|
62
|
-
merge_branch(self, branch: Branch, update=True) -> None:
|
63
|
-
Merges another branch into the current branch.
|
64
|
-
|
65
|
-
register_tools(self, tools: Union[Tool, list[Tool]]) -> None:
|
66
|
-
Registers tools in the branch's tool manager.
|
67
|
-
|
68
|
-
delete_tools(self, tools: Union[T, list[T], str, list[str]], verbose=True) -> bool:
|
69
|
-
Deletes tools from the branch's tool manager.
|
70
|
-
|
71
|
-
send(self, recipient: str, category: str, package: Any) -> None:
|
72
|
-
Sends a mail to a recipient.
|
73
|
-
|
74
|
-
receive(self, sender: str, messages=True, tools=True, service=True, llmconfig=True) -> None:
|
75
|
-
Receives mails from a sender and updates the branch accordingly.
|
76
|
-
|
77
|
-
receive_all(self) -> None:
|
78
|
-
Receives all pending mails and updates the branch accordingly.
|
79
|
-
|
80
|
-
_add_service(service, llmconfig) -> tuple[BaseService, dict]:
|
81
|
-
Adds a service and its configuration to the branch.
|
82
|
-
|
83
|
-
_is_invoked(self) -> bool:
|
84
|
-
Checks if the conversation has been invoked with an action response.
|
85
|
-
"""
|
86
|
-
|
87
|
-
def __init__(
|
88
|
-
self,
|
89
|
-
name: str | None = None,
|
90
|
-
system: dict | list | System | None = None,
|
91
|
-
messages: dataframe.ln_DataFrame | None = None,
|
92
|
-
service: BaseService | None = None,
|
93
|
-
sender: str | None = None,
|
94
|
-
llmconfig: dict[str, str | int | dict] | None = None,
|
95
|
-
tools: list[Callable | Tool] | None = None,
|
96
|
-
datalogger: None | DataLogger = None,
|
97
|
-
persist_path: str | Path | None = None, # instruction_sets=None,
|
98
|
-
tool_manager: ToolManager | None = None,
|
99
|
-
**kwargs,
|
100
|
-
):
|
101
|
-
"""
|
102
|
-
Initializes the Branch instance.
|
103
|
-
|
104
|
-
Args:
|
105
|
-
name (str): The name of the branch (optional).
|
106
|
-
system (dict | list | System): The system message for the branch (optional).
|
107
|
-
messages (dataframe.ln_DataFrame): The messages in the branch (optional).
|
108
|
-
service (BaseService): The service associated with the branch (optional).
|
109
|
-
sender (str): The sender of the branch (optional, default: "system").
|
110
|
-
llmconfig (dict[str, str | int | dict]): The configuration for the language model (optional).
|
111
|
-
tools (list[Callable | Tool]): The tools to register in the branch (optional).
|
112
|
-
datalogger (DataLogger): The data logger for the branch (optional).
|
113
|
-
persist_path (str | Path): The path to persist the branch data (optional).
|
114
|
-
tool_manager (ToolManager): The tool manager for the branch (optional).
|
115
|
-
**kwargs: Additional keyword arguments.
|
116
|
-
|
117
|
-
Raises:
|
118
|
-
TypeError: If there is an error in registering the tools.
|
119
|
-
"""
|
120
|
-
|
121
|
-
super().__init__(
|
122
|
-
messages=messages,
|
123
|
-
datalogger=datalogger,
|
124
|
-
persist_path=persist_path,
|
125
|
-
name=name,
|
126
|
-
**kwargs,
|
127
|
-
)
|
128
|
-
|
129
|
-
self.sender = sender or "system"
|
130
|
-
self.tool_manager = tool_manager or ToolManager()
|
131
|
-
|
132
|
-
if tools:
|
133
|
-
try:
|
134
|
-
tools_ = []
|
135
|
-
_tools = convert.to_list(tools)
|
136
|
-
for i in _tools:
|
137
|
-
if isinstance(i, Tool):
|
138
|
-
tools_.append(i)
|
139
|
-
else:
|
140
|
-
tools_.append(func_to_tool(i))
|
141
|
-
|
142
|
-
self.register_tools(tools_)
|
143
|
-
except Exception as e:
|
144
|
-
raise TypeError(f"Error in registering tools: {e}") from e
|
145
|
-
|
146
|
-
self.service, self.llmconfig = self._add_service(service, llmconfig)
|
147
|
-
self.status_tracker = StatusTracker()
|
148
|
-
|
149
|
-
# add instruction sets
|
150
|
-
# self.instruction_sets = instruction_sets
|
151
|
-
|
152
|
-
self.pending_ins = {}
|
153
|
-
self.pending_outs = deque()
|
154
|
-
|
155
|
-
if system is not None:
|
156
|
-
self.add_message(system=system)
|
157
|
-
|
158
|
-
@classmethod
|
159
|
-
def from_csv(
|
160
|
-
cls,
|
161
|
-
filepath,
|
162
|
-
name: str | None = None,
|
163
|
-
service: BaseService | None = None,
|
164
|
-
llmconfig: dict[str, str | int | dict] | None = None,
|
165
|
-
tools: TOOL_TYPE | None = None,
|
166
|
-
datalogger: None | DataLogger = None,
|
167
|
-
persist_path: str | Path | None = None, # instruction_sets=None,
|
168
|
-
tool_manager: ToolManager | None = None,
|
169
|
-
read_kwargs=None,
|
170
|
-
**kwargs,
|
171
|
-
) -> "Branch":
|
172
|
-
"""
|
173
|
-
Creates a Branch instance from a CSV file.
|
174
|
-
|
175
|
-
Args:
|
176
|
-
filepath: The path to the CSV file.
|
177
|
-
name (str): The name of the branch (optional).
|
178
|
-
service (BaseService): The service associated with the branch (optional).
|
179
|
-
llmconfig (dict[str, str | int | dict]): The configuration for the language model (optional).
|
180
|
-
tools (TOOL_TYPE): The tools to register in the branch (optional).
|
181
|
-
datalogger (DataLogger): The data logger for the branch (optional).
|
182
|
-
persist_path (str | Path): The path to persist the branch data (optional).
|
183
|
-
tool_manager (ToolManager): The tool manager for the branch (optional).
|
184
|
-
read_kwargs: Additional keyword arguments for reading the CSV file (optional).
|
185
|
-
**kwargs: Additional keyword arguments.
|
186
|
-
|
187
|
-
Returns:
|
188
|
-
Branch: The created Branch instance.
|
189
|
-
"""
|
190
|
-
return cls._from_csv(
|
191
|
-
filepath=filepath,
|
192
|
-
read_kwargs=read_kwargs,
|
193
|
-
name=name,
|
194
|
-
service=service,
|
195
|
-
llmconfig=llmconfig,
|
196
|
-
tools=tools,
|
197
|
-
datalogger=datalogger,
|
198
|
-
persist_path=persist_path,
|
199
|
-
# instruction_sets=instruction_sets,
|
200
|
-
tool_manager=tool_manager,
|
201
|
-
**kwargs,
|
202
|
-
)
|
203
|
-
|
204
|
-
@classmethod
|
205
|
-
def from_json_string(
|
206
|
-
cls,
|
207
|
-
filepath,
|
208
|
-
name: str | None = None,
|
209
|
-
service: BaseService | None = None,
|
210
|
-
llmconfig: dict[str, str | int | dict] | None = None,
|
211
|
-
tools: TOOL_TYPE | None = None,
|
212
|
-
datalogger: None | DataLogger = None,
|
213
|
-
persist_path: str | Path | None = None, # instruction_sets=None,
|
214
|
-
tool_manager: ToolManager | None = None,
|
215
|
-
read_kwargs=None,
|
216
|
-
**kwargs,
|
217
|
-
) -> "Branch":
|
218
|
-
"""
|
219
|
-
Creates a Branch instance from a JSON string file.
|
220
|
-
|
221
|
-
Args:
|
222
|
-
filepath: The path to the JSON string file.
|
223
|
-
name (str): The name of the branch (optional).
|
224
|
-
service (BaseService): The service associated with the branch (optional).
|
225
|
-
llmconfig (dict[str, str | int | dict]): The configuration for the language model (optional).
|
226
|
-
tools (TOOL_TYPE): The tools to register in the branch (optional).
|
227
|
-
datalogger (DataLogger): The data logger for the branch (optional).
|
228
|
-
persist_path (str | Path): The path to persist the branch data (optional).
|
229
|
-
tool_manager (ToolManager): The tool manager for the branch (optional).
|
230
|
-
read_kwargs: Additional keyword arguments for reading the JSON string file (optional).
|
231
|
-
**kwargs: Additional keyword arguments.
|
232
|
-
|
233
|
-
Returns:
|
234
|
-
Branch: The created Branch instance.
|
235
|
-
"""
|
236
|
-
return cls._from_json(
|
237
|
-
filepath=filepath,
|
238
|
-
read_kwargs=read_kwargs,
|
239
|
-
name=name,
|
240
|
-
service=service,
|
241
|
-
llmconfig=llmconfig,
|
242
|
-
tools=tools,
|
243
|
-
datalogger=datalogger,
|
244
|
-
persist_path=persist_path,
|
245
|
-
# instruction_sets=instruction_sets,
|
246
|
-
tool_manager=tool_manager,
|
247
|
-
**kwargs,
|
248
|
-
)
|
249
|
-
|
250
|
-
def messages_describe(self) -> dict[str, Any]:
|
251
|
-
"""
|
252
|
-
Describes the messages in the branch.
|
253
|
-
|
254
|
-
Returns:
|
255
|
-
dict[str, Any]: A dictionary describing the messages in the branch.
|
256
|
-
"""
|
257
|
-
return dict(
|
258
|
-
total_messages=len(self.messages),
|
259
|
-
summary_by_role=self._info(),
|
260
|
-
summary_by_sender=self._info(use_sender=True),
|
261
|
-
# instruction_sets=self.instruction_sets,
|
262
|
-
registered_tools=self.tool_manager.registry,
|
263
|
-
messages=[msg.to_dict() for _, msg in self.messages.iterrows()],
|
264
|
-
)
|
265
|
-
|
266
|
-
@property
|
267
|
-
def has_tools(self) -> bool:
|
268
|
-
"""
|
269
|
-
Checks if the branch has any registered tools.
|
270
|
-
|
271
|
-
Returns:
|
272
|
-
bool: True if the branch has registered tools, False otherwise.
|
273
|
-
"""
|
274
|
-
return self.tool_manager.registry != {}
|
275
|
-
|
276
|
-
# todo: also update other attributes
|
277
|
-
def merge_branch(self, branch: "Branch", update: bool = True) -> None:
|
278
|
-
"""
|
279
|
-
Merges another branch into the current branch.
|
280
|
-
|
281
|
-
Args:
|
282
|
-
branch (Branch): The branch to merge.
|
283
|
-
update (bool): Whether to update the existing attributes or add new ones (default: True).
|
284
|
-
"""
|
285
|
-
message_copy = branch.messages.copy()
|
286
|
-
self.messages = self.messages.merge(message_copy, how="outer")
|
287
|
-
self.datalogger.extend(branch.datalogger.log)
|
288
|
-
|
289
|
-
if update:
|
290
|
-
# self.instruction_sets.update(branch.instruction_sets)
|
291
|
-
self.tool_manager.registry.update(branch.tool_manager.registry)
|
292
|
-
else:
|
293
|
-
for key, value in branch.instruction_sets.items():
|
294
|
-
if key not in self.instruction_sets:
|
295
|
-
self.instruction_sets[key] = value
|
296
|
-
|
297
|
-
for key, value in branch.tool_manager.registry.items():
|
298
|
-
if key not in self.tool_manager.registry:
|
299
|
-
self.tool_manager.registry[key] = value
|
300
|
-
|
301
|
-
# ----- tool manager methods ----- #
|
302
|
-
def register_tools(
|
303
|
-
self, tools: Union[Tool, list[Tool | Callable], Callable]
|
304
|
-
) -> None:
|
305
|
-
"""
|
306
|
-
Registers tools in the branch's tool manager.
|
307
|
-
|
308
|
-
Args:
|
309
|
-
tools (Union[Tool, list[Tool]]): The tool(s) to register.
|
310
|
-
"""
|
311
|
-
if not isinstance(tools, list):
|
312
|
-
tools = [tools]
|
313
|
-
self.tool_manager.register_tools(tools=tools)
|
314
|
-
|
315
|
-
def delete_tools(
|
316
|
-
self,
|
317
|
-
tools: Union[T, list[T], str, list[str]],
|
318
|
-
verbose: bool = True,
|
319
|
-
) -> bool:
|
320
|
-
"""
|
321
|
-
Deletes tools from the branch's tool manager.
|
322
|
-
|
323
|
-
Args:
|
324
|
-
tools (Union[T, list[T], str, list[str]]): The tool(s) to delete.
|
325
|
-
verbose (bool): Whether to print success/failure messages (default: True).
|
326
|
-
|
327
|
-
Returns:
|
328
|
-
bool: True if the tools were successfully deleted, False otherwise.
|
329
|
-
"""
|
330
|
-
if not isinstance(tools, list):
|
331
|
-
tools = [tools]
|
332
|
-
if convert.is_same_dtype(tools, str):
|
333
|
-
for act_ in tools:
|
334
|
-
if act_ in self.tool_manager.registry:
|
335
|
-
self.tool_manager.registry.pop(act_)
|
336
|
-
if verbose:
|
337
|
-
print("tools successfully deleted")
|
338
|
-
return True
|
339
|
-
elif convert.is_same_dtype(tools, Tool):
|
340
|
-
for act_ in tools:
|
341
|
-
if act_.schema_["function"]["name"] in self.tool_manager.registry:
|
342
|
-
self.tool_manager.registry.pop(act_.schema_["function"]["name"])
|
343
|
-
if verbose:
|
344
|
-
print("tools successfully deleted")
|
345
|
-
return True
|
346
|
-
if verbose:
|
347
|
-
print("tools deletion failed")
|
348
|
-
return False
|
349
|
-
|
350
|
-
def send(self, recipient_id: str, category: str, package: Any) -> None:
|
351
|
-
"""
|
352
|
-
Sends a mail to a recipient.
|
353
|
-
|
354
|
-
Args:
|
355
|
-
recipient (str): The recipient of the mail.
|
356
|
-
category (str): The category of the mail.
|
357
|
-
package (Any): The package to send in the mail.
|
358
|
-
"""
|
359
|
-
mail = BaseMail(
|
360
|
-
sender_id=self.id_,
|
361
|
-
recipient_id=recipient_id,
|
362
|
-
category=category,
|
363
|
-
package=package,
|
364
|
-
)
|
365
|
-
self.pending_outs.append(mail)
|
366
|
-
|
367
|
-
def receive(
|
368
|
-
self,
|
369
|
-
sender: str,
|
370
|
-
messages: bool = True,
|
371
|
-
tools: bool = True,
|
372
|
-
service: bool = True,
|
373
|
-
llmconfig: bool = True,
|
374
|
-
) -> None:
|
375
|
-
"""
|
376
|
-
Receives mails from a sender and updates the branch accordingly.
|
377
|
-
|
378
|
-
Args:
|
379
|
-
sender (str): The sender of the mails.
|
380
|
-
messages (bool): Whether to receive message updates (default: True).
|
381
|
-
tools (bool): Whether to receive tool updates (default: True).
|
382
|
-
service (bool): Whether to receive service updates (default: True).
|
383
|
-
llmconfig (bool): Whether to receive language model configuration updates (default: True).
|
384
|
-
|
385
|
-
Raises:
|
386
|
-
ValueError: If there are no packages from the specified sender.
|
387
|
-
If the messages format is invalid.
|
388
|
-
If the tools format is invalid.
|
389
|
-
If the provider format is invalid.
|
390
|
-
If the llmconfig format is invalid.
|
391
|
-
"""
|
392
|
-
skipped_requests = deque()
|
393
|
-
if sender not in self.pending_ins:
|
394
|
-
raise ValueError(f"No package from {sender}")
|
395
|
-
while self.pending_ins[sender]:
|
396
|
-
mail_ = self.pending_ins[sender].popleft()
|
397
|
-
|
398
|
-
if mail_.category == "messages" and messages:
|
399
|
-
if not isinstance(mail_.package, dataframe.ln_DataFrame):
|
400
|
-
raise ValueError("Invalid messages format")
|
401
|
-
MessageUtil.validate_messages(mail_.package)
|
402
|
-
self.messages = self.messages.merge(mail_.package, how="outer")
|
403
|
-
|
404
|
-
elif mail_.category == "tools" and tools:
|
405
|
-
if not isinstance(mail_.package, Tool):
|
406
|
-
raise ValueError("Invalid tools format")
|
407
|
-
self.tool_manager.register_tools([mail_.package])
|
408
|
-
|
409
|
-
elif mail_.category == "provider" and service:
|
410
|
-
from lionagi.libs.ln_api import BaseService
|
411
|
-
|
412
|
-
if not isinstance(mail_.package, BaseService):
|
413
|
-
raise ValueError("Invalid provider format")
|
414
|
-
self.service = mail_.package
|
415
|
-
|
416
|
-
elif mail_.category == "llmconfig" and llmconfig:
|
417
|
-
if not isinstance(mail_.package, dict):
|
418
|
-
raise ValueError("Invalid llmconfig format")
|
419
|
-
self.llmconfig.update(mail_.package)
|
420
|
-
|
421
|
-
else:
|
422
|
-
skipped_requests.append(mail_)
|
423
|
-
|
424
|
-
self.pending_ins[sender] = skipped_requests
|
425
|
-
if self.pending_ins[sender] == deque():
|
426
|
-
self.pending_ins.pop(sender)
|
427
|
-
|
428
|
-
def receive_all(self) -> None:
|
429
|
-
"""
|
430
|
-
Receives all pending mails and updates the branch accordingly.
|
431
|
-
"""
|
432
|
-
for key in list(self.pending_ins.keys()):
|
433
|
-
self.receive(key)
|
434
|
-
|
435
|
-
@staticmethod
|
436
|
-
def _add_service(service, llmconfig):
|
437
|
-
from lionagi.integrations.provider.oai import OpenAIService
|
438
|
-
|
439
|
-
if service is None:
|
440
|
-
try:
|
441
|
-
from lionagi.integrations.provider import Services
|
442
|
-
|
443
|
-
service = Services.OpenAI()
|
444
|
-
|
445
|
-
except:
|
446
|
-
raise ValueError("No available service")
|
447
|
-
if llmconfig is None:
|
448
|
-
if isinstance(service, OpenAIService):
|
449
|
-
from lionagi.integrations.config import oai_schema
|
450
|
-
|
451
|
-
llmconfig = oai_schema["chat/completions"]["config"]
|
452
|
-
else:
|
453
|
-
llmconfig = {}
|
454
|
-
return service, llmconfig
|
455
|
-
|
456
|
-
def _is_invoked(self) -> bool:
|
457
|
-
"""
|
458
|
-
Check if the conversation has been invoked with an action response.
|
459
|
-
|
460
|
-
Returns:
|
461
|
-
bool: True if the conversation has been invoked, False otherwise.
|
462
|
-
|
463
|
-
"""
|
464
|
-
content = self.messages.iloc[-1]["content"]
|
465
|
-
try:
|
466
|
-
if convert.to_dict(content)["action_response"].keys() >= {
|
467
|
-
"function",
|
468
|
-
"arguments",
|
469
|
-
"output",
|
470
|
-
}:
|
471
|
-
return True
|
472
|
-
except Exception:
|
473
|
-
return False
|
474
|
-
return False
|
@@ -1,96 +0,0 @@
|
|
1
|
-
from abc import ABC
|
2
|
-
from typing import Any, Optional, Union, TypeVar
|
3
|
-
|
4
|
-
from lionagi.core.tool import Tool, TOOL_TYPE
|
5
|
-
from lionagi.core.messages.schema import Instruction, System
|
6
|
-
from lionagi.core.flow.monoflow import MonoChat, MonoFollowup, MonoReAct
|
7
|
-
|
8
|
-
T = TypeVar("T", bound=Tool)
|
9
|
-
|
10
|
-
|
11
|
-
class BranchFlowMixin(ABC):
|
12
|
-
|
13
|
-
async def chat(
|
14
|
-
self,
|
15
|
-
instruction: Union[Instruction, str] = None,
|
16
|
-
context: Optional[Any] = None,
|
17
|
-
sender: Optional[str] = None,
|
18
|
-
system: Optional[Union[System, str, dict[str, Any]]] = None,
|
19
|
-
tools: TOOL_TYPE = False,
|
20
|
-
out: bool = True,
|
21
|
-
invoke: bool = True,
|
22
|
-
output_fields=None,
|
23
|
-
form=None,
|
24
|
-
**kwargs,
|
25
|
-
) -> Any:
|
26
|
-
flow = MonoChat(self)
|
27
|
-
return await flow.chat(
|
28
|
-
instruction=instruction,
|
29
|
-
context=context,
|
30
|
-
sender=sender,
|
31
|
-
system=system,
|
32
|
-
tools=tools,
|
33
|
-
out=out,
|
34
|
-
invoke=invoke,
|
35
|
-
output_fields=output_fields,
|
36
|
-
form=form,
|
37
|
-
**kwargs,
|
38
|
-
)
|
39
|
-
|
40
|
-
async def ReAct(
|
41
|
-
self,
|
42
|
-
instruction: Instruction | str | dict[str, dict | str],
|
43
|
-
context=None,
|
44
|
-
sender=None,
|
45
|
-
system=None,
|
46
|
-
tools=None,
|
47
|
-
auto=False,
|
48
|
-
num_rounds: int = 1,
|
49
|
-
reason_prompt=None,
|
50
|
-
action_prompt=None,
|
51
|
-
output_prompt=None,
|
52
|
-
**kwargs,
|
53
|
-
):
|
54
|
-
flow = MonoReAct(self)
|
55
|
-
return await flow.ReAct(
|
56
|
-
instruction=instruction,
|
57
|
-
context=context,
|
58
|
-
sender=sender,
|
59
|
-
system=system,
|
60
|
-
tools=tools,
|
61
|
-
auto=auto,
|
62
|
-
num_rounds=num_rounds,
|
63
|
-
reason_prompt=reason_prompt,
|
64
|
-
action_prompt=action_prompt,
|
65
|
-
output_prompt=output_prompt,
|
66
|
-
**kwargs,
|
67
|
-
)
|
68
|
-
|
69
|
-
async def followup(
|
70
|
-
self,
|
71
|
-
instruction: Instruction | str | dict[str, dict | str],
|
72
|
-
context=None,
|
73
|
-
sender=None,
|
74
|
-
system=None,
|
75
|
-
tools=None,
|
76
|
-
max_followup: int = 1,
|
77
|
-
auto=False,
|
78
|
-
followup_prompt=None,
|
79
|
-
output_prompt=None,
|
80
|
-
out=True,
|
81
|
-
**kwargs,
|
82
|
-
):
|
83
|
-
flow = MonoFollowup(self)
|
84
|
-
return await flow.followup(
|
85
|
-
instruction=instruction,
|
86
|
-
context=context,
|
87
|
-
sender=sender,
|
88
|
-
system=system,
|
89
|
-
tools=tools,
|
90
|
-
max_followup=max_followup,
|
91
|
-
auto=auto,
|
92
|
-
followup_prompt=followup_prompt,
|
93
|
-
output_prompt=output_prompt,
|
94
|
-
out=out,
|
95
|
-
**kwargs,
|
96
|
-
)
|