llm_batch_helper 0.1.4__tar.gz → 0.1.6__tar.gz

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.
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: llm_batch_helper
3
- Version: 0.1.4
3
+ Version: 0.1.6
4
4
  Summary: A Python package that enables batch submission of prompts to LLM APIs, with built-in async capabilities and response caching.
5
5
  License: MIT
6
- Keywords: llm,openai,batch,async,ai,nlp,api
6
+ Keywords: llm,openai,together,batch,async,ai,nlp,api
7
7
  Author: Tianyi Peng
8
8
  Author-email: tianyipeng95@gmail.com
9
9
  Requires-Python: >=3.11,<4.0
@@ -19,7 +19,6 @@ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
19
19
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
20
20
  Requires-Dist: httpx (>=0.24.0,<2.0.0)
21
21
  Requires-Dist: openai (>=1.0.0,<2.0.0)
22
- Requires-Dist: python-dotenv (>=1.0.0,<2.0.0)
23
22
  Requires-Dist: tenacity (>=8.0.0,<9.0.0)
24
23
  Requires-Dist: tqdm (>=4.65.0,<5.0.0)
25
24
  Project-URL: Homepage, https://github.com/TianyiPeng/LLM_batch_helper
@@ -28,14 +27,36 @@ Description-Content-Type: text/markdown
28
27
 
29
28
  # LLM Batch Helper
30
29
 
