Embedding Providers¶
The providers module defines an abstract base class for embedding providers and three concrete implementations for OpenAI, Ollama, and FastEmbed.
Quick Start¶
from markdown_vault_mcp.providers import get_embedding_provider
# Auto-detect based on environment variables
provider = get_embedding_provider()
# Embed a batch of texts
vectors = provider.embed(["hello world", "example text"])
print(f"Dimension: {provider.dimension}")
Provider Selection¶
The get_embedding_provider() function auto-detects the best available provider:
- OpenAI — if
OPENAI_API_KEYis set - Ollama — if
OLLAMA_HOSTis reachable - FastEmbed — if the package is installed
Override with EMBEDDING_PROVIDER=openai|ollama|fastembed.
API Reference¶
EmbeddingProvider
¶
Bases: ABC
Abstract base class for embedding providers.
dimension
abstractmethod
property
¶
Embedding dimension size.
Returns:
| Type | Description |
|---|---|
int
|
Integer dimension of each embedding vector. |
embed(texts)
abstractmethod
¶
Embed a batch of texts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
list[str]
|
List of strings to embed. |
required |
Returns:
| Type | Description |
|---|---|
list[list[float]]
|
List of embedding vectors, one per input text. |
OllamaProvider()
¶
Bases: EmbeddingProvider
Embedding provider backed by the Ollama REST API.
Configuration via environment variables:
OLLAMA_HOST: base URL of the Ollama server (default:http://localhost:11434).MARKDOWN_VAULT_MCP_OLLAMA_MODEL: model name to use (default:nomic-embed-text).MARKDOWN_VAULT_MCP_OLLAMA_CPU_ONLY: set totrueto force CPU-only inference (default:false).
Initialise OllamaProvider from environment variables.
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
Source code in src/markdown_vault_mcp/providers.py
dimension
property
¶
Embedding dimension size.
Embeds a test string on first access to determine the dimension.
Returns:
| Type | Description |
|---|---|
int
|
Integer dimension of each embedding vector. |
embed(texts)
¶
Embed a batch of texts via the Ollama REST API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
list[str]
|
List of strings to embed. |
required |
Returns:
| Type | Description |
|---|---|
list[list[float]]
|
List of embedding vectors, one per input text. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the Ollama API returns an error response. |
Source code in src/markdown_vault_mcp/providers.py
OpenAIProvider()
¶
Bases: EmbeddingProvider
Embedding provider backed by the OpenAI Embeddings API.
Configuration via environment variables:
OPENAI_API_KEY: required API key.
Uses the text-embedding-3-small model.
Initialise OpenAIProvider from environment variables.
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
RuntimeError
|
If |
Source code in src/markdown_vault_mcp/providers.py
dimension
property
¶
Embedding dimension size.
Embeds a test string on first access to determine the dimension.
Returns:
| Type | Description |
|---|---|
int
|
Integer dimension of each embedding vector. |
embed(texts)
¶
Embed a batch of texts via the OpenAI Embeddings API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
list[str]
|
List of strings to embed. |
required |
Returns:
| Type | Description |
|---|---|
list[list[float]]
|
List of embedding vectors in input order. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the OpenAI API returns an error response. |
Source code in src/markdown_vault_mcp/providers.py
FastEmbedProvider(model_name=None, cache_dir=None)
¶
Bases: EmbeddingProvider
Embedding provider backed by the local fastembed library.
The fastembed package is imported lazily at instantiation
time so that it does not need to be installed unless this provider is used.
Initialise FastEmbed model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str | None
|
FastEmbed model identifier. |
None
|
cache_dir
|
str | None
|
Optional model cache directory. |
None
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
Source code in src/markdown_vault_mcp/providers.py
dimension
property
¶
Embedding dimension size from the loaded model.
Returns:
| Type | Description |
|---|---|
int
|
Integer dimension of each embedding vector. |
embed(texts)
¶
Embed a batch of texts using the local fastembed model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
list[str]
|
List of strings to embed. |
required |
Returns:
| Type | Description |
|---|---|
list[list[float]]
|
List of embedding vectors, one per input text. |
Source code in src/markdown_vault_mcp/providers.py
get_embedding_provider()
¶
Auto-detect and return an embedding provider.
Checks the EMBEDDING_PROVIDER environment variable first. When that
variable is not set, probes for available providers in this order:
- If
OPENAI_API_KEYis set → :class:OpenAIProvider. - If Ollama is reachable at
OLLAMA_HOST→ :class:OllamaProvider. - If
fastembedcan be imported → :class:FastEmbedProvider. - Raises :class:
RuntimeErrorwith installation instructions.
Returns:
| Type | Description |
|---|---|
EmbeddingProvider
|
An initialised :class: |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no provider is available and |
ValueError
|
If |
Source code in src/markdown_vault_mcp/providers.py
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | |