lionagi 0.0.208__py3-none-any.whl → 0.0.210__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- lionagi/__init__.py +4 -6
- lionagi/api_service/base_endpoint.py +65 -0
- lionagi/api_service/base_rate_limiter.py +121 -0
- lionagi/api_service/base_service.py +146 -0
- lionagi/api_service/chat_completion.py +6 -0
- lionagi/api_service/embeddings.py +6 -0
- lionagi/api_service/payload_package.py +47 -0
- lionagi/api_service/status_tracker.py +29 -0
- lionagi/core/__init__.py +5 -9
- lionagi/core/branch.py +1191 -0
- lionagi/core/flow.py +423 -0
- lionagi/core/{instruction_set/instruction_set.py → instruction_set.py} +3 -3
- lionagi/core/session.py +872 -0
- lionagi/schema/__init__.py +5 -8
- lionagi/schema/base_schema.py +821 -0
- lionagi/{_services → services}/base_service.py +4 -4
- lionagi/{_services → services}/oai.py +4 -4
- lionagi/structures/graph.py +1 -1
- lionagi/structures/relationship.py +1 -1
- lionagi/structures/structure.py +1 -1
- lionagi/tools/tool_manager.py +0 -163
- lionagi/tools/tool_util.py +2 -1
- lionagi/utils/__init__.py +7 -14
- lionagi/utils/api_util.py +63 -2
- lionagi/utils/core_utils.py +338 -0
- lionagi/utils/sys_util.py +3 -3
- lionagi/version.py +1 -1
- {lionagi-0.0.208.dist-info → lionagi-0.0.210.dist-info}/METADATA +28 -29
- lionagi-0.0.210.dist-info/RECORD +56 -0
- lionagi/_services/anthropic.py +0 -79
- lionagi/_services/anyscale.py +0 -0
- lionagi/_services/azure.py +0 -1
- lionagi/_services/bedrock.py +0 -0
- lionagi/_services/everlyai.py +0 -0
- lionagi/_services/gemini.py +0 -0
- lionagi/_services/gpt4all.py +0 -0
- lionagi/_services/huggingface.py +0 -0
- lionagi/_services/litellm.py +0 -33
- lionagi/_services/localai.py +0 -0
- lionagi/_services/openllm.py +0 -0
- lionagi/_services/openrouter.py +0 -44
- lionagi/_services/perplexity.py +0 -0
- lionagi/_services/predibase.py +0 -0
- lionagi/_services/rungpt.py +0 -0
- lionagi/_services/vllm.py +0 -0
- lionagi/_services/xinference.py +0 -0
- lionagi/agents/planner.py +0 -1
- lionagi/agents/prompter.py +0 -1
- lionagi/agents/scorer.py +0 -1
- lionagi/agents/summarizer.py +0 -1
- lionagi/agents/validator.py +0 -1
- lionagi/bridge/__init__.py +0 -22
- lionagi/bridge/langchain.py +0 -195
- lionagi/bridge/llama_index.py +0 -266
- lionagi/core/branch/__init__.py +0 -0
- lionagi/core/branch/branch.py +0 -841
- lionagi/core/branch/cluster.py +0 -1
- lionagi/core/branch/conversation.py +0 -787
- lionagi/core/core_util.py +0 -0
- lionagi/core/flow/__init__.py +0 -0
- lionagi/core/flow/flow.py +0 -19
- lionagi/core/flow/flow_util.py +0 -62
- lionagi/core/instruction_set/__init__.py +0 -0
- lionagi/core/messages/__init__.py +0 -0
- lionagi/core/sessions/__init__.py +0 -0
- lionagi/core/sessions/session.py +0 -504
- lionagi/datastores/__init__.py +0 -1
- lionagi/datastores/chroma.py +0 -1
- lionagi/datastores/deeplake.py +0 -1
- lionagi/datastores/elasticsearch.py +0 -1
- lionagi/datastores/lantern.py +0 -1
- lionagi/datastores/pinecone.py +0 -1
- lionagi/datastores/postgres.py +0 -1
- lionagi/datastores/qdrant.py +0 -1
- lionagi/loaders/__init__.py +0 -18
- lionagi/loaders/chunker.py +0 -166
- lionagi/loaders/load_util.py +0 -240
- lionagi/loaders/reader.py +0 -122
- lionagi/models/__init__.py +0 -0
- lionagi/models/base_model.py +0 -0
- lionagi/models/imodel.py +0 -53
- lionagi/schema/async_queue.py +0 -158
- lionagi/schema/base_condition.py +0 -1
- lionagi/schema/base_node.py +0 -422
- lionagi/schema/base_tool.py +0 -44
- lionagi/schema/data_logger.py +0 -126
- lionagi/schema/data_node.py +0 -88
- lionagi/schema/status_tracker.py +0 -37
- lionagi/tests/test_utils/test_encrypt_util.py +0 -323
- lionagi/utils/encrypt_util.py +0 -283
- lionagi/utils/url_util.py +0 -55
- lionagi-0.0.208.dist-info/RECORD +0 -106
- lionagi/{agents → api_service}/__init__.py +0 -0
- lionagi/core/{branch/branch_manager.py → branch_manager.py} +0 -0
- lionagi/core/{messages/messages.py → messages.py} +3 -3
- /lionagi/{_services → services}/__init__.py +0 -0
- /lionagi/{_services → services}/mistralai.py +0 -0
- /lionagi/{_services → services}/mlx_service.py +0 -0
- /lionagi/{_services → services}/ollama.py +0 -0
- /lionagi/{_services → services}/services.py +0 -0
- /lionagi/{_services → services}/transformers.py +0 -0
- {lionagi-0.0.208.dist-info → lionagi-0.0.210.dist-info}/LICENSE +0 -0
- {lionagi-0.0.208.dist-info → lionagi-0.0.210.dist-info}/WHEEL +0 -0
- {lionagi-0.0.208.dist-info → lionagi-0.0.210.dist-info}/top_level.txt +0 -0
lionagi/core/branch.py
ADDED
@@ -0,0 +1,1191 @@
|
|
1
|
+
import json
|
2
|
+
from collections import deque
|
3
|
+
from datetime import datetime
|
4
|
+
from typing import Any, Dict, List, Optional, Union
|
5
|
+
|
6
|
+
import pandas as pd
|
7
|
+
|
8
|
+
from lionagi.utils.sys_util import create_path, is_same_dtype
|
9
|
+
from lionagi.utils import as_dict, lcall,to_df, to_list, CoreUtil
|
10
|
+
|
11
|
+
|
12
|
+
from lionagi.services.base_service import BaseService, StatusTracker
|
13
|
+
from lionagi.services.oai import OpenAIService
|
14
|
+
from lionagi.services.openrouter import OpenRouterService
|
15
|
+
from lionagi.configs.oai_configs import oai_schema
|
16
|
+
from lionagi.configs.openrouter_configs import openrouter_schema
|
17
|
+
from lionagi.schema import DataLogger, Tool
|
18
|
+
from lionagi.tools.tool_manager import ToolManager
|
19
|
+
from lionagi.core.branch_manager import Request
|
20
|
+
from lionagi.core.instruction_set import InstructionSet
|
21
|
+
from lionagi.core.messages import Instruction, Message, Response, System
|
22
|
+
from lionagi.core.flow import ChatFlow
|
23
|
+
|
24
|
+
OAIService = None
|
25
|
+
try:
|
26
|
+
OAIService = OpenAIService()
|
27
|
+
except:
|
28
|
+
pass
|
29
|
+
|
30
|
+
class Branch:
|
31
|
+
"""
|
32
|
+
Represents a branch in a conversation with messages, instruction sets, and tool management.
|
33
|
+
|
34
|
+
A `Branch` is a subset of a conversation that contains messages, instruction sets, and tools for managing interactions
|
35
|
+
within the conversation. It encapsulates the state and behavior of a specific branch of conversation flow.
|
36
|
+
|
37
|
+
Attributes:
|
38
|
+
_cols (List[str]): A list of column names for the DataFrame containing messages.
|
39
|
+
messages (pd.DataFrame): A DataFrame containing messages for the branch.
|
40
|
+
instruction_sets (Dict[str, InstructionSet]): A dictionary of instruction sets associated with the branch.
|
41
|
+
tool_manager (ToolManager): The tool manager for managing tools within the branch.
|
42
|
+
service (Optional[BaseService]): The service associated with the branch.
|
43
|
+
llmconfig (Optional[Dict]): Configuration for the LLM (Large Language Model) service.
|
44
|
+
name (Optional[str]): The name of the branch.
|
45
|
+
pending_ins (Dict): Dictionary to store pending inputs for the branch.
|
46
|
+
pending_outs (Deque): Queue to store pending outputs for the branch.
|
47
|
+
logger (Optional[DataLogger]): Logger for data logging.
|
48
|
+
status_tracker (StatusTracker): Tracks the status of the branch.
|
49
|
+
"""
|
50
|
+
_cols = ["node_id", "role", "sender", "timestamp", "content"]
|
51
|
+
|
52
|
+
def __init__(self, name: Optional[str] = None, messages: Optional[pd.DataFrame] = None,
|
53
|
+
instruction_sets: Optional[Dict[str, InstructionSet]] = None,
|
54
|
+
tool_manager: Optional[ToolManager] = None, service: Optional[BaseService] = None, llmconfig: Optional[Dict] = None, tools=None, dir=None, logger=None):
|
55
|
+
"""
|
56
|
+
Initializes a new instance of the Branch class.
|
57
|
+
|
58
|
+
Args:
|
59
|
+
name (Optional[str]): The name of the branch.
|
60
|
+
messages (Optional[pd.DataFrame]): A DataFrame containing messages for the branch.
|
61
|
+
instruction_sets (Optional[Dict[str, InstructionSet]]): A dictionary of instruction sets.
|
62
|
+
tool_manager (Optional[ToolManager]): The tool manager for the branch.
|
63
|
+
service (Optional[BaseService]): The service associated with the branch.
|
64
|
+
llmconfig (Optional[Dict]): Configuration for the LLM service.
|
65
|
+
tools (Optional[List[Tool]]): Initial list of tools to register with the tool manager.
|
66
|
+
dir (Optional[str]): Directory path for data logging.
|
67
|
+
|
68
|
+
Examples:
|
69
|
+
>>> branch = Branch(name="CustomerService")
|
70
|
+
>>> branch_with_messages = Branch(name="Support", messages=pd.DataFrame(columns=["node_id", "content"]))
|
71
|
+
"""
|
72
|
+
|
73
|
+
self.messages = pd.DataFrame(columns=Branch._cols)
|
74
|
+
self.messages = (
|
75
|
+
messages
|
76
|
+
if messages is not None
|
77
|
+
else pd.DataFrame(
|
78
|
+
columns=["node_id", "role", "sender", "timestamp", "content"]
|
79
|
+
)
|
80
|
+
)
|
81
|
+
self.tool_manager = tool_manager if tool_manager else ToolManager()
|
82
|
+
try:
|
83
|
+
self.register_tools(tools)
|
84
|
+
except Exception as e:
|
85
|
+
raise TypeError(f"Error in registering tools: {e}")
|
86
|
+
|
87
|
+
self.instruction_sets = instruction_sets if instruction_sets else {}
|
88
|
+
self.status_tracker = StatusTracker()
|
89
|
+
self._add_service(service, llmconfig)
|
90
|
+
self.name = name
|
91
|
+
self.pending_ins = {}
|
92
|
+
self.pending_outs = deque()
|
93
|
+
self.logger = logger or DataLogger(dir=dir)
|
94
|
+
|
95
|
+
|
96
|
+
# ---- properties ---- #
|
97
|
+
@property
|
98
|
+
def chat_messages(self):
|
99
|
+
"""
|
100
|
+
Generates chat completion messages without sender information.
|
101
|
+
|
102
|
+
Returns:
|
103
|
+
List[Dict[str, Any]]: A list of dictionaries representing chat messages.
|
104
|
+
"""
|
105
|
+
return self._to_chatcompletion_message()
|
106
|
+
|
107
|
+
@property
|
108
|
+
def chat_messages_with_sender(self):
|
109
|
+
"""
|
110
|
+
Generates chat completion messages including sender information.
|
111
|
+
|
112
|
+
Returns:
|
113
|
+
List[Dict[str, Any]]: A list of dictionaries representing chat messages with sender details.
|
114
|
+
"""
|
115
|
+
return self._to_chatcompletion_message(with_sender=True)
|
116
|
+
|
117
|
+
@property
|
118
|
+
def messages_describe(self) -> Dict[str, Any]:
|
119
|
+
"""
|
120
|
+
Provides a descriptive summary of all messages in the branch.
|
121
|
+
|
122
|
+
Returns:
|
123
|
+
Dict[str, Any]: A dictionary containing summaries of messages by role and sender,
|
124
|
+
total message count, instruction sets, registered tools, and message details.
|
125
|
+
"""
|
126
|
+
return {
|
127
|
+
"total_messages": len(self.messages),
|
128
|
+
"summary_by_role": self._info(),
|
129
|
+
"summary_by_sender": self._info(use_sender=True),
|
130
|
+
"instruction_sets": self.instruction_sets,
|
131
|
+
"registered_tools": self.tool_manager.registry,
|
132
|
+
"messages": [
|
133
|
+
msg.to_dict() for _, msg in self.messages.iterrows()
|
134
|
+
],
|
135
|
+
}
|
136
|
+
|
137
|
+
@property
|
138
|
+
def has_tools(self) -> bool:
|
139
|
+
"""
|
140
|
+
Checks if there are any tools registered in the tool manager.
|
141
|
+
|
142
|
+
Returns:
|
143
|
+
bool: True if there are tools registered, False otherwise.
|
144
|
+
"""
|
145
|
+
return self.tool_manager.registry != {}
|
146
|
+
|
147
|
+
@property
|
148
|
+
def last_message(self) -> pd.Series:
|
149
|
+
"""
|
150
|
+
Retrieves the last message from the conversation.
|
151
|
+
|
152
|
+
Returns:
|
153
|
+
pd.Series: The last message as a pandas Series.
|
154
|
+
"""
|
155
|
+
return CoreUtil.get_rows(self.messages, n=1, from_='last')
|
156
|
+
|
157
|
+
@property
|
158
|
+
def first_system(self) -> pd.Series:
|
159
|
+
"""
|
160
|
+
Retrieves the first system message from the conversation.
|
161
|
+
|
162
|
+
Returns:
|
163
|
+
pd.Series: The first system message as a pandas Series.
|
164
|
+
"""
|
165
|
+
return CoreUtil.get_rows(self.messages, role='system', n=1, from_='front')
|
166
|
+
|
167
|
+
@property
|
168
|
+
def last_response(self) -> pd.Series:
|
169
|
+
"""
|
170
|
+
Retrieves the last response message from the conversation.
|
171
|
+
|
172
|
+
Returns:
|
173
|
+
pd.Series: The last response message as a pandas Series.
|
174
|
+
"""
|
175
|
+
return CoreUtil.get_rows(self.messages, role='assistant', n=1, from_='last')
|
176
|
+
|
177
|
+
@property
|
178
|
+
def last_response_content(self) -> Dict:
|
179
|
+
"""
|
180
|
+
Retrieves the content of the last response message from the conversation.
|
181
|
+
|
182
|
+
Returns:
|
183
|
+
Dict: The content of the last response message as a dictionary
|
184
|
+
"""
|
185
|
+
return as_dict(self.last_response.content.iloc[-1])
|
186
|
+
|
187
|
+
@property
|
188
|
+
def action_request(self) -> pd.DataFrame:
|
189
|
+
"""
|
190
|
+
Retrieves all action request messages from the conversation.
|
191
|
+
|
192
|
+
Returns:
|
193
|
+
pd.DataFrame: A DataFrame containing all action request messages.
|
194
|
+
"""
|
195
|
+
return to_df(self.messages[self.messages.sender == 'action_request'])
|
196
|
+
|
197
|
+
@property
|
198
|
+
def action_response(self) -> pd.DataFrame:
|
199
|
+
"""
|
200
|
+
Retrieves all action response messages from the conversation.
|
201
|
+
|
202
|
+
Returns:
|
203
|
+
pd.DataFrame: A DataFrame containing all action response messages.
|
204
|
+
"""
|
205
|
+
return to_df(self.messages[self.messages.sender == 'action_response'])
|
206
|
+
|
207
|
+
@property
|
208
|
+
def responses(self) -> pd.DataFrame:
|
209
|
+
"""
|
210
|
+
Retrieves all response messages from the conversation.
|
211
|
+
|
212
|
+
Returns:
|
213
|
+
pd.DataFrame: A DataFrame containing all response messages.
|
214
|
+
"""
|
215
|
+
return to_df(self.messages[self.messages.role == 'assistant'])
|
216
|
+
|
217
|
+
@property
|
218
|
+
def assistant_responses(self) -> pd.DataFrame:
|
219
|
+
"""
|
220
|
+
Retrieves all assistant responses from the conversation, excluding action requests and responses.
|
221
|
+
|
222
|
+
Returns:
|
223
|
+
pd.DataFrame: A DataFrame containing assistant responses excluding action requests and responses.
|
224
|
+
"""
|
225
|
+
a_responses = self.responses[self.responses.sender != 'action_response']
|
226
|
+
a_responses = a_responses[a_responses.sender != 'action_request']
|
227
|
+
return to_df(a_responses)
|
228
|
+
|
229
|
+
@property
|
230
|
+
def info(self) -> Dict[str, int]:
|
231
|
+
"""
|
232
|
+
Get a summary of the conversation messages categorized by role.
|
233
|
+
|
234
|
+
Returns:
|
235
|
+
Dict[str, int]: A dictionary with keys as message roles and values as counts.
|
236
|
+
"""
|
237
|
+
|
238
|
+
return self._info()
|
239
|
+
|
240
|
+
@property
|
241
|
+
def sender_info(self) -> Dict[str, int]:
|
242
|
+
"""
|
243
|
+
Provides a descriptive summary of the conversation, including total message count and summary by sender.
|
244
|
+
|
245
|
+
Returns:
|
246
|
+
Dict[str, Any]: A dictionary containing the total number of messages and a summary categorized by sender.
|
247
|
+
"""
|
248
|
+
return self._info(use_sender=True)
|
249
|
+
|
250
|
+
@property
|
251
|
+
def describe(self) -> Dict[str, Any]:
|
252
|
+
"""
|
253
|
+
Provides a descriptive summary of the conversation, including the total number of messages,
|
254
|
+
a summary by role, and the first five messages.
|
255
|
+
|
256
|
+
Returns:
|
257
|
+
Dict[str, Any]: A dictionary containing the total number of messages, summary by role, and a list of the first maximum five message dictionaries.
|
258
|
+
"""
|
259
|
+
return {
|
260
|
+
"total_messages": len(self.messages),
|
261
|
+
"summary_by_role": self._info(),
|
262
|
+
"messages": [
|
263
|
+
msg.to_dict() for _, msg in self.messages.iterrows()
|
264
|
+
][: self.len_messages -1 if self.len_messages < 5 else 5],
|
265
|
+
}
|
266
|
+
|
267
|
+
# ---- I/O ---- #
|
268
|
+
@classmethod
|
269
|
+
def from_csv(cls, filepath: str, name: Optional[str] = None,
|
270
|
+
instruction_sets: Optional[Dict[str, InstructionSet]] = None,
|
271
|
+
tool_manager: Optional[ToolManager] = None, service: Optional[BaseService] = None,
|
272
|
+
llmconfig: Optional[Dict] = None, tools=None, **kwargs) -> 'Branch':
|
273
|
+
"""
|
274
|
+
Creates a Branch instance from a CSV file containing messages.
|
275
|
+
|
276
|
+
Args:
|
277
|
+
filepath (str): Path to the CSV file.
|
278
|
+
name (Optional[str]): Name of the branch, default is None.
|
279
|
+
instruction_sets (Optional[Dict[str, InstructionSet]]): Instruction sets, default is None.
|
280
|
+
tool_manager (Optional[ToolManager]): Tool manager for the branch, default is None.
|
281
|
+
service (Optional[BaseService]): External service for the branch, default is None.
|
282
|
+
llmconfig (Optional[Dict]): Configuration for language learning models, default is None.
|
283
|
+
tools (Optional[List[Tool]]): Initial list of tools to register, default is None.
|
284
|
+
**kwargs: Additional keyword arguments for pd.read_csv().
|
285
|
+
|
286
|
+
Returns:
|
287
|
+
Branch: A new Branch instance created from the CSV data.
|
288
|
+
|
289
|
+
Examples:
|
290
|
+
>>> branch = Branch.from_csv("path/to/messages.csv", name="ImportedBranch")
|
291
|
+
"""
|
292
|
+
df = pd.read_csv(filepath, **kwargs)
|
293
|
+
self = cls(
|
294
|
+
name=name,
|
295
|
+
messages=df,
|
296
|
+
instruction_sets=instruction_sets,
|
297
|
+
tool_manager=tool_manager,
|
298
|
+
service=service,
|
299
|
+
llmconfig=llmconfig,
|
300
|
+
tools=tools
|
301
|
+
)
|
302
|
+
|
303
|
+
return self
|
304
|
+
|
305
|
+
@classmethod
|
306
|
+
def from_json(cls, filepath: str, name: Optional[str] = None,
|
307
|
+
instruction_sets: Optional[Dict[str, InstructionSet]] = None,
|
308
|
+
tool_manager: Optional[ToolManager] = None, service: Optional[BaseService] = None,
|
309
|
+
llmconfig: Optional[Dict] = None, **kwargs) -> 'Branch':
|
310
|
+
"""
|
311
|
+
Creates a Branch instance from a JSON file containing messages.
|
312
|
+
|
313
|
+
Args:
|
314
|
+
filepath (str): Path to the JSON file.
|
315
|
+
name (Optional[str]): Name of the branch, default is None.
|
316
|
+
instruction_sets (Optional[Dict[str, InstructionSet]]): Instruction sets, default is None.
|
317
|
+
tool_manager (Optional[ToolManager]): Tool manager for the branch, default is None.
|
318
|
+
service (Optional[BaseService]): External service for the branch, default is None.
|
319
|
+
llmconfig (Optional[Dict]): Configuration for language learning models, default is None.
|
320
|
+
**kwargs: Additional keyword arguments for pd.read_json().
|
321
|
+
|
322
|
+
Returns:
|
323
|
+
Branch: A new Branch instance created from the JSON data.
|
324
|
+
|
325
|
+
Examples:
|
326
|
+
>>> branch = Branch.from_json("path/to/messages.json", name="JSONBranch")
|
327
|
+
"""
|
328
|
+
df = pd.read_json(filepath, **kwargs)
|
329
|
+
self = cls(
|
330
|
+
name=name,
|
331
|
+
messages=df,
|
332
|
+
instruction_sets=instruction_sets,
|
333
|
+
tool_manager=tool_manager,
|
334
|
+
service=service,
|
335
|
+
llmconfig=llmconfig
|
336
|
+
)
|
337
|
+
return self
|
338
|
+
|
339
|
+
|
340
|
+
def to_csv(self, filename: str = 'messages.csv', file_exist_ok: bool = False,
|
341
|
+
timestamp: bool = True, time_prefix: bool = False,
|
342
|
+
verbose: bool = True, clear: bool = True, **kwargs):
|
343
|
+
"""
|
344
|
+
Saves the branch's messages to a CSV file.
|
345
|
+
|
346
|
+
Args:
|
347
|
+
filename (str): The name of the output CSV file, default is 'messages.csv'.
|
348
|
+
file_exist_ok (bool): If True, does not raise an error if the directory already exists, default is False.
|
349
|
+
timestamp (bool): If True, appends a timestamp to the filename, default is True.
|
350
|
+
time_prefix (bool): If True, adds a timestamp prefix to the filename, default is False.
|
351
|
+
verbose (bool): If True, prints a message upon successful save, default is True.
|
352
|
+
clear (bool): If True, clears the messages after saving, default is True.
|
353
|
+
**kwargs: Additional keyword arguments for DataFrame.to_csv().
|
354
|
+
|
355
|
+
Examples:
|
356
|
+
>>> branch.to_csv("exported_messages.csv")
|
357
|
+
>>> branch.to_csv("timed_export.csv", timestamp=True, time_prefix=True)
|
358
|
+
"""
|
359
|
+
|
360
|
+
if not filename.endswith('.csv'):
|
361
|
+
filename += '.csv'
|
362
|
+
|
363
|
+
filepath = create_path(
|
364
|
+
self.logger.dir, filename, timestamp=timestamp,
|
365
|
+
dir_exist_ok=file_exist_ok, time_prefix=time_prefix
|
366
|
+
)
|
367
|
+
|
368
|
+
try:
|
369
|
+
self.messages.to_csv(filepath, **kwargs)
|
370
|
+
if verbose:
|
371
|
+
print(f"{len(self.messages)} messages saved to {filepath}")
|
372
|
+
if clear:
|
373
|
+
self.clear_messages()
|
374
|
+
except Exception as e:
|
375
|
+
raise ValueError(f"Error in saving to csv: {e}")
|
376
|
+
|
377
|
+
def to_json(self, filename: str = 'messages.json', file_exist_ok: bool = False,
|
378
|
+
timestamp: bool = True, time_prefix: bool = False,
|
379
|
+
verbose: bool = True, clear: bool = True, **kwargs):
|
380
|
+
"""
|
381
|
+
Saves the branch's messages to a JSON file.
|
382
|
+
|
383
|
+
Args:
|
384
|
+
filename (str): The name of the output JSON file, default is 'messages.json'.
|
385
|
+
file_exist_ok (bool): If True, does not raise an error if the directory already exists, default is False.
|
386
|
+
timestamp (bool): If True, appends a timestamp to the filename, default is True.
|
387
|
+
time_prefix (bool): If True, adds a timestamp prefix to the filename, default is False.
|
388
|
+
verbose (bool): If True, prints a message upon successful save, default is True.
|
389
|
+
clear (bool): If True, clears the messages after saving, default is True.
|
390
|
+
**kwargs: Additional keyword arguments for DataFrame.to_json().
|
391
|
+
|
392
|
+
Examples:
|
393
|
+
>>> branch.to_json("exported_messages.json")
|
394
|
+
>>> branch.to_json("timed_export.json", timestamp=True, time_prefix=True)
|
395
|
+
"""
|
396
|
+
|
397
|
+
if not filename.endswith('.json'):
|
398
|
+
filename += '.json'
|
399
|
+
|
400
|
+
filepath = create_path(
|
401
|
+
self.dir, filename, timestamp=timestamp,
|
402
|
+
dir_exist_ok=file_exist_ok, time_prefix=time_prefix
|
403
|
+
)
|
404
|
+
|
405
|
+
try:
|
406
|
+
self.messages.to_json(
|
407
|
+
filepath, orient="records", lines=True,
|
408
|
+
date_format="iso", **kwargs
|
409
|
+
)
|
410
|
+
if clear:
|
411
|
+
self.clear_messages()
|
412
|
+
if verbose:
|
413
|
+
print(f"{len(self.messages)} messages saved to {filepath}")
|
414
|
+
except Exception as e:
|
415
|
+
raise ValueError(f"Error in saving to json: {e}")
|
416
|
+
|
417
|
+
def log_to_csv(self, filename: str = 'log.csv', file_exist_ok: bool = False, timestamp: bool = True,
|
418
|
+
time_prefix: bool = False, verbose: bool = True, clear: bool = True, **kwargs):
|
419
|
+
"""
|
420
|
+
Saves the branch's log data to a CSV file.
|
421
|
+
|
422
|
+
This method is designed to export log data, potentially including operations and interactions,
|
423
|
+
to a CSV file for analysis or record-keeping.
|
424
|
+
|
425
|
+
Args:
|
426
|
+
filename (str): The name of the output CSV file. Defaults to 'log.csv'.
|
427
|
+
file_exist_ok (bool): If True, will not raise an error if the directory already exists. Defaults to False.
|
428
|
+
timestamp (bool): If True, appends a timestamp to the filename for uniqueness. Defaults to True.
|
429
|
+
time_prefix (bool): If True, adds a timestamp prefix to the filename. Defaults to False.
|
430
|
+
verbose (bool): If True, prints a success message upon completion. Defaults to True.
|
431
|
+
clear (bool): If True, clears the log after saving. Defaults to True.
|
432
|
+
**kwargs: Additional keyword arguments for `DataFrame.to_csv()`.
|
433
|
+
|
434
|
+
Examples:
|
435
|
+
>>> branch.log_to_csv("branch_log.csv")
|
436
|
+
>>> branch.log_to_csv("detailed_branch_log.csv", timestamp=True, verbose=True)
|
437
|
+
"""
|
438
|
+
self.logger.to_csv(
|
439
|
+
filename=filename, file_exist_ok=file_exist_ok, timestamp=timestamp,
|
440
|
+
time_prefix=time_prefix, verbose=verbose, clear=clear, **kwargs
|
441
|
+
)
|
442
|
+
|
443
|
+
def log_to_json(self, filename: str = 'log.json', file_exist_ok: bool = False, timestamp: bool = True,
|
444
|
+
time_prefix: bool = False, verbose: bool = True, clear: bool = True, **kwargs):
|
445
|
+
"""
|
446
|
+
Saves the branch's log data to a JSON file.
|
447
|
+
|
448
|
+
Useful for exporting log data in JSON format, allowing for easy integration with web applications
|
449
|
+
and services that consume JSON.
|
450
|
+
|
451
|
+
Args:
|
452
|
+
filename (str): The name of the output JSON file. Defaults to 'log.json'.
|
453
|
+
file_exist_ok (bool): If directory existence should not raise an error. Defaults to False.
|
454
|
+
timestamp (bool): If True, appends a timestamp to the filename. Defaults to True.
|
455
|
+
time_prefix (bool): If True, adds a timestamp prefix to the filename. Defaults to False.
|
456
|
+
verbose (bool): If True, prints a success message upon completion. Defaults to True.
|
457
|
+
clear (bool): If True, clears the log after saving. Defaults to True.
|
458
|
+
**kwargs: Additional keyword arguments for `DataFrame.to_json()`.
|
459
|
+
|
460
|
+
Examples:
|
461
|
+
>>> branch.log_to_json("branch_log.json")
|
462
|
+
>>> branch.log_to_json("detailed_branch_log.json", verbose=True, timestamp=True)
|
463
|
+
"""
|
464
|
+
self.logger.to_json(
|
465
|
+
filename=filename, file_exist_ok=file_exist_ok, timestamp=timestamp,
|
466
|
+
time_prefix=time_prefix, verbose=verbose, clear=clear, **kwargs
|
467
|
+
)
|
468
|
+
|
469
|
+
# ----- chatflow ----#
|
470
|
+
async def call_chatcompletion(self, sender=None, with_sender=False, tokenizer_kwargs={}, **kwargs):
|
471
|
+
"""
|
472
|
+
Asynchronously calls the chat completion service with the current message queue.
|
473
|
+
|
474
|
+
This method prepares the messages for chat completion, sends the request to the configured service, and handles the response. The method supports additional keyword arguments that are passed directly to the service.
|
475
|
+
|
476
|
+
Args:
|
477
|
+
sender (Optional[str]): The name of the sender to be included in the chat completion request. Defaults to None.
|
478
|
+
with_sender (bool): If True, includes the sender's name in the messages. Defaults to False.
|
479
|
+
**kwargs: Arbitrary keyword arguments passed directly to the chat completion service.
|
480
|
+
|
481
|
+
Examples:
|
482
|
+
>>> await branch.call_chatcompletion()
|
483
|
+
"""
|
484
|
+
await ChatFlow.call_chatcompletion(
|
485
|
+
self, sender=sender, with_sender=with_sender,
|
486
|
+
tokenizer_kwargs=tokenizer_kwargs, **kwargs
|
487
|
+
)
|
488
|
+
|
489
|
+
async def chat(
|
490
|
+
self,
|
491
|
+
instruction: Union[Instruction, str],
|
492
|
+
context: Optional[Any] = None,
|
493
|
+
sender: Optional[str] = None,
|
494
|
+
system: Optional[Union[System, str, Dict[str, Any]]] = None,
|
495
|
+
tools: Union[bool, Tool, List[Tool], str, List[str]] = False,
|
496
|
+
out: bool = True,
|
497
|
+
invoke: bool = True,
|
498
|
+
**kwargs) -> Any:
|
499
|
+
"""
|
500
|
+
Initiates a chat conversation, processing instructions and system messages, optionally invoking tools.
|
501
|
+
|
502
|
+
Args:
|
503
|
+
branch: The Branch instance to perform chat operations.
|
504
|
+
instruction (Union[Instruction, str]): The instruction for the chat.
|
505
|
+
context (Optional[Any]): Additional context for the chat.
|
506
|
+
sender (Optional[str]): The sender of the chat message.
|
507
|
+
system (Optional[Union[System, str, Dict[str, Any]]]): System message to be processed.
|
508
|
+
tools (Union[bool, Tool, List[Tool], str, List[str]]): Specifies tools to be invoked.
|
509
|
+
out (bool): If True, outputs the chat response.
|
510
|
+
invoke (bool): If True, invokes tools as part of the chat.
|
511
|
+
**kwargs: Arbitrary keyword arguments for chat completion.
|
512
|
+
|
513
|
+
Examples:
|
514
|
+
>>> await ChatFlow.chat(branch, "Ask about user preferences")
|
515
|
+
"""
|
516
|
+
return await ChatFlow.chat(
|
517
|
+
self, instruction=instruction, context=context,
|
518
|
+
sender=sender, system=system, tools=tools,
|
519
|
+
out=out, invoke=invoke, **kwargs
|
520
|
+
)
|
521
|
+
|
522
|
+
async def ReAct(
|
523
|
+
self,
|
524
|
+
instruction: Union[Instruction, str],
|
525
|
+
context = None,
|
526
|
+
sender = None,
|
527
|
+
system = None,
|
528
|
+
tools = None,
|
529
|
+
num_rounds: int = 1,
|
530
|
+
**kwargs ):
|
531
|
+
"""
|
532
|
+
Performs a reason-action cycle with optional tool invocation over multiple rounds.
|
533
|
+
|
534
|
+
Args:
|
535
|
+
branch: The Branch instance to perform ReAct operations.
|
536
|
+
instruction (Union[Instruction, str]): Initial instruction for the cycle.
|
537
|
+
context: Context relevant to the instruction.
|
538
|
+
sender (Optional[str]): Identifier for the message sender.
|
539
|
+
system: Initial system message or configuration.
|
540
|
+
tools: Tools to be registered or used during the cycle.
|
541
|
+
num_rounds (int): Number of reason-action cycles to perform.
|
542
|
+
**kwargs: Additional keyword arguments for customization.
|
543
|
+
|
544
|
+
Examples:
|
545
|
+
>>> await ChatFlow.ReAct(branch, "Analyze user feedback", num_rounds=2)
|
546
|
+
"""
|
547
|
+
return await ChatFlow.ReAct(
|
548
|
+
self, instruction=instruction, context=context,
|
549
|
+
sender=sender, system=system, tools=tools,
|
550
|
+
num_rounds=num_rounds, **kwargs
|
551
|
+
)
|
552
|
+
|
553
|
+
async def auto_followup(
|
554
|
+
self,
|
555
|
+
instruction: Union[Instruction, str],
|
556
|
+
context = None,
|
557
|
+
sender = None,
|
558
|
+
system = None,
|
559
|
+
tools: Union[bool, Tool, List[Tool], str, List[str], List[Dict]] = False,
|
560
|
+
max_followup: int = 3,
|
561
|
+
out=True,
|
562
|
+
**kwargs
|
563
|
+
) -> None:
|
564
|
+
"""
|
565
|
+
Automatically performs follow-up actions based on chat interactions and tool invocations.
|
566
|
+
|
567
|
+
Args:
|
568
|
+
branch: The Branch instance to perform follow-up operations.
|
569
|
+
instruction (Union[Instruction, str]): The initial instruction for follow-up.
|
570
|
+
context: Context relevant to the instruction.
|
571
|
+
sender (Optional[str]): Identifier for the message sender.
|
572
|
+
system: Initial system message or configuration.
|
573
|
+
tools: Specifies tools to be considered for follow-up actions.
|
574
|
+
max_followup (int): Maximum number of follow-up chats allowed.
|
575
|
+
out (bool): If True, outputs the result of the follow-up action.
|
576
|
+
**kwargs: Additional keyword arguments for follow-up customization.
|
577
|
+
|
578
|
+
Examples:
|
579
|
+
>>> await ChatFlow.auto_followup(branch, "Finalize report", max_followup=2)
|
580
|
+
"""
|
581
|
+
return await ChatFlow.auto_followup(
|
582
|
+
self, instruction=instruction, context=context,
|
583
|
+
sender=sender, system=system, tools=tools,
|
584
|
+
max_followup=max_followup, out=out, **kwargs
|
585
|
+
)
|
586
|
+
|
587
|
+
# ---- branch operations ---- #
|
588
|
+
def clone(self) -> 'Branch':
|
589
|
+
"""
|
590
|
+
Creates a copy of the current Branch instance.
|
591
|
+
|
592
|
+
This method is useful for duplicating the branch's state, including its messages,
|
593
|
+
instruction sets, and tool registrations, into a new, independent Branch instance.
|
594
|
+
|
595
|
+
Returns:
|
596
|
+
Branch: A new Branch instance that is a deep copy of the current one.
|
597
|
+
|
598
|
+
Examples:
|
599
|
+
>>> cloned_branch = original_branch.clone()
|
600
|
+
>>> assert cloned_branch.messages.equals(original_branch.messages)
|
601
|
+
"""
|
602
|
+
cloned = Branch(
|
603
|
+
messages=self.messages.copy(),
|
604
|
+
instruction_sets=self.instruction_sets.copy(),
|
605
|
+
tool_manager=ToolManager()
|
606
|
+
)
|
607
|
+
tools = [
|
608
|
+
tool for tool in self.tool_manager.registry.values()]
|
609
|
+
|
610
|
+
cloned.register_tools(tools)
|
611
|
+
|
612
|
+
return cloned
|
613
|
+
|
614
|
+
def merge_branch(self, branch: 'Branch', update: bool = True):
|
615
|
+
"""
|
616
|
+
Merges another branch into the current Branch instance.
|
617
|
+
|
618
|
+
Incorporates messages, instruction sets, and tool registrations from the specified branch,
|
619
|
+
optionally updating existing instruction sets and tools if duplicates are found.
|
620
|
+
|
621
|
+
Args:
|
622
|
+
branch (Branch): The branch to merge into the current one.
|
623
|
+
update (bool): If True, existing instruction sets and tools are updated. Defaults to True.
|
624
|
+
|
625
|
+
Examples:
|
626
|
+
>>> main_branch.merge_branch(another_branch, update=True)
|
627
|
+
>>> main_branch.merge_branch(temporary_branch, update=False)
|
628
|
+
"""
|
629
|
+
|
630
|
+
message_copy = branch.messages.copy()
|
631
|
+
self.messages = self.messages.merge(message_copy, how='outer')
|
632
|
+
|
633
|
+
if update:
|
634
|
+
self.instruction_sets.update(branch.instruction_sets)
|
635
|
+
self.tool_manager.registry.update(
|
636
|
+
branch.tool_manager.registry
|
637
|
+
)
|
638
|
+
else:
|
639
|
+
for key, value in branch.instruction_sets.items():
|
640
|
+
if key not in self.instruction_sets:
|
641
|
+
self.instruction_sets[key] = value
|
642
|
+
|
643
|
+
for key, value in branch.tool_manager.registry.items():
|
644
|
+
if key not in self.tool_manager.registry:
|
645
|
+
self.tool_manager.registry[key] = value
|
646
|
+
|
647
|
+
# ----- tool manager methods ----- #
|
648
|
+
def register_tools(self, tools: Union[Tool, List[Tool]]) -> None:
|
649
|
+
"""
|
650
|
+
Registers one or more tools with the branch's tool manager.
|
651
|
+
|
652
|
+
This method allows for the dynamic addition of tools to the branch, enhancing its
|
653
|
+
capabilities and interactions with users or external systems.
|
654
|
+
|
655
|
+
Args:
|
656
|
+
tools (Union[Tool, List[Tool]]): A single tool instance or a list of Tool instances to register.
|
657
|
+
|
658
|
+
Examples:
|
659
|
+
>>> branch.register_tools(tool_instance)
|
660
|
+
>>> branch.register_tools([tool_one, tool_two, tool_three])
|
661
|
+
"""
|
662
|
+
if not isinstance(tools, list):
|
663
|
+
tools = [tools]
|
664
|
+
self.tool_manager.register_tools(tools=tools)
|
665
|
+
|
666
|
+
def delete_tool(self, tools: Union[Tool, List[Tool], str, List[str]], verbose=True) -> bool:
|
667
|
+
"""
|
668
|
+
Deletes one or more tools from the branch's tool manager.
|
669
|
+
|
670
|
+
This method allows for the removal of tools previously registered with the branch, either by tool instance or name.
|
671
|
+
|
672
|
+
Args:
|
673
|
+
tools (Union[Tool, List[Tool], str, List[str]]): A single tool instance, tool name, or a list of either to be deleted.
|
674
|
+
verbose (bool): If True, prints a success message upon deletion. Defaults to True.
|
675
|
+
|
676
|
+
Returns:
|
677
|
+
bool: True if the tool(s) were successfully deleted, False otherwise.
|
678
|
+
|
679
|
+
Examples:
|
680
|
+
>>> branch.delete_tool("tool_name")
|
681
|
+
>>> branch.delete_tool(tool_instance)
|
682
|
+
>>> branch.delete_tool(["tool_one", "tool_two"])
|
683
|
+
"""
|
684
|
+
if isinstance(tools, list):
|
685
|
+
if is_same_dtype(tools, str):
|
686
|
+
for tool in tools:
|
687
|
+
if tool in self.tool_manager.registry:
|
688
|
+
self.tool_manager.registry.pop(tool)
|
689
|
+
if verbose:
|
690
|
+
print("tools successfully deleted")
|
691
|
+
return True
|
692
|
+
elif is_same_dtype(tools, Tool):
|
693
|
+
for tool in tools:
|
694
|
+
if tool.name in self.tool_manager.registry:
|
695
|
+
self.tool_manager.registry.pop(tool.name)
|
696
|
+
if verbose:
|
697
|
+
print("tools successfully deleted")
|
698
|
+
return True
|
699
|
+
if verbose:
|
700
|
+
print("tools deletion failed")
|
701
|
+
return False
|
702
|
+
|
703
|
+
# ---- message operations ----#
|
704
|
+
def add_message(self, system: Optional[Union[dict, list, System]] = None,
|
705
|
+
instruction: Optional[Union[dict, list, Instruction]] = None,
|
706
|
+
context: Optional[Union[str, Dict[str, Any]]] = None,
|
707
|
+
response: Optional[Union[dict, list, Response]] = None,
|
708
|
+
sender: Optional[str] = None) -> None:
|
709
|
+
"""
|
710
|
+
Adds a message to the branch's conversation.
|
711
|
+
|
712
|
+
Supports adding different types of messages: system, instruction, and response. Each message
|
713
|
+
type is added with a timestamp and sender information.
|
714
|
+
|
715
|
+
Args:
|
716
|
+
system (Optional[Union[dict, list, System]]): System message to add.
|
717
|
+
instruction (Optional[Union[dict, list, Instruction]]): Instruction message to add.
|
718
|
+
context (Optional[Union[str, Dict[str, Any]]]): Context associated with the instruction.
|
719
|
+
response (Optional[Union[dict, list, Response]]): Response message to add.
|
720
|
+
sender (Optional[str]): Identifier for the sender of the message.
|
721
|
+
|
722
|
+
Examples:
|
723
|
+
>>> branch.add_message(system={'content': 'System initialized'}, sender='system')
|
724
|
+
>>> branch.add_message(instruction={'content': 'Please respond'}, sender='user')
|
725
|
+
"""
|
726
|
+
msg = self._create_message(
|
727
|
+
system=system, instruction=instruction,
|
728
|
+
context=context, response=response, sender=sender
|
729
|
+
)
|
730
|
+
message_dict = msg.to_dict()
|
731
|
+
if isinstance(as_dict(message_dict['content']), dict):
|
732
|
+
message_dict['content'] = json.dumps(message_dict['content'])
|
733
|
+
message_dict['timestamp'] = datetime.now().isoformat()
|
734
|
+
self.messages.loc[len(self.messages)] = message_dict
|
735
|
+
|
736
|
+
def remove_message(self, node_id: str) -> None:
|
737
|
+
"""
|
738
|
+
Removes a message from the branch's conversation based on its node ID.
|
739
|
+
|
740
|
+
Args:
|
741
|
+
node_id (str): The unique identifier of the message to be removed.
|
742
|
+
|
743
|
+
Examples:
|
744
|
+
>>> branch.remove_message("12345")
|
745
|
+
"""
|
746
|
+
CoreUtil.remove_message(self.messages, node_id)
|
747
|
+
|
748
|
+
def update_message(
|
749
|
+
self, value: Any, node_id: Optional[str] = None, col: str = 'node_id'
|
750
|
+
) -> None:
|
751
|
+
"""
|
752
|
+
Updates a message in the conversation based on its node_id.
|
753
|
+
|
754
|
+
Args:
|
755
|
+
value (Any): The new value to update the message with.
|
756
|
+
node_id (Optional[str], optional): The node_id of the message to be updated. Defaults to None.
|
757
|
+
col (str, optional): The column to be updated. Defaults to 'node_id'.
|
758
|
+
|
759
|
+
Returns:
|
760
|
+
bool: True if the update was successful, False otherwise.
|
761
|
+
|
762
|
+
Examples:
|
763
|
+
>>> conversation.update_message('Updated content', node_id='12345', col='content')
|
764
|
+
"""
|
765
|
+
return CoreUtil.update_row(self.messages, node_id=node_id, col=col, value=value)
|
766
|
+
|
767
|
+
def change_first_system_message(
|
768
|
+
self, system: Union[str, Dict[str, Any], System], sender: Optional[str] = None
|
769
|
+
):
|
770
|
+
"""
|
771
|
+
Updates the first system message in the branch's conversation.
|
772
|
+
|
773
|
+
Args:
|
774
|
+
system (Union[str, Dict[str, Any], System]): The new system message content.
|
775
|
+
sender (Optional[str]): The sender of the system message. Defaults to None.
|
776
|
+
|
777
|
+
Raises:
|
778
|
+
ValueError: If there are no system messages in the conversation.
|
779
|
+
|
780
|
+
Examples:
|
781
|
+
>>> branch.change_first_system_message({'content': 'System rebooted'}, sender='system')
|
782
|
+
"""
|
783
|
+
if self.len_systems == 0:
|
784
|
+
raise ValueError("There is no system message in the messages.")
|
785
|
+
|
786
|
+
if not isinstance(system, (str, Dict, System)):
|
787
|
+
raise ValueError("Input cannot be converted into a system message.")
|
788
|
+
|
789
|
+
elif isinstance(system, (str, Dict)):
|
790
|
+
system = System(system, sender=sender)
|
791
|
+
|
792
|
+
elif isinstance(system, System):
|
793
|
+
message_dict = system.to_dict()
|
794
|
+
if sender:
|
795
|
+
message_dict['sender'] = sender
|
796
|
+
message_dict['timestamp'] = datetime.now().isoformat()
|
797
|
+
sys_index = self.messages[self.messages.role == 'system'].index
|
798
|
+
self.messages.loc[sys_index[0]] = message_dict
|
799
|
+
|
800
|
+
def rollback(self, steps: int) -> None:
|
801
|
+
"""
|
802
|
+
Removes the last 'n' messages from the conversation.
|
803
|
+
|
804
|
+
Args:
|
805
|
+
steps (int): The number of messages to remove from the end of the conversation.
|
806
|
+
|
807
|
+
Raises:
|
808
|
+
ValueError: If 'steps' is not a positive integer or exceeds the number of messages.
|
809
|
+
|
810
|
+
Examples:
|
811
|
+
>>> conversation.rollback(2)
|
812
|
+
"""
|
813
|
+
return CoreUtil.remove_last_n_rows(self.messages, steps)
|
814
|
+
|
815
|
+
def clear_messages(self) -> None:
|
816
|
+
"""
|
817
|
+
Clears all messages from the conversation, resetting it to an empty state.
|
818
|
+
|
819
|
+
Examples:
|
820
|
+
>>> conversation.clear_messages()
|
821
|
+
"""
|
822
|
+
self.messages = pd.DataFrame(columns=Branch._cols)
|
823
|
+
|
824
|
+
def replace_keyword(
|
825
|
+
self,
|
826
|
+
keyword: str,
|
827
|
+
replacement: str,
|
828
|
+
col: str = 'content',
|
829
|
+
case_sensitive: bool = False
|
830
|
+
) -> None:
|
831
|
+
"""
|
832
|
+
Replaces all occurrences of a keyword in a specified column of the conversation's messages with a given replacement.
|
833
|
+
|
834
|
+
Args:
|
835
|
+
keyword (str): The keyword to be replaced.
|
836
|
+
replacement (str): The string to replace the keyword with.
|
837
|
+
col (str, optional): The column where the replacement should occur. Defaults to 'content'.
|
838
|
+
case_sensitive (bool, optional): If True, the replacement is case sensitive. Defaults to False.
|
839
|
+
|
840
|
+
Examples:
|
841
|
+
>>> conversation.replace_keyword('hello', 'hi', col='content')
|
842
|
+
"""
|
843
|
+
CoreUtil.replace_keyword(
|
844
|
+
self.messages, keyword, replacement, col=col,
|
845
|
+
case_sensitive=case_sensitive
|
846
|
+
)
|
847
|
+
|
848
|
+
def search_keywords(
|
849
|
+
self,
|
850
|
+
keywords: Union[str, list],
|
851
|
+
case_sensitive: bool = False, reset_index: bool = False, dropna: bool = False
|
852
|
+
) -> pd.DataFrame:
|
853
|
+
"""
|
854
|
+
Searches for messages containing specified keywords within the conversation.
|
855
|
+
|
856
|
+
Args:
|
857
|
+
keywords (Union[str, list]): The keyword(s) to search for within the conversation's messages.
|
858
|
+
case_sensitive (bool, optional): If True, the search is case sensitive. Defaults to False.
|
859
|
+
reset_index (bool, optional): If True, resets the index of the resulting DataFrame. Defaults to False.
|
860
|
+
dropna (bool, optional): If True, drops messages with NA values before searching. Defaults to False.
|
861
|
+
|
862
|
+
Returns:
|
863
|
+
pd.DataFrame: A DataFrame containing messages that match the search criteria.
|
864
|
+
|
865
|
+
Examples:
|
866
|
+
>>> df_matching = conversation.search_keywords('urgent', case_sensitive=True)
|
867
|
+
"""
|
868
|
+
return CoreUtil.search_keywords(
|
869
|
+
self.messages, keywords, case_sensitive, reset_index, dropna
|
870
|
+
)
|
871
|
+
|
872
|
+
def extend(self, messages: pd.DataFrame, **kwargs) -> None:
|
873
|
+
"""
|
874
|
+
Extends the conversation by appending new messages, optionally avoiding duplicates based on specified criteria.
|
875
|
+
|
876
|
+
Args:
|
877
|
+
messages (pd.DataFrame): A DataFrame containing new messages to append to the conversation.
|
878
|
+
**kwargs: Additional keyword arguments for handling duplicates (passed to pandas' drop_duplicates method).
|
879
|
+
|
880
|
+
Examples:
|
881
|
+
>>> new_messages = pd.DataFrame([...])
|
882
|
+
>>> conversation.extend(new_messages)
|
883
|
+
"""
|
884
|
+
self.messages = CoreUtil.extend(self.messages, messages, **kwargs)
|
885
|
+
|
886
|
+
def filter_by(
|
887
|
+
self,
|
888
|
+
role: Optional[str] = None,
|
889
|
+
sender: Optional[str] = None,
|
890
|
+
start_time: Optional[datetime] = None,
|
891
|
+
end_time: Optional[datetime] = None,
|
892
|
+
content_keywords: Optional[Union[str, list]] = None,
|
893
|
+
case_sensitive: bool = False
|
894
|
+
) -> pd.DataFrame:
|
895
|
+
"""
|
896
|
+
Filters the conversation's messages based on specified criteria such as role, sender, time range, and keywords.
|
897
|
+
|
898
|
+
Args:
|
899
|
+
role (Optional[str]): Filter messages by role (e.g., 'user', 'assistant', 'system').
|
900
|
+
sender (Optional[str]): Filter messages by sender.
|
901
|
+
start_time (Optional[datetime]): Filter messages sent after this time.
|
902
|
+
end_time (Optional[datetime]): Filter messages sent before this time.
|
903
|
+
content_keywords (Optional[Union[str, list]]): Filter messages containing these keywords.
|
904
|
+
case_sensitive (bool, optional): If True, keyword search is case sensitive. Defaults to False.
|
905
|
+
|
906
|
+
Returns:
|
907
|
+
pd.DataFrame: A DataFrame containing messages that match the filter criteria.
|
908
|
+
|
909
|
+
Examples:
|
910
|
+
>>> filtered_df = conversation.filter_by(role='user', content_keywords=['urgent', 'immediate'])
|
911
|
+
"""
|
912
|
+
return CoreUtil.filter_messages_by(
|
913
|
+
self.messages, role=role, sender=sender,
|
914
|
+
start_time=start_time, end_time=end_time,
|
915
|
+
content_keywords=content_keywords, case_sensitive=case_sensitive
|
916
|
+
)
|
917
|
+
|
918
|
+
# ----- intra-branch communication methods ----- #
|
919
|
+
def send(self, to_name, title, package):
|
920
|
+
"""
|
921
|
+
Sends a request package to a specified recipient.
|
922
|
+
|
923
|
+
Packages are queued in `pending_outs` for dispatch. The function doesn't immediately send the package but prepares it for delivery.
|
924
|
+
|
925
|
+
Args:
|
926
|
+
to_name (str): The name of the recipient branch.
|
927
|
+
title (str): The title or category of the request (e.g., 'messages', 'tool', 'service', 'llmconfig').
|
928
|
+
package (Any): The actual data or object to be sent, its expected type depends on the title.
|
929
|
+
|
930
|
+
Examples:
|
931
|
+
>>> branch.send("another_branch", "messages", message_dataframe)
|
932
|
+
>>> branch.send("service_branch", "service", service_config)
|
933
|
+
"""
|
934
|
+
request = Request(from_name=self.name, to_name=to_name, title=title, request=package)
|
935
|
+
self.pending_outs.append(request)
|
936
|
+
|
937
|
+
def receive(self, from_name, messages=True, tool=True, service=True, llmconfig=True):
|
938
|
+
"""
|
939
|
+
Processes and integrates received request packages based on their titles.
|
940
|
+
|
941
|
+
Handles incoming requests by updating the branch's state with the received data. It can selectively process requests based on the type specified by the `title` of the request.
|
942
|
+
|
943
|
+
Args:
|
944
|
+
from_name (str): The name of the sender whose packages are to be processed.
|
945
|
+
messages (bool): If True, processes 'messages' requests. Defaults to True.
|
946
|
+
tool (bool): If True, processes 'tool' requests. Defaults to True.
|
947
|
+
service (bool): If True, processes 'service' requests. Defaults to True.
|
948
|
+
llmconfig (bool): If True, processes 'llmconfig' requests. Defaults to True.
|
949
|
+
|
950
|
+
Raises:
|
951
|
+
ValueError: If no package is found from the specified sender, or if any of the packages have an invalid format.
|
952
|
+
|
953
|
+
Examples:
|
954
|
+
>>> branch.receive("another_branch")
|
955
|
+
"""
|
956
|
+
skipped_requests = deque()
|
957
|
+
if from_name not in self.pending_ins:
|
958
|
+
raise ValueError(f'No package from {from_name}')
|
959
|
+
while self.pending_ins[from_name]:
|
960
|
+
request = self.pending_ins[from_name].popleft()
|
961
|
+
|
962
|
+
if request.title == 'messages' and messages:
|
963
|
+
if not isinstance(request.request, pd.DataFrame):
|
964
|
+
raise ValueError('Invalid messages format')
|
965
|
+
CoreUtil.validate_messages(request.request)
|
966
|
+
self.messages = self.messages.merge(request.request, how='outer')
|
967
|
+
continue
|
968
|
+
|
969
|
+
elif request.title == 'tool' and tool:
|
970
|
+
if not isinstance(request.request, Tool):
|
971
|
+
raise ValueError('Invalid tool format')
|
972
|
+
self.tool_manager.register_tools([request.request])
|
973
|
+
|
974
|
+
elif request.title == 'service' and service:
|
975
|
+
if not isinstance(request.request, BaseService):
|
976
|
+
raise ValueError('Invalid service format')
|
977
|
+
self.service = request.request
|
978
|
+
|
979
|
+
elif request.title == 'llmconfig' and llmconfig:
|
980
|
+
if not isinstance(request.request, dict):
|
981
|
+
raise ValueError('Invalid llmconfig format')
|
982
|
+
self.llmconfig.update(request.request)
|
983
|
+
|
984
|
+
else:
|
985
|
+
skipped_requests.append(request)
|
986
|
+
|
987
|
+
self.pending_ins[from_name] = skipped_requests
|
988
|
+
|
989
|
+
def receive_all(self):
|
990
|
+
"""
|
991
|
+
Processes all pending incoming requests from all senders.
|
992
|
+
|
993
|
+
This method iterates through all senders with pending requests and processes each using the `receive` method. It ensures that all queued incoming data is integrated into the branch's state.
|
994
|
+
|
995
|
+
Examples:
|
996
|
+
>>> branch.receive_all()
|
997
|
+
"""
|
998
|
+
for key in list(self.pending_ins.keys()):
|
999
|
+
self.receive(key)
|
1000
|
+
|
1001
|
+
def _add_service(self, service, llmconfig):
|
1002
|
+
service = service or OpenAIService()
|
1003
|
+
self.service=service
|
1004
|
+
if llmconfig:
|
1005
|
+
self.llmconfig = llmconfig
|
1006
|
+
else:
|
1007
|
+
if isinstance(service, OpenAIService):
|
1008
|
+
self.llmconfig = oai_schema["chat/completions"]["config"]
|
1009
|
+
elif isinstance(service, OpenRouterService):
|
1010
|
+
self.llmconfig = openrouter_schema["chat/completions"]["config"]
|
1011
|
+
else:
|
1012
|
+
self.llmconfig = {}
|
1013
|
+
|
1014
|
+
def _to_chatcompletion_message(self, with_sender=False) -> List[Dict[str, Any]]:
|
1015
|
+
message = []
|
1016
|
+
|
1017
|
+
for _, row in self.messages.iterrows():
|
1018
|
+
content_ = row['content']
|
1019
|
+
if content_.startswith('Sender'):
|
1020
|
+
content_ = content_.split(':', 1)[1]
|
1021
|
+
|
1022
|
+
if isinstance(content_, str):
|
1023
|
+
try:
|
1024
|
+
content_ = json.dumps(as_dict(content_))
|
1025
|
+
except Exception as e:
|
1026
|
+
raise ValueError(f"Error in serealizing, {row['node_id']} {content_}: {e}")
|
1027
|
+
|
1028
|
+
out = {"role": row['role'], "content": content_}
|
1029
|
+
if with_sender:
|
1030
|
+
out['content'] = f"Sender {row['sender']}: {content_}"
|
1031
|
+
|
1032
|
+
message.append(out)
|
1033
|
+
return message
|
1034
|
+
|
1035
|
+
def _is_invoked(self) -> bool:
|
1036
|
+
"""
|
1037
|
+
Check if the conversation has been invoked with an action response.
|
1038
|
+
|
1039
|
+
Returns:
|
1040
|
+
bool: True if the conversation has been invoked, False otherwise.
|
1041
|
+
|
1042
|
+
"""
|
1043
|
+
content = self.messages.iloc[-1]['content']
|
1044
|
+
try:
|
1045
|
+
if (
|
1046
|
+
as_dict(content)['action_response'].keys() >= {'function', 'arguments', 'output'}
|
1047
|
+
):
|
1048
|
+
return True
|
1049
|
+
except:
|
1050
|
+
return False
|
1051
|
+
|
1052
|
+
def _create_message(
|
1053
|
+
self,
|
1054
|
+
system: Optional[Union[dict, list, System]] = None,
|
1055
|
+
instruction: Optional[Union[dict, list, Instruction]] = None,
|
1056
|
+
context: Optional[Union[str, Dict[str, Any]]] = None,
|
1057
|
+
response: Optional[Union[dict, list, Response]] = None,
|
1058
|
+
sender: Optional[str] = None
|
1059
|
+
) -> Message:
|
1060
|
+
"""
|
1061
|
+
Creates a message object based on the given parameters, ensuring only one message type is specified.
|
1062
|
+
|
1063
|
+
Args:
|
1064
|
+
system (Optional[Union[dict, list, System]]): System message to be added.
|
1065
|
+
instruction (Optional[Union[dict, list, Instruction]]): Instruction message to be added.
|
1066
|
+
context (Optional[Union[str, Dict[str, Any]]]): Context for the instruction message.
|
1067
|
+
response (Optional[Union[dict, list, Response]]): Response message to be added.
|
1068
|
+
sender (Optional[str]): The sender of the message.
|
1069
|
+
|
1070
|
+
Returns:
|
1071
|
+
Message: A Message object created from the provided parameters.
|
1072
|
+
|
1073
|
+
Raises:
|
1074
|
+
ValueError: If more than one message type is specified or if the parameters do not form a valid message.
|
1075
|
+
"""
|
1076
|
+
if sum(lcall([system, instruction, response], bool)) != 1:
|
1077
|
+
raise ValueError("Error: Message must have one and only one role.")
|
1078
|
+
|
1079
|
+
else:
|
1080
|
+
if isinstance(any([system, instruction, response]), Message):
|
1081
|
+
if system:
|
1082
|
+
return system
|
1083
|
+
elif instruction:
|
1084
|
+
return instruction
|
1085
|
+
elif response:
|
1086
|
+
return response
|
1087
|
+
|
1088
|
+
msg = 0
|
1089
|
+
if response:
|
1090
|
+
msg = Response(response=response, sender=sender)
|
1091
|
+
elif instruction:
|
1092
|
+
msg = Instruction(instruction=instruction,
|
1093
|
+
context=context, sender=sender)
|
1094
|
+
elif system:
|
1095
|
+
msg = System(system=system, sender=sender)
|
1096
|
+
return msg
|
1097
|
+
|
1098
|
+
def _info(self, use_sender: bool = False) -> Dict[str, int]:
|
1099
|
+
"""
|
1100
|
+
Generates a summary of the conversation's messages, either by role or sender.
|
1101
|
+
|
1102
|
+
Args:
|
1103
|
+
use_sender (bool, optional): If True, generates the summary based on sender. If False, uses role. Defaults to False.
|
1104
|
+
|
1105
|
+
Returns:
|
1106
|
+
Dict[str, int]: A dictionary with counts of messages, categorized either by role or sender.
|
1107
|
+
"""
|
1108
|
+
messages = self.messages['sender'] if use_sender else self.messages['role']
|
1109
|
+
result = messages.value_counts().to_dict()
|
1110
|
+
result['total'] = len(self.len_messages)
|
1111
|
+
|
1112
|
+
return result
|
1113
|
+
|
1114
|
+
|
1115
|
+
|
1116
|
+
# def add_instruction_set(self, name: str, instruction_set: InstructionSet):
|
1117
|
+
# """
|
1118
|
+
# Add an instruction set to the conversation.
|
1119
|
+
#
|
1120
|
+
# Args:
|
1121
|
+
# name (str): The name of the instruction set.
|
1122
|
+
# instruction_set (InstructionSet): The instruction set to add.
|
1123
|
+
#
|
1124
|
+
# Examples:
|
1125
|
+
# >>> branch.add_instruction_set("greet", InstructionSet(instructions=["Hello", "Hi"]))
|
1126
|
+
# """
|
1127
|
+
# self.instruction_sets[name] = instruction_set
|
1128
|
+
|
1129
|
+
# def remove_instruction_set(self, name: str) -> bool:
|
1130
|
+
# """
|
1131
|
+
# Remove an instruction set from the conversation.
|
1132
|
+
#
|
1133
|
+
# Args:
|
1134
|
+
# name (str): The name of the instruction set to remove.
|
1135
|
+
#
|
1136
|
+
# Returns:
|
1137
|
+
# bool: True if the instruction set was removed, False otherwise.
|
1138
|
+
#
|
1139
|
+
# Examples:
|
1140
|
+
# >>> branch.remove_instruction_set("greet")
|
1141
|
+
# True
|
1142
|
+
# """
|
1143
|
+
# return self.instruction_sets.pop(name)
|
1144
|
+
|
1145
|
+
# async def instruction_set_auto_followup(
|
1146
|
+
# self,
|
1147
|
+
# instruction_set: InstructionSet,
|
1148
|
+
# num: Union[int, List[int]] = 3,
|
1149
|
+
# **kwargs
|
1150
|
+
# ) -> None:
|
1151
|
+
# """
|
1152
|
+
# Automatically perform follow-up chats for an entire instruction set.
|
1153
|
+
#
|
1154
|
+
# This method asynchronously conducts follow-up chats for each instruction in the provided instruction set,
|
1155
|
+
# handling tool invocations as specified.
|
1156
|
+
#
|
1157
|
+
# Args:
|
1158
|
+
# instruction_set (InstructionSet): The instruction set to process.
|
1159
|
+
# num (Union[int, List[int]]): The maximum number of follow-up chats to perform for each instruction,
|
1160
|
+
# or a list of maximum numbers corresponding to each instruction.
|
1161
|
+
# **kwargs: Additional keyword arguments to pass to the chat completion service.
|
1162
|
+
#
|
1163
|
+
# Raises:
|
1164
|
+
# ValueError: If the length of `num` as a list does not match the number of instructions in the set.
|
1165
|
+
#
|
1166
|
+
# Examples:
|
1167
|
+
# >>> instruction_set = InstructionSet(instructions=["What's the weather?", "And for tomorrow?"])
|
1168
|
+
# >>> await branch.instruction_set_auto_followup(instruction_set)
|
1169
|
+
# """
|
1170
|
+
#
|
1171
|
+
# if isinstance(num, List):
|
1172
|
+
# if len(num) != instruction_set.instruct_len:
|
1173
|
+
# raise ValueError(
|
1174
|
+
# 'Unmatched auto_followup num size and instructions set size'
|
1175
|
+
# )
|
1176
|
+
# current_instruct_node = instruction_set.get_instruction_by_id(
|
1177
|
+
# instruction_set.first_instruct
|
1178
|
+
# )
|
1179
|
+
# for i in range(instruction_set.instruct_len):
|
1180
|
+
# num_ = num if isinstance(num, int) else num[i]
|
1181
|
+
# tools = instruction_set.get_tools(current_instruct_node)
|
1182
|
+
# if tools:
|
1183
|
+
# await self.auto_followup(
|
1184
|
+
# current_instruct_node, num=num_, tools=tools, self=self, **kwargs
|
1185
|
+
# )
|
1186
|
+
# else:
|
1187
|
+
# await self.chat(current_instruct_node)
|
1188
|
+
# current_instruct_node = instruction_set.get_next_instruction(
|
1189
|
+
# current_instruct_node
|
1190
|
+
# )
|
1191
|
+
|