Embedder¶
Pluggable text embedding with multiple provider support.
Embedder ¶
Bases: ABC
Abstract base for text embedding providers.
Subclasses must implement dimension
and embed. Override
embed_batch for providers that
support batched requests natively.
supported_models
classmethod
¶
Static catalogue of models this subclass officially supports.
Returns [] by default. Subclasses that target paid providers
with a known fixed model list (OpenAI, Gemini, Voyage, ...)
should override this so spkt init can render a picker
without hardcoding a registry in core.
Local-server subclasses (LM Studio, Ollama) should leave this
empty and rely on :meth:detect_dimension instead — the user
already chose the model on the server side.
detect_dimension
async
¶
Probe the live endpoint to determine the embedding dimension.
Calls :meth:embed once with a tiny payload and returns the
length of the resulting vector. Useful for spkt init and
similar tooling that wants to fill in dimension automatically
without making the user count it themselves.
Subclasses do not normally need to override this — but a
provider that bills per call (OpenAI, Gemini, etc.) is expected
to refuse the probe and rely on :meth:supported_models
instead, so the metered providers don't surprise users with a
cost during spkt init.
embed
abstractmethod
async
¶
Embed a single text string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Input text to embed. |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
Vector of floats with length equal to |
embed_batch
async
¶
Embed multiple texts.
Default implementation calls embed() sequentially.
Override for providers that support native batching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
list[str]
|
List of input texts. |
required |
Returns:
| Type | Description |
|---|---|
list[list[float]]
|
List of embedding vectors, one per input text. |
apply_prefix ¶
Prepend a task-type prefix appropriate for this provider.
The default implementation is a no-op. Subclasses whose models
benefit from task-type prefixes (e.g. Nomic, Cohere) should
override this or accept a prefix_style at construction time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Raw text to embed. |
required |
embedding_type
|
EmbeddingType
|
Whether the text is a stored document or a query. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Text with the appropriate prefix prepended (or unchanged). |
OpenAICompatEmbedder ¶
OpenAICompatEmbedder(*, base_url: str = 'http://localhost:1234/v1', model: str = 'text-embedding-nomic-embed-text-v1.5', dimension: int = 768, api_key: str = 'not-needed', timeout: float = 30.0, prefix_style: str = 'none')
Bases: Embedder
Embedder using any OpenAI-compatible /v1/embeddings endpoint.
Works with LM Studio, Ollama (/v1), vLLM, OpenAI, and any
service that implements the OpenAI embeddings API.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_url
|
str
|
API base URL (without trailing slash). |
'http://localhost:1234/v1'
|
model
|
str
|
Model identifier. |
'text-embedding-nomic-embed-text-v1.5'
|
dimension
|
int
|
Expected embedding dimension. |
768
|
api_key
|
str
|
Bearer token (default |
'not-needed'
|
timeout
|
float
|
HTTP request timeout in seconds. |
30.0
|
prefix_style
|
str
|
Task-type prefix style. Pick the one that matches your model family — using the wrong prefix silently degrades retrieval quality.
|
'none'
|
OllamaEmbedder ¶
OllamaEmbedder(*, base_url: str = 'http://localhost:11434', model: str = 'nomic-embed-text', dimension: int = 768, timeout: float = 30.0, prefix_style: str = 'none')
Bases: Embedder
Embedder using Ollama's native /api/embed endpoint.
Use this when connecting directly to Ollama without the OpenAI compatibility layer.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_url
|
str
|
Ollama server URL. |
'http://localhost:11434'
|
model
|
str
|
Model name as shown in |
'nomic-embed-text'
|
dimension
|
int
|
Expected embedding dimension. |
768
|
timeout
|
float
|
HTTP request timeout in seconds. |
30.0
|
prefix_style
|
str
|
Task-type prefix style (same as |
'none'
|
NullEmbedder ¶
Bases: Embedder
Returns zero vectors. For testing and when no provider is configured.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimension
|
int
|
Vector dimension (default 768). |
768
|
Factory¶
create_embedder ¶
create_embedder(provider: str, *, base_url: str = '', model: str = '', dimension: int = 768, api_key: str = 'not-needed', timeout: float = 30.0, prefix_style: str = 'none') -> Embedder | None
Factory: create an Embedder from config values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
str
|
|
required |
base_url
|
str
|
API base URL. |
''
|
model
|
str
|
Model identifier. |
''
|
dimension
|
int
|
Embedding dimension. |
768
|
api_key
|
str
|
Bearer token (OpenAI-compat only). |
'not-needed'
|
timeout
|
float
|
HTTP timeout in seconds. |
30.0
|
prefix_style
|
str
|
Task-type prefix style. One of |
'none'
|
Returns:
| Type | Description |
|---|---|
Embedder | None
|
An Embedder instance, or |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provider is unknown. |
Serialization Helpers¶
vec_to_blob ¶
Pack a float list into a little-endian binary blob for sqlite-vec.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vec
|
list[float]
|
Embedding vector. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
Binary blob suitable for |
blob_to_vec ¶
Unpack a sqlite-vec binary blob into a float list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blob
|
bytes
|
Binary blob from sqlite-vec. |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
List of floats. |