windlass.interfaces.embedding¶
embedding
¶
The embedding-model interface.
An embedding provider turns text into dense vectors. Windlass distinguishes document embeddings from query embeddings because several modern models (E5, BGE, Nomic) require different instruction prefixes for each — getting that wrong quietly halves retrieval quality, so the interface makes it explicit.
Implementers override one coroutine, :meth:Embedder.aembed_texts.
Example
from windlass.providers.embeddings.hash import HashEmbedder vectors = HashEmbedder(dimensions=8).embed(["hello", "world"]) len(vectors), len(vectors[0]) (2, 8)
Embedder
¶
Embedder(
model: str = "",
*,
dimensions: int | None = None,
batch_size: int | None = None,
normalize: bool = True,
cache: Cache | None = None,
name: str | None = None,
**config: Any
)
Bases: Component
Abstract text-embedding model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Model identifier passed to the provider. |
''
|
dimensions
|
int | None
|
Output dimensionality. Providers that expose a native value
should override :meth: |
None
|
batch_size
|
int | None
|
How many texts to send per provider request. |
None
|
normalize
|
bool
|
L2-normalise every vector. Recommended: it makes cosine similarity a plain dot product and matches what FAISS expects. |
True
|
cache
|
Cache | None
|
Optional cache for embeddings. Embeddings are deterministic, so caching them is always safe. |
None
|
name
|
str | None
|
Component name for traces. |
None
|
**config
|
Any
|
Provider-specific options. |
{}
|
Attributes:
| Name | Type | Description |
|---|---|---|
model |
Configured model identifier. |
|
batch_size |
int
|
Configured request batch size. |
normalize |
bool
|
Whether vectors are normalised on the way out. |
Example
Implementing a provider takes one method::
class MyEmbedder(Embedder):
provider_name = "mine"
async def aembed_texts(self, texts, *, kind="document"):
return [await my_sdk.embed(t) for t in texts]
Source code in src\windlass\interfaces\embedding.py
default_model
classmethod
¶
aembed_texts
abstractmethod
async
¶
Embed a batch of texts.
This is the only method a provider must implement. It receives at most
:attr:batch_size texts, already prefixed for kind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
list[str]
|
The texts to embed. Never empty. |
required |
kind
|
str
|
|
'document'
|
Returns:
| Type | Description |
|---|---|
list[list[float]]
|
One vector per input, in the same order. |
Raises:
| Type | Description |
|---|---|
ProviderError
|
For any provider-side failure. |
Source code in src\windlass\interfaces\embedding.py
dimension
¶
Return the vector dimensionality.
Determined from configuration when possible, otherwise by embedding a one-character probe and measuring the result. The probe runs at most once per instance.
Returns:
| Type | Description |
|---|---|
int
|
The number of components in each vector. |
Example
from windlass.providers.embeddings.hash import HashEmbedder HashEmbedder(dimensions=64).dimension() 64
Source code in src\windlass\interfaces\embedding.py
aembed
async
¶
aembed(
texts: Sequence[str], *, kind: str = "document", concurrency: int | None = None
) -> list[list[float]]
Embed many texts with batching, bounded concurrency and caching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
Sequence[str]
|
The texts to embed. |
required |
kind
|
str
|
|
'document'
|
concurrency
|
int | None
|
Maximum simultaneous provider requests. Defaults to the
global |
None
|
Returns:
| Type | Description |
|---|---|
list[list[float]]
|
One vector per input, in input order. |
Raises:
| Type | Description |
|---|---|
ProviderError
|
For any provider-side failure. |
ValueError
|
If |
Performance
Texts are grouped into :attr:batch_size batches and the batches
run concurrently up to concurrency. Cached texts never reach the
provider, so re-ingesting a mostly-unchanged corpus is cheap.
Example
import asyncio from windlass.providers.embeddings.hash import HashEmbedder len(asyncio.run(HashEmbedder(dimensions=8).aembed(["a", "b"]))) 2
Source code in src\windlass\interfaces\embedding.py
embed
¶
aembed_one
async
¶
Embed a single text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to embed. |
required |
kind
|
str
|
|
'document'
|
Returns:
| Type | Description |
|---|---|
list[float]
|
One vector. |