lionagi 0.0.201__py3-none-any.whl → 0.0.204__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. lionagi/_services/anthropic.py +79 -1
  2. lionagi/_services/base_service.py +1 -1
  3. lionagi/_services/services.py +61 -25
  4. lionagi/_services/transformers.py +46 -0
  5. lionagi/agents/__init__.py +0 -0
  6. lionagi/configs/oai_configs.py +1 -1
  7. lionagi/configs/openrouter_configs.py +1 -1
  8. lionagi/core/__init__.py +3 -7
  9. lionagi/core/branch/__init__.py +0 -0
  10. lionagi/core/branch/branch.py +589 -0
  11. lionagi/core/branch/branch_manager.py +139 -0
  12. lionagi/core/branch/cluster.py +1 -0
  13. lionagi/core/branch/conversation.py +484 -0
  14. lionagi/core/core_util.py +59 -0
  15. lionagi/core/flow/__init__.py +0 -0
  16. lionagi/core/flow/flow.py +19 -0
  17. lionagi/core/instruction_set/__init__.py +0 -0
  18. lionagi/core/instruction_set/instruction_set.py +343 -0
  19. lionagi/core/messages/__init__.py +0 -0
  20. lionagi/core/messages/messages.py +176 -0
  21. lionagi/core/sessions/__init__.py +0 -0
  22. lionagi/core/sessions/session.py +428 -0
  23. lionagi/models/__init__.py +0 -0
  24. lionagi/models/base_model.py +0 -0
  25. lionagi/models/imodel.py +53 -0
  26. lionagi/schema/data_logger.py +75 -155
  27. lionagi/tests/test_utils/test_call_util.py +658 -657
  28. lionagi/tools/tool_manager.py +121 -188
  29. lionagi/utils/__init__.py +5 -10
  30. lionagi/utils/call_util.py +667 -585
  31. lionagi/utils/io_util.py +3 -0
  32. lionagi/utils/nested_util.py +17 -211
  33. lionagi/utils/pd_util.py +57 -0
  34. lionagi/utils/sys_util.py +220 -184
  35. lionagi/utils/url_util.py +55 -0
  36. lionagi/version.py +1 -1
  37. {lionagi-0.0.201.dist-info → lionagi-0.0.204.dist-info}/METADATA +12 -8
  38. {lionagi-0.0.201.dist-info → lionagi-0.0.204.dist-info}/RECORD +47 -32
  39. lionagi/core/branch.py +0 -193
  40. lionagi/core/conversation.py +0 -341
  41. lionagi/core/flow.py +0 -8
  42. lionagi/core/instruction_set.py +0 -150
  43. lionagi/core/messages.py +0 -243
  44. lionagi/core/sessions.py +0 -474
  45. /lionagi/{tools → agents}/planner.py +0 -0
  46. /lionagi/{tools → agents}/prompter.py +0 -0
  47. /lionagi/{tools → agents}/scorer.py +0 -0
  48. /lionagi/{tools → agents}/summarizer.py +0 -0
  49. /lionagi/{tools → agents}/validator.py +0 -0
  50. /lionagi/core/{flow_util.py → flow/flow_util.py} +0 -0
  51. {lionagi-0.0.201.dist-info → lionagi-0.0.204.dist-info}/LICENSE +0 -0
  52. {lionagi-0.0.201.dist-info → lionagi-0.0.204.dist-info}/WHEEL +0 -0
  53. {lionagi-0.0.201.dist-info → lionagi-0.0.204.dist-info}/top_level.txt +0 -0
@@ -1,6 +1,7 @@
1
1
  from collections import deque
2
- from typing import Optional
3
- from ..utils import create_path, IOUtil
2
+ from typing import Dict, Any
3
+ from ..utils.sys_util import get_timestamp, create_path, as_dict
4
+ from ..utils.io_util import IOUtil
4
5
 
5
6
 
6
7
  class DataLogger:
@@ -38,49 +39,18 @@ class DataLogger:
38
39
  self.dir = dir
39
40
  self.log = deque(log) if log else deque()
40
41
 
