rag.embed

Backward-compatibility shim. Embeddings has moved to ffai.core.embeddings.

Backward-compatibility shim. Embeddings has moved to ffai.core.embeddings.

class Embeddings(model='mistral/mistral-embed', api_key=None, api_base=None, cache_enabled=True, cache_size=256, device='cpu', **kwargs)[source]

Bases: object

Compute text embeddings using local or remote models.

Supports local sentence-transformer models (prefix local/) and remote providers via LiteLLM (e.g. mistral/mistral-embed, openai/text-embedding-3-small). Results are optionally cached in an LRU-style in-memory cache.

Parameters:
  • model (str) – Model identifier. Use local/<name> for local sentence-transformer models, or <provider>/<model> for remote APIs.

  • api_key (str | None) – API key for the remote provider. If None, the key is read from a provider-specific environment variable.

  • api_base (str | None) – Optional custom API base URL.

  • cache_enabled (bool) – Whether to cache embedding results in memory.

  • cache_size (int) – Maximum number of entries in the embedding cache.

  • device (str) – Torch device for local models (e.g. "cpu", "cuda").

  • **kwargs (Any) – Extra keyword arguments forwarded to the LiteLLM embedding call.

async aembed(texts)[source]

Compute embeddings for one or more texts asynchronously.

Parameters:

texts (str | list[str]) – A single text string or a list of text strings.

Returns:

List of embedding vectors, one per input text.

Return type:

list[list[float]]

async aembed_single(text)[source]

Compute the embedding for a single text asynchronously.

Parameters:

text (str) – The text to embed.

Returns:

The embedding vector.

Return type:

list[float]

embed(texts)[source]

Compute embeddings for one or more texts synchronously.

Parameters:

texts (str | list[str]) – A single text string or a list of text strings.

Returns:

List of embedding vectors, one per input text.

Return type:

list[list[float]]

embed_single(text)[source]

Compute the embedding for a single text synchronously.

Parameters:

text (str) – The text to embed.

Returns:

The embedding vector.

Return type:

list[float]

clear_cache()[source]

Clear the embedding cache.

Returns:

Number of entries that were in the cache before clearing.

Return type:

int

cache_stats()[source]

Return statistics about the embedding cache.

Returns:

Dictionary with keys enabled, max_size, and entries.

Return type:

dict[str, Any]

static cosine_similarity(a, b)[source]

Compute cosine similarity between two vectors.

Parameters:
Returns:

Cosine similarity score in the range [-1, 1]. Returns 0.0 if either vector has zero magnitude.

Return type:

float

property provider: str
property is_local: bool