31
- A Python package that enables batch submission of prompts to LLM APIs, with built-in async capabilities and response caching.
30
+ [![PyPI version](https://badge.fury.io/py/llm_batch_helper.svg)](https://badge.fury.io/py/llm_batch_helper)
31
+ [![Downloads](https://pepy.tech/badge/llm_batch_helper)](https://pepy.tech/project/llm_batch_helper)
32
+ [![Downloads/Month](https://pepy.tech/badge/llm_batch_helper/month)](https://pepy.tech/project/llm_batch_helper)
33
+ [![Documentation Status](https://readthedocs.org/projects/llm-batch-helper/badge/?version=latest)](https://llm-batch-helper.readthedocs.io/en/latest/?badge=latest)
34
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
35
+
36
+ A Python package that enables batch submission of prompts to LLM APIs, with built-in async capabilities, response caching, prompt verification, and more. This package is designed to streamline applications like LLM simulation, LLM-as-a-judge, and other batch processing scenarios.
37
+
38
+ 📖 **[Complete Documentation](https://llm-batch-helper.readthedocs.io/)** | 🚀 **[Quick Start Guide](https://llm-batch-helper.readthedocs.io/en/latest/quickstart.html)**
39
+
40
+ ## Why we designed this package
41
+
42
+ Calling LLM APIs has become increasingly common, but several pain points exist in practice:
43
+
44
+ 1. **Efficient Batch Processing**: How do you run LLM calls in batches efficiently? Our async implementation is 3X-100X faster than multi-thread/multi-process approaches.
45
+
46
+ 2. **API Reliability**: LLM APIs can be unstable, so we need robust retry mechanisms when calls get interrupted.
47
+
48
+ 3. **Long-Running Simulations**: During long-running LLM simulations, computers can crash and APIs can fail. Can we cache LLM API calls to avoid repeating completed work?
49
+
50
+ 4. **Output Validation**: LLM outputs often have format requirements. If the output isn't right, we need to retry with validation.
51
+
52
+ This package is designed to solve these exact pain points with async processing, intelligent caching, and comprehensive error handling. If there are some additional features you need, please post an issue.
32
53
 
33
54
  ## Features
34
55
 
35
56
  - **Async Processing**: Submit multiple prompts concurrently for faster processing
36
57
  - **Response Caching**: Automatically cache responses to avoid redundant API calls
37
58
  - **Multiple Input Formats**: Support for both file-based and list-based prompts
38
- - **Provider Support**: Works with OpenAI API
59
+ - **Provider Support**: Works with OpenAI and Together.ai APIs
39
60
  - **Retry Logic**: Built-in retry mechanism with exponential backoff
40
61
  - **Verification Callbacks**: Custom verification for response quality
41
62
  - **Progress Tracking**: Real-time progress bars for batch operations
@@ -67,9 +88,29 @@ poetry shell
67
88
 
68
89
  ### 1. Set up environment variables
69
90
 
91
+ **Option A: Environment Variables**
70
92
  ```bash
71
93
  # For OpenAI
72
94
  export OPENAI_API_KEY="your-openai-api-key"
95
+
96
+ # For Together.ai
97
+ export TOGETHER_API_KEY="your-together-api-key"
98
+ ```
99
+
100
+ **Option B: .env File (Recommended for Development)**
101
+ ```python
102
+ # In your script, before importing llm_batch_helper
103
+ from dotenv import load_dotenv
104
+ load_dotenv() # Load from .env file
105
+
106
+ # Then use the package normally
107
+ from llm_batch_helper import LLMConfig, process_prompts_batch
108
+ ```
109
+
110
+ Create a `.env` file in your project:
111
+ ```
112
+ OPENAI_API_KEY=your-openai-api-key
113
+ TOGETHER_API_KEY=your-together-api-key
73
114
  ```
74
115
 
75
116
  ### 2. Interactive Tutorial (Recommended)
@@ -82,14 +123,18 @@ The tutorial covers all features with interactive examples!
82
123
 
83
124
  ```python
84
125
  import asyncio
126
+ from dotenv import load_dotenv # Optional: for .env file support
85
127
  from llm_batch_helper import LLMConfig, process_prompts_batch
86
128
 
129
+ # Optional: Load environment variables from .env file
130
+ load_dotenv()
131
+
87
132
  async def main():
88
133
  # Create configuration
89
134
  config = LLMConfig(
90
135
  model_name="gpt-4o-mini",
91
136
  temperature=0.7,
92
- max_tokens=100,
137
+ max_completion_tokens=100, # or use max_tokens for backward compatibility
93
138
  max_concurrent_requests=30 # number of concurrent requests with asyncIO
94
139
  )
95
140
 
@@ -127,7 +172,7 @@ async def process_files():
127
172
  config = LLMConfig(
128
173
  model_name="gpt-4o-mini",
129
174
  temperature=0.7,
130
- max_tokens=200
175
+ max_completion_tokens=200
131
176
  )
132
177
 
133
178
  # Process all .txt files in a directory
@@ -183,7 +228,8 @@ Configuration class for LLM requests.
183
228
  LLMConfig(
184
229
  model_name: str,
185
230
  temperature: float = 0.7,
186
- max_tokens: Optional[int] = None,
231
+ max_completion_tokens: Optional[int] = None, # Preferred parameter
232
+ max_tokens: Optional[int] = None, # Deprecated, kept for backward compatibility
187
233
  system_instruction: Optional[str] = None,
188
234
  max_retries: int = 10,
189
235
  max_concurrent_requests: int = 5,
@@ -199,7 +245,7 @@ Main function for batch processing of prompts.
199
245
  ```python
200
246
  async def process_prompts_batch(
201
247
  config: LLMConfig,
202
- provider: str, # "openai"
248
+ provider: str, # "openai" or "together"
203
249
  prompts: Optional[List[str]] = None,
204
250
  input_dir: Optional[str] = None,
205
251
  cache_dir: str = "llm_cache",
@@ -256,6 +302,23 @@ llm_batch_helper/
256
302
  - gpt-4
257
303
  - gpt-3.5-turbo
258
304
 
305
+ ### Together.ai
306
+ - meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo
307
+ - meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo
308
+ - mistralai/Mixtral-8x7B-Instruct-v0.1
309
+ - And many other open-source models
310
+
311
+ ## Documentation
312
+
313
+ 📖 **[Complete Documentation](https://llm-batch-helper.readthedocs.io/)** - Comprehensive docs on Read the Docs
314
+
315
+ ### Quick Links:
316
+ - [Quick Start Guide](https://llm-batch-helper.readthedocs.io/en/latest/quickstart.html) - Get started quickly
317
+ - [API Reference](https://llm-batch-helper.readthedocs.io/en/latest/api.html) - Complete API documentation
318
+ - [Examples](https://llm-batch-helper.readthedocs.io/en/latest/examples.html) - Practical usage examples
319
+ - [Tutorials](https://llm-batch-helper.readthedocs.io/en/latest/tutorials.html) - Step-by-step tutorials
320
+ - [Provider Guide](https://llm-batch-helper.readthedocs.io/en/latest/providers.html) - OpenAI & Together.ai setup
321
+
259
322
  ## Contributing
260
323
 
261
324
  1. Fork the repository
@@ -271,6 +334,12 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
271
334
 
272
335
  ## Changelog
273
336
 
337
+ ### v0.1.5
338
+ - Added Together.ai provider support
339
+ - Support for open-source models (Llama, Mixtral, etc.)
340
+ - Enhanced documentation with Read the Docs
341
+ - Updated examples and tutorials
342
+
274
343
  ### v0.1.0
275
344
  - Initial release
276
345
  - Support for OpenAI API
@@ -1,13 +1,35 @@
1
1
  # LLM Batch Helper
2
2
 
3
- A Python package that enables batch submission of prompts to LLM APIs, with built-in async capabilities and response caching.
3
+ [![PyPI version](https://badge.fury.io/py/llm_batch_helper.svg)](https://badge.fury.io/py/llm_batch_helper)
4
+ [![Downloads](https://pepy.tech/badge/llm_batch_helper)](https://pepy.tech/project/llm_batch_helper)
5
+ [![Downloads/Month](https://pepy.tech/badge/llm_batch_helper/month)](https://pepy.tech/project/llm_batch_helper)
6
+ [![Documentation Status](https://readthedocs.org/projects/llm-batch-helper/badge/?version=latest)](https://llm-batch-helper.readthedocs.io/en/latest/?badge=latest)
7
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
8
+
9
+ A Python package that enables batch submission of prompts to LLM APIs, with built-in async capabilities, response caching, prompt verification, and more. This package is designed to streamline applications like LLM simulation, LLM-as-a-judge, and other batch processing scenarios.
10
+
11
+ 📖 **[Complete Documentation](https://llm-batch-helper.readthedocs.io/)** | 🚀 **[Quick Start Guide](https://llm-batch-helper.readthedocs.io/en/latest/quickstart.html)**
12
+
13
+ ## Why we designed this package
14
+
15
+ Calling LLM APIs has become increasingly common, but several pain points exist in practice:
16
+
17
+ 1. **Efficient Batch Processing**: How do you run LLM calls in batches efficiently? Our async implementation is 3X-100X faster than multi-thread/multi-process approaches.
18
+
19
+ 2. **API Reliability**: LLM APIs can be unstable, so we need robust retry mechanisms when calls get interrupted.
20
+
21
+ 3. **Long-Running Simulations**: During long-running LLM simulations, computers can crash and APIs can fail. Can we cache LLM API calls to avoid repeating completed work?
22
+
23
+ 4. **Output Validation**: LLM outputs often have format requirements. If the output isn't right, we need to retry with validation.
24
+
25
+ This package is designed to solve these exact pain points with async processing, intelligent caching, and comprehensive error handling. If there are some additional features you need, please post an issue.
4
26
 
5
27
  ## Features
6
28
 
7
29
  - **Async Processing**: Submit multiple prompts concurrently for faster processing
8
30
  - **Response Caching**: Automatically cache responses to avoid redundant API calls
9
31
  - **Multiple Input Formats**: Support for both file-based and list-based prompts
10
- - **Provider Support**: Works with OpenAI API
32
+ - **Provider Support**: Works with OpenAI and Together.ai APIs
11
33
  - **Retry Logic**: Built-in retry mechanism with exponential backoff
12
34
  - **Verification Callbacks**: Custom verification for response quality
13
35
  - **Progress Tracking**: Real-time progress bars for batch operations
@@ -39,9 +61,29 @@ poetry shell
39
61
 
40
62
  ### 1. Set up environment variables
41
63
 
64
+ **Option A: Environment Variables**
42
65
  ```bash
43
66
  # For OpenAI
44
67
  export OPENAI_API_KEY="your-openai-api-key"
68
+
69
+ # For Together.ai
70
+ export TOGETHER_API_KEY="your-together-api-key"
71
+ ```
72
+
73
+ **Option B: .env File (Recommended for Development)**
74
+ ```python
75
+ # In your script, before importing llm_batch_helper
76
+ from dotenv import load_dotenv
77
+ load_dotenv() # Load from .env file
78
+
79
+ # Then use the package normally
80
+ from llm_batch_helper import LLMConfig, process_prompts_batch
81
+ ```
82
+
83
+ Create a `.env` file in your project:
84
+ ```
85
+ OPENAI_API_KEY=your-openai-api-key
86
+ TOGETHER_API_KEY=your-together-api-key
45
87
  ```
46
88
 
47
89
  ### 2. Interactive Tutorial (Recommended)
@@ -54,14 +96,18 @@ The tutorial covers all features with interactive examples!
54
96
 
55
97
  ```python
56
98
  import asyncio
99
+ from dotenv import load_dotenv # Optional: for .env file support
57
100
  from llm_batch_helper import LLMConfig, process_prompts_batch
58
101
 
102
+ # Optional: Load environment variables from .env file
103
+ load_dotenv()
104
+
59
105
  async def main():
60
106
  # Create configuration
61
107
  config = LLMConfig(
62
108
  model_name="gpt-4o-mini",
63
109
  temperature=0.7,
64
- max_tokens=100,
110
+ max_completion_tokens=100, # or use max_tokens for backward compatibility
65
111
  max_concurrent_requests=30 # number of concurrent requests with asyncIO
66
112
  )
67
113
 
@@ -99,7 +145,7 @@ async def process_files():
99
145
  config = LLMConfig(
100
146
  model_name="gpt-4o-mini",
101
147
  temperature=0.7,
102
- max_tokens=200
148
+ max_completion_tokens=200
103
149
  )
104
150
 
105
151
  # Process all .txt files in a directory
@@ -155,7 +201,8 @@ Configuration class for LLM requests.
155
201
  LLMConfig(
156
202
  model_name: str,
157
203
  temperature: float = 0.7,
158
- max_tokens: Optional[int] = None,
204
+ max_completion_tokens: Optional[int] = None, # Preferred parameter
205
+ max_tokens: Optional[int] = None, # Deprecated, kept for backward compatibility
159
206
  system_instruction: Optional[str] = None,
160
207
  max_retries: int = 10,
161
208
  max_concurrent_requests: int = 5,
@@ -171,7 +218,7 @@ Main function for batch processing of prompts.
171
218
  ```python
172
219
  async def process_prompts_batch(
173
220
  config: LLMConfig,
174
- provider: str, # "openai"
221
+ provider: str, # "openai" or "together"
175
222
  prompts: Optional[List[str]] = None,
176
223
  input_dir: Optional[str] = None,
177
224
  cache_dir: str = "llm_cache",
@@ -228,6 +275,23 @@ llm_batch_helper/
228
275
  - gpt-4
229
276
  - gpt-3.5-turbo
230
277
 
278
+ ### Together.ai
279
+ - meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo
280
+ - meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo
281
+ - mistralai/Mixtral-8x7B-Instruct-v0.1
282
+ - And many other open-source models
283
+
284
+ ## Documentation
285
+
286
+ 📖 **[Complete Documentation](https://llm-batch-helper.readthedocs.io/)** - Comprehensive docs on Read the Docs
287
+
288
+ ### Quick Links:
289
+ - [Quick Start Guide](https://llm-batch-helper.readthedocs.io/en/latest/quickstart.html) - Get started quickly
290
+ - [API Reference](https://llm-batch-helper.readthedocs.io/en/latest/api.html) - Complete API documentation
291
+ - [Examples](https://llm-batch-helper.readthedocs.io/en/latest/examples.html) - Practical usage examples
292
+ - [Tutorials](https://llm-batch-helper.readthedocs.io/en/latest/tutorials.html) - Step-by-step tutorials
293
+ - [Provider Guide](https://llm-batch-helper.readthedocs.io/en/latest/providers.html) - OpenAI & Together.ai setup
294
+
231
295
  ## Contributing
232
296
 
233
297
  1. Fork the repository
@@ -243,6 +307,12 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
243
307
 
244
308
  ## Changelog
245
309
 
310
+ ### v0.1.5
311
+ - Added Together.ai provider support
312
+ - Support for open-source models (Llama, Mixtral, etc.)
313
+ - Enhanced documentation with Read the Docs
314
+ - Updated examples and tutorials
315
+
246
316
  ### v0.1.0
247
317
  - Initial release
248
318
  - Support for OpenAI API
@@ -3,7 +3,7 @@ from .config import LLMConfig
3
3
  from .input_handlers import get_prompts, read_prompt_files, read_prompt_list
4
4
  from .providers import process_prompts_batch
5
5
 
6
- __version__ = "0.1.1"
6
+ __version__ = "0.1.6"
7
7
 
8
8
  __all__ = [
9
9
  "LLMCache",
@@ -4,7 +4,6 @@ from typing import Any, Dict, List, Optional, Tuple, Union
4
4
 
5
5
  import httpx
6
6
  import openai
7
- from dotenv import load_dotenv
8
7
  from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential
9
8
  from tqdm.asyncio import tqdm_asyncio
10
9
 
@@ -12,8 +11,6 @@ from .cache import LLMCache
12
11
  from .config import LLMConfig
13
12
  from .input_handlers import get_prompts
14
13
 
15
- load_dotenv()
16
-
17
14
 
18
15
  @retry(
19
16
  stop=stop_after_attempt(5),
@@ -60,6 +57,65 @@ async def _get_openai_response_direct(
60
57
  "usage_details": usage_details,
61
58
  }
62
59
 
60
+
61
+ @retry(
62
+ stop=stop_after_attempt(5),
63
+ wait=wait_exponential(multiplier=1, min=4, max=60),
64
+ retry=retry_if_exception_type(
65
+ (
66
+ ConnectionError,
67
+ TimeoutError,
68
+ httpx.HTTPStatusError,
69
+ httpx.RequestError,
70
+ )
71
+ ),
72
+ reraise=True,
73
+ )
74
+ async def _get_together_response_direct(
75
+ prompt: str, config: LLMConfig
76
+ ) -> Dict[str, Union[str, Dict]]:
77
+ api_key = os.environ.get("TOGETHER_API_KEY")
78
+ if not api_key:
79
+ raise ValueError("TOGETHER_API_KEY environment variable not set")
80
+
81
+ async with httpx.AsyncClient(timeout=1000.0) as client:
82
+ messages = [
83
+ {"role": "system", "content": config.system_instruction},
84
+ {"role": "user", "content": prompt},
85
+ ]
86
+
87
+ headers = {
88
+ "Authorization": f"Bearer {api_key}",
89
+ "Content-Type": "application/json",
90
+ }
91
+
92
+ payload = {
93
+ "model": config.model_name,
94
+ "messages": messages,
95
+ "temperature": config.temperature,
96
+ "max_tokens": config.max_completion_tokens,
97
+ }
98
+
99
+ response = await client.post(
100
+ "https://api.together.xyz/chat/completions",
101
+ json=payload,
102
+ headers=headers,
103
+ )
104
+ response.raise_for_status()
105
+
106
+ response_data = response.json()
107
+ usage = response_data.get("usage", {})
108
+ usage_details = {
109
+ "prompt_token_count": usage.get("prompt_tokens", 0),
110
+ "completion_token_count": usage.get("completion_tokens", 0),
111
+ "total_token_count": usage.get("total_tokens", 0),
112
+ }
113
+
114
+ return {
115
+ "response_text": response_data["choices"][0]["message"]["content"],
116
+ "usage_details": usage_details,
117
+ }
118
+
63
119
  async def get_llm_response_with_internal_retry(
64
120
  prompt_id: str,
65
121
  prompt: str,
@@ -77,6 +133,8 @@ async def get_llm_response_with_internal_retry(
77
133
  try:
78
134
  if provider.lower() == "openai":
79
135
  response = await _get_openai_response_direct(prompt, config)
136
+ elif provider.lower() == "together":
137
+ response = await _get_together_response_direct(prompt, config)
80
138
  else:
81
139
  raise ValueError(f"Unsupported provider: {provider}")
82
140
 
@@ -107,7 +165,7 @@ async def process_prompts_batch(
107
165
  prompts: Optional list of prompts in any supported format (string, tuple, or dict)
108
166
  input_dir: Optional path to directory containing prompt files
109
167
  config: LLM configuration
110
- provider: LLM provider to use ("openai" or "gemini")
168
+ provider: LLM provider to use ("openai", "together", or "gemini")
111
169
  desc: Description for progress bar
112
170
  cache_dir: Optional directory for caching responses
113
171
  force: If True, force regeneration even if cached response exists
@@ -1,13 +1,13 @@
1
1
  [tool.poetry]
2
2
  name = "llm_batch_helper"
3
- version = "0.1.4"
3
+ version = "0.1.6"
4
4
  description = "A Python package that enables batch submission of prompts to LLM APIs, with built-in async capabilities and response caching."
5
5
  authors = ["Tianyi Peng <tianyipeng95@gmail.com>"]
6
6
  readme = "README.md"
7
7
  license = "MIT"
8
8
  homepage = "https://github.com/TianyiPeng/LLM_batch_helper"
9
9
  repository = "https://github.com/TianyiPeng/LLM_batch_helper"
10
- keywords = ["llm", "openai", "batch", "async", "ai", "nlp", "api"]
10
+ keywords = ["llm", "openai", "together", "batch", "async", "ai", "nlp", "api"]
11
11
  classifiers = [
12
12
  "Development Status :: 4 - Beta",
13
13
  "Intended Audience :: Developers",
@@ -25,11 +25,11 @@ packages = [{include = "llm_batch_helper"}]
25
25
  python = "^3.11"
26
26
  httpx = ">=0.24.0,<2.0.0"
27
27
  openai = "^1.0.0"
28
- python-dotenv = "^1.0.0"
29
28
  tenacity = "^8.0.0"
30
29
  tqdm = "^4.65.0"
31
30
 
32
31
  [tool.poetry.group.dev.dependencies]
32
+ python-dotenv = "^1.0.0" # Optional for .env file support
33
33
  pytest = "^7.0.0"
34
34
  black = "^23.0.0"
35
35
  isort = "^5.12.0"
@@ -39,6 +39,10 @@ jupyter = "^1.0.0"
39
39
  twine = "^6.1.0"
40
40
  ipython = "^9.4.0"
41
41
  ipykernel = "^6.29.5"
42
+ sphinx = "^7.0.0"
43
+ sphinx-rtd-theme = "^2.0.0"
44
+ myst-parser = "^2.0.0"
45
+ sphinx-autodoc-typehints = "^1.24.0"
42
46
 
43
47
  [build-system]
44
48
  requires = ["poetry-core"]