guidellm.backend
Backend
Bases: ABC
Abstract base class for generative AI backends.
This class provides a common interface for creating and interacting with different generative AI backends. Subclasses should implement the abstract methods to define specific backend behavior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type_ | BackendType | The type of the backend. | required |
Attributes:
Name | Type | Description |
---|---|---|
_registry | dict[BackendType, type[Backend]] | A registration dictionary that maps BackendType to backend classes. |
Source code in src/guidellm/backend/backend.py
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 |
|
info
abstractmethod
property
Returns:
Type | Description |
---|---|
dict[str, Any] | The information about the backend. |
model
abstractmethod
property
Returns:
Type | Description |
---|---|
Optional[str] | The model used for the backend requests. |
target
abstractmethod
property
Returns:
Type | Description |
---|---|
str | The target location for the backend. |
type_
property
Returns:
Type | Description |
---|---|
BackendType | The type of the backend. |
available_models()
abstractmethod
async
Get the list of available models for the backend.
Returns:
Type | Description |
---|---|
List[str] | The list of available models. |
chat_completions(content, request_id=None, prompt_token_count=None, output_token_count=None, raw_content=False, **kwargs)
abstractmethod
async
Generate chat completions for the given content. Supports multiple modalities, complicated chat interfaces, and chat templates. Specifically, it requests with the content, which can be any combination of text, images, and audio provided the target model supports it, and returns the output text. Additionally, any chat templates for the model are applied within the backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content | Union[str, list[Union[str, dict[str, Union[str, dict[str, str]]], Path, Image]], Any] | The content (or list of content) to generate a completion for. This supports any combination of text, images, and audio (model dependent). Supported text only request examples: content="Sample prompt", content=["Sample prompt", "Second prompt"], content=[{"type": "text", "value": "Sample prompt"}. Supported text and image request examples: content=["Describe the image", PIL.Image.open("image.jpg")], content=["Describe the image", Path("image.jpg")], content=["Describe the image", {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}]. Supported text and audio request examples: content=["Transcribe the audio", Path("audio.wav")], content=["Transcribe the audio", {"type": "input_audio", "input_audio": {"data": f"{base64_bytes}", "format": "wav}]. Additionally, if raw_content=True then the content is passed directly to the backend without any processing. | required |
request_id | Optional[str] | The unique identifier for the request, if any. Added to logging statements and the response for tracking purposes. | None |
prompt_token_count | Optional[int] | The number of tokens measured in the prompt, if any. Returned in the response stats for later analysis, if applicable. | None |
output_token_count | Optional[int] | If supplied, the number of tokens to enforce generation of for the output for this request. | None |
kwargs | Additional keyword arguments to pass with the request. | {} |
Returns:
Type | Description |
---|---|
AsyncGenerator[Union[StreamingTextResponse, ResponseSummary], None] | An async generator that yields a StreamingTextResponse for start, a StreamingTextResponse for each received iteration, and a ResponseSummary for the final response. |
Source code in src/guidellm/backend/backend.py
check_setup()
abstractmethod
async
Check the setup for the backend. If unsuccessful, raises the appropriate exception.
Raises:
Type | Description |
---|---|
ValueError | If the setup check fails. |
create(type_, **kwargs)
classmethod
Factory method to create a backend instance based on the backend type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type_ | BackendType | The type of backend to create. | required |
kwargs | Additional arguments for backend initialization. | {} |
Returns:
Type | Description |
---|---|
Backend | An instance of a subclass of Backend. |
Raises:
Type | Description |
---|---|
ValueError | If the backend type is not registered. |
Source code in src/guidellm/backend/backend.py
prepare_multiprocessing()
abstractmethod
async
Prepare the backend for use in a multiprocessing environment. This is useful for backends that have instance state that can not be shared across processes and should be cleared out and re-initialized for each new process.
Source code in src/guidellm/backend/backend.py
register(backend_type)
classmethod
A decorator to register a backend class in the backend registry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
backend_type | BackendType | The type of backend to register. | required |
Returns:
Type | Description |
---|---|
Type[Backend] | The decorated backend class. |
Source code in src/guidellm/backend/backend.py
text_completions(prompt, request_id=None, prompt_token_count=None, output_token_count=None, **kwargs)
abstractmethod
async
Generate text only completions for the given prompt. Does not support multiple modalities, complicated chat interfaces, or chat templates. Specifically, it requests with only the prompt.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt | Union[str, list[str]] | The prompt (or list of prompts) to generate a completion for. If a list is supplied, these are concatenated and run through the model for a single prompt. | required |
request_id | Optional[str] | The unique identifier for the request, if any. Added to logging statements and the response for tracking purposes. | None |
prompt_token_count | Optional[int] | The number of tokens measured in the prompt, if any. Returned in the response stats for later analysis, if applicable. | None |
output_token_count | Optional[int] | If supplied, the number of tokens to enforce generation of for the output for this request. | None |
kwargs | Additional keyword arguments to pass with the request. | {} |
Returns:
Type | Description |
---|---|
AsyncGenerator[Union[StreamingTextResponse, ResponseSummary], None] | An async generator that yields a StreamingTextResponse for start, a StreamingTextResponse for each received iteration, and a ResponseSummary for the final response. |
Source code in src/guidellm/backend/backend.py
validate()
async
Handle final setup and validate the backend is ready for use. If not successful, raises the appropriate exception.
Source code in src/guidellm/backend/backend.py
OpenAIHTTPBackend
Bases: Backend
A HTTP-based backend implementation for requests to an OpenAI compatible server. For example, a vLLM server instance or requests to OpenAI's API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target | Optional[str] | The target URL string for the OpenAI server. ex: http://0.0.0.0:8000 | None |
model | Optional[str] | The model to use for all requests on the target server. If none is provided, the first available model will be used. | None |
api_key | Optional[str] | The API key to use for requests to the OpenAI server. If provided, adds an Authorization header with the value "Authorization: Bearer {api_key}". If not provided, no Authorization header is added. | None |
organization | Optional[str] | The organization to use for requests to the OpenAI server. For example, if set to "org_123", adds an OpenAI-Organization header with the value "OpenAI-Organization: org_123". If not provided, no OpenAI-Organization header is added. | None |
project | Optional[str] | The project to use for requests to the OpenAI server. For example, if set to "project_123", adds an OpenAI-Project header with the value "OpenAI-Project: project_123". If not provided, no OpenAI-Project header is added. | None |
timeout | Optional[float] | The timeout to use for requests to the OpenAI server. If not provided, the default timeout provided from settings is used. | None |
http2 | Optional[bool] | If True, uses HTTP/2 for requests to the OpenAI server. Defaults to True. | True |
follow_redirects | Optional[bool] | If True, the HTTP client will follow redirect responses. If not provided, the default value from settings is used. | None |
max_output_tokens | Optional[int] | The maximum number of tokens to request for completions. If not provided, the default maximum tokens provided from settings is used. | None |
extra_query | Optional[dict] | Query parameters to include in requests to the OpenAI server. If "chat_completions", "models", or "text_completions" are included as keys, the values of these keys will be used as the parameters for the respective endpoint. If not provided, no extra query parameters are added. | None |
Source code in src/guidellm/backend/openai.py
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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 |
|
info
property
Returns:
Type | Description |
---|---|
dict[str, Any] | The information about the backend. |
model
property
Returns:
Type | Description |
---|---|
Optional[str] | The model to use for all requests on the target server. If validate hasn't been called yet and no model was passed in, this will be None until validate is called to set the default. |
target
property
Returns:
Type | Description |
---|---|
str | The target URL string for the OpenAI server. |
available_models()
async
Get the available models for the target server using the OpenAI models endpoint: /v1/models
Source code in src/guidellm/backend/openai.py
chat_completions(content, request_id=None, prompt_token_count=None, output_token_count=None, raw_content=False, **kwargs)
async
Generate chat completions for the given content using the OpenAI chat completions endpoint: /v1/chat/completions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content | Union[str, list[Union[str, dict[str, Union[str, dict[str, str]]], Path, Image]], Any] | The content (or list of content) to generate a completion for. This supports any combination of text, images, and audio (model dependent). Supported text only request examples: content="Sample prompt", content=["Sample prompt", "Second prompt"], content=[{"type": "text", "value": "Sample prompt"}. Supported text and image request examples: content=["Describe the image", PIL.Image.open("image.jpg")], content=["Describe the image", Path("image.jpg")], content=["Describe the image", {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}]. Supported text and audio request examples: content=["Transcribe the audio", Path("audio.wav")], content=["Transcribe the audio", {"type": "input_audio", "input_audio": {"data": f"{base64_bytes}", "format": "wav}]. Additionally, if raw_content=True then the content is passed directly to the backend without any processing. | required |
request_id | Optional[str] | The unique identifier for the request, if any. Added to logging statements and the response for tracking purposes. | None |
prompt_token_count | Optional[int] | The number of tokens measured in the prompt, if any. Returned in the response stats for later analysis, if applicable. | None |
output_token_count | Optional[int] | If supplied, the number of tokens to enforce generation of for the output for this request. | None |
kwargs | Additional keyword arguments to pass with the request. | {} |
Returns:
Type | Description |
---|---|
AsyncGenerator[Union[StreamingTextResponse, ResponseSummary], None] | An async generator that yields a StreamingTextResponse for start, a StreamingTextResponse for each received iteration, and a ResponseSummary for the final response. |
Source code in src/guidellm/backend/openai.py
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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
|
check_setup()
async
Check if the backend is setup correctly and can be used for requests. Specifically, if a model is not provided, it grabs the first available model. If no models are available, raises a ValueError. If a model is provided and not available, raises a ValueError.
Raises:
Type | Description |
---|---|
ValueError | If no models or the provided model is not available. |
Source code in src/guidellm/backend/openai.py
prepare_multiprocessing()
async
Prepare the backend for use in a multiprocessing environment. Clears out the sync and async clients to ensure they are re-initialized for each process.
Source code in src/guidellm/backend/openai.py
text_completions(prompt, request_id=None, prompt_token_count=None, output_token_count=None, **kwargs)
async
Generate text completions for the given prompt using the OpenAI completions endpoint: /v1/completions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt | Union[str, list[str]] | The prompt (or list of prompts) to generate a completion for. If a list is supplied, these are concatenated and run through the model for a single prompt. | required |
request_id | Optional[str] | The unique identifier for the request, if any. Added to logging statements and the response for tracking purposes. | None |
prompt_token_count | Optional[int] | The number of tokens measured in the prompt, if any. Returned in the response stats for later analysis, if applicable. | None |
output_token_count | Optional[int] | If supplied, the number of tokens to enforce generation of for the output for this request. | None |
kwargs | Additional keyword arguments to pass with the request. | {} |
Returns:
Type | Description |
---|---|
AsyncGenerator[Union[StreamingTextResponse, ResponseSummary], None] | An async generator that yields a StreamingTextResponse for start, a StreamingTextResponse for each received iteration, and a ResponseSummary for the final response. |
Source code in src/guidellm/backend/openai.py
RequestArgs
Bases: StandardBaseModel
A model representing the arguments for a request to a backend. Biases towards an HTTP request, but can be used for other types of backends.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target | The target URL or function for the request. | required | |
headers | The headers, if any, included in the request such as authorization. | required | |
params | The query parameters, if any, included in the request. | required | |
payload | The payload / arguments for the request including the prompt / content and other configurations. | required | |
timeout | The timeout for the request in seconds, if any. | required | |
http2 | Whether HTTP/2 was used for the request, if applicable. | required | |
follow_redirects | Whether the request should follow redirect responses. | required |
Source code in src/guidellm/backend/response.py
ResponseSummary
Bases: StandardBaseModel
A model representing a summary of a backend request. Always returned as the final iteration of a streaming request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | The final value returned from the request. | required | |
request_args | The arguments used to make the request. | required | |
iterations | The number of iterations in the request. | required | |
start_time | The time the request started. | required | |
end_time | The time the request ended. | required | |
first_iter_time | The time the first iteration was received. | required | |
last_iter_time | The time the last iteration was received. | required | |
request_prompt_tokens | The number of tokens measured in the prompt for the request, if any. | required | |
request_output_tokens | The number of tokens enforced for the output for the request, if any. | required | |
response_prompt_tokens | The number of tokens measured in the prompt for the response, if any. | required | |
response_output_tokens | The number of tokens measured in the output for the response, if any. | required | |
request_id | The unique identifier for the request, if any. | required | |
error | The error message, if any, returned from making the request. | required |
Source code in src/guidellm/backend/response.py
output_tokens
property
The number of tokens measured in the output based on preferences for trusting the input or response.
Returns:
Type | Description |
---|---|
Optional[int] | The number of tokens in the output, if any. |
prompt_tokens
property
The number of tokens measured in the prompt based on preferences for trusting the input or response.
Returns:
Type | Description |
---|---|
Optional[int] | The number of tokens in the prompt, if any. |
StreamingTextResponse
Bases: StandardBaseModel
A model representing the response content for a streaming text request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type_ | The type of the response; either 'start' or 'iter'. | required | |
value | The value of the response up to this iteration. | required | |
start_time | The time.time() the request started. | required | |
iter_count | The iteration count for the response. For 'start' this is 0 and for the first 'iter' it is 1. | required | |
delta | The text delta added to the response for this stream iteration. | required | |
time | If 'start', the time.time() the request started. If 'iter', the time.time() the iteration was received. | required | |
request_id | The unique identifier for the request, if any. | required |