lionagi 0.0.112__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 (94) hide show
  1. lionagi/__init__.py +3 -3
  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 +8 -2
  9. lionagi/core/instruction_sets.py +1 -3
  10. lionagi/core/messages.py +2 -2
  11. lionagi/core/sessions.py +174 -27
  12. lionagi/datastore/__init__.py +1 -0
  13. lionagi/loader/__init__.py +9 -4
  14. lionagi/loader/chunker.py +157 -0
  15. lionagi/loader/reader.py +124 -0
  16. lionagi/objs/__init__.py +7 -0
  17. lionagi/objs/messenger.py +163 -0
  18. lionagi/objs/tool_registry.py +247 -0
  19. lionagi/schema/__init__.py +11 -0
  20. lionagi/schema/base_schema.py +239 -0
  21. lionagi/schema/base_tool.py +9 -0
  22. lionagi/schema/data_logger.py +94 -0
  23. lionagi/services/__init__.py +14 -0
  24. lionagi/{service_/oai.py → services/base_api_service.py} +49 -82
  25. lionagi/{endpoint/base_endpoint.py → services/chatcompletion.py} +19 -22
  26. lionagi/services/oai.py +34 -0
  27. lionagi/services/openrouter.py +32 -0
  28. lionagi/{service_/service_utils.py → services/service_objs.py} +0 -1
  29. lionagi/structure/__init__.py +7 -0
  30. lionagi/structure/relationship.py +128 -0
  31. lionagi/structure/structure.py +160 -0
  32. lionagi/tests/test_flatten_util.py +426 -0
  33. lionagi/tools/__init__.py +0 -5
  34. lionagi/tools/coder.py +1 -0
  35. lionagi/tools/scorer.py +1 -0
  36. lionagi/tools/validator.py +1 -0
  37. lionagi/utils/__init__.py +46 -20
  38. lionagi/utils/api_util.py +86 -0
  39. lionagi/utils/call_util.py +347 -0
  40. lionagi/utils/flat_util.py +540 -0
  41. lionagi/utils/io_util.py +102 -0
  42. lionagi/utils/load_utils.py +190 -0
  43. lionagi/utils/sys_util.py +191 -0
  44. lionagi/utils/tool_util.py +92 -0
  45. lionagi/utils/type_util.py +81 -0
  46. lionagi/version.py +1 -1
  47. {lionagi-0.0.112.dist-info → lionagi-0.0.113.dist-info}/METADATA +37 -13
  48. lionagi-0.0.113.dist-info/RECORD +84 -0
  49. lionagi/endpoint/chat_completion.py +0 -20
  50. lionagi/endpoint/endpoint_utils.py +0 -0
  51. lionagi/llm_configs.py +0 -21
  52. lionagi/loader/load_utils.py +0 -161
  53. lionagi/schema.py +0 -275
  54. lionagi/service_/__init__.py +0 -6
  55. lionagi/service_/base_service.py +0 -48
  56. lionagi/service_/openrouter.py +0 -1
  57. lionagi/services.py +0 -1
  58. lionagi/tools/tool_utils.py +0 -75
  59. lionagi/utils/sys_utils.py +0 -799
  60. lionagi-0.0.112.dist-info/RECORD +0 -67
  61. /lionagi/{core/responses.py → datastore/chroma.py} +0 -0
  62. /lionagi/{endpoint/assistants.py → datastore/deeplake.py} +0 -0
  63. /lionagi/{endpoint/audio.py → datastore/elasticsearch.py} +0 -0
  64. /lionagi/{endpoint/embeddings.py → datastore/lantern.py} +0 -0
  65. /lionagi/{endpoint/files.py → datastore/pinecone.py} +0 -0
  66. /lionagi/{endpoint/fine_tuning.py → datastore/postgres.py} +0 -0
  67. /lionagi/{endpoint/images.py → datastore/qdrant.py} +0 -0
  68. /lionagi/{endpoint/messages.py → schema/base_condition.py} +0 -0
  69. /lionagi/{service_ → services}/anthropic.py +0 -0
  70. /lionagi/{service_ → services}/anyscale.py +0 -0
  71. /lionagi/{service_ → services}/azure.py +0 -0
  72. /lionagi/{service_ → services}/bedrock.py +0 -0
  73. /lionagi/{service_ → services}/everlyai.py +0 -0
  74. /lionagi/{service_ → services}/gemini.py +0 -0
  75. /lionagi/{service_ → services}/gpt4all.py +0 -0
  76. /lionagi/{service_ → services}/huggingface.py +0 -0
  77. /lionagi/{service_ → services}/litellm.py +0 -0
  78. /lionagi/{service_ → services}/localai.py +0 -0
  79. /lionagi/{service_ → services}/mistralai.py +0 -0
  80. /lionagi/{service_ → services}/ollama.py +0 -0
  81. /lionagi/{service_ → services}/openllm.py +0 -0
  82. /lionagi/{service_ → services}/perplexity.py +0 -0
  83. /lionagi/{service_ → services}/predibase.py +0 -0
  84. /lionagi/{service_ → services}/rungpt.py +0 -0
  85. /lionagi/{service_ → services}/vllm.py +0 -0
  86. /lionagi/{service_ → services}/xinference.py +0 -0
  87. /lionagi/{endpoint → tests}/__init__.py +0 -0
  88. /lionagi/{endpoint/models.py → tools/planner.py} +0 -0
  89. /lionagi/{endpoint/moderations.py → tools/prompter.py} +0 -0
  90. /lionagi/{endpoint/runs.py → tools/sandbox.py} +0 -0
  91. /lionagi/{endpoint/threads.py → tools/summarizer.py} +0 -0
  92. {lionagi-0.0.112.dist-info → lionagi-0.0.113.dist-info}/LICENSE +0 -0
  93. {lionagi-0.0.112.dist-info → lionagi-0.0.113.dist-info}/WHEEL +0 -0
  94. {lionagi-0.0.112.dist-info → lionagi-0.0.113.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,86 @@
1
+ import logging
2
+ import re
3
+ from typing import Callable
4
+
5
+
6
+ def api_method(http_session, method: str = "post") -> Callable:
7
+ """
8
+ Retrieves the appropriate HTTP method from an HTTP session object.
9
+
10
+ Parameters:
11
+ http_session: The HTTP session object from which to retrieve the method.
12
+ method (str): The HTTP method to retrieve. Defaults to 'post'.
13
+
14
+ Returns:
15
+ Callable: The HTTP method function from the session object.
16
+
17
+ Raises:
18
+ ValueError: If the provided method is not one of ['post', 'delete', 'head', 'options', 'patch'].
19
+
20
+ Examples:
21
+ api_method = api_methods(session, "post") # Retrieves the 'post' method from the session
22
+ """
23
+ if method not in ["post", "delete", "head", "options", "patch"]:
24
+ raise ValueError("Invalid request, method must be in ['post', 'delete', 'head', 'options', 'patch']")
25
+ elif method == "post":
26
+ return http_session.post
27
+ elif method == "delete":
28
+ return http_session.delete
29
+ elif method == "head":
30
+ return http_session.head
31
+ elif method == "options":
32
+ return http_session.options
33
+ elif method == "patch":
34
+ return http_session.patch
35
+
36
+ def api_endpoint_from_url(request_url: str) -> str:
37
+ """
38
+ Extracts the API endpoint from a given URL.
39
+
40
+ Parameters:
41
+ request_url (str): The URL from which to extract the API endpoint.
42
+
43
+ Returns:
44
+ str: The extracted API endpoint, or an empty string if no match is found.
45
+
46
+ Examples:
47
+ endpoint = api_endpoint_from_url("https://api.example.com/v1/users")
48
+ # endpoint will be 'users'
49
+ """
50
+ match = re.search(r"^https://[^/]+/v\d+/(.+)$", request_url)
51
+ return match.group(1) if match else ""
52
+
53
+ def api_error(response_json: dict) -> bool:
54
+ """
55
+ Logs a warning and returns True if an error is found in the API response.
56
+
57
+ Parameters:
58
+ response_json (dict): The JSON response from the API call.
59
+
60
+ Returns:
61
+ bool: True if an error is present in the response, False otherwise.
62
+
63
+ Examples:
64
+ if api_error(response):
65
+ # Handle the error
66
+ """
67
+ if "error" in response_json:
68
+ logging.warning(f"API call failed with error: {response_json['error']}")
69
+ return True
70
+ return False
71
+
72
+ def api_rate_limit_error(response_json: dict) -> bool:
73
+ """
74
+ Checks if the API response indicates a rate limit error.
75
+
76
+ Parameters:
77
+ response_json (dict): The JSON response from the API call.
78
+
79
+ Returns:
80
+ bool: True if the response contains a rate limit error message, False otherwise.
81
+
82
+ Examples:
83
+ if rate_limit_error(response):
84
+ # Handle the rate limit error
85
+ """
86
+ return "Rate limit" in response_json["error"].get("message", "")
@@ -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)