dspy.retrievers.Embeddings¶
dspy.Embeddings(corpus: list[str], embedder, k: int = 5, callbacks: list[Any] | None = None, cache: bool = False, brute_force_threshold: int = 20000, normalize: bool = True)
¶
Source code in dspy/retrievers/embeddings.py
Functions¶
__call__(query: str)
¶
forward(query: str)
¶
from_saved(path: str, embedder)
classmethod
¶
Create an Embeddings instance from a saved index.
This is the recommended way to load saved embeddings as it creates a new instance without unnecessarily computing embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Directory path where the embeddings were saved |
required |
embedder
|
The embedder function to use for new queries |
required |
Returns:
| Type | Description |
|---|---|
|
Embeddings instance loaded from disk |
Example
Source code in dspy/retrievers/embeddings.py
load(path: str, embedder)
¶
Load the embeddings index from disk into the current instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Directory path where the embeddings were saved |
required |
embedder
|
The embedder function to use for new queries |
required |
Returns:
| Name | Type | Description |
|---|---|---|
self |
Returns self for method chaining |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the save directory or required files don't exist |
ValueError
|
If the saved config is invalid or incompatible |
Source code in dspy/retrievers/embeddings.py
save(path: str)
¶
Save the embeddings index to disk.
This saves the corpus, embeddings, FAISS index (if present), and configuration to allow for fast loading without recomputing embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Directory path where the embeddings will be saved |
required |
Source code in dspy/retrievers/embeddings.py
:::