Skip to content

Backends

Optional ML / LLM backends implementing the pluggable detector interfaces.

Each backend lives in a submodule and requires its own optional extra ([llm], etc.). Core installs are unaffected — backends import lazily.

Embeddings similarity

EmbeddingsSimilarity

EmbeddingsSimilarity(*, model_name: str | None = None, device: str | None = None, cache_size: int = 10000, model: _Encoder | None = None)

Cosine-similarity backend using sentence-transformer embeddings.

This class is callable — instances conform to the SimilarityFn protocol and can be passed directly to DeathLoopDetector(similarity_fn=...).

Embeddings are cached by text so repeated comparisons against the same response don't re-run the model.

Initialize the backend.

Parameters:

Name Type Description Default
model_name str | None

Any Hugging Face sentence-transformers model id. Defaults to all-MiniLM-L6-v2 (90 MB, CPU-friendly).

None
device str | None

"cpu", "cuda", "mps", etc. Passed to the model.

None
cache_size int

Maximum distinct texts to keep embeddings for. LRU.

10000
model _Encoder | None

Pre-built encoder instance. If provided, model_name and device are ignored. Useful for tests and for reusing a single model across multiple detectors.

None