41
- def __call__(self, entry):
42
+ def add_entry(self, entry: Dict[str, Any], level: str = "INFO") -> None:
42
43
  """
43
- Adds a new entry to the log.
44
+ Adds a new entry to the log with a timestamp and a log level.
44
45
 
45
- Parameters:
46
- entry: The data entry to be added to the log.
47
- """
48
- self.log.append(entry)
49
-
50
- # need fixing
51
- # def to_csv(self, filename: str, dir: Optional[str] = None, verbose: bool = True,
52
- # timestamp: bool = True, dir_exist_ok: bool = True, file_exist_ok: bool = False) -> None:
53
- # """
54
- # Exports the logged data to a CSV file and optionally clears the log.
55
-
56
- # Parameters:
57
- # filename (str): The name of the CSV file.
58
-
59
- # dir (Optional[str]): The directory to save the file. Defaults to the instance's dir attribute.
60
-
61
- # verbose (bool): If True, prints a message upon completion. Defaults to True.
62
-
63
- # timestamp (bool): If True, appends a timestamp to the filename. Defaults to True.
64
-
65
- # dir_exist_ok (bool): If True, will not raise an error if the directory already exists. Defaults to True.
66
-
67
- # file_exist_ok (bool): If True, overwrites the file if it exists. Defaults to False.
68
-
69
- # Side Effects:
70
- # Clears the log after saving the CSV file.
71
-
72
- # Prints a message indicating the save location and number of logs saved if verbose is True.
73
- # """
74
- # dir = dir or self.dir
75
- # filepath = create_path(
76
- # dir=dir, filename=filename, timestamp=timestamp, dir_exist_ok=dir_exist_ok)
77
- # # IOUtil.to_csv(list(self.log), filepath, file_exist_ok=file_exist_ok)
78
-
79
- # n_logs = len(list(self.log))
80
- # self.log = deque()
81
- # if verbose:
82
- # print(f"{n_logs} logs saved to {filepath}")
83
-
46
+ Args:
47
+ entry (Dict[str, Any]): The data entry to be added to the log.
48
+ level (str): The log level for the entry (e.g., "INFO", "ERROR"). Defaults to "INFO".
49
+ """
50
+ self.log.append({
51
+ "timestamp": get_timestamp(), "level": level, **as_dict(entry)
52
+ })
53
+
84
54
  def set_dir(self, dir: str) -> None:
85
55
  """
86
56
  Sets the default directory for saving CSV files.
@@ -89,118 +59,68 @@ class DataLogger:
89
59
  dir (str): The directory to be set as the default for saving files.
