lionagi 0.0.107__tar.gz → 0.0.111__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- {lionagi-0.0.107 → lionagi-0.0.111}/PKG-INFO +1 -1
- {lionagi-0.0.107 → lionagi-0.0.111}/lionagi/api/__init__.py +2 -1
- {lionagi-0.0.107 → lionagi-0.0.111}/lionagi/api/oai_service.py +61 -24
- lionagi-0.0.111/lionagi/session/__init__.py +7 -0
- {lionagi-0.0.107 → lionagi-0.0.111}/lionagi/session/session.py +8 -4
- lionagi-0.0.111/lionagi/utils/__init__.py +11 -0
- {lionagi-0.0.107 → lionagi-0.0.111}/lionagi/utils/api_util.py +4 -4
- lionagi-0.0.111/lionagi/version.py +1 -0
- {lionagi-0.0.107 → lionagi-0.0.111}/lionagi.egg-info/PKG-INFO +1 -1
- lionagi-0.0.107/lionagi/session/__init__.py +0 -5
- lionagi-0.0.107/lionagi/utils/__init__.py +0 -7
- lionagi-0.0.107/lionagi/version.py +0 -1
- {lionagi-0.0.107 → lionagi-0.0.111}/LICENSE +0 -0
- {lionagi-0.0.107 → lionagi-0.0.111}/README.md +0 -0
- {lionagi-0.0.107 → lionagi-0.0.111}/README.rst +0 -0
- {lionagi-0.0.107 → lionagi-0.0.111}/lionagi/__init__.py +0 -0
- {lionagi-0.0.107 → lionagi-0.0.111}/lionagi/api/oai_config.py +0 -0
- {lionagi-0.0.107 → lionagi-0.0.111}/lionagi/session/conversation.py +0 -0
- {lionagi-0.0.107 → lionagi-0.0.111}/lionagi/session/message.py +0 -0
- {lionagi-0.0.107 → lionagi-0.0.111}/lionagi/utils/doc_util.py +0 -0
- {lionagi-0.0.107 → lionagi-0.0.111}/lionagi/utils/log_util.py +0 -0
- {lionagi-0.0.107 → lionagi-0.0.111}/lionagi/utils/sys_util.py +0 -0
- {lionagi-0.0.107 → lionagi-0.0.111}/lionagi/utils/tool_util.py +0 -0
- {lionagi-0.0.107 → lionagi-0.0.111}/lionagi.egg-info/SOURCES.txt +0 -0
- {lionagi-0.0.107 → lionagi-0.0.111}/lionagi.egg-info/dependency_links.txt +0 -0
- {lionagi-0.0.107 → lionagi-0.0.111}/lionagi.egg-info/requires.txt +0 -0
- {lionagi-0.0.107 → lionagi-0.0.111}/lionagi.egg-info/top_level.txt +0 -0
- {lionagi-0.0.107 → lionagi-0.0.111}/pyproject.toml +0 -0
- {lionagi-0.0.107 → lionagi-0.0.111}/setup.cfg +0 -0
- {lionagi-0.0.107 → lionagi-0.0.111}/setup.py +0 -0
@@ -31,7 +31,9 @@ class OpenAIRateLimiter(RateLimiter):
|
|
31
31
|
Calculates the required tokens for a request.
|
32
32
|
"""
|
33
33
|
|
34
|
-
def __init__(
|
34
|
+
def __init__(
|
35
|
+
self, max_requests_per_minute: int, max_tokens_per_minute: int
|
36
|
+
) -> None:
|
35
37
|
"""
|
36
38
|
Initializes the rate limiter with specific limits for OpenAI API.
|
37
39
|
|
@@ -41,9 +43,18 @@ class OpenAIRateLimiter(RateLimiter):
|
|
41
43
|
max_tokens_per_minute (int): The maximum number of tokens that can accumulate per minute.
|
42
44
|
"""
|
43
45
|
super().__init__(max_requests_per_minute, max_tokens_per_minute)
|
44
|
-
|
45
|
-
|
46
|
-
|
46
|
+
|
47
|
+
@classmethod
|
48
|
+
async def create(
|
49
|
+
cls, max_requests_per_minute: int, max_tokens_per_minute: int
|
50
|
+
) -> None:
|
51
|
+
self = cls(max_requests_per_minute, max_tokens_per_minute)
|
52
|
+
if not os.getenv("env_readthedocs"):
|
53
|
+
self.rate_limit_replenisher_task = await asyncio.create_task(
|
54
|
+
self.rate_limit_replenisher()
|
55
|
+
)
|
56
|
+
return self
|
57
|
+
|
47
58
|
async def rate_limit_replenisher(self) -> NoReturn:
|
48
59
|
"""
|
49
60
|
Asynchronously replenishes the rate limit capacities at regular intervals.
|
@@ -61,9 +72,13 @@ class OpenAIRateLimiter(RateLimiter):
|
|
61
72
|
await asyncio.sleep(60) # Replenishes every 60 seconds
|
62
73
|
self.available_request_capacity = self.max_requests_per_minute
|
63
74
|
self.available_token_capacity = self.max_tokens_per_minute
|
64
|
-
|
65
|
-
def calculate_num_token(
|
66
|
-
|
75
|
+
|
76
|
+
def calculate_num_token(
|
77
|
+
self,
|
78
|
+
payload: Dict[str, Any] = None,
|
79
|
+
api_endpoint: str = None,
|
80
|
+
token_encoding_name: str = None,
|
81
|
+
) -> int:
|
67
82
|
"""
|
68
83
|
Calculates the number of tokens required for a request based on the payload and API endpoint.
|
69
84
|
|
@@ -76,6 +91,8 @@ class OpenAIRateLimiter(RateLimiter):
|
|
76
91
|
|
77
92
|
api_endpoint (str): The specific API endpoint for the request.
|
78
93
|
|
94
|
+
token_encoding_name (str): The name of the token encoding method.
|
95
|
+
|
79
96
|
Returns:
|
80
97
|
int: The estimated number of tokens required for the request.
|
81
98
|
|
@@ -100,7 +117,9 @@ class OpenAIRateLimiter(RateLimiter):
|
|
100
117
|
for key, value in message.items():
|
101
118
|
num_tokens += len(encoding.encode(value))
|
102
119
|
if key == "name": # if there's a name, the role is omitted
|
103
|
-
num_tokens -=
|
120
|
+
num_tokens -= (
|
121
|
+
1 # role is always required and always 1 token
|
122
|
+
)
|
104
123
|
num_tokens += 2 # every reply is primed with <im_start>assistant
|
105
124
|
return num_tokens + completion_tokens
|
106
125
|
# normal completions
|
@@ -134,7 +153,7 @@ class OpenAIRateLimiter(RateLimiter):
|
|
134
153
|
raise NotImplementedError(
|
135
154
|
f'API endpoint "{api_endpoint}" not implemented in this script'
|
136
155
|
)
|
137
|
-
|
156
|
+
|
138
157
|
|
139
158
|
class OpenAIService(BaseAPIService):
|
140
159
|
"""
|
@@ -159,9 +178,9 @@ class OpenAIService(BaseAPIService):
|
|
159
178
|
max_attempts: int = 3,
|
160
179
|
max_requests_per_minute: int = 500,
|
161
180
|
max_tokens_per_minute: int = 150_000,
|
162
|
-
ratelimiter
|
181
|
+
ratelimiter=OpenAIRateLimiter,
|
163
182
|
status_tracker: Optional[StatusTracker] = None,
|
164
|
-
queue: Optional[AsyncQueue] = None
|
183
|
+
queue: Optional[AsyncQueue] = None,
|
165
184
|
):
|
166
185
|
"""
|
167
186
|
Initializes the OpenAI service with configuration for API interaction.
|
@@ -191,11 +210,20 @@ class OpenAIService(BaseAPIService):
|
|
191
210
|
# Service is configured for interacting with OpenAI API.
|
192
211
|
"""
|
193
212
|
api_key = api_key or os.getenv("OPENAI_API_KEY")
|
194
|
-
super().__init__(
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
213
|
+
super().__init__(
|
214
|
+
api_key,
|
215
|
+
token_encoding_name,
|
216
|
+
max_attempts,
|
217
|
+
max_requests_per_minute,
|
218
|
+
max_tokens_per_minute,
|
219
|
+
ratelimiter,
|
220
|
+
status_tracker,
|
221
|
+
queue,
|
222
|
+
)
|
223
|
+
|
224
|
+
async def call_api(
|
225
|
+
self, http_session, endpoint, payload: Dict[str, any] = None
|
226
|
+
) -> Optional[Dict[str, any]]:
|
199
227
|
"""
|
200
228
|
Call an OpenAI API endpoint with a specific payload and handle the response.
|
201
229
|
|
@@ -221,15 +249,20 @@ class OpenAIService(BaseAPIService):
|
|
221
249
|
... )
|
222
250
|
# Calls the specified API endpoint with the given payload.
|
223
251
|
"""
|
224
|
-
endpoint = self.api_endpoint_from_url(self.base_url+endpoint)
|
225
|
-
|
252
|
+
endpoint = self.api_endpoint_from_url(self.base_url + endpoint)
|
253
|
+
|
226
254
|
while True:
|
227
|
-
if
|
255
|
+
if (
|
256
|
+
self.rate_limiter.available_request_capacity < 1
|
257
|
+
or self.rate_limiter.available_token_capacity < 10
|
258
|
+
): # Minimum token count
|
228
259
|
await asyncio.sleep(1) # Wait for capacity
|
229
260
|
continue
|
230
|
-
|
231
|
-
required_tokens = self.rate_limiter.calculate_num_token(
|
232
|
-
|
261
|
+
|
262
|
+
required_tokens = self.rate_limiter.calculate_num_token(
|
263
|
+
payload, endpoint, self.token_encoding_name
|
264
|
+
)
|
265
|
+
|
233
266
|
if self.rate_limiter.available_token_capacity >= required_tokens:
|
234
267
|
self.rate_limiter.available_request_capacity -= 1
|
235
268
|
self.rate_limiter.available_token_capacity -= required_tokens
|
@@ -240,7 +273,9 @@ class OpenAIService(BaseAPIService):
|
|
240
273
|
while attempts_left > 0:
|
241
274
|
try:
|
242
275
|
async with http_session.post(
|
243
|
-
url=(self.base_url+endpoint),
|
276
|
+
url=(self.base_url + endpoint),
|
277
|
+
headers=request_headers,
|
278
|
+
json=payload,
|
244
279
|
) as response:
|
245
280
|
response_json = await response.json()
|
246
281
|
|
@@ -250,7 +285,9 @@ class OpenAIService(BaseAPIService):
|
|
250
285
|
)
|
251
286
|
attempts_left -= 1
|
252
287
|
|
253
|
-
if "Rate limit" in response_json["error"].get(
|
288
|
+
if "Rate limit" in response_json["error"].get(
|
289
|
+
"message", ""
|
290
|
+
):
|
254
291
|
await asyncio.sleep(15)
|
255
292
|
else:
|
256
293
|
return response_json
|
@@ -1,9 +1,10 @@
|
|
1
|
+
import os
|
1
2
|
import aiohttp
|
2
3
|
import asyncio
|
3
4
|
import json
|
4
5
|
from typing import Any
|
5
6
|
|
6
|
-
|
7
|
+
|
7
8
|
from .conversation import Conversation
|
8
9
|
from ..utils.sys_util import to_list, l_call, al_call
|
9
10
|
from ..utils.log_util import DataLogger
|
@@ -13,7 +14,7 @@ from ..api.oai_service import OpenAIService
|
|
13
14
|
from ..api.oai_config import oai_llmconfig
|
14
15
|
|
15
16
|
status_tracker = StatusTracker()
|
16
|
-
OAIService = OpenAIService()
|
17
|
+
OAIService = OpenAIService(api_key=os.getenv('OPENAI_API_KEY'))
|
17
18
|
|
18
19
|
|
19
20
|
class Session():
|
@@ -52,6 +53,9 @@ class Session():
|
|
52
53
|
_output(output, invoke=True, out=True) -> Any:
|
53
54
|
Process the output, invoke tools if needed, and optionally return the output.
|
54
55
|
|
56
|
+
_is_invoked():
|
57
|
+
Checks if the current message indicates the invocation of a function call.
|
58
|
+
|
55
59
|
register_tools(tools, funcs, update=False, new=False, prefix=None, postfix=None):
|
56
60
|
Register tools and their corresponding functions.
|
57
61
|
|
@@ -64,10 +68,10 @@ class Session():
|
|
64
68
|
auto_followup(self, instruct, num=3, tool_parser=None, **kwags):
|
65
69
|
Automates the follow-up process for a specified number of times or until the session concludes.
|
66
70
|
|
67
|
-
|
71
|
+
_create_payload_chatcompletion(**kwargs) -> dict:
|
68
72
|
Create a payload for chat completion based on the conversation state and configuration.
|
69
73
|
|
70
|
-
|
74
|
+
_call_chatcompletion(sleep=0.1, **kwargs) -> None:
|
71
75
|
Make a call to the chat completion API and process the response.
|
72
76
|
|
73
77
|
messages_to_csv(dir=None, filename="_messages.csv", **kwargs) -> None:
|
@@ -0,0 +1,11 @@
|
|
1
|
+
from .sys_util import to_flat_dict, to_list, str_to_num, make_copy, to_temp, to_csv, hold_call, ahold_call, l_call, al_call, m_call, am_call, e_call, ae_call, get_timestamp, create_path, append_to_jsonl
|
2
|
+
from .doc_util import dir_to_path, read_text, dir_to_files, file_to_chunks, get_bins
|
3
|
+
from .log_util import DataLogger
|
4
|
+
from .tool_util import ToolManager
|
5
|
+
from .api_util import StatusTracker, AsyncQueue, RateLimiter, BaseAPIService
|
6
|
+
|
7
|
+
__all__ = [
|
8
|
+
"to_flat_dict", "to_list", "str_to_num", "make_copy", "to_temp", "to_csv", "hold_call", "ahold_call",
|
9
|
+
"l_call", "al_call", "m_call", "am_call", "e_call", "ae_call", "get_timestamp", "create_path", "append_to_jsonl",
|
10
|
+
"dir_to_path", "read_text", "dir_to_files", "file_to_chunks", "get_bins", "DataLogger", "ToolManager", "StatusTracker", "AsyncQueue", "RateLimiter", "BaseAPIService"
|
11
|
+
]
|
@@ -288,18 +288,18 @@ class BaseAPIService(ABC):
|
|
288
288
|
The maximum number of retry attempts for API calls.
|
289
289
|
status_tracker (StatusTracker):
|
290
290
|
Tracker for API call statuses.
|
291
|
-
rate_limiter (RateLimiter):
|
292
|
-
Limiter to control the rate of API calls.
|
293
291
|
queue (AsyncQueue):
|
294
292
|
Queue for managing API call tasks.
|
293
|
+
rate_limiter (RateLimiter):
|
294
|
+
Limiter to control the rate of API calls.
|
295
|
+
append_to_jsonl (callable):
|
296
|
+
Callable for appending data to a file in JSONL format.
|
295
297
|
|
296
298
|
Methods:
|
297
299
|
call_api:
|
298
300
|
Abstract method to define API call mechanism in subclasses.
|
299
301
|
handle_error:
|
300
302
|
Handle errors by logging and saving details to a JSONL file.
|
301
|
-
append_to_jsonl:
|
302
|
-
Append data to a file in JSONL format.
|
303
303
|
api_endpoint_from_url:
|
304
304
|
Extract the API endpoint from a URL.
|
305
305
|
task_id_generator_function:
|
@@ -0,0 +1 @@
|
|
1
|
+
__version__ = "0.0.111"
|
@@ -1,7 +0,0 @@
|
|
1
|
-
from .sys_util import to_flat_dict, to_list, str_to_num, make_copy, to_temp, to_csv, hold_call, ahold_call, l_call, al_call, m_call, am_call, e_call, ae_call, get_timestamp, create_path
|
2
|
-
from .doc_util import dir_to_path, read_text, dir_to_files, file_to_chunks, get_bins
|
3
|
-
|
4
|
-
__all__ = [
|
5
|
-
"to_list", "str_to_num", "make_copy", "to_temp", "to_csv", "hold_call", "ahold_call", "l_call", "al_call", "m_call", "am_call", "e_call", "ae_call", "get_timestamp", "create_path", "to_flat_dict",
|
6
|
-
"dir_to_path", "read_text", "dir_to_files", "file_to_chunks", "get_bins"
|
7
|
-
]
|
@@ -1 +0,0 @@
|
|
1
|
-
__version__ = "0.0.107"
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|