dragon.ai.inference.llm_engine.LLMInferenceEngine
- class LLMInferenceEngine[source]
Bases:
objectHandles LLM inference using vLLM in a tensor-parallel environment.
This class is responsible ONLY for LLM inference, completely separated from batching, guardrails, and other preprocessing logic.
Engine Modes
The engine supports two operational modes controlled by the
use_async_streamingconfig flag:Synchronous Mode (
use_async_streaming: false, default): Uses vLLM’s synchronousLLMengine. Best for batch workloads. Callgenerate()for single or batched requests.Async Streaming Mode (
use_async_streaming: true): Uses vLLM’s V1AsyncLLMengine for token-by-token streaming. Best for interactive workloads requiring low latency. Callgenerate_stream()for streaming orgenerate_single()for non-streaming responses.
Note
Async streaming and batching are mutually exclusive. Configuration validation will reject
use_async_streaming: truewithinput_batching.toggle_on: true.Request Routing
use_async_streaming``| ``streamMethod
false
false
false
true
Error
true
false
true
true
- __init__(model_config: ModelConfig, batching_config: BatchingConfig, hostname: str , devices: List [int ])[source]
Initialize the LLM inference engine.
- Parameters:
model_config (ModelConfig) – Model configuration.
batching_config (BatchingConfig) – Batching configuration (used for
max_num_seqs).hostname (str ) – Current process hostname.
Methods
__init__(model_config, batching_config, ...)Initialize the LLM inference engine.
generate(prompts[, json_schemas])Generate responses for a batch of prompts.
generate_single(prompt[, json_schema])Generate a complete response for a single prompt (non-streaming).
generate_stream(prompt[, json_schema])Generate a streaming response for a single prompt.
Return the tokenizer from the underlying vLLM engine.
Initialize the vLLM model and sampling parameters.
shutdown()Shutdown the LLM engine and release resources.
- __init__(model_config: ModelConfig, batching_config: BatchingConfig, hostname: str , devices: List [int ])[source]
Initialize the LLM inference engine.
- Parameters:
model_config (ModelConfig) – Model configuration.
batching_config (BatchingConfig) – Batching configuration (used for
max_num_seqs).hostname (str ) – Current process hostname.
- initialize() None [source]
Initialize the vLLM model and sampling parameters.
This should be called within the worker process to avoid serialization issues with CUDA objects.
- generate(prompts: List [str ], json_schemas: List = None) Tuple [List [str ], Dict [str , float ]][source]
Generate responses for a batch of prompts.
- Parameters:
- Returns:
Tuple
(responses, metrics)whereresponsesis a list of generated strings andmetricsis a dictionary of performance metrics.- Return type:
- generate_single(prompt: str , json_schema: dict | None = None) Tuple [str , Dict [str , float ]][source]
Generate a complete response for a single prompt (non-streaming).
This method is used when the AsyncLLM engine is active but the HTTP request has
stream=false. It runs the async generator to completion and returns the final response.- Parameters:
- Returns:
Tuple
(response, metrics)whereresponseis the complete generated string andmetricsis a dictionary of performance metrics.- Return type:
- Raises:
RuntimeError – If AsyncLLM engine is not available.
- generate_stream(prompt: str , json_schema: dict | None = None) Iterator [StreamChunk][source]
Generate a streaming response for a single prompt.
Yields
StreamChunkobjects as tokens are generated. The final chunk hasis_finished=Trueand includes metrics.Requires
use_async_streaming=Truein config to enable the AsyncLLM (V1 engine) backend. Streaming is not available when using the synchronous LLMEngine.Streaming is single-request only (no batching) to ensure low latency token delivery.
- Parameters:
- Yields:
StreamChunk objects containing incremental text.
- Return type:
Iterator[StreamChunk]
- Raises:
RuntimeError – If
initialize()has not been called or if AsyncLLM is not available.
- get_tokenizer()[source]
Return the tokenizer from the underlying vLLM engine.
The inference worker uses this to apply a model-specific chat template for requests that arrive as OpenAI-style message lists. The tokenizer is only available after
initialize()has constructed the vLLM engine instance.- Returns:
Tokenizer owned by the vLLM engine.
- Return type:
transformers.PreTrainedTokenizerBase
- Raises:
RuntimeError – If
initialize()has not been called.