async-batch-llm 0.5.0__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.
- async_batch_llm/__init__.py +198 -0
- async_batch_llm/base.py +607 -0
- async_batch_llm/classifiers/__init__.py +5 -0
- async_batch_llm/classifiers/gemini.py +140 -0
- async_batch_llm/core/__init__.py +14 -0
- async_batch_llm/core/config.py +201 -0
- async_batch_llm/core/protocols.py +53 -0
- async_batch_llm/llm_strategies.py +937 -0
- async_batch_llm/middleware/__init__.py +5 -0
- async_batch_llm/middleware/base.py +82 -0
- async_batch_llm/observers/__init__.py +6 -0
- async_batch_llm/observers/base.py +51 -0
- async_batch_llm/observers/metrics.py +192 -0
- async_batch_llm/parallel.py +1323 -0
- async_batch_llm/strategies/__init__.py +25 -0
- async_batch_llm/strategies/errors.py +202 -0
- async_batch_llm/strategies/rate_limit.py +122 -0
- async_batch_llm/testing/__init__.py +6 -0
- async_batch_llm/testing/mocks.py +146 -0
- async_batch_llm/testing/strategies.py +34 -0
- async_batch_llm-0.5.0.dist-info/METADATA +815 -0
- async_batch_llm-0.5.0.dist-info/RECORD +24 -0
- async_batch_llm-0.5.0.dist-info/WHEEL +4 -0
- async_batch_llm-0.5.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"""Batch LLM processing utilities for handling bulk LLM requests.
|
|
2
|
+
|
|
3
|
+
This module provides a flexible framework for processing multiple LLM requests
|
|
4
|
+
efficiently using a strategy pattern for provider-agnostic LLM integration.
|
|
5
|
+
|
|
6
|
+
Key features:
|
|
7
|
+
- Strategy pattern for any LLM provider (OpenAI, Anthropic, Google, LangChain, custom)
|
|
8
|
+
- Built-in strategies: PydanticAIStrategy, GeminiStrategy, GeminiCachedStrategy
|
|
9
|
+
- Provider-agnostic error classification
|
|
10
|
+
- Pluggable rate limit strategies
|
|
11
|
+
- Middleware pipeline for extensibility
|
|
12
|
+
- Observer pattern for monitoring
|
|
13
|
+
- Configuration-based setup
|
|
14
|
+
|
|
15
|
+
Example:
|
|
16
|
+
>>> from async_batch_llm import (
|
|
17
|
+
... ParallelBatchProcessor,
|
|
18
|
+
... ProcessorConfig,
|
|
19
|
+
... LLMWorkItem,
|
|
20
|
+
... PydanticAIStrategy,
|
|
21
|
+
... )
|
|
22
|
+
>>> from pydantic_ai import Agent
|
|
23
|
+
>>>
|
|
24
|
+
>>> agent = Agent("openai:gpt-4o-mini", result_type=MyOutput)
|
|
25
|
+
>>> strategy = PydanticAIStrategy(agent=agent)
|
|
26
|
+
>>> config = ProcessorConfig(max_workers=5, timeout_per_item=60.0)
|
|
27
|
+
>>>
|
|
28
|
+
>>> async with ParallelBatchProcessor(config=config) as processor:
|
|
29
|
+
... await processor.add_work(LLMWorkItem(
|
|
30
|
+
... item_id="item_1",
|
|
31
|
+
... strategy=strategy,
|
|
32
|
+
... prompt="Process this",
|
|
33
|
+
... ))
|
|
34
|
+
... result = await processor.process_all()
|
|
35
|
+
|
|
36
|
+
Type Aliases:
|
|
37
|
+
For convenience, type aliases are provided to reduce verbosity:
|
|
38
|
+
|
|
39
|
+
- ``SimpleBatchProcessor[T]``: Processor with string input, output type T, no context
|
|
40
|
+
Equivalent to ``ParallelBatchProcessor[str, T, None]``
|
|
41
|
+
|
|
42
|
+
- ``SimpleWorkItem[T]``: Work item with string input, output type T, no context
|
|
43
|
+
Equivalent to ``LLMWorkItem[str, T, None]``
|
|
44
|
+
|
|
45
|
+
- ``SimpleResult[T]``: Result with output type T, no context
|
|
46
|
+
Equivalent to ``WorkItemResult[T, None]``
|
|
47
|
+
|
|
48
|
+
Example using type aliases:
|
|
49
|
+
>>> from async_batch_llm import SimpleBatchProcessor, SimpleWorkItem
|
|
50
|
+
>>>
|
|
51
|
+
>>> async with SimpleBatchProcessor[MyOutput](config=config) as processor:
|
|
52
|
+
... await processor.add_work(SimpleWorkItem[MyOutput](
|
|
53
|
+
... item_id="item_1",
|
|
54
|
+
... strategy=strategy,
|
|
55
|
+
... prompt="Process this",
|
|
56
|
+
... ))
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
from typing import TypeVar
|
|
60
|
+
|
|
61
|
+
# Core classes
|
|
62
|
+
from .base import (
|
|
63
|
+
BatchProcessor,
|
|
64
|
+
BatchResult,
|
|
65
|
+
LLMWorkItem,
|
|
66
|
+
PostProcessorFunc,
|
|
67
|
+
ProcessingStats,
|
|
68
|
+
ProgressCallbackFunc,
|
|
69
|
+
RetryState,
|
|
70
|
+
TokenUsage,
|
|
71
|
+
WorkItemResult,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
# Classifiers
|
|
75
|
+
from .classifiers import GeminiErrorClassifier
|
|
76
|
+
|
|
77
|
+
# Configuration
|
|
78
|
+
from .core import ProcessorConfig, RateLimitConfig, RetryConfig
|
|
79
|
+
|
|
80
|
+
# LLM call strategies
|
|
81
|
+
from .llm_strategies import (
|
|
82
|
+
GeminiCachedStrategy,
|
|
83
|
+
GeminiResponse,
|
|
84
|
+
GeminiStrategy,
|
|
85
|
+
LLMCallStrategy,
|
|
86
|
+
PydanticAIStrategy,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
# Middleware
|
|
90
|
+
from .middleware import BaseMiddleware, Middleware
|
|
91
|
+
|
|
92
|
+
# Observers
|
|
93
|
+
from .observers import BaseObserver, MetricsObserver, ProcessingEvent, ProcessorObserver
|
|
94
|
+
|
|
95
|
+
# Main processor
|
|
96
|
+
from .parallel import ParallelBatchProcessor
|
|
97
|
+
|
|
98
|
+
# Error classification and rate limit strategies
|
|
99
|
+
from .strategies import (
|
|
100
|
+
DefaultErrorClassifier,
|
|
101
|
+
ErrorClassifier,
|
|
102
|
+
ErrorInfo,
|
|
103
|
+
ExponentialBackoffStrategy,
|
|
104
|
+
FixedDelayStrategy,
|
|
105
|
+
FrameworkTimeoutError,
|
|
106
|
+
RateLimitStrategy,
|
|
107
|
+
TokenTrackingError,
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
# Type variable for output type in simplified aliases
|
|
111
|
+
_T = TypeVar("_T")
|
|
112
|
+
|
|
113
|
+
# Type aliases for common use cases
|
|
114
|
+
# These reduce verbosity for the most common pattern: string input, typed output, no context
|
|
115
|
+
SimpleBatchProcessor = ParallelBatchProcessor[str, _T, None]
|
|
116
|
+
"""Type alias for ParallelBatchProcessor[str, T, None].
|
|
117
|
+
|
|
118
|
+
Use when you have string prompts, a typed output, and no context.
|
|
119
|
+
|
|
120
|
+
Example:
|
|
121
|
+
async with SimpleBatchProcessor[MyOutput](config=config) as processor:
|
|
122
|
+
...
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
SimpleWorkItem = LLMWorkItem[str, _T, None]
|
|
126
|
+
"""Type alias for LLMWorkItem[str, T, None].
|
|
127
|
+
|
|
128
|
+
Use when creating work items with string prompts, typed output, and no context.
|
|
129
|
+
|
|
130
|
+
Example:
|
|
131
|
+
item = SimpleWorkItem[MyOutput](item_id="1", strategy=strategy, prompt="Hello")
|
|
132
|
+
"""
|
|
133
|
+
|
|
134
|
+
SimpleResult = WorkItemResult[_T, None]
|
|
135
|
+
"""Type alias for WorkItemResult[T, None].
|
|
136
|
+
|
|
137
|
+
Use when working with results that have no context.
|
|
138
|
+
|
|
139
|
+
Example:
|
|
140
|
+
result: SimpleResult[MyOutput] = results[0]
|
|
141
|
+
"""
|
|
142
|
+
|
|
143
|
+
__all__ = [
|
|
144
|
+
# Core
|
|
145
|
+
"BatchProcessor",
|
|
146
|
+
"BatchResult",
|
|
147
|
+
"LLMWorkItem",
|
|
148
|
+
"PostProcessorFunc",
|
|
149
|
+
"ProcessingStats",
|
|
150
|
+
"ProgressCallbackFunc",
|
|
151
|
+
"RetryState",
|
|
152
|
+
"TokenUsage",
|
|
153
|
+
"WorkItemResult",
|
|
154
|
+
# Configuration
|
|
155
|
+
"ProcessorConfig",
|
|
156
|
+
"RateLimitConfig",
|
|
157
|
+
"RetryConfig",
|
|
158
|
+
# LLM Strategies
|
|
159
|
+
"GeminiCachedStrategy",
|
|
160
|
+
"GeminiResponse",
|
|
161
|
+
"GeminiStrategy",
|
|
162
|
+
"LLMCallStrategy",
|
|
163
|
+
"PydanticAIStrategy",
|
|
164
|
+
# Error Classification Strategies
|
|
165
|
+
"ErrorClassifier",
|
|
166
|
+
"ErrorInfo",
|
|
167
|
+
"DefaultErrorClassifier",
|
|
168
|
+
"FrameworkTimeoutError",
|
|
169
|
+
"TokenTrackingError",
|
|
170
|
+
"RateLimitStrategy",
|
|
171
|
+
"ExponentialBackoffStrategy",
|
|
172
|
+
"FixedDelayStrategy",
|
|
173
|
+
# Middleware
|
|
174
|
+
"Middleware",
|
|
175
|
+
"BaseMiddleware",
|
|
176
|
+
# Observers
|
|
177
|
+
"ProcessorObserver",
|
|
178
|
+
"BaseObserver",
|
|
179
|
+
"MetricsObserver",
|
|
180
|
+
"ProcessingEvent",
|
|
181
|
+
# Classifiers
|
|
182
|
+
"GeminiErrorClassifier",
|
|
183
|
+
# Processor
|
|
184
|
+
"ParallelBatchProcessor",
|
|
185
|
+
# Type aliases (convenience)
|
|
186
|
+
"SimpleBatchProcessor",
|
|
187
|
+
"SimpleWorkItem",
|
|
188
|
+
"SimpleResult",
|
|
189
|
+
]
|
|
190
|
+
|
|
191
|
+
# Version is read from package metadata (single source of truth in pyproject.toml)
|
|
192
|
+
try:
|
|
193
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
194
|
+
|
|
195
|
+
__version__ = version("async-batch-llm")
|
|
196
|
+
except PackageNotFoundError:
|
|
197
|
+
# Package not installed (e.g., running from source in development)
|
|
198
|
+
__version__ = "0.0.0+dev"
|