- LLMClient Protocol for provider-agnostic inference - VLLMClient for OpenAI-compatible /v1/chat/completions API - LLM client factory with provider routing (ollama/vllm) - VLLMConfig with VLLM_* environment variable loading - Updated extractor worker with health check and provider switching - Updated event classifier to use LLMClient protocol - Helm values for vLLM configuration - 18 unit tests + 6 property-based tests - Full backward compatibility preserved
14 KiB
Requirements Document
Introduction
Add remote vLLM support to the Stonks Oracle platform. The system currently uses Ollama exclusively for LLM inference via the /api/chat endpoint. A remote vLLM server running RedHatAI/Qwen3.6-35B-A3B-NVFP4 on a 5090 GPU with tensor parallelism is available at http://192.168.42.254:8000 and exposes an OpenAI-compatible /v1/chat/completions API. This feature introduces a provider abstraction layer so that both Ollama and vLLM backends can be used interchangeably, selected per-agent via the existing model_provider database column and environment variable configuration. The abstraction preserves all existing behavior (retry logic, JSON repair, audit trail, backoff, context window override) while adapting to the differences between the two API protocols.
Glossary
- LLM_Client: An abstract interface defining the contract for sending chat completion requests to any LLM backend. Concrete implementations exist for Ollama and vLLM.
- Ollama_Backend: The existing Ollama inference server at
ollama.ollama-service.svc.cluster.local:11434(cluster) orhttp://10.1.1.12:2701(external), using the/api/chatendpoint with Ollama-specific payload fields (think,options.num_ctx,options.num_predict). - VLLM_Backend: A remote vLLM inference server at
http://192.168.42.254:8000exposing the OpenAI-compatible/v1/chat/completionsendpoint. RunsRedHatAI/Qwen3.6-35B-A3B-NVFP4on a 5090 GPU with tensor parallelism. - Provider: A string identifier (
ollamaorvllm) that determines which LLM_Client implementation is used for a given agent. Stored in themodel_providercolumn ofai_agentsandagent_variantstables. - LLM_Config: A provider-agnostic configuration dataclass containing connection and inference parameters (base_url, model, timeout, retries, max_tokens, context_window) used to construct an LLM_Client.
- Extraction_Pipeline: The document intelligence extraction workflow in
services/extractor/client.pythat sends documents to an LLM and parses structured JSON responses. - Event_Classification_Pipeline: The macro event classification workflow in
services/extractor/event_classifier.pythat classifies global news articles via an LLM. - Agent_Config_Resolver: The
AgentConfigResolverinservices/shared/agent_config.pythat resolves runtime configuration from theai_agentsandagent_variantsdatabase tables, including themodel_providerfield. - OpenAI_Chat_Format: The request/response format used by
/v1/chat/completions— messages array with role/content,max_tokens,temperature, and response inchoices[0].message.content. - JSON_Repair: The existing
json-repairlibrary usage that fixes malformed JSON from model output, applied regardless of provider. - Model_Metadata: The
ModelMetadataPydantic model inservices/shared/schemas.pythat tracksprovider,model_name,prompt_version, andschema_versionfor audit.
Requirements
Requirement 1: Provider Abstraction Layer
User Story: As a developer, I want a provider abstraction layer that decouples LLM inference from any specific backend, so that the extraction and classification pipelines can use either Ollama or vLLM without code changes in the calling services.
Acceptance Criteria
- THE LLM_Client interface SHALL define an async method that accepts a messages list (system and user prompts), a JSON schema hint, and optional document text, and returns an attempt result containing raw output, validation report, error string, duration, and model name.
- THE LLM_Client interface SHALL define an async
closemethod for releasing underlying HTTP resources. - WHEN the Extraction_Pipeline calls the LLM, THE Extraction_Pipeline SHALL use the LLM_Client interface instead of calling Ollama-specific endpoints directly.
- WHEN the Event_Classification_Pipeline calls the LLM, THE Event_Classification_Pipeline SHALL use the LLM_Client interface instead of calling
_call_ollama()directly. - THE Ollama_Backend implementation of LLM_Client SHALL preserve the existing
/api/chatpayload structure includingthink: false,stream: false,options.num_predict, andoptions.num_ctx. - THE VLLM_Backend implementation of LLM_Client SHALL send requests to
/v1/chat/completionsusing the OpenAI_Chat_Format withmodel,messages,max_tokens, andtemperaturefields. - FOR ALL valid prompt inputs, sending a prompt through the Ollama_Backend and parsing the response SHALL produce the same ExtractionAttempt structure as the current
_call_ollama()method (round-trip equivalence with existing behavior).
Requirement 2: vLLM Client Implementation
User Story: As a developer, I want a vLLM client that communicates with the remote vLLM server using the OpenAI-compatible API, so that the platform can leverage the 5090 GPU for inference.
Acceptance Criteria
- THE VLLM_Backend SHALL send POST requests to
{base_url}/v1/chat/completionswith a JSON payload containingmodel,messages(array of role/content objects),max_tokens, andtemperature. - THE VLLM_Backend SHALL extract the response content from
choices[0].message.contentin the OpenAI-compatible response format. - THE VLLM_Backend SHALL apply the same markdown fence stripping logic as the Ollama_Backend to handle model output wrapped in
json ...blocks. - THE VLLM_Backend SHALL apply the same JSON_Repair logic as the Ollama_Backend to fix malformed JSON in model output.
- WHEN the vLLM server returns an HTTP timeout, THE VLLM_Backend SHALL report the error as
timeoutin the attempt result, consistent with the Ollama_Backend error format. - WHEN the vLLM server returns an HTTP error status, THE VLLM_Backend SHALL report the error as
http_{status_code}in the attempt result, consistent with the Ollama_Backend error format. - WHEN the vLLM server returns an empty
choicesarray or missingcontent, THE VLLM_Backend SHALL report the error asempty_model_response. - IF the vLLM server is unreachable, THEN THE VLLM_Backend SHALL report the error as
connection_error: {details}, consistent with the Ollama_Backend error format. - THE VLLM_Backend SHALL use the same
httpx.AsyncClienttimeout configuration as the Ollama_Backend, derived from the LLM_Config timeout value. - THE VLLM_Backend SHALL support an optional
temperatureparameter from the resolved agent config, defaulting to 0.7 when not specified.
Requirement 3: Provider-Aware Configuration
User Story: As an operator, I want to configure the vLLM backend via environment variables and database agent config, so that I can switch providers without code changes.
Acceptance Criteria
- THE Configuration SHALL include a
VLLMConfigdataclass with fields:base_url(defaulthttp://192.168.42.254:8000),model(defaultRedHatAI/Qwen3.6-35B-A3B-NVFP4),timeout(default 120),max_retries(default 2),retry_base_delay,retry_max_delay,retry_backoff_multiplier,max_tokens(default 32768), andtemperature(default 0.7). - THE Configuration SHALL load VLLMConfig values from environment variables prefixed with
VLLM_(e.g.,VLLM_BASE_URL,VLLM_MODEL,VLLM_TIMEOUT), following the same pattern as OllamaConfig. - THE AppConfig dataclass SHALL include a
vllmfield of type VLLMConfig alongside the existingollamafield. - WHEN the Agent_Config_Resolver resolves a
model_providervalue ofvllm, THE service SHALL use the VLLMConfig base_url and construct a VLLM_Backend client instead of an Ollama_Backend client. - WHEN the Agent_Config_Resolver resolves a
model_providervalue ofollamaor when nomodel_provideris specified, THE service SHALL continue to use the OllamaConfig and Ollama_Backend client as the default. - THE
_build_ollama_config_from_resolvedfunction inservices/extractor/main.pySHALL be generalized to a provider-aware factory that returns the appropriate config and client type based on the resolvedmodel_provider.
Requirement 4: Provider Selection in Extractor Worker
User Story: As a developer, I want the extractor worker to select the correct LLM client based on the resolved agent config provider, so that each agent can independently use Ollama or vLLM.
Acceptance Criteria
- WHEN the extractor worker starts, THE worker SHALL construct the default LLM_Client based on the environment variable configuration (defaulting to Ollama_Backend).
- WHEN the Agent_Config_Resolver returns a resolved config with
model_provider = "vllm"for thedocument-extractorslug, THE worker SHALL construct a VLLM_Backend client using the VLLMConfig base_url and the resolved model_name. - WHEN the Agent_Config_Resolver returns a resolved config with
model_provider = "vllm"for theevent-classifierslug, THE worker SHALL construct a VLLM_Backend client for the event classification pipeline. - WHEN the resolved config changes provider during a config refresh cycle (every 100 jobs), THE worker SHALL close the old LLM_Client and construct a new one matching the updated provider.
- WHEN the resolved config changes from
ollamatovllmor vice versa, THE worker SHALL log the provider switch at INFO level including the old and new provider, model name, and variant ID.
Requirement 5: Retry and Error Handling Parity
User Story: As a developer, I want the vLLM client to use the same retry logic, backoff strategy, and error classification as the Ollama client, so that reliability behavior is consistent across providers.
Acceptance Criteria
- THE VLLM_Backend SHALL use the same exponential backoff computation as the Ollama_Backend, using
retry_base_delay,retry_max_delay, andretry_backoff_multiplierfrom the LLM_Config. - THE VLLM_Backend SHALL classify HTTP 400, 401, 403, 404, and 422 errors as non-retryable, consistent with the Ollama_Backend.
- THE VLLM_Backend SHALL classify HTTP 500, 502, 503, 429, timeout, and connection errors as retryable, consistent with the Ollama_Backend.
- WHEN the VLLM_Backend encounters a retryable error, THE Extraction_Pipeline SHALL retry up to
max_retriestimes with exponential backoff, preserving each attempt in the audit trail. - WHEN the VLLM_Backend encounters a non-retryable error, THE Extraction_Pipeline SHALL stop retries immediately and record the attempt as non-retryable.
- FOR ALL error types, the VLLM_Backend error string format SHALL match the Ollama_Backend error string format so that
_is_retryable()works without modification.
Requirement 6: Audit Trail and Model Metadata
User Story: As a developer, I want the audit trail and model metadata to correctly reflect which provider and model were used for each extraction, so that I can trace results back to the specific backend.
Acceptance Criteria
- WHEN the VLLM_Backend completes an extraction attempt, THE attempt record SHALL include the vLLM model name in the
modelfield. - WHEN an extraction or classification succeeds via the VLLM_Backend, THE Model_Metadata in the result SHALL have
providerset to"vllm"andmodel_nameset to the vLLM model identifier. - WHEN the
agent_performance_logrecords an invocation that used the VLLM_Backend, THE log entry SHALL be attributed to the correct agent_id and variant_id, consistent with Ollama_Backend logging. - THE MinIO prompt and result artifacts persisted by the Event_Classification_Pipeline SHALL include the provider name and model name in the stored JSON, regardless of which backend was used.
Requirement 7: Health Check and Connectivity Validation
User Story: As an operator, I want the system to validate connectivity to the vLLM server at startup, so that misconfiguration is detected early rather than failing silently on the first inference request.
Acceptance Criteria
- WHEN the extractor worker starts and the resolved or default config specifies
model_provider = "vllm", THE worker SHALL send a GET request to{vllm_base_url}/v1/modelsto verify the vLLM server is reachable. - IF the vLLM health check fails at startup, THEN THE worker SHALL log a WARNING and fall back to the Ollama_Backend, continuing operation with degraded capability.
- IF the vLLM health check succeeds, THEN THE worker SHALL log an INFO message confirming the vLLM connection including the server URL and available model name.
- THE health check SHALL use a timeout of 10 seconds to avoid blocking worker startup on an unresponsive server.
Requirement 8: Context Window and Token Handling for vLLM
User Story: As a developer, I want the vLLM client to handle context window and token limits appropriately for the vLLM API, so that large documents are processed correctly on the remote GPU.
Acceptance Criteria
- WHEN the resolved agent config specifies a non-zero
context_window, THE VLLM_Backend SHALL omit thenum_ctxOllama-specific option and instead rely on the vLLM server's model configuration for context window sizing. - THE VLLM_Backend SHALL pass
max_tokensin the OpenAI-compatible request payload to control the maximum number of output tokens generated. - WHEN the resolved agent config specifies a non-zero
input_token_limit, THE Extraction_Pipeline SHALL truncate the input text before sending it to the VLLM_Backend, using the same truncation logic as for the Ollama_Backend. - WHEN the resolved agent config specifies a non-zero
token_budget, THE worker SHALL enforce the same hourly token budget check for vLLM invocations as for Ollama invocations.
Requirement 9: Backward Compatibility
User Story: As a developer, I want the vLLM integration to be fully backward compatible, so that existing Ollama-based deployments continue to work without any configuration changes.
Acceptance Criteria
- WHEN no
VLLM_BASE_URLenvironment variable is set and no agent config specifiesmodel_provider = "vllm", THE system SHALL behave identically to the current Ollama-only implementation. - THE existing
OllamaConfigdataclass and its environment variable loading SHALL remain unchanged. - THE existing
OllamaClientclass SHALL continue to function for Ollama-specific usage, with the LLM_Client interface added as a compatible layer on top. - THE existing test suite in
tests/test_ollama_client.pySHALL continue to pass without modification. - WHEN the
model_providercolumn inai_agentsoragent_variantscontains"ollama"or NULL, THE system SHALL use the Ollama_Backend, preserving current behavior. - THE database migration for this feature SHALL NOT alter existing table structures; it SHALL only add new columns or tables if needed.