lionagi 0.0.115__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.
- lionagi/__init__.py +1 -2
- lionagi/_services/__init__.py +5 -0
- lionagi/_services/anthropic.py +79 -0
- lionagi/_services/base_service.py +414 -0
- lionagi/_services/oai.py +98 -0
- lionagi/_services/openrouter.py +44 -0
- lionagi/_services/services.py +91 -0
- lionagi/_services/transformers.py +46 -0
- lionagi/bridge/langchain.py +26 -16
- lionagi/bridge/llama_index.py +50 -20
- lionagi/configs/oai_configs.py +2 -14
- lionagi/configs/openrouter_configs.py +2 -2
- lionagi/core/__init__.py +7 -8
- lionagi/core/branch/branch.py +589 -0
- lionagi/core/branch/branch_manager.py +139 -0
- lionagi/core/branch/conversation.py +484 -0
- lionagi/core/core_util.py +59 -0
- lionagi/core/flow/flow.py +19 -0
- lionagi/core/flow/flow_util.py +62 -0
- lionagi/core/instruction_set/__init__.py +0 -5
- lionagi/core/instruction_set/instruction_set.py +343 -0
- lionagi/core/messages/messages.py +176 -0
- lionagi/core/sessions/__init__.py +0 -5
- lionagi/core/sessions/session.py +428 -0
- lionagi/loaders/chunker.py +51 -47
- lionagi/loaders/load_util.py +2 -2
- lionagi/loaders/reader.py +45 -39
- lionagi/models/imodel.py +53 -0
- lionagi/schema/async_queue.py +158 -0
- lionagi/schema/base_node.py +318 -147
- lionagi/schema/base_tool.py +31 -1
- lionagi/schema/data_logger.py +74 -38
- lionagi/schema/data_node.py +57 -6
- lionagi/structures/graph.py +132 -10
- lionagi/structures/relationship.py +58 -20
- lionagi/structures/structure.py +36 -25
- lionagi/tests/test_utils/test_api_util.py +219 -0
- lionagi/tests/test_utils/test_call_util.py +785 -0
- lionagi/tests/test_utils/test_encrypt_util.py +323 -0
- lionagi/tests/test_utils/test_io_util.py +238 -0
- lionagi/tests/test_utils/test_nested_util.py +338 -0
- lionagi/tests/test_utils/test_sys_util.py +358 -0
- lionagi/tools/tool_manager.py +186 -0
- lionagi/tools/tool_util.py +266 -3
- lionagi/utils/__init__.py +21 -61
- lionagi/utils/api_util.py +359 -71
- lionagi/utils/call_util.py +839 -264
- lionagi/utils/encrypt_util.py +283 -16
- lionagi/utils/io_util.py +178 -93
- lionagi/utils/nested_util.py +672 -0
- lionagi/utils/pd_util.py +57 -0
- lionagi/utils/sys_util.py +284 -156
- lionagi/utils/url_util.py +55 -0
- lionagi/version.py +1 -1
- {lionagi-0.0.115.dist-info → lionagi-0.0.204.dist-info}/METADATA +21 -17
- lionagi-0.0.204.dist-info/RECORD +106 -0
- lionagi/core/conversations/__init__.py +0 -5
- lionagi/core/conversations/conversation.py +0 -107
- lionagi/core/flows/__init__.py +0 -8
- lionagi/core/flows/flow.py +0 -8
- lionagi/core/flows/flow_util.py +0 -62
- lionagi/core/instruction_set/instruction_sets.py +0 -7
- lionagi/core/sessions/sessions.py +0 -185
- lionagi/endpoints/__init__.py +0 -5
- lionagi/endpoints/audio.py +0 -17
- lionagi/endpoints/chatcompletion.py +0 -54
- lionagi/messages/__init__.py +0 -11
- lionagi/messages/instruction.py +0 -15
- lionagi/messages/message.py +0 -110
- lionagi/messages/response.py +0 -33
- lionagi/messages/system.py +0 -12
- lionagi/objs/__init__.py +0 -11
- lionagi/objs/abc_objs.py +0 -39
- lionagi/objs/async_queue.py +0 -135
- lionagi/objs/messenger.py +0 -85
- lionagi/objs/tool_manager.py +0 -253
- lionagi/services/__init__.py +0 -11
- lionagi/services/base_api_service.py +0 -230
- lionagi/services/oai.py +0 -34
- lionagi/services/openrouter.py +0 -31
- lionagi/tests/test_api_util.py +0 -46
- lionagi/tests/test_call_util.py +0 -115
- lionagi/tests/test_convert_util.py +0 -202
- lionagi/tests/test_encrypt_util.py +0 -33
- lionagi/tests/test_flat_util.py +0 -426
- lionagi/tests/test_sys_util.py +0 -0
- lionagi/utils/convert_util.py +0 -229
- lionagi/utils/flat_util.py +0 -599
- lionagi-0.0.115.dist-info/RECORD +0 -110
- /lionagi/{services → _services}/anyscale.py +0 -0
- /lionagi/{services → _services}/azure.py +0 -0
- /lionagi/{services → _services}/bedrock.py +0 -0
- /lionagi/{services → _services}/everlyai.py +0 -0
- /lionagi/{services → _services}/gemini.py +0 -0
- /lionagi/{services → _services}/gpt4all.py +0 -0
- /lionagi/{services → _services}/huggingface.py +0 -0
- /lionagi/{services → _services}/litellm.py +0 -0
- /lionagi/{services → _services}/localai.py +0 -0
- /lionagi/{services → _services}/mistralai.py +0 -0
- /lionagi/{services → _services}/ollama.py +0 -0
- /lionagi/{services → _services}/openllm.py +0 -0
- /lionagi/{services → _services}/perplexity.py +0 -0
- /lionagi/{services → _services}/predibase.py +0 -0
- /lionagi/{services → _services}/rungpt.py +0 -0
- /lionagi/{services → _services}/vllm.py +0 -0
- /lionagi/{services → _services}/xinference.py +0 -0
- /lionagi/{endpoints/assistants.py → agents/__init__.py} +0 -0
- /lionagi/{tools → agents}/planner.py +0 -0
- /lionagi/{tools → agents}/prompter.py +0 -0
- /lionagi/{tools → agents}/scorer.py +0 -0
- /lionagi/{tools → agents}/summarizer.py +0 -0
- /lionagi/{tools → agents}/validator.py +0 -0
- /lionagi/{endpoints/embeddings.py → core/branch/__init__.py} +0 -0
- /lionagi/{services/anthropic.py → core/branch/cluster.py} +0 -0
- /lionagi/{endpoints/finetune.py → core/flow/__init__.py} +0 -0
- /lionagi/{endpoints/image.py → core/messages/__init__.py} +0 -0
- /lionagi/{endpoints/moderation.py → models/__init__.py} +0 -0
- /lionagi/{endpoints/vision.py → models/base_model.py} +0 -0
- /lionagi/{objs → schema}/status_tracker.py +0 -0
- /lionagi/tests/{test_io_util.py → test_utils/__init__.py} +0 -0
- {lionagi-0.0.115.dist-info → lionagi-0.0.204.dist-info}/LICENSE +0 -0
- {lionagi-0.0.115.dist-info → lionagi-0.0.204.dist-info}/WHEEL +0 -0
- {lionagi-0.0.115.dist-info → lionagi-0.0.204.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,219 @@
|
|
1
|
+
# import unittest
|
2
|
+
# import os
|
3
|
+
# import aiohttp
|
4
|
+
# import json
|
5
|
+
# import hashlib
|
6
|
+
# from aioresponses import aioresponses
|
7
|
+
# from lionagi.utils.api_util import APIUtil
|
8
|
+
|
9
|
+
|
10
|
+
# class TestAPIUtil(unittest.IsolatedAsyncioTestCase):
|
11
|
+
|
12
|
+
# async def asyncSetUp(self):
|
13
|
+
# self.session = aiohttp.ClientSession()
|
14
|
+
# self.file_path = "test_file.txt"
|
15
|
+
|
16
|
+
# with open(self.file_path, 'w') as f:
|
17
|
+
# f.write("Test data")
|
18
|
+
|
19
|
+
# async def asyncTearDown(self):
|
20
|
+
# await self.session.close()
|
21
|
+
# os.remove(self.file_path)
|
22
|
+
|
23
|
+
# def test_api_method(self):
|
24
|
+
# for method in ["post", "delete", "head", "options", "patch"]:
|
25
|
+
# with self.subTest(method=method):
|
26
|
+
# http_method = APIUtil.api_method(self.session, method)
|
27
|
+
# self.assertTrue(callable(http_method))
|
28
|
+
# with self.assertRaises(ValueError):
|
29
|
+
# APIUtil.api_method(self.session, "invalid_method")
|
30
|
+
|
31
|
+
# def test_api_error(self):
|
32
|
+
# response_json_with_error = {"error": "Something went wrong"}
|
33
|
+
# self.assertTrue(APIUtil.api_error(response_json_with_error))
|
34
|
+
|
35
|
+
# response_json_without_error = {"result": "Success"}
|
36
|
+
# self.assertFalse(APIUtil.api_error(response_json_without_error))
|
37
|
+
|
38
|
+
# def test_api_rate_limit_error(self):
|
39
|
+
# response_json_with_rate_limit = {"error": {"message": "Rate limit exceeded"}}
|
40
|
+
# self.assertTrue(APIUtil.api_rate_limit_error(response_json_with_rate_limit))
|
41
|
+
|
42
|
+
# response_json_without_rate_limit = {"error": {"message": "Another error"}}
|
43
|
+
# self.assertFalse(APIUtil.api_rate_limit_error(response_json_without_rate_limit))
|
44
|
+
|
45
|
+
# def test_api_endpoint_from_url(self):
|
46
|
+
# # Test with valid URL
|
47
|
+
# valid_url = "https://api.example.com/v1/users"
|
48
|
+
# self.assertEqual(APIUtil.api_endpoint_from_url(valid_url), 'users')
|
49
|
+
|
50
|
+
# # Test with another valid URL
|
51
|
+
# another_valid_url = "https://api.example.com/v1/products"
|
52
|
+
# self.assertEqual(APIUtil.api_endpoint_from_url(another_valid_url), 'products')
|
53
|
+
|
54
|
+
# # Test with invalid URL (no version)
|
55
|
+
# invalid_url_no_version = "https://api.example.com/users"
|
56
|
+
# self.assertEqual(APIUtil.api_endpoint_from_url(invalid_url_no_version), '')
|
57
|
+
|
58
|
+
# # Test with invalid URL (different format)
|
59
|
+
# invalid_url_different_format = "https://example.com/api/users"
|
60
|
+
# self.assertEqual(APIUtil.api_endpoint_from_url(invalid_url_different_format), '')
|
61
|
+
|
62
|
+
# # Test with completely incorrect URL format
|
63
|
+
# completely_invalid_url = "JustSomeRandomString"
|
64
|
+
# self.assertEqual(APIUtil.api_endpoint_from_url(completely_invalid_url), '')
|
65
|
+
|
66
|
+
# async def test_unified_api_call(self):
|
67
|
+
# url = "https://api.example.com/v1/test"
|
68
|
+
# success_response = {'result': 'Success'}
|
69
|
+
# rate_limit_response = {'error': {'message': 'Rate limit exceeded'}}
|
70
|
+
|
71
|
+
# with aioresponses() as m:
|
72
|
+
# # Mock a rate limit error followed by a successful response
|
73
|
+
# m.post(url, status=429, payload=rate_limit_response)
|
74
|
+
# m.post(url, status=200, payload=success_response)
|
75
|
+
|
76
|
+
# response = await APIUtil.unified_api_call(self.session, 'post', url)
|
77
|
+
# self.assertEqual(response, success_response)
|
78
|
+
|
79
|
+
# # Ensure that the first call was a rate limit error
|
80
|
+
# self.assertTrue(APIUtil.api_rate_limit_error(rate_limit_response))
|
81
|
+
|
82
|
+
# async def test_unified_api_call_with_all_rate_limited(self):
|
83
|
+
# url = "https://api.example.com/v1/test"
|
84
|
+
# rate_limit_response = {'error': {'message': 'Rate limit exceeded'}}
|
85
|
+
|
86
|
+
# with aioresponses() as m:
|
87
|
+
# # Mock three rate limit errors in a row
|
88
|
+
# for _ in range(3):
|
89
|
+
# m.post(url, status=429, payload=rate_limit_response)
|
90
|
+
|
91
|
+
# response = await APIUtil.unified_api_call(self.session, 'post', url)
|
92
|
+
# self.assertTrue(APIUtil.api_rate_limit_error(response))
|
93
|
+
|
94
|
+
# def test_get_cache_key(self):
|
95
|
+
# url = "https://api.example.com/v1/test"
|
96
|
+
# params = {"param1": "value1", "param2": "value2"}
|
97
|
+
|
98
|
+
# # Test cache key generation with parameters
|
99
|
+
# key_with_params = APIUtil.get_cache_key(url, params)
|
100
|
+
# expected_key_with_params = hashlib.md5((url + json.dumps(params, sort_keys=True)).encode('utf-8')).hexdigest()
|
101
|
+
# self.assertEqual(key_with_params, expected_key_with_params)
|
102
|
+
|
103
|
+
# # Test cache key generation without parameters
|
104
|
+
# key_without_params = APIUtil.get_cache_key(url, None)
|
105
|
+
# expected_key_without_params = hashlib.md5(url.encode('utf-8')).hexdigest()
|
106
|
+
# self.assertEqual(key_without_params, expected_key_without_params)
|
107
|
+
|
108
|
+
# # Test cache key generation with empty parameters
|
109
|
+
# key_with_empty_params = APIUtil.get_cache_key(url, {})
|
110
|
+
# self.assertEqual(key_with_empty_params, expected_key_without_params)
|
111
|
+
|
112
|
+
# # Test cache key consistency
|
113
|
+
# self.assertEqual(APIUtil.get_cache_key(url, params), key_with_params)
|
114
|
+
|
115
|
+
# async def test_retry_api_call_success(self):
|
116
|
+
# url = "https://api.example.com/v1/test"
|
117
|
+
# success_response = {'result': 'Success'}
|
118
|
+
|
119
|
+
# with aioresponses() as m:
|
120
|
+
# m.get(url, status=200, payload=success_response)
|
121
|
+
|
122
|
+
# response = await APIUtil.retry_api_call(self.session, url)
|
123
|
+
# self.assertEqual(response, success_response)
|
124
|
+
|
125
|
+
# async def test_retry_api_call_with_failures(self):
|
126
|
+
# url = "https://api.example.com/v1/test"
|
127
|
+
# failure_response = {'error': 'Temporary failure'}
|
128
|
+
# success_response = {'result': 'Success'}
|
129
|
+
|
130
|
+
# with aioresponses() as m:
|
131
|
+
# # First two calls fail, third call succeeds
|
132
|
+
# m.get(url, status=500, payload=failure_response)
|
133
|
+
# m.get(url, status=500, payload=failure_response)
|
134
|
+
# m.get(url, status=200, payload=success_response)
|
135
|
+
|
136
|
+
# response = await APIUtil.retry_api_call(self.session, url, retries=3)
|
137
|
+
# self.assertEqual(response, success_response)
|
138
|
+
|
139
|
+
# async def test_retry_api_call_all_failures(self):
|
140
|
+
# url = "https://api.example.com/v1/test"
|
141
|
+
# failure_response = {'error': 'Temporary failure'}
|
142
|
+
|
143
|
+
# with aioresponses() as m:
|
144
|
+
# # All calls fail
|
145
|
+
# m.get(url, status=500, payload=failure_response, repeat=3)
|
146
|
+
|
147
|
+
# response = await APIUtil.retry_api_call(self.session, url, retries=3)
|
148
|
+
# self.assertIsNone(response)
|
149
|
+
|
150
|
+
# async def test_upload_file_with_retry_success(self):
|
151
|
+
# url = "https://api.example.com/v1/upload"
|
152
|
+
# success_response = {'result': 'File uploaded successfully'}
|
153
|
+
|
154
|
+
# with aioresponses() as m:
|
155
|
+
# m.post(url, status=200, payload=success_response)
|
156
|
+
|
157
|
+
# response = await APIUtil.upload_file_with_retry(self.session, url, self.file_path)
|
158
|
+
# self.assertEqual(response, success_response)
|
159
|
+
|
160
|
+
# async def test_upload_file_with_retry_failures(self):
|
161
|
+
# url = "https://api.example.com/v1/upload"
|
162
|
+
# failure_response = {'error': 'Temporary failure'}
|
163
|
+
|
164
|
+
# with aioresponses() as m:
|
165
|
+
# # First two attempts fail, third attempt succeeds
|
166
|
+
# m.post(url, status=500, payload=failure_response)
|
167
|
+
# m.post(url, status=500, payload=failure_response)
|
168
|
+
# m.post(url, status=200, payload={'result': 'File uploaded successfully'})
|
169
|
+
|
170
|
+
# response = await APIUtil.upload_file_with_retry(self.session, url, self.file_path, retries=3)
|
171
|
+
# self.assertIsNotNone(response)
|
172
|
+
# self.assertEqual(response['result'], 'File uploaded successfully')
|
173
|
+
|
174
|
+
# async def test_get_oauth_token_with_cache_success(self):
|
175
|
+
# auth_url = "https://auth.example.com/token"
|
176
|
+
# client_id = "client_id"
|
177
|
+
# client_secret = "client_secret"
|
178
|
+
# scope = "read"
|
179
|
+
# token_response = {'access_token': 'mock_access_token'}
|
180
|
+
|
181
|
+
# with aioresponses() as m:
|
182
|
+
# m.post(auth_url, status=200, payload=token_response)
|
183
|
+
|
184
|
+
# # Call the method to retrieve the token
|
185
|
+
# token = await APIUtil.get_oauth_token_with_cache(self.session, auth_url, client_id, client_secret, scope)
|
186
|
+
# self.assertEqual(token, 'mock_access_token')
|
187
|
+
|
188
|
+
# # Call the method again - expecting to use cached value
|
189
|
+
# token_cached = await APIUtil.get_oauth_token_with_cache(self.session, auth_url, client_id, client_secret,
|
190
|
+
# scope)
|
191
|
+
# self.assertEqual(token_cached, 'mock_access_token')
|
192
|
+
|
193
|
+
# async def test_cached_api_call_success(self):
|
194
|
+
# url = "https://api.example.com/v1/data"
|
195
|
+
# response_data = {'result': 'Success'}
|
196
|
+
|
197
|
+
# with aioresponses() as m:
|
198
|
+
# m.get(url, status=200, payload=response_data)
|
199
|
+
|
200
|
+
# # Call the method to retrieve data
|
201
|
+
# response = await APIUtil.cached_api_call(self.session, url)
|
202
|
+
# self.assertEqual(response, response_data)
|
203
|
+
|
204
|
+
# # Call the method again - expecting the same result (ideally from cache, but not explicitly tested here)
|
205
|
+
# response_cached = await APIUtil.cached_api_call(self.session, url)
|
206
|
+
# self.assertEqual(response_cached, response_data)
|
207
|
+
|
208
|
+
# async def test_cached_api_call_failure(self):
|
209
|
+
# url = "https://api.example.com/v1/data"
|
210
|
+
|
211
|
+
# with aioresponses() as m:
|
212
|
+
# m.get(url, status=500)
|
213
|
+
|
214
|
+
# # Call the method - expecting a failure
|
215
|
+
# response = await APIUtil.cached_api_call(self.session, url)
|
216
|
+
# self.assertIsNone(response)
|
217
|
+
|
218
|
+
# if __name__ == '__main__':
|
219
|
+
# unittest.main()
|