Backends

OpenAI-compatible HTTP API backend plugin.

class nlp_shap.backends.api.ApiBackend(config: BackendConfig, *, transport: Any | None = None)[source]

Bases: object

Generate text through an OpenAI-compatible /chat/completions endpoint.

property model_id: str

Return the configured remote model identifier.

async generate(snapshot: ConversationSnapshot, max_new_tokens: int, temperature: float, top_k: int) GenerationRecord[source]

Generate assistant text for snapshot via the HTTP API.

async aclose() None[source]

Close the underlying HTTP client.

Chat-completions request and response helpers.

nlp_shap.backends.api.payload.build_chat_payload(model_id: str, messages: list[dict[str, str]], max_new_tokens: int, temperature: float, top_k: int) dict[str, Any][source]

Build an OpenAI-style chat-completions JSON body.

nlp_shap.backends.api.payload.payload_cache_key(payload: dict[str, Any]) str[source]

Return a stable SHA256 key for deterministic request deduplication.

nlp_shap.backends.api.payload.extract_message_content(data: dict[str, Any]) str[source]

Parse assistant text from a chat-completions JSON response.

LM Studio backend plugin.

class nlp_shap.backends.lmstudio.LmStudioBackend(config: BackendConfig)[source]

Bases: object

Generate text through a local LM Studio API server.

property model_id: str

Return the configured LM Studio model identifier.

async generate(snapshot: ConversationSnapshot, max_new_tokens: int, temperature: float, top_k: int) GenerationRecord[source]

Generate assistant text for snapshot via LM Studio.

async aclose() None[source]

Close the underlying LM Studio client connection.

Mock backend exports.

class nlp_shap.backends.mock.MockBackend(config: BackendConfig | None = None, *, model_id: str = 'mock')[source]

Bases: object

Hash-based generative backend with no external model dependencies.

property model_id: str

Return the backend model identifier.

async generate(snapshot: ConversationSnapshot, max_new_tokens: int, temperature: float, top_k: int) GenerationRecord[source]

Return deterministic generation output for snapshot.

Hugging Face causal language model backend (text-only).

class nlp_shap.backends.transformers.text.TransformersTextBackend(config: BackendConfig)[source]

Bases: object

Generate text with a local Hugging Face causal language model.

property model_id: str

Return the configured Hugging Face model identifier.

property kv_cache_hits: int

Return the number of prefix-cache hits during this backend lifetime.

set_kv_cache_enabled(enabled: bool) None[source]

Enable or disable prefix-cache reuse for coalition generation.

async generate(snapshot: ConversationSnapshot, max_new_tokens: int, temperature: float, top_k: int) GenerationRecord[source]

Generate assistant text for snapshot with optional prefix caching.

async aclose() None[source]

Release loaded model references.