Implementation:Langchain ai Langchain OllamaEmbeddings
| Knowledge Sources | |
|---|---|
| Domains | Embeddings, Ollama, Local Inference |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
OllamaEmbeddings is a LangChain embedding model integration that generates text embeddings using a locally running Ollama server.
Description
The OllamaEmbeddings class, defined in the langchain-ollama partner package, extends both BaseModel (Pydantic) and Embeddings (LangChain core). It connects to a local Ollama server via the official ollama Python client to generate embeddings using locally hosted models. The class supports both synchronous and asynchronous embedding operations, configurable sampling parameters (temperature, top_k, top_p, mirostat, etc.), custom httpx client kwargs, and URL-based authentication for proxied Ollama servers. It optionally validates that the specified model exists locally on initialization.
Usage
Import this class when you need to generate text embeddings using models hosted locally through Ollama, such as for semantic search or RAG pipelines in air-gapped or privacy-sensitive environments.
Code Reference
Source Location
- Repository: Langchain_ai_Langchain
- File:
libs/partners/ollama/langchain_ollama/embeddings.py - Lines: 1-332
Signature
class OllamaEmbeddings(BaseModel, Embeddings):
model: str
validate_model_on_init: bool = False
base_url: str | None = None
client_kwargs: dict | None = {}
async_client_kwargs: dict | None = {}
sync_client_kwargs: dict | None = {}
mirostat: int | None = None
mirostat_eta: float | None = None
mirostat_tau: float | None = None
num_ctx: int | None = None
num_gpu: int | None = None
keep_alive: int | None = None
num_thread: int | None = None
repeat_last_n: int | None = None
repeat_penalty: float | None = None
temperature: float | None = None
stop: list[str] | None = None
tfs_z: float | None = None
top_k: int | None = None
top_p: float | None = None
Import
from langchain_ollama import OllamaEmbeddings
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | str | Yes | Name of the Ollama model to use for generating embeddings. |
| base_url | str or None | No | Base URL where the Ollama server is hosted. Defaults to the Ollama client default. Supports userinfo auth in the URL.
|
| validate_model_on_init | bool | No | Whether to validate the model exists locally on initialization. Defaults to False. |
| client_kwargs | dict or None | No | Additional kwargs passed to both sync and async httpx clients. |
| temperature | float or None | No | Sampling temperature. Higher values produce more creative output. |
| num_ctx | int or None | No | Context window size for token generation. Defaults to 2048. |
| keep_alive | int or None | No | Duration in seconds the model stays loaded in memory after a request. |
| top_k | int or None | No | Limits next-token selection to K most probable tokens. Defaults to 40. |
| top_p | float or None | No | Nucleus sampling parameter. Defaults to 0.9. |
Outputs
| Name | Type | Description |
|---|---|---|
| embed_documents | list[list[float]] | Returns a list of embedding vectors, one per input document. |
| embed_query | list[float] | Returns a single embedding vector for a query text. |
Usage Examples
Basic Usage
from langchain_ollama import OllamaEmbeddings
embed = OllamaEmbeddings(model="llama3")
# Embed a single query
vector = embed.embed_query("The meaning of life is 42")
print(vector[:3])
# Embed multiple documents
vectors = embed.embed_documents(["Document 1...", "Document 2..."])
print(len(vectors))
Async Usage
from langchain_ollama import OllamaEmbeddings
embed = OllamaEmbeddings(model="llama3")
vector = await embed.aembed_query("The meaning of life is 42")
print(vector[:3])