lionagi 0.0.312__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 +61 -3
- lionagi/core/__init__.py +0 -14
- 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/__init__.py +0 -3
- lionagi/core/agent/base_agent.py +45 -36
- 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/collections/_logger.py +319 -0
- 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/engine/branch_engine.py +333 -0
- lionagi/core/engine/instruction_map_engine.py +204 -0
- lionagi/core/engine/sandbox_.py +14 -0
- lionagi/core/engine/script_engine.py +99 -0
- lionagi/core/executor/base_executor.py +90 -0
- lionagi/core/executor/graph_executor.py +330 -0
- lionagi/core/executor/neo4j_executor.py +384 -0
- lionagi/core/generic/__init__.py +7 -0
- lionagi/core/generic/edge.py +112 -0
- 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 +220 -0
- lionagi/core/generic/tree.py +48 -0
- lionagi/core/generic/tree_node.py +79 -0
- lionagi/core/mail/__init__.py +7 -3
- lionagi/core/mail/mail.py +25 -0
- lionagi/core/mail/mail_manager.py +142 -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/__init__.py +0 -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/__init__.py +0 -3
- lionagi/core/session/branch.py +431 -0
- lionagi/core/session/directive_mixin.py +287 -0
- lionagi/core/session/session.py +230 -902
- 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/__init__.py +0 -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/__init__.py +0 -0
- lionagi/core/validator/validator.py +364 -0
- lionagi/core/work/__init__.py +0 -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/__init__.py +0 -0
- lionagi/experimental/compressor/__init__.py +0 -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/__init__.py +0 -0
- lionagi/experimental/directive/parser/base_parser.py +282 -0
- lionagi/experimental/directive/template/__init__.py +0 -0
- lionagi/experimental/directive/template/base_template.py +79 -0
- lionagi/experimental/directive/template/schema.py +36 -0
- lionagi/experimental/directive/tokenizer.py +73 -0
- lionagi/experimental/evaluator/__init__.py +0 -0
- lionagi/experimental/evaluator/ast_evaluator.py +131 -0
- lionagi/experimental/evaluator/base_evaluator.py +218 -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/autogen_/__init__.py +0 -0
- lionagi/integrations/bridge/autogen_/autogen_.py +124 -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/bridge/llamaindex_/llama_pack.py +227 -0
- lionagi/integrations/bridge/llamaindex_/node_parser.py +6 -9
- lionagi/integrations/bridge/pydantic_/pydantic_bridge.py +1 -0
- lionagi/integrations/bridge/transformers_/__init__.py +0 -0
- lionagi/integrations/bridge/transformers_/install_.py +36 -0
- lionagi/integrations/chunker/__init__.py +0 -0
- lionagi/integrations/chunker/chunk.py +312 -0
- lionagi/integrations/config/oai_configs.py +38 -7
- lionagi/integrations/config/ollama_configs.py +1 -1
- lionagi/integrations/config/openrouter_configs.py +14 -2
- lionagi/integrations/loader/__init__.py +0 -0
- lionagi/integrations/loader/load.py +253 -0
- lionagi/integrations/loader/load_util.py +195 -0
- 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 +7 -6
- 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 -0
- lionagi/integrations/storage/neo4j.py +665 -0
- lionagi/integrations/storage/storage_util.py +287 -0
- lionagi/integrations/storage/structure_excel.py +285 -0
- lionagi/integrations/storage/to_csv.py +63 -0
- lionagi/integrations/storage/to_excel.py +83 -0
- lionagi/libs/__init__.py +26 -1
- lionagi/libs/ln_api.py +78 -23
- 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_knowledge_graph.py +405 -0
- lionagi/libs/ln_nested.py +26 -11
- lionagi/libs/ln_parse.py +110 -14
- lionagi/libs/ln_queue.py +117 -0
- lionagi/libs/ln_tokenize.py +164 -0
- lionagi/{core/prompt/field_validator.py → libs/ln_validate.py} +79 -14
- lionagi/libs/special_tokens.py +172 -0
- lionagi/libs/sys_util.py +107 -2
- lionagi/lions/__init__.py +0 -0
- lionagi/lions/coder/__init__.py +0 -0
- lionagi/lions/coder/add_feature.py +20 -0
- lionagi/lions/coder/base_prompts.py +22 -0
- lionagi/lions/coder/code_form.py +13 -0
- lionagi/lions/coder/coder.py +168 -0
- lionagi/lions/coder/util.py +96 -0
- lionagi/lions/researcher/__init__.py +0 -0
- lionagi/lions/researcher/data_source/__init__.py +0 -0
- lionagi/lions/researcher/data_source/finhub_.py +191 -0
- lionagi/lions/researcher/data_source/google_.py +199 -0
- lionagi/lions/researcher/data_source/wiki_.py +96 -0
- lionagi/lions/researcher/data_source/yfinance_.py +21 -0
- lionagi/tests/integrations/__init__.py +0 -0
- lionagi/tests/libs/__init__.py +0 -0
- lionagi/tests/libs/test_field_validators.py +353 -0
- lionagi/tests/{test_libs → libs}/test_func_call.py +23 -21
- lionagi/tests/{test_libs → libs}/test_nested.py +36 -21
- lionagi/tests/{test_libs → libs}/test_parse.py +1 -1
- lionagi/tests/libs/test_queue.py +67 -0
- 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 -292
- 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.0.312.dist-info → lionagi-0.2.1.dist-info}/LICENSE +12 -11
- {lionagi-0.0.312.dist-info → lionagi-0.2.1.dist-info}/METADATA +19 -118
- lionagi-0.2.1.dist-info/RECORD +240 -0
- lionagi/core/branch/__init__.py +0 -4
- lionagi/core/branch/base_branch.py +0 -654
- lionagi/core/branch/branch.py +0 -471
- lionagi/core/branch/branch_flow_mixin.py +0 -96
- lionagi/core/branch/executable_branch.py +0 -347
- lionagi/core/branch/util.py +0 -323
- lionagi/core/direct/__init__.py +0 -6
- lionagi/core/direct/predict.py +0 -161
- lionagi/core/direct/score.py +0 -278
- lionagi/core/direct/select.py +0 -169
- lionagi/core/direct/utils.py +0 -87
- lionagi/core/direct/vote.py +0 -64
- lionagi/core/flow/base/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 -263
- lionagi/core/flow/monoflow/followup.py +0 -214
- lionagi/core/flow/polyflow/__init__.py +0 -1
- lionagi/core/flow/polyflow/chat.py +0 -248
- lionagi/core/mail/schema.py +0 -56
- lionagi/core/messages/__init__.py +0 -3
- lionagi/core/messages/schema.py +0 -533
- lionagi/core/prompt/prompt_template.py +0 -316
- lionagi/core/schema/__init__.py +0 -22
- lionagi/core/schema/action_node.py +0 -29
- lionagi/core/schema/base_mixin.py +0 -296
- lionagi/core/schema/base_node.py +0 -199
- lionagi/core/schema/condition.py +0 -24
- lionagi/core/schema/data_logger.py +0 -354
- lionagi/core/schema/data_node.py +0 -93
- lionagi/core/schema/prompt_template.py +0 -67
- lionagi/core/schema/structure.py +0 -910
- lionagi/core/tool/__init__.py +0 -3
- lionagi/core/tool/tool_manager.py +0 -280
- lionagi/integrations/bridge/pydantic_/base_model.py +0 -7
- lionagi/tests/test_core/test_base_branch.py +0 -427
- 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 -312
- lionagi/tests/test_core/test_tool_manager.py +0 -95
- lionagi-0.0.312.dist-info/RECORD +0 -111
- /lionagi/core/{branch/base → _setting}/__init__.py +0 -0
- /lionagi/core/{flow → agent/eval}/__init__.py +0 -0
- /lionagi/core/{flow/base → agent/learn}/__init__.py +0 -0
- /lionagi/core/{prompt → agent/plan}/__init__.py +0 -0
- /lionagi/core/{tool/manual.py → agent/plan/plan.py} +0 -0
- /lionagi/{tests/test_integrations → core/director}/__init__.py +0 -0
- /lionagi/{tests/test_libs → core/engine}/__init__.py +0 -0
- /lionagi/{tests/test_libs/test_async.py → core/executor/__init__.py} +0 -0
- /lionagi/tests/{test_libs → libs}/test_api.py +0 -0
- /lionagi/tests/{test_libs → libs}/test_convert.py +0 -0
- /lionagi/tests/{test_libs → libs}/test_sys_util.py +0 -0
- {lionagi-0.0.312.dist-info → lionagi-0.2.1.dist-info}/WHEEL +0 -0
- {lionagi-0.0.312.dist-info → lionagi-0.2.1.dist-info}/top_level.txt +0 -0
@@ -1,654 +0,0 @@
|
|
1
|
-
from abc import ABC
|
2
|
-
from typing import Any
|
3
|
-
|
4
|
-
from lionagi.libs.sys_util import PATH_TYPE
|
5
|
-
from lionagi.libs import convert, dataframe, SysUtil
|
6
|
-
|
7
|
-
from ..schema.base_node import BaseRelatableNode
|
8
|
-
from ..schema.data_logger import DataLogger, DLog
|
9
|
-
from ..messages.schema import (
|
10
|
-
BranchColumns,
|
11
|
-
System,
|
12
|
-
Response,
|
13
|
-
Instruction,
|
14
|
-
BaseMessage,
|
15
|
-
)
|
16
|
-
from .util import MessageUtil
|
17
|
-
|
18
|
-
|
19
|
-
class BaseBranch(BaseRelatableNode, ABC):
|
20
|
-
"""
|
21
|
-
Base class for managing branches of conversation, incorporating messages
|
22
|
-
and logging functionality.
|
23
|
-
|
24
|
-
Attributes:
|
25
|
-
messages (dataframe.ln_DataFrame): Holds the messages in the branch.
|
26
|
-
datalogger (DataLogger): Logs data related to the branch's operation.
|
27
|
-
persist_path (PATH_TYPE): Filesystem path for data persistence.
|
28
|
-
"""
|
29
|
-
|
30
|
-
_columns: list[str] = BranchColumns.COLUMNS.value
|
31
|
-
|
32
|
-
def __init__(
|
33
|
-
self,
|
34
|
-
messages: dataframe.ln_DataFrame | None = None,
|
35
|
-
datalogger: DataLogger | None = None,
|
36
|
-
persist_path: PATH_TYPE | None = None,
|
37
|
-
name=None,
|
38
|
-
**kwargs,
|
39
|
-
) -> None:
|
40
|
-
super().__init__(**kwargs)
|
41
|
-
if isinstance(messages, dataframe.ln_DataFrame):
|
42
|
-
if MessageUtil.validate_messages(messages):
|
43
|
-
self.messages = messages
|
44
|
-
else:
|
45
|
-
raise ValueError("Invalid messages format")
|
46
|
-
else:
|
47
|
-
self.messages = dataframe.ln_DataFrame(columns=self._columns)
|
48
|
-
|
49
|
-
self.datalogger = datalogger or DataLogger(persist_path=persist_path)
|
50
|
-
self.name = name
|
51
|
-
|
52
|
-
def add_message(
|
53
|
-
self,
|
54
|
-
system: dict | list | System | None = None,
|
55
|
-
instruction: dict | list | Instruction | None = None,
|
56
|
-
context: str | dict[str, Any] | None = None,
|
57
|
-
response: dict | list | BaseMessage | None = None,
|
58
|
-
output_fields=None,
|
59
|
-
recipient=None,
|
60
|
-
**kwargs,
|
61
|
-
) -> None:
|
62
|
-
"""
|
63
|
-
Adds a message to the branch.
|
64
|
-
|
65
|
-
Args:
|
66
|
-
system: Information for creating a System message.
|
67
|
-
instruction: Information for creating an Instruction message.
|
68
|
-
context: Context information for the message.
|
69
|
-
response: Response data for creating a message.
|
70
|
-
**kwargs: Additional keyword arguments for message creation.
|
71
|
-
"""
|
72
|
-
_msg = MessageUtil.create_message(
|
73
|
-
system=system,
|
74
|
-
instruction=instruction,
|
75
|
-
context=context,
|
76
|
-
response=response,
|
77
|
-
output_fields=output_fields,
|
78
|
-
recipient=recipient,
|
79
|
-
**kwargs,
|
80
|
-
)
|
81
|
-
|
82
|
-
if isinstance(_msg, System):
|
83
|
-
self.system_node = _msg
|
84
|
-
|
85
|
-
# sourcery skip: merge-nested-ifs
|
86
|
-
if isinstance(_msg, Instruction):
|
87
|
-
if recipient is None and self.name is not None:
|
88
|
-
_msg.recipient = self.name
|
89
|
-
|
90
|
-
if isinstance(_msg, Response):
|
91
|
-
if "action_response" in _msg.content.keys():
|
92
|
-
if recipient is None and self.name is not None:
|
93
|
-
_msg.recipient = self.name
|
94
|
-
if recipient is not None and self.name is None:
|
95
|
-
_msg.recipient = recipient
|
96
|
-
if "response" in _msg.content.keys():
|
97
|
-
if self.name is not None:
|
98
|
-
_msg.sender = self.name
|
99
|
-
|
100
|
-
_msg.content = _msg.msg_content
|
101
|
-
self.messages.loc[len(self.messages)] = _msg.to_pd_series()
|
102
|
-
|
103
|
-
def _to_chatcompletion_message(
|
104
|
-
self, with_sender: bool = False
|
105
|
-
) -> list[dict[str, Any]]:
|
106
|
-
"""
|
107
|
-
Converts messages to a list of dictionaries formatted for chat completion,
|
108
|
-
optionally including sender information.
|
109
|
-
|
110
|
-
Args:
|
111
|
-
with_sender: Flag to include sender information in the output.
|
112
|
-
|
113
|
-
Returns:
|
114
|
-
A list of message dictionaries, each with 'role' and 'content' keys,
|
115
|
-
and optionally prefixed by 'Sender' if with_sender is True.
|
116
|
-
"""
|
117
|
-
|
118
|
-
message = []
|
119
|
-
|
120
|
-
for _, row in self.messages.iterrows():
|
121
|
-
content_ = row["content"]
|
122
|
-
if content_.startswith("Sender"):
|
123
|
-
content_ = content_.split(":", 1)[1]
|
124
|
-
|
125
|
-
# if isinstance(content_, str):
|
126
|
-
# try:
|
127
|
-
# content_ = json.dumps(to_dict(content_))
|
128
|
-
# except Exception as e:
|
129
|
-
# raise ValueError(
|
130
|
-
# f"Error in serializing, {row['node_id']} {content_}: {e}"
|
131
|
-
# )
|
132
|
-
|
133
|
-
out = {"role": row["role"], "content": content_}
|
134
|
-
if with_sender:
|
135
|
-
out["content"] = f"Sender {row['sender']}: {content_}"
|
136
|
-
|
137
|
-
message.append(out)
|
138
|
-
return message
|
139
|
-
|
140
|
-
@property
|
141
|
-
def chat_messages(self) -> list[dict[str, Any]]:
|
142
|
-
"""
|
143
|
-
Retrieves all chat messages without sender information.
|
144
|
-
|
145
|
-
Returns:
|
146
|
-
A list of dictionaries representing chat messages.
|
147
|
-
"""
|
148
|
-
|
149
|
-
return self._to_chatcompletion_message()
|
150
|
-
|
151
|
-
@property
|
152
|
-
def chat_messages_with_sender(self) -> list[dict[str, Any]]:
|
153
|
-
"""
|
154
|
-
Retrieves all chat messages, including sender information.
|
155
|
-
|
156
|
-
Returns:
|
157
|
-
A list of dictionaries representing chat messages, each prefixed with its sender.
|
158
|
-
"""
|
159
|
-
|
160
|
-
return self._to_chatcompletion_message(with_sender=True)
|
161
|
-
|
162
|
-
@property
|
163
|
-
def last_message(self) -> dataframe.ln_DataFrame:
|
164
|
-
"""
|
165
|
-
Retrieves the last message from the branch as a pandas Series.
|
166
|
-
|
167
|
-
Returns:
|
168
|
-
A pandas Series representing the last message in the branch.
|
169
|
-
"""
|
170
|
-
|
171
|
-
return MessageUtil.get_message_rows(self.messages, n=1, from_="last")
|
172
|
-
|
173
|
-
@property
|
174
|
-
def last_message_content(self) -> dict[str, Any]:
|
175
|
-
"""
|
176
|
-
Extracts the content of the last message in the branch.
|
177
|
-
|
178
|
-
Returns:
|
179
|
-
A dictionary representing the content of the last message.
|
180
|
-
"""
|
181
|
-
|
182
|
-
return convert.to_dict(self.messages.content.iloc[-1])
|
183
|
-
|
184
|
-
@property
|
185
|
-
def first_system(self) -> dataframe.ln_DataFrame:
|
186
|
-
"""
|
187
|
-
Retrieves the first message marked with the 'system' role.
|
188
|
-
|
189
|
-
Returns:
|
190
|
-
A pandas Series representing the first 'system' message in the branch.
|
191
|
-
"""
|
192
|
-
|
193
|
-
return MessageUtil.get_message_rows(
|
194
|
-
self.messages, role="system", n=1, from_="front"
|
195
|
-
)
|
196
|
-
|
197
|
-
@property
|
198
|
-
def last_response(self) -> dataframe.ln_DataFrame:
|
199
|
-
"""
|
200
|
-
Retrieves the last message marked with the 'assistant' role.
|
201
|
-
|
202
|
-
Returns:
|
203
|
-
A pandas Series representing the last 'assistant' (response) message in the branch.
|
204
|
-
"""
|
205
|
-
|
206
|
-
return MessageUtil.get_message_rows(
|
207
|
-
self.messages, role="assistant", n=1, from_="last"
|
208
|
-
)
|
209
|
-
|
210
|
-
@property
|
211
|
-
def last_response_content(self) -> dict[str, Any]:
|
212
|
-
"""
|
213
|
-
Extracts the content of the last 'assistant' (response) message.
|
214
|
-
|
215
|
-
Returns:
|
216
|
-
A dictionary representing the content of the last 'assistant' message.
|
217
|
-
"""
|
218
|
-
|
219
|
-
return convert.to_dict(self.last_response.content.iloc[-1])
|
220
|
-
|
221
|
-
@property
|
222
|
-
def action_request(self) -> dataframe.ln_DataFrame:
|
223
|
-
"""
|
224
|
-
Filters and retrieves all messages sent by 'action_request'.
|
225
|
-
|
226
|
-
Returns:
|
227
|
-
A pandas DataFrame containing all 'action_request' messages.
|
228
|
-
"""
|
229
|
-
|
230
|
-
return convert.to_df(self.messages[self.messages.sender == "action_request"])
|
231
|
-
|
232
|
-
@property
|
233
|
-
def action_response(self) -> dataframe.ln_DataFrame:
|
234
|
-
"""
|
235
|
-
Filters and retrieves all messages sent by 'action_response'.
|
236
|
-
|
237
|
-
Returns:
|
238
|
-
A pandas DataFrame containing all 'action_response' messages.
|
239
|
-
"""
|
240
|
-
|
241
|
-
return convert.to_df(self.messages[self.messages.sender == "action_response"])
|
242
|
-
|
243
|
-
@property
|
244
|
-
def responses(self) -> dataframe.ln_DataFrame:
|
245
|
-
"""
|
246
|
-
Retrieves all messages marked with the 'assistant' role.
|
247
|
-
|
248
|
-
Returns:
|
249
|
-
A pandas DataFrame containing all messages with an 'assistant' role.
|
250
|
-
"""
|
251
|
-
|
252
|
-
return convert.to_df(self.messages[self.messages.role == "assistant"])
|
253
|
-
|
254
|
-
@property
|
255
|
-
def assistant_responses(self) -> dataframe.ln_DataFrame:
|
256
|
-
"""
|
257
|
-
Filters 'assistant' role messages excluding 'action_request' and 'action_response'.
|
258
|
-
|
259
|
-
Returns:
|
260
|
-
A pandas DataFrame of 'assistant' messages excluding action requests/responses.
|
261
|
-
"""
|
262
|
-
|
263
|
-
a_responses = self.responses[self.responses.sender != "action_response"]
|
264
|
-
a_responses = a_responses[a_responses.sender != "action_request"]
|
265
|
-
return convert.to_df(a_responses)
|
266
|
-
|
267
|
-
@property
|
268
|
-
def last_assistant_response(self):
|
269
|
-
return self.assistant_responses.iloc[-1]
|
270
|
-
|
271
|
-
@property
|
272
|
-
def info(self) -> dict[str, Any]:
|
273
|
-
"""
|
274
|
-
Summarizes branch information, including message counts by role.
|
275
|
-
|
276
|
-
Returns:
|
277
|
-
A dictionary containing counts of messages categorized by their role.
|
278
|
-
"""
|
279
|
-
|
280
|
-
return self._info()
|
281
|
-
|
282
|
-
@property
|
283
|
-
def sender_info(self) -> dict[str, int]:
|
284
|
-
"""
|
285
|
-
Provides a summary of message counts categorized by sender.
|
286
|
-
|
287
|
-
Returns:
|
288
|
-
A dictionary with senders as keys and counts of their messages as values.
|
289
|
-
"""
|
290
|
-
|
291
|
-
return self._info(use_sender=True)
|
292
|
-
|
293
|
-
@property
|
294
|
-
def describe(self) -> dict[str, Any]:
|
295
|
-
"""
|
296
|
-
Provides a detailed description of the branch, including a summary of messages.
|
297
|
-
|
298
|
-
Returns:
|
299
|
-
A dictionary with a summary of total messages, a breakdown by role, and
|
300
|
-
a preview of the first five messages.
|
301
|
-
"""
|
302
|
-
|
303
|
-
return {
|
304
|
-
"total_messages": len(self.messages),
|
305
|
-
"summary_by_role": self._info(),
|
306
|
-
"messages": [msg.to_dict() for _, msg in self.messages.iterrows()][
|
307
|
-
: len(self.messages) - 1 if len(self.messages) < 5 else 5
|
308
|
-
],
|
309
|
-
}
|
310
|
-
|
311
|
-
@classmethod
|
312
|
-
def _from_csv(cls, filename: str, read_kwargs=None, **kwargs) -> "BaseBranch":
|
313
|
-
read_kwargs = {} if read_kwargs is None else read_kwargs
|
314
|
-
messages = dataframe.read_csv(filename, **read_kwargs)
|
315
|
-
return cls(messages=messages, **kwargs)
|
316
|
-
|
317
|
-
@classmethod
|
318
|
-
def from_csv(cls, **kwargs) -> "BaseBranch":
|
319
|
-
|
320
|
-
return cls._from_csv(**kwargs)
|
321
|
-
|
322
|
-
@classmethod
|
323
|
-
def from_json_string(cls, **kwargs) -> "BaseBranch":
|
324
|
-
|
325
|
-
return cls._from_json(**kwargs)
|
326
|
-
|
327
|
-
@classmethod
|
328
|
-
def _from_json(cls, filename: str, read_kwargs=None, **kwargs) -> "BaseBranch":
|
329
|
-
read_kwargs = {} if read_kwargs is None else read_kwargs
|
330
|
-
messages = dataframe.read_json(filename, **read_kwargs)
|
331
|
-
return cls(messages=messages, **kwargs)
|
332
|
-
|
333
|
-
def to_csv_file(
|
334
|
-
self,
|
335
|
-
filename: PATH_TYPE = "messages.csv",
|
336
|
-
dir_exist_ok: bool = True,
|
337
|
-
timestamp: bool = True,
|
338
|
-
time_prefix: bool = False,
|
339
|
-
verbose: bool = True,
|
340
|
-
clear: bool = True,
|
341
|
-
**kwargs,
|
342
|
-
) -> None:
|
343
|
-
"""
|
344
|
-
Exports the branch messages to a CSV file.
|
345
|
-
|
346
|
-
Args:
|
347
|
-
filepath: Destination path for the CSV file. Defaults to 'messages.csv'.
|
348
|
-
dir_exist_ok: If False, an error is raised if the directory exists. Defaults to True.
|
349
|
-
timestamp: If True, appends a timestamp to the filename. Defaults to True.
|
350
|
-
time_prefix: If True, prefixes the filename with a timestamp. Defaults to False.
|
351
|
-
verbose: If True, prints a message upon successful export. Defaults to True.
|
352
|
-
clear: If True, clears the messages after exporting. Defaults to True.
|
353
|
-
**kwargs: Additional keyword arguments for pandas.DataFrame.to_csv().
|
354
|
-
"""
|
355
|
-
|
356
|
-
if not filename.endswith(".csv"):
|
357
|
-
filename += ".csv"
|
358
|
-
|
359
|
-
filename = SysUtil.create_path(
|
360
|
-
self.datalogger.persist_path,
|
361
|
-
filename,
|
362
|
-
timestamp=timestamp,
|
363
|
-
dir_exist_ok=dir_exist_ok,
|
364
|
-
time_prefix=time_prefix,
|
365
|
-
)
|
366
|
-
|
367
|
-
try:
|
368
|
-
self.messages.to_csv(filename, **kwargs)
|
369
|
-
if verbose:
|
370
|
-
print(f"{len(self.messages)} messages saved to {filename}")
|
371
|
-
if clear:
|
372
|
-
self.clear_messages()
|
373
|
-
except Exception as e:
|
374
|
-
raise ValueError(f"Error in saving to csv: {e}") from e
|
375
|
-
|
376
|
-
def to_json_file(
|
377
|
-
self,
|
378
|
-
filename: PATH_TYPE = "messages.json",
|
379
|
-
dir_exist_ok: bool = True,
|
380
|
-
timestamp: bool = True,
|
381
|
-
time_prefix: bool = False,
|
382
|
-
verbose: bool = True,
|
383
|
-
clear: bool = True,
|
384
|
-
**kwargs,
|
385
|
-
) -> None:
|
386
|
-
"""
|
387
|
-
Exports the branch messages to a JSON file.
|
388
|
-
|
389
|
-
Args:
|
390
|
-
filename: Destination path for the JSON file. Defaults to 'messages.json'.
|
391
|
-
dir_exist_ok: If False, an error is raised if the dirctory exists. Defaults to True.
|
392
|
-
timestamp: If True, appends a timestamp to the filename. Defaults to True.
|
393
|
-
time_prefix: If True, prefixes the filename with a timestamp. Defaults to False.
|
394
|
-
verbose: If True, prints a message upon successful export. Defaults to True.
|
395
|
-
clear: If True, clears the messages after exporting. Defaults to True.
|
396
|
-
**kwargs: Additional keyword arguments for pandas.DataFrame.to_json().
|
397
|
-
"""
|
398
|
-
|
399
|
-
if not filename.endswith(".json"):
|
400
|
-
filename += ".json"
|
401
|
-
|
402
|
-
filename = SysUtil.create_path(
|
403
|
-
self.datalogger.persist_path,
|
404
|
-
filename,
|
405
|
-
timestamp=timestamp,
|
406
|
-
dir_exist_ok=dir_exist_ok,
|
407
|
-
time_prefix=time_prefix,
|
408
|
-
)
|
409
|
-
|
410
|
-
try:
|
411
|
-
self.messages.to_json(
|
412
|
-
filename, orient="records", lines=True, date_format="iso", **kwargs
|
413
|
-
)
|
414
|
-
if verbose:
|
415
|
-
print(f"{len(self.messages)} messages saved to {filename}")
|
416
|
-
if clear:
|
417
|
-
self.clear_messages()
|
418
|
-
except Exception as e:
|
419
|
-
raise ValueError(f"Error in saving to json: {e}") from e
|
420
|
-
|
421
|
-
def log_to_csv(
|
422
|
-
self,
|
423
|
-
filename: PATH_TYPE = "log.csv",
|
424
|
-
dir_exist_ok: bool = True,
|
425
|
-
timestamp: bool = True,
|
426
|
-
time_prefix: bool = False,
|
427
|
-
verbose: bool = True,
|
428
|
-
clear: bool = True,
|
429
|
-
flatten_=True,
|
430
|
-
sep="[^_^]",
|
431
|
-
**kwargs,
|
432
|
-
) -> None:
|
433
|
-
"""
|
434
|
-
Exports the data logger contents to a CSV file.
|
435
|
-
|
436
|
-
Args:
|
437
|
-
filename: Destination path for the CSV file. Defaults to 'log.csv'.
|
438
|
-
dir_exist_ok: If False, an error is raised if the directory exists. Defaults to True.
|
439
|
-
timestamp: If True, appends a timestamp to the filename. Defaults to True.
|
440
|
-
time_prefix: If True, prefixes the filename with a timestamp. Defaults to False.
|
441
|
-
verbose: If True, prints a message upon successful export. Defaults to True.
|
442
|
-
clear: If True, clears the logger after exporting. Defaults to True.
|
443
|
-
**kwargs: Additional keyword arguments for pandas.DataFrame.to_csv().
|
444
|
-
"""
|
445
|
-
self.datalogger.to_csv_file(
|
446
|
-
filename=filename,
|
447
|
-
dir_exist_ok=dir_exist_ok,
|
448
|
-
timestamp=timestamp,
|
449
|
-
time_prefix=time_prefix,
|
450
|
-
verbose=verbose,
|
451
|
-
clear=clear,
|
452
|
-
flatten_=flatten_,
|
453
|
-
sep=sep,
|
454
|
-
**kwargs,
|
455
|
-
)
|
456
|
-
|
457
|
-
def log_to_json(
|
458
|
-
self,
|
459
|
-
filename: PATH_TYPE = "log.json",
|
460
|
-
dir_exist_ok: bool = True,
|
461
|
-
timestamp: bool = True,
|
462
|
-
time_prefix: bool = False,
|
463
|
-
verbose: bool = True,
|
464
|
-
clear: bool = True,
|
465
|
-
flatten_=True,
|
466
|
-
sep="[^_^]",
|
467
|
-
**kwargs,
|
468
|
-
) -> None:
|
469
|
-
"""
|
470
|
-
Exports the data logger contents to a JSON file.
|
471
|
-
|
472
|
-
Args:
|
473
|
-
filename: Destination path for the JSON file. Defaults to 'log.json'.
|
474
|
-
dir_exist_ok: If False, an error is raised if the directory exists. Defaults to True.
|
475
|
-
timestamp: If True, appends a timestamp to the filename. Defaults to True.
|
476
|
-
time_prefix: If True, prefixes the filename with a timestamp. Defaults to False.
|
477
|
-
verbose: If True, prints a message upon successful export. Defaults to True.
|
478
|
-
clear: If True, clears the logger after exporting. Defaults to True.
|
479
|
-
**kwargs: Additional keyword arguments for pandas.DataFrame.to_json().
|
480
|
-
"""
|
481
|
-
|
482
|
-
self.datalogger.to_json_file(
|
483
|
-
filename=filename,
|
484
|
-
dir_exist_ok=dir_exist_ok,
|
485
|
-
timestamp=timestamp,
|
486
|
-
time_prefix=time_prefix,
|
487
|
-
verbose=verbose,
|
488
|
-
clear=clear,
|
489
|
-
flatten_=flatten_,
|
490
|
-
sep=sep,
|
491
|
-
**kwargs,
|
492
|
-
)
|
493
|
-
|
494
|
-
def load_log(self, filename, flattened=True, sep="[^_^]", verbose=True, **kwargs):
|
495
|
-
df = ""
|
496
|
-
try:
|
497
|
-
if filename.endswith(".csv"):
|
498
|
-
df = dataframe.read_csv(filename, **kwargs)
|
499
|
-
|
500
|
-
elif filename.endswith(".json"):
|
501
|
-
df = dataframe.read_json(filename, **kwargs)
|
502
|
-
|
503
|
-
for _, row in df.iterrows():
|
504
|
-
self.datalogger.log.append(
|
505
|
-
DLog.deserialize(
|
506
|
-
input_str=row.input_data,
|
507
|
-
output_str=row.output_data,
|
508
|
-
unflatten_=flattened,
|
509
|
-
sep=sep,
|
510
|
-
)
|
511
|
-
)
|
512
|
-
|
513
|
-
if verbose:
|
514
|
-
print(f"Loaded {len(df)} logs from {filename}")
|
515
|
-
except Exception as e:
|
516
|
-
raise ValueError(f"Error in loading log: {e}") from e
|
517
|
-
|
518
|
-
def remove_message(self, node_id: str) -> None:
|
519
|
-
"""
|
520
|
-
Removes a message from the branch based on its node ID.
|
521
|
-
|
522
|
-
Args:
|
523
|
-
node_id: The unique identifier of the message to be removed.
|
524
|
-
"""
|
525
|
-
MessageUtil.remove_message(self.messages, node_id)
|
526
|
-
|
527
|
-
def update_message(self, node_id: str, column: str, value: Any) -> bool:
|
528
|
-
"""
|
529
|
-
Updates a specific column of a message identified by node_id with a new value.
|
530
|
-
|
531
|
-
Args:
|
532
|
-
value: The new value to update the message with.
|
533
|
-
node_id: The unique identifier of the message to update.
|
534
|
-
column: The column of the message to update.
|
535
|
-
"""
|
536
|
-
|
537
|
-
index = self.messages[self.messages["node_id"] == node_id].index[0]
|
538
|
-
|
539
|
-
return dataframe.update_row(
|
540
|
-
self.messages, row=index, column=column, value=value
|
541
|
-
)
|
542
|
-
|
543
|
-
def change_first_system_message(
|
544
|
-
self, system: str | dict[str, Any] | System, sender: str | None = None
|
545
|
-
) -> None:
|
546
|
-
"""
|
547
|
-
Updates the first system message with new content and/or sender.
|
548
|
-
|
549
|
-
Args:
|
550
|
-
system: The new system message content or a System object.
|
551
|
-
sender: The identifier of the sender for the system message.
|
552
|
-
"""
|
553
|
-
|
554
|
-
if len(self.messages[self.messages["role"] == "system"]) == 0:
|
555
|
-
raise ValueError("There is no system message in the messages.")
|
556
|
-
|
557
|
-
if not isinstance(system, (str, dict, System)):
|
558
|
-
raise ValueError("Input cannot be converted into a system message.")
|
559
|
-
|
560
|
-
if isinstance(system, (str, dict)):
|
561
|
-
system = System(system, sender=sender)
|
562
|
-
|
563
|
-
if isinstance(system, System):
|
564
|
-
system.timestamp = SysUtil.get_timestamp()
|
565
|
-
sys_index = self.messages[self.messages.role == "system"].index
|
566
|
-
self.messages.loc[sys_index[0]] = system.to_pd_series()
|
567
|
-
|
568
|
-
def rollback(self, steps: int) -> None:
|
569
|
-
"""
|
570
|
-
Removes the last 'n' messages from the branch.
|
571
|
-
|
572
|
-
Args:
|
573
|
-
steps: The number of messages to remove from the end.
|
574
|
-
"""
|
575
|
-
|
576
|
-
self.messages = dataframe.remove_last_n_rows(self.messages, steps)
|
577
|
-
|
578
|
-
def clear_messages(self) -> None:
|
579
|
-
"""
|
580
|
-
Clears all messages from the branch.
|
581
|
-
"""
|
582
|
-
self.messages = dataframe.ln_DataFrame(columns=self._columns)
|
583
|
-
|
584
|
-
def replace_keyword(
|
585
|
-
self,
|
586
|
-
keyword: str,
|
587
|
-
replacement: str,
|
588
|
-
column: str = "content",
|
589
|
-
case_sensitive: bool = False,
|
590
|
-
) -> None:
|
591
|
-
|
592
|
-
dataframe.replace_keyword(
|
593
|
-
self.messages,
|
594
|
-
keyword,
|
595
|
-
replacement,
|
596
|
-
column=column,
|
597
|
-
case_sensitive=case_sensitive,
|
598
|
-
)
|
599
|
-
|
600
|
-
def search_keywords(
|
601
|
-
self,
|
602
|
-
keywords: str | list[str],
|
603
|
-
case_sensitive: bool = False,
|
604
|
-
reset_index: bool = False,
|
605
|
-
dropna: bool = False,
|
606
|
-
) -> dataframe.ln_DataFrame:
|
607
|
-
return dataframe.search_keywords(
|
608
|
-
self.messages,
|
609
|
-
keywords,
|
610
|
-
case_sensitive=case_sensitive,
|
611
|
-
reset_index=reset_index,
|
612
|
-
dropna=dropna,
|
613
|
-
)
|
614
|
-
|
615
|
-
def extend(self, messages: dataframe.ln_DataFrame, **kwargs) -> None:
|
616
|
-
|
617
|
-
self.messages = MessageUtil.extend(self.messages, messages, **kwargs)
|
618
|
-
|
619
|
-
def filter_by(
|
620
|
-
self,
|
621
|
-
role: str | None = None,
|
622
|
-
sender: str | None = None,
|
623
|
-
start_time=None,
|
624
|
-
end_time=None,
|
625
|
-
content_keywords: str | list[str] | None = None,
|
626
|
-
case_sensitive: bool = False,
|
627
|
-
) -> dataframe.ln_DataFrame:
|
628
|
-
|
629
|
-
return MessageUtil.filter_messages_by(
|
630
|
-
self.messages,
|
631
|
-
role=role,
|
632
|
-
sender=sender,
|
633
|
-
start_time=start_time,
|
634
|
-
end_time=end_time,
|
635
|
-
content_keywords=content_keywords,
|
636
|
-
case_sensitive=case_sensitive,
|
637
|
-
)
|
638
|
-
|
639
|
-
# noinspection PyTestUnpassedFixture
|
640
|
-
def _info(self, use_sender: bool = False) -> dict[str, int]:
|
641
|
-
"""
|
642
|
-
Helper method to generate summaries of messages either by role or sender.
|
643
|
-
|
644
|
-
Args:
|
645
|
-
use_sender: If True, summary is categorized by sender. Otherwise, by role.
|
646
|
-
|
647
|
-
Returns:
|
648
|
-
A dictionary summarizing the count of messages either by role or sender.
|
649
|
-
"""
|
650
|
-
|
651
|
-
messages = self.messages["sender"] if use_sender else self.messages["role"]
|
652
|
-
result = messages.value_counts().to_dict()
|
653
|
-
result["total"] = len(self.messages)
|
654
|
-
return result
|