LLM Clients API
AXEtract abstracts LLM interactions through a unified client interface, supporting HuggingFace local execution, vLLM high-throughput serving, and LiteLLM for API-based models.
Base Client
axetract.llm.base_client.BaseClient
Bases: ABC
Abstract base class for calling LLMs across any backend.
Source code in src/axetract/llm/base_client.py
__init__(config=None)
Initialize the LLM client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Optional[dict]
|
Backend-specific configuration. |
None
|
call_api(prompt, adapter_name=None, **kwargs)
abstractmethod
Call a single LLM completion API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
Input text. |
required |
adapter_name
|
Optional[str]
|
Name of the LoRA adapter to use. |
None
|
**kwargs
|
Generation parameter overrides. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The generated text. |
Source code in src/axetract/llm/base_client.py
call_batch(prompts, max_workers=8, chunk_size=None, raise_on_error=False, adapter_name=None, per_result_callback=None, **call_api_kwargs)
Process a batch of prompts using threaded parallelism.
NOTE: Local models (HF/vLLM) should override this to use native engine batching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompts
|
Iterable[str]
|
Batch of input texts. |
required |
max_workers
|
int
|
ThreadPool worker count. |
8
|
chunk_size
|
Optional[int]
|
If set, processes in sub-batches. |
None
|
raise_on_error
|
bool
|
Whether to abort on first API error. |
False
|
adapter_name
|
Optional[str]
|
LoRA adapter name. |
None
|
per_result_callback
|
Optional[Callable]
|
Hook called for each result. |
None
|
**call_api_kwargs
|
Common parameters passed to call_api. |
{}
|
Returns:
| Type | Description |
|---|---|
List[Optional[str]]
|
List[Optional[str]]: List of completions in matching order. |
Source code in src/axetract/llm/base_client.py
Hugging Face Client
axetract.llm.hf_client.HuggingFaceClient
Bases: BaseClient
Connects directly to Hugging Face transformers.
Supports native tensor batching and on-the-fly PEFT LoRA switching. Optimized for maximum throughput with Flash Attention 2, torch.compile, and static KV cache when available.
Performance optimizations in call_batch: - Phase 1: All tokenization happens OUTSIDE the GPU lock (CPU work) - Phase 2: Dynamic batching by token length OUTSIDE the lock - Phase 3: Tensor padding/construction OUTSIDE the lock - Phase 4: Only adapter switch + model.generate inside the lock - Phase 5: Token decoding OUTSIDE the lock
Source code in src/axetract/llm/hf_client.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | |
__init__(config)
Initialize the Hugging Face client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict
|
Configuration containing model_name, lora_modules, etc. |
required |
Raises:
| Type | Description |
|---|---|
ImportError
|
If torch/transformers/peft are not installed. |
ValueError
|
If model_name is missing. |
Source code in src/axetract/llm/hf_client.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
call_api(prompt, adapter_name=None, thinking=False, **kwargs)
Call a single prompt completion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
Input text. |
required |
adapter_name
|
Optional[str]
|
LoRA adapter name. |
None
|
thinking
|
bool
|
Enable thinking tags. |
False
|
**kwargs
|
Generation overrides. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Generated text. |
Source code in src/axetract/llm/hf_client.py
call_batch(prompts, adapter_name=None, chunk_size=8, thinking=False, **kwargs)
Optimized batch generation with minimal GPU lock scope.
All CPU work (tokenization, sorting, padding, decoding) happens OUTSIDE the GPU lock. Only adapter switching and model.generate() are protected, maximizing GPU utilization in concurrent pipelines.
Architecture
Phase 1 (CPU, unlocked): Pre-tokenize all prompts (single pass) Phase 2 (CPU, unlocked): Dynamic batching by token length Phase 3 (CPU, unlocked): Pad and build tensors per chunk Phase 4 (GPU, LOCKED): Adapter switch + generate for each chunk Phase 5 (CPU, unlocked): Decode generated tokens
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompts
|
Iterable[str]
|
Batch of prompts. |
required |
adapter_name
|
Optional[str]
|
Target LoRA adapter. |
None
|
chunk_size
|
int
|
Internal batch size for GPU processing. |
8
|
thinking
|
bool
|
Enable thinking tags in prompt. |
False
|
**kwargs
|
Generation parameter overrides. |
{}
|
Returns:
| Type | Description |
|---|---|
List[Optional[str]]
|
List[Optional[str]]: Decoded completions. |
Source code in src/axetract/llm/hf_client.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
vLLM Client
axetract.llm.vllm_client.LocalVLLMClient
Bases: BaseClient
Connects to a local vLLM engine for high-performance inference.
Supports native LoRA switching and batch generation.
Source code in src/axetract/llm/vllm_client.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
__init__(config)
Initialize the vLLM client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict
|
Configuration containing model_name, engine_args, etc. |
required |
Raises:
| Type | Description |
|---|---|
ImportError
|
If vLLM is not installed. |
Source code in src/axetract/llm/vllm_client.py
call_api(prompt, adapter_name=None, thinking=False, **kwargs)
Call a single prompt completion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
Input text. |
required |
adapter_name
|
str
|
LoRA adapter name. |
None
|
thinking
|
bool
|
Enable thinking tags. |
False
|
**kwargs
|
Generation overrides. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Generated text. |
Source code in src/axetract/llm/vllm_client.py
call_batch(prompts, adapter_name=None, thinking=False, **kwargs)
High-throughput batch generation using the vLLM engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompts
|
Iterable[str]
|
Batch of prompts. |
required |
adapter_name
|
str
|
LoRA adapter name. |
None
|
thinking
|
bool
|
Enable thinking tags. |
False
|
**kwargs
|
Generation parameter overrides. |
{}
|
Returns:
| Type | Description |
|---|---|
List[Optional[str]]
|
List[Optional[str]]: Generated texts. |
Source code in src/axetract/llm/vllm_client.py
LiteLLM Client
axetract.llm.litellm_client.LiteLLMClient
Bases: BaseClient
LLM client using LiteLLM to support multiple providers (OpenAI, Anthropic, etc.).
Source code in src/axetract/llm/litellm_client.py
__init__(config)
Initialize the LiteLLM client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict
|
Configuration including model_name, api_key, etc. |
required |
Raises:
| Type | Description |
|---|---|
ImportError
|
If litellm is not installed. |
Source code in src/axetract/llm/litellm_client.py
call_api(prompt, adapter_name=None, **kwargs)
Call the completion API via LiteLLM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
Input text. |
required |
adapter_name
|
Optional[str]
|
Target adapter (used as model name for vLLM). |
None
|
**kwargs
|
Overrides for generation parameters. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Generated text. |