lionagi 0.0.111__py3-none-any.whl → 0.0.113__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. lionagi/__init__.py +7 -2
  2. lionagi/bridge/__init__.py +7 -0
  3. lionagi/bridge/langchain.py +131 -0
  4. lionagi/bridge/llama_index.py +157 -0
  5. lionagi/configs/__init__.py +7 -0
  6. lionagi/configs/oai_configs.py +49 -0
  7. lionagi/configs/openrouter_config.py +49 -0
  8. lionagi/core/__init__.py +15 -0
  9. lionagi/{session/conversation.py → core/conversations.py} +10 -17
  10. lionagi/core/flows.py +1 -0
  11. lionagi/core/instruction_sets.py +1 -0
  12. lionagi/{session/message.py → core/messages.py} +5 -5
  13. lionagi/core/sessions.py +262 -0
  14. lionagi/datastore/__init__.py +1 -0
  15. lionagi/datastore/chroma.py +1 -0
  16. lionagi/datastore/deeplake.py +1 -0
  17. lionagi/datastore/elasticsearch.py +1 -0
  18. lionagi/datastore/lantern.py +1 -0
  19. lionagi/datastore/pinecone.py +1 -0
  20. lionagi/datastore/postgres.py +1 -0
  21. lionagi/datastore/qdrant.py +1 -0
  22. lionagi/loader/__init__.py +12 -0
  23. lionagi/loader/chunker.py +157 -0
  24. lionagi/loader/reader.py +124 -0
  25. lionagi/objs/__init__.py +7 -0
  26. lionagi/objs/messenger.py +163 -0
  27. lionagi/objs/tool_registry.py +247 -0
  28. lionagi/schema/__init__.py +11 -0
  29. lionagi/schema/base_condition.py +1 -0
  30. lionagi/schema/base_schema.py +239 -0
  31. lionagi/schema/base_tool.py +9 -0
  32. lionagi/schema/data_logger.py +94 -0
  33. lionagi/services/__init__.py +14 -0
  34. lionagi/services/anthropic.py +1 -0
  35. lionagi/services/anyscale.py +0 -0
  36. lionagi/services/azure.py +1 -0
  37. lionagi/{api/oai_service.py → services/base_api_service.py} +74 -148
  38. lionagi/services/bedrock.py +0 -0
  39. lionagi/services/chatcompletion.py +48 -0
  40. lionagi/services/everlyai.py +0 -0
  41. lionagi/services/gemini.py +0 -0
  42. lionagi/services/gpt4all.py +0 -0
  43. lionagi/services/huggingface.py +0 -0
  44. lionagi/services/litellm.py +1 -0
  45. lionagi/services/localai.py +0 -0
  46. lionagi/services/mistralai.py +0 -0
  47. lionagi/services/oai.py +34 -0
  48. lionagi/services/ollama.py +1 -0
  49. lionagi/services/openllm.py +0 -0
  50. lionagi/services/openrouter.py +32 -0
  51. lionagi/services/perplexity.py +0 -0
  52. lionagi/services/predibase.py +0 -0
  53. lionagi/services/rungpt.py +0 -0
  54. lionagi/services/service_objs.py +282 -0
  55. lionagi/services/vllm.py +0 -0
  56. lionagi/services/xinference.py +0 -0
  57. lionagi/structure/__init__.py +7 -0
  58. lionagi/structure/relationship.py +128 -0
  59. lionagi/structure/structure.py +160 -0
  60. lionagi/tests/__init__.py +0 -0
  61. lionagi/tests/test_flatten_util.py +426 -0
  62. lionagi/tools/__init__.py +0 -0
  63. lionagi/tools/coder.py +1 -0
  64. lionagi/tools/planner.py +1 -0
  65. lionagi/tools/prompter.py +1 -0
  66. lionagi/tools/sandbox.py +1 -0
  67. lionagi/tools/scorer.py +1 -0
  68. lionagi/tools/summarizer.py +1 -0
  69. lionagi/tools/validator.py +1 -0
  70. lionagi/utils/__init__.py +46 -8
  71. lionagi/utils/api_util.py +63 -416
  72. lionagi/utils/call_util.py +347 -0
  73. lionagi/utils/flat_util.py +540 -0
  74. lionagi/utils/io_util.py +102 -0
  75. lionagi/utils/load_utils.py +190 -0
  76. lionagi/utils/sys_util.py +85 -660
  77. lionagi/utils/tool_util.py +82 -199
  78. lionagi/utils/type_util.py +81 -0
  79. lionagi/version.py +1 -1
  80. {lionagi-0.0.111.dist-info → lionagi-0.0.113.dist-info}/METADATA +44 -15
  81. lionagi-0.0.113.dist-info/RECORD +84 -0
  82. lionagi/api/__init__.py +0 -8
  83. lionagi/api/oai_config.py +0 -16
  84. lionagi/session/__init__.py +0 -7
  85. lionagi/session/session.py +0 -380
  86. lionagi/utils/doc_util.py +0 -331
  87. lionagi/utils/log_util.py +0 -86
  88. lionagi-0.0.111.dist-info/RECORD +0 -20
  89. {lionagi-0.0.111.dist-info → lionagi-0.0.113.dist-info}/LICENSE +0 -0
  90. {lionagi-0.0.111.dist-info → lionagi-0.0.113.dist-info}/WHEEL +0 -0
  91. {lionagi-0.0.111.dist-info → lionagi-0.0.113.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,347 @@
1
+ import asyncio
2
+ import time
3
+ from typing import Any, Callable, List, Optional, Union
4
+
5
+ from .sys_util import create_copy
6
+ from .type_util import to_list
7
+
8
+
9
+ def hcall(
10
+ input: Any, func: Callable, sleep: int = 0.1,
11
+ message: Optional[str] = None,
12
+ ignore_error: bool = False, **kwargs
13
+ ) -> Any:
14
+ """
15
+ Executes a function after a specified delay, handling exceptions optionally.
16
+
17
+ Waits for 'sleep' seconds before calling 'func' with 'input'. Handles exceptions by
18
+ printing a message and optionally re-raising them.
19
+
20
+ Parameters:
21
+ input (Any): Input to the function.
22
+
23
+ func (Callable): Function to execute.
24
+
25
+ sleep (int, optional): Time in seconds to wait before calling the function. Defaults to 0.1.
26
+
27
+ message (Optional[str], optional): Message to print on exception. Defaults to None.
28
+
29
+ ignore_error (bool, optional): If True, ignores exceptions. Defaults to False.
30
+
31
+ **kwargs: Additional keyword arguments for the function.
32
+
33
+ Returns:
34
+ Any: Result of the function call.
35
+
36
+ Raises:
37
+ Exception: Re-raises the exception unless 'ignore_error' is True.
38
+
39
+ Example:
40
+ >>> def add_one(x):
41
+ ... return x + 1
42
+ >>> hold_call(5, add_one, sleep=2)
43
+ 6
44
+ """
45
+ try:
46
+ time.sleep(sleep)
47
+ return func(input, **kwargs)
48
+ except Exception as e:
49
+ if message:
50
+ print(f"{message} Error: {e}")
51
+ else:
52
+ print(f"An error occurred: {e}")
53
+ if not ignore_error:
54
+ raise
55
+
56
+ async def ahcall(
57
+ input_: Any, func: Callable, sleep: int = 5,
58
+ message: Optional[str] = None,
59
+ ignore_error: bool = False, **kwargs
60
+ ) -> Any:
61
+ """
62
+ Asynchronously executes a function after a specified delay, handling exceptions optionally.
63
+
64
+ Waits for 'sleep' seconds before calling 'func' with 'input'. Handles exceptions by
65
+ printing a message and optionally re-raising them.
66
+
67
+ Parameters:
68
+ input (Any): Input to the function.
69
+
70
+ func (Callable): Asynchronous function to execute.
71
+
72
+ sleep (int, optional): Time in seconds to wait before calling the function. Defaults to 5.
73
+
74
+ message (Optional[str], optional): Message to print on exception. Defaults to None.
75
+
76
+ ignore_error (bool, optional): If True, ignores exceptions. Defaults to False.
77
+
78
+ **kwargs: Additional keyword arguments for the function.
79
+
80
+ Returns:
81
+ Any: Result of the asynchronous function call.
82
+
83
+ Raises:
84
+ Exception: Re-raises the exception unless 'ignore_error' is True.
85
+
86
+ Example:
87
+ >>> async def async_add_one(x):
88
+ ... return x + 1
89
+ >>> asyncio.run(ahold_call(5, async_add_one, sleep=2))
90
+ 6
91
+ """
92
+ try:
93
+ if not asyncio.iscoroutinefunction(func):
94
+ raise TypeError(f"The function {func} must be an asynchronous function.")
95
+ await asyncio.sleep(sleep)
96
+ return await func(input_, **kwargs)
97
+ except Exception as e:
98
+ if message:
99
+ print(f"{message} Error: {e}")
100
+ else:
101
+ print(f"An error occurred: {e}")
102
+ if not ignore_error:
103
+ raise
104
+
105
+ def lcall(
106
+ input_: Any, func_: Callable, flatten: bool = False,
107
+ dropna: bool = False, **kwargs
108
+ ) -> List[Any]:
109
+ """
110
+ Applies a function to each element of `input`, after converting it to a list.
111
+
112
+ This function converts the `input` to a list, with options to flatten structures
113
+ and lists, and then applies a given `func` to each element of the list.
114
+
115
+ Parameters:
116
+ input (Any): The input to be converted to a list and processed.
117
+
118
+ func (Callable): The function to apply to each element of the list.
119
+
120
+ flatten_dict (bool, optional): If True, flattens dictionaries in the input. Defaults to False.
121
+
122
+ flat (bool, optional): If True, flattens nested lists in the input. Defaults to False.
123
+
124
+ dropna (bool, optional): If True, drops None values during flattening. Defaults to True.
125
+
126
+ Returns:
127
+ List[Any]: A list containing the results of applying the `func` to each element.
128
+
129
+ Raises:
130
+ ValueError: If the `func` cannot be applied to the `input`.
131
+
132
+ Example:
133
+ >>> def square(x):
134
+ ... return x * x
135
+ >>> l_call([1, 2, 3], square)
136
+ [1, 4, 9]
137
+ """
138
+ try:
139
+ lst = to_list(input_=input_, flatten=flatten, dropna=dropna)
140
+ return [func_(i, **kwargs) for i in lst]
141
+ except Exception as e:
142
+ raise ValueError(f"Given function cannot be applied to the input. Error: {e}")
143
+
144
+ async def alcall(
145
+ input_: Any, func_: Callable, flatten: bool = False, dropna: bool = True, **kwargs
146
+ ) -> List[Any]:
147
+ """
148
+ Asynchronously applies a function to each element of `input`, after converting it to a list.
149
+
150
+ This function converts the `input` to a list, with options to flatten
151
+ dictionaries and lists, and then applies a given asynchronous `func` to
152
+ each element of the list asynchronously.
153
+
154
+ Parameters:
155
+ input (Any): The input to be converted to a list and processed.
156
+
157
+ func (Callable): The asynchronous function to apply to each element of the list.
158
+
159
+ flatten_dict (bool, optional): If True, flattens dictionaries in the input. Defaults to False.
160
+
161
+ flat (bool, optional): If True, flattens nested lists in the input. Defaults to False.
162
+
163
+ dropna (bool, optional): If True, drops None values during flattening. Defaults to True.
164
+
165
+ Returns:
166
+ List[Any]: A list containing the results of applying the `func` to each element.
167
+
168
+ Raises:
169
+ ValueError: If the `func` cannot be applied to the `input`.
170
+
171
+ Example:
172
+ >>> async def async_square(x):
173
+ ... return x * x
174
+ >>> asyncio.run(al_call([1, 2, 3], async_square))
175
+ [1, 4, 9]
176
+ """
177
+ try:
178
+ lst = to_list(input_=input_, flatten=flatten, dropna=dropna)
179
+ tasks = [func_(i, **kwargs) for i in lst]
180
+ return await asyncio.gather(*tasks)
181
+ except Exception as e:
182
+ raise ValueError(f"Given function cannot be applied to the input. Error: {e}")
183
+
184
+ def mcall(
185
+ input_: Union[Any, List[Any]], func_: Union[Callable, List[Callable]],
186
+ flatten: bool = True, dropna: bool = True, **kwargs
187
+ ) -> List[Any]:
188
+ """
189
+ Maps multiple functions to corresponding elements of the input.
190
+
191
+ This function applies a list of functions to a list of inputs, with each function
192
+ being applied to its corresponding input element. It asserts that the number of inputs
193
+ and functions are the same and raises an error if they are not.
194
+
195
+ Parameters:
196
+ input (Union[Any, List[Any]]): The input or list of inputs to be processed.
197
+
198
+ func (Union[Callable, List[Callable]]): The function or list of functions to apply.
199
+
200
+ flatten_dict (bool, optional): Whether to flatten dictionaries in the input. Defaults to False.
201
+
202
+ flat (bool, optional): Whether the output list should be flattened. Defaults to True.
203
+
204
+ dropna (bool, optional): Whether to drop None values during flattening. Defaults to True.
205
+
206
+ Returns:
207
+ List[Any]: A list containing the results from applying each function to its corresponding input.
208
+
209
+ Raises:
210
+ ValueError: If the number of provided inputs and functions are not the same.
211
+
212
+ Example:
213
+ >>> def add_one(x):
214
+ ... return x + 1
215
+ >>> m_call([1, 2], [add_one, add_one])
216
+ [2, 3]
217
+ """
218
+ input_ = to_list(input_=input_, flatten=flatten, dropna=dropna)
219
+ func_ = to_list(func_)
220
+ assert len(input_) == len(func_), "The number of inputs and functions must be the same."
221
+
222
+ return to_list(
223
+ [
224
+ lcall(input_=inp, func_=f, flatten=flatten, dropna=dropna, **kwargs)
225
+ for f, inp in zip(func_, input_)
226
+ ]
227
+ )
228
+
229
+ async def amcall(
230
+ input_: Union[Any, List[Any]], func_: Union[Callable, List[Callable]],
231
+ flatten: bool = True, dropna: bool = True, **kwargs
232
+ ) -> List[Any]:
233
+ """
234
+ Asynchronously applies multiple functions to corresponding elements of the input.
235
+
236
+ This asynchronous function maps a list of functions to a list of inputs, with each
237
+ function being applied to its corresponding input element asynchronously. It ensures
238
+ that the number of inputs and functions are the same, raising a `ValueError` if not.
239
+
240
+ Parameters:
241
+ input (Union[Any, List[Any]]): The input or list of inputs to be processed.
242
+
243
+ func (Union[Callable, List[Callable]]): The function or list of functions to apply.
244
+
245
+ flatten_dict (bool, optional): Whether to flatten dictionaries in the input. Defaults to False.
246
+
247
+ flat (bool, optional): Whether the output list should be flattened. Defaults to True.
248
+
249
+ dropna (bool, optional): Whether to drop None values during flattening. Defaults to True.
250
+
251
+ Returns:
252
+ List[Any]: A list containing the results from applying each function to its corresponding input.
253
+
254
+ Raises:
255
+ ValueError: If the number of inputs and functions do not match.
256
+
257
+ Example:
258
+ >>> async def async_add_one(x):
259
+ ... return x + 1
260
+ >>> asyncio.run(am_call([1, 2], [async_add_one, async_add_one]))
261
+ [2, 3]
262
+ """
263
+ input = to_list(input_=input_, flatten=flatten, dropna=dropna)
264
+ func_ = to_list(func_)
265
+ assert len(input) == len(func_), "Input and function counts must match."
266
+
267
+ tasks = [
268
+ alcall(input_=inp, func_=f, flatten=flatten, dropna=dropna, **kwargs)
269
+ for f, inp in zip(func_, input)
270
+ ]
271
+
272
+ out = await asyncio.gather(*tasks)
273
+ return to_list(out, flat=True)
274
+
275
+ def ecall(
276
+ input_: Union[Any, List[Any]], func_: Union[Callable, List[Callable]],
277
+ flatten: bool = True, dropna: bool = True, **kwargs
278
+ ) -> List[Any]:
279
+ """
280
+ Applies each function in a list of functions to all elements in the input.
281
+
282
+ This function expands the input to match the number of functions and then
283
+ applies each function to the entire input. It is useful for applying a series
284
+ of different transformations to the same input.
285
+
286
+ Parameters:
287
+ input (Union[Any, List[Any]]): The input or list of inputs to be processed.
288
+
289
+ func (Union[Callable, List[Callable]]): The function or list of functions to apply.
290
+
291
+ flatten_dict (bool, optional): Whether to flatten dictionaries in the input. Defaults to False.
292
+
293
+ flat (bool, optional): Whether the output list should be flattened. Defaults to True.
294
+
295
+ dropna (bool, optional): Whether to drop None values during flattening. Defaults to True.
296
+
297
+ Returns:
298
+ List[Any]: A list of results after applying each function to the input.
299
+
300
+ Example:
301
+ >>> def square(x):
302
+ ... return x**2
303
+ >>> e_call([1, 2, 3], [square])
304
+ [[1], [4], [9]]
305
+ """
306
+
307
+ _f = lambda x, y: mcall(
308
+ input_=create_copy(x, len(to_list(y))), func_=y,
309
+ flatten=False, dropna=dropna, **kwargs
310
+ )
311
+ return [_f(x=inp, y=func_) for inp in to_list(input_)]
312
+
313
+ async def aecall(
314
+ input_: Union[Any, List[Any]], func_: Union[Callable, List[Callable]],
315
+ dropna: bool = True, **kwargs
316
+ ) -> List[Any]:
317
+ """
318
+ Asynchronously applies each function in a list of functions to all elements in the input.
319
+
320
+ This asynchronous function expands the input to match the number of functions and
321
+ then asynchronously applies each function to the entire input. It is useful for applying a series
322
+ of different asynchronous transformations to the same input.
323
+
324
+ Parameters:
325
+ input_ (Union[Any, List[Any]]): The input or list of inputs to be processed.
326
+
327
+ func_ (Union[Callable, List[Callable]]): The function or list of functions to apply.
328
+
329
+ flatten_dict (bool, optional): Whether to flatten dictionaries in the input. Defaults to False.
330
+
331
+ flat (bool, optional): Whether the output list should be flattened. Defaults to True.
332
+
333
+ dropna (bool, optional): Whether to drop None values during flattening. Defaults to True.
334
+
335
+ Example:
336
+ >>> async def async_square(x):
337
+ ... return x**2
338
+ >>> asyncio.run(ae_call([1, 2, 3], [async_square]))
339
+ [[1, 4, 9]]
340
+ """
341
+ async def _async_f(x, y):
342
+ return await amcall(
343
+ create_copy(x, len(to_list(y))), y, flatten=False, dropna=dropna, **kwargs
344
+ )
345
+
346
+ tasks = [_async_f(inp, func_) for inp in to_list(input_)]
347
+ return await asyncio.gather(*tasks)