90
60
  """
91
61
  self.dir = dir
62
+
63
+ def to_csv(
64
+ self, filename: str,
65
+ file_exist_ok: bool = False,
66
+ timestamp = True,
67
+ time_prefix: bool = False,
68
+ verbose: bool = True,
69
+ clear = True
70
+ ) -> None:
71
+ """
72
+ Exports the logged data to a CSV file, using the provided utilities for path creation and timestamping.
73
+
74
+ Args:
75
+ filename (str): The name of the CSV file.
76
+ file_exist_ok (bool): If True, creates the directory for the file if it does not exist. Defaults to False.
77
+ verbose (bool): If True, prints a message upon completion. Defaults to True.
78
+ time_prefix (bool): If True, adds the timestamp as a prefix to the filename. Defaults to False.
79
+ """
80
+ if not filename.endswith('.csv'):
81
+ filename += '.csv'
82
+
83
+ filepath = create_path(
84
+ self.dir, filename, timestamp=timestamp,
85
+ dir_exist_ok=file_exist_ok, time_prefix=time_prefix
86
+ )
87
+ IOUtil.to_csv(list(self.log), filepath)
88
+
89
+ if verbose:
90
+ print(f"{len(self.log)} logs saved to {filepath}")
91
+
92
+ if clear:
93
+ self.log.clear()
94
+
95
+ def to_jsonl(
96
+ self, filename: str,
97
+ timestamp = False,
98
+ time_prefix=False,
99
+ file_exist_ok: bool = False,
100
+ verbose: bool = True,
101
+ clear = True
102
+ ) -> None:
103
+ """
104
+ Exports the logged data to a JSONL file and optionally clears the log.
105
+
106
+ Parameters:
107
+ filename (str): The name of the JSONL file.
108
+ file_exist_ok (bool): If True, creates the directory for the file if it does not exist. Defaults to False.
109
+ verbose (bool): If True, prints a message upon completion. Defaults to True.
110
+ """
111
+ if not filename.endswith('.jsonl'):
112
+ filename += '.jsonl'
113
+
114
+ filepath = create_path(
115
+ self.dir, filename, timestamp=timestamp,
116
+ dir_exist_ok=file_exist_ok, time_prefix=time_prefix
117
+ )
92
118
 
119
+ for entry in self.log:
120
+ IOUtil.append_to_jsonl(entry, filepath)
93
121
 
122
+ if verbose:
123
+ print(f"{len(self.log)} logs saved to {filepath}")
94
124
 
95
- # class DataLogger:
96
- # """A class for logging data entries and saving them to a CSV or JSON file.
97
-
98
- # Attributes:
99
- # dir (Optional[str]): The directory where log files will be saved.
100
- # log (Deque[Dict[str, Any]]): A deque that stores log entries with a maximum size.
101
- # buffer_limit (int): The maximum number of entries the log can hold.
102
- # auto_flush_size (int): The number of entries to reach before auto-flushing the log.
103
- # lock (Lock): A threading lock to ensure thread safety.
104
-
105
- # Examples:
106
- # >>> logger = EnhancedDataLogger(dir="logs", auto_flush_size=10)
107
- # >>> logger({"event": "start", "value": 1}, level="INFO")
108
- # >>> logger({"event": "stop", "value": 0}, level="ERROR")
109
- # >>> logger.flush(filename="event_log", to_json=False)
110
-
111
- # This will create a CSV file 'event_log.csv' in the 'logs' directory with two log entries.
112
-
113
- # >>> logger = EnhancedDataLogger(dir="logs", auto_flush_size=10)
114
- # >>> logger({"event": "start", "value": 1}, level="INFO")
115
- # >>> logger({"event": "update", "value": 5}, level="DEBUG")
116
- # >>> logger.flush(filename="event_log", to_json=True)
117
-
118
- # This will create a JSON file 'event_log.jsonl' in the 'logs' directory with two log entries.
119
- # """
120
-
121
- # def __init__(self, dir: Optional[str] = None, log: Optional[List[Dict[str, Any]]] = None, buffer_limit: int = 1000, auto_flush_size: int = 100) -> None:
122
- # self.dir = dir
123
- # self.log: Deque[Dict[str, Any]] = deque(log, maxlen=buffer_limit) if log else deque(maxlen=buffer_limit)
124
- # self.buffer_limit = buffer_limit
125
- # self.auto_flush_size = auto_flush_size
126
- # self.lock = Lock()
127
-
128
- # def __call__(self, entry: Dict[str, Any], level: str = "INFO") -> None:
129
- # """Add a log entry to the log deque with a specified log level.
130
-
131
- # Args:
132
- # entry (Dict[str, Any]): The log entry to be added.
133
- # level (str): The log level for the entry (e.g., "INFO", "ERROR").
134
- # """
135
- # with self.lock:
136
- # self.log.append({"timestamp": self.get_timestamp(), "level": level, **entry})
137
-
138
- # def flush(self, filename: str = "log", to_json: bool = False) -> None:
139
- # """Flush the log entries to a file in either CSV or JSON format.
140
-
141
- # Args:
142
- # filename (str): The base name of the file to write the logs to.
143
- # to_json (bool): If True, logs are written in JSON format; otherwise, in CSV format.
144
- # """
145
- # if not self.log:
146
- # return
147
- # dir = self.dir or "."
148
- # filepath = self.create_path(dir=dir, filename=filename, to_json=to_json)
149
- # num_entries = len(self.log)
150
- # if to_json:
151
- # self.to_json(filepath)
152
- # else:
153
- # self.to_csv(filepath)
154
- # self.log.clear()
155
- # print(f"Flushed {num_entries} entries to {'JSON' if to_json else 'CSV'} file.")
156
-
157
- # def to_csv(self, filepath: str) -> None:
158
- # """Write the log entries to a CSV file.
159
-
160
- # Args:
161
- # filepath (str): The full path to the CSV file.
162
- # """
163
- # num_entries = len(self.log)
164
- # with open(filepath, 'a', newline='') as csv_file:
165
- # writer = csv.DictWriter(csv_file, fieldnames=self.log[0].keys())
166
- # if os.stat(filepath).st_size == 0: # Write header only if file is empty
167
- # writer.writeheader()
168
- # writer.writerows(self.log)
169
- # print(f"Wrote {num_entries} entries to CSV file.")
170
-
171
- # def to_json(self, filepath: str) -> None:
172
- # """Write the log entries to a JSON file.
173
-
174
- # Args:
175
- # filepath (str): The full path to the JSON file.
176
- # """
177
- # num_entries = len(self.log)
178
- # with open(filepath, 'a') as json_file:
179
- # for entry in self.log:
180
- # json_file.write(json.dumps(entry) + "\n")
181
- # print(f"Wrote {num_entries} entries to JSON file.")
182
-
183
- # def create_path(self, dir: str, filename: str, to_json: bool = False) -> str:
184
- # """Create the full file path for the log file, ensuring the directory exists.
185
-
186
- # Args:
187
- # dir (str): The directory where the file will be created.
188
- # filename (str): The base name of the file.
189
- # to_json (bool): If True, the file extension will be '.jsonl'; otherwise, '.csv'.
190
-
191
- # Returns:
192
- # str: The full file path for the log file.
193
- # """
194
- # ext = "jsonl" if to_json else "csv"
195
- # filepath = f"{dir}/{filename}.{ext}"
196
- # os.makedirs(dir, exist_ok=True)
197
- # return filepath
198
-
199
- # def get_timestamp(self) -> str:
200
- # """Get the current timestamp in 'YYYYMMDDHHMMSS' format.
201
-
202
- # Returns:
203
- # str: The current timestamp.
204
- # """
205
- # return datetime.now().strftime("%Y%m%d%H%M%S")
206
-
125
+ if clear:
126
+ self.log.clear()