lm-deluge 0.0.24__py3-none-any.whl → 0.0.26__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of lm-deluge might be problematic. Click here for more details.

lm_deluge/batches.py CHANGED
@@ -169,7 +169,8 @@ async def submit_batches_oa(
169
169
  batch_size: int = 50_000,
170
170
  ):
171
171
  """Write OpenAI batch requests to a file and submit."""
172
- BATCH_SIZE = batch_size
172
+ MAX_BATCH_SIZE_BYTES = 200 * 1024 * 1024 # 200MB
173
+ MAX_BATCH_SIZE_ITEMS = batch_size
173
174
 
174
175
  prompts = prompts_to_conversations(prompts)
175
176
  if any(p is None for p in prompts):
@@ -178,29 +179,71 @@ async def submit_batches_oa(
178
179
  model_obj = APIModel.from_registry(model)
179
180
 
180
181
  tasks = []
182
+ current_batch = []
183
+ current_batch_size = 0
184
+ # current_batch_start_idx = 0
185
+
186
+ for idx, prompt in enumerate(prompts):
187
+ assert isinstance(prompt, Conversation)
188
+ context = RequestContext(
189
+ task_id=idx,
190
+ model_name=model,
191
+ prompt=prompt,
192
+ sampling_params=sampling_params,
193
+ )
194
+ request = {
195
+ "custom_id": str(idx),
196
+ "method": "POST",
197
+ "url": "/v1/chat/completions",
198
+ "body": await _build_oa_chat_request(model_obj, context),
199
+ }
181
200
 
182
- for start in range(0, len(prompts), BATCH_SIZE):
183
- batch_prompts = prompts[start : start + BATCH_SIZE]
184
- with tempfile.NamedTemporaryFile(mode="w+", suffix=".jsonl", delete=False) as f:
185
- for idx, prompt in enumerate(batch_prompts, start=start):
186
- assert isinstance(prompt, Conversation)
187
- context = RequestContext(
188
- task_id=idx,
189
- model_name=model,
190
- prompt=prompt,
191
- sampling_params=sampling_params,
192
- )
193
- request = {
194
- "custom_id": str(idx),
195
- "method": "POST",
196
- "url": "/v1/chat/completions",
197
- "body": await _build_oa_chat_request(model_obj, context),
198
- }
199
- json.dump(request, f)
200
- f.write("\n")
201
-
202
- file_path = f.name
203
-
201
+ # Calculate size of this request
202
+ request_json = json.dumps(request) + "\n"
203
+ request_size = len(request_json.encode("utf-8"))
204
+
205
+ # Check if adding this request would exceed limits
206
+ would_exceed_size = current_batch_size + request_size > MAX_BATCH_SIZE_BYTES
207
+ would_exceed_items = len(current_batch) >= MAX_BATCH_SIZE_ITEMS
208
+
209
+ if current_batch and (would_exceed_size or would_exceed_items):
210
+ # Submit current batch
211
+ def write_batch_file():
212
+ with tempfile.NamedTemporaryFile(
213
+ mode="w+", suffix=".jsonl", delete=False
214
+ ) as f:
215
+ for batch_request in current_batch:
216
+ json.dump(batch_request, f)
217
+ f.write("\n")
218
+ print("wrote", len(current_batch), "items")
219
+ return f.name
220
+
221
+ file_path = await asyncio.to_thread(write_batch_file)
222
+ tasks.append(asyncio.create_task(submit_batch_oa(file_path)))
223
+
224
+ # Start new batch
225
+ current_batch = []
226
+ current_batch_size = 0
227
+ # current_batch_start_idx = idx
228
+
229
+ # Add request to current batch
230
+ current_batch.append(request)
231
+ current_batch_size += request_size
232
+
233
+ # Submit final batch if it has items
234
+ if current_batch:
235
+
236
+ def write_final_batch_file():
237
+ with tempfile.NamedTemporaryFile(
238
+ mode="w+", suffix=".jsonl", delete=False
239
+ ) as f:
240
+ for batch_request in current_batch:
241
+ json.dump(batch_request, f)
242
+ f.write("\n")
243
+ print("wrote", len(current_batch), "items")
244
+ return f.name
245
+
246
+ file_path = await asyncio.to_thread(write_final_batch_file)
204
247
  tasks.append(asyncio.create_task(submit_batch_oa(file_path)))
205
248
 
206
249
  batch_ids = await asyncio.gather(*tasks)
@@ -229,34 +272,80 @@ async def submit_batches_anthropic(
229
272
 
230
273
  Returns: batch_ids (list[str])
231
274
  """
275
+ MAX_BATCH_SIZE_BYTES = 200 * 1024 * 1024 # 200MB
276
+ MAX_BATCH_SIZE_ITEMS = batch_size
232
277
 
233
278
  # Convert prompts to Conversations
234
279
  prompts = prompts_to_conversations(prompts)
235
280
 
236
281
  request_headers = None
237
- BATCH_SIZE = batch_size
238
282
  batch_tasks = []
239
-
240
- for start in range(0, len(prompts), BATCH_SIZE):
241
- batch_prompts = prompts[start : start + BATCH_SIZE]
242
- with tempfile.NamedTemporaryFile(mode="w+", suffix=".jsonl", delete=False) as f:
243
- for idx, prompt in enumerate(batch_prompts, start=start):
244
- assert isinstance(prompt, Conversation)
245
- context = RequestContext(
246
- task_id=idx,
247
- model_name=model,
248
- prompt=prompt,
249
- sampling_params=sampling_params,
250
- cache=cache,
251
- )
252
- request_body, request_headers = _build_anthropic_request(
253
- APIModel.from_registry(model), context
283
+ current_batch = []
284
+ current_batch_size = 0
285
+
286
+ for idx, prompt in enumerate(prompts):
287
+ assert isinstance(prompt, Conversation)
288
+ context = RequestContext(
289
+ task_id=idx,
290
+ model_name=model,
291
+ prompt=prompt,
292
+ sampling_params=sampling_params,
293
+ cache=cache,
294
+ )
295
+ request_body, request_headers = _build_anthropic_request(
296
+ APIModel.from_registry(model), context
297
+ )
298
+ request = {"custom_id": str(idx), "params": request_body}
299
+
300
+ # Calculate size of this request
301
+ request_json = json.dumps(request) + "\n"
302
+ request_size = len(request_json.encode("utf-8"))
303
+
304
+ # Check if adding this request would exceed limits
305
+ would_exceed_size = current_batch_size + request_size > MAX_BATCH_SIZE_BYTES
306
+ would_exceed_items = len(current_batch) >= MAX_BATCH_SIZE_ITEMS
307
+
308
+ if current_batch and (would_exceed_size or would_exceed_items):
309
+ # Submit current batch
310
+ def write_batch_file():
311
+ with tempfile.NamedTemporaryFile(
312
+ mode="w+", suffix=".jsonl", delete=False
313
+ ) as f:
314
+ for batch_request in current_batch:
315
+ json.dump(batch_request, f)
316
+ f.write("\n")
317
+ print("wrote", len(current_batch), "items")
318
+ return f.name
319
+
320
+ file_path = await asyncio.to_thread(write_batch_file)
321
+ batch_tasks.append(
322
+ asyncio.create_task(
323
+ _submit_anthropic_batch(file_path, request_headers, model) # type: ignore
254
324
  )
255
- json.dump({"custom_id": str(idx), "params": request_body}, f)
256
- f.write("\n")
325
+ )
326
+
327
+ # Start new batch
328
+ current_batch = []
329
+ current_batch_size = 0
330
+
331
+ # Add request to current batch
332
+ current_batch.append(request)
333
+ current_batch_size += request_size
334
+
335
+ # Submit final batch if it has items
336
+ if current_batch:
257
337
 
258
- file_path = f.name
338
+ def write_final_batch_file():
339
+ with tempfile.NamedTemporaryFile(
340
+ mode="w+", suffix=".jsonl", delete=False
341
+ ) as f:
342
+ for batch_request in current_batch:
343
+ json.dump(batch_request, f)
344
+ f.write("\n")
345
+ print("wrote", len(current_batch), "items")
346
+ return f.name
259
347
 
348
+ file_path = await asyncio.to_thread(write_final_batch_file)
260
349
  batch_tasks.append(
261
350
  asyncio.create_task(
262
351
  _submit_anthropic_batch(file_path, request_headers, model) # type: ignore
lm_deluge/file.py CHANGED
@@ -1,3 +1,4 @@
1
+ from functools import cached_property
1
2
  import os
2
3
  import io
3
4
  import requests
@@ -68,13 +69,13 @@ class File:
68
69
  return encoded
69
70
  return f"data:{self._mime()};base64,{encoded}"
70
71
 
71
- @property
72
+ @cached_property
72
73
  def fingerprint(self) -> str:
73
74
  # Hash the file contents for fingerprinting
74
75
  file_bytes = self._bytes()
75
76
  return xxhash.xxh64(file_bytes).hexdigest()
76
77
 
77
- @property
78
+ @cached_property
78
79
  def size(self) -> int:
79
80
  """Return file size in bytes."""
80
81
  return len(self._bytes())
@@ -1,4 +1,5 @@
1
1
  from dataclasses import dataclass, field
2
+ from functools import cached_property
2
3
  from typing import Any, Callable
3
4
 
4
5
  from .config import SamplingParams
@@ -39,14 +40,18 @@ class RequestContext:
39
40
 
40
41
  # Computed properties
41
42
  cache_key: str = field(init=False)
42
- num_tokens: int = field(init=False)
43
+ # num_tokens: int = field(init=False)
43
44
 
44
- def __post_init__(self):
45
- # Compute cache key from prompt fingerprint
46
- self.cache_key = self.prompt.fingerprint
45
+ # def __post_init__(self):
46
+ # # Compute cache key from prompt fingerprint
47
+ # # self.cache_key = self.prompt.fingerprint
47
48
 
48
- # Compute token count
49
- self.num_tokens = self.prompt.count_tokens(self.sampling_params.max_new_tokens)
49
+ # # Compute token count
50
+ # self.num_tokens =
51
+
52
+ @cached_property
53
+ def num_tokens(self):
54
+ return self.prompt.count_tokens(self.sampling_params.max_new_tokens)
50
55
 
51
56
  def maybe_callback(self, response, tracker):
52
57
  if not self.callback:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lm_deluge
3
- Version: 0.0.24
3
+ Version: 0.0.26
4
4
  Summary: Python utility for using LLM API models.
5
5
  Author-email: Benjamin Anderson <ben@trytaylor.ai>
6
6
  Requires-Python: >=3.10
@@ -1,17 +1,17 @@
1
1
  lm_deluge/__init__.py,sha256=mAztMuxINmh7dGbYnT8tsmw1eryQAvd0jpY8yHzd0EE,315
2
2
  lm_deluge/agent.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
- lm_deluge/batches.py,sha256=Q7uETobLxgMJ7_Y_oJU1MKOYCa7ttmCU6CX6EuPv2qY,18636
3
+ lm_deluge/batches.py,sha256=vJXVnuuGkIQnXoDPODPERrvdG9X1Ov1jnXExnPe6ZAc,21772
4
4
  lm_deluge/cache.py,sha256=VB1kv8rM2t5XWPR60uhszFcxLDnVKOe1oA5hYjVDjIo,4375
5
5
  lm_deluge/client.py,sha256=nAGMwdUPDVx-x23hZF6U5Yhug6Zf5FT27RHj_kj8nZk,26369
6
6
  lm_deluge/config.py,sha256=H1tQyJDNHGFuwxqQNL5Z-CjWAC0luHSBA3iY_pxmACM,932
7
7
  lm_deluge/embed.py,sha256=CO-TOlC5kOTAM8lcnicoG4u4K664vCBwHF1vHa-nAGg,13382
8
8
  lm_deluge/errors.py,sha256=oHjt7YnxWbh-eXMScIzov4NvpJMo0-2r5J6Wh5DQ1tk,209
9
- lm_deluge/file.py,sha256=zQH1STMjCG9pczO7Fk9Jw0_0Pj_8CogcdIxTe4J4AJw,5414
9
+ lm_deluge/file.py,sha256=ggOXcAOI-LAykXLb3N3WfLlHdBjQIJtW9ltmfKCIsNQ,5466
10
10
  lm_deluge/gemini_limits.py,sha256=V9mpS9JtXYz7AY6OuKyQp5TuIMRH1BVv9YrSNmGmHNA,1569
11
11
  lm_deluge/image.py,sha256=Qpa0k5yXfrpSaHzVUwW_TEn7yEgmwzYGL17Sa7-KhSA,7729
12
12
  lm_deluge/models.py,sha256=3vgI1BlfT4_Higev25QhhXJufQvsI6pd0yjF9YL0crA,49812
13
13
  lm_deluge/prompt.py,sha256=cfwzCAmT-1K0v7SfEMUrxpBkJGgf7IFlWfNLJrCcoBM,37025
14
- lm_deluge/request_context.py,sha256=0X-5m8BKn51rnnjzGDDXqbuSUEFGjdayirQjbvPcjMI,2425
14
+ lm_deluge/request_context.py,sha256=o33LSEwnK6YPhZeulUoSE_VrdKCXiCQa0tjjixK2K6M,2540
15
15
  lm_deluge/rerank.py,sha256=-NBAJdHz9OB-SWWJnHzkFmeVO4wR6lFV7Vw-SxG7aVo,11457
16
16
  lm_deluge/tool.py,sha256=_coOKB9nPNVZoseMRumRyQ8BMR7_d0IlstzMHNT69JY,15732
17
17
  lm_deluge/tracker.py,sha256=-EkFDAklh5mclIFR-5SthAwNL4p1yKS8LUN7rhpOVPQ,9266
@@ -48,8 +48,8 @@ lm_deluge/util/logprobs.py,sha256=UkBZakOxWluaLqHrjARu7xnJ0uCHVfLGHJdnYlEcutk,11
48
48
  lm_deluge/util/spatial.py,sha256=BsF_UKhE-x0xBirc-bV1xSKZRTUhsOBdGqsMKme20C8,4099
49
49
  lm_deluge/util/validation.py,sha256=hz5dDb3ebvZrZhnaWxOxbNSVMI6nmaOODBkk0htAUhs,1575
50
50
  lm_deluge/util/xml.py,sha256=Ft4zajoYBJR3HHCt2oHwGfymGLdvp_gegVmJ-Wqk4Ck,10547
51
- lm_deluge-0.0.24.dist-info/licenses/LICENSE,sha256=uNNXGXPCw2TC7CUs7SEBkA-Mz6QBQFWUUEWDMgEs1dU,1058
52
- lm_deluge-0.0.24.dist-info/METADATA,sha256=mXZ-AgyVqmY5fD_U8qbCb0Hm5Ekx4jkSyW8dQ3zYDJw,12978
53
- lm_deluge-0.0.24.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
54
- lm_deluge-0.0.24.dist-info/top_level.txt,sha256=hqU-TJX93yBwpgkDtYcXyLr3t7TLSCCZ_reytJjwBaE,10
55
- lm_deluge-0.0.24.dist-info/RECORD,,
51
+ lm_deluge-0.0.26.dist-info/licenses/LICENSE,sha256=uNNXGXPCw2TC7CUs7SEBkA-Mz6QBQFWUUEWDMgEs1dU,1058
52
+ lm_deluge-0.0.26.dist-info/METADATA,sha256=2zzPvIBE0SdA0RAAKGnyUQDIq-sE_Eo34pfRu_ygi4I,12978
53
+ lm_deluge-0.0.26.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
54
+ lm_deluge-0.0.26.dist-info/top_level.txt,sha256=hqU-TJX93yBwpgkDtYcXyLr3t7TLSCCZ_reytJjwBaE,10
55
+ lm_deluge-0.0.26.dist-info/RECORD,,