ollama_embed.py
OllamaEmbeddings Class Breakdown
Custom embedding class for LangChain, using Ollama’s /api/embed endpoint to generate vector embeddings for documents and queries.
Purpose
Provides a drop-in replacement for LangChain-compatible embedding models, allowing use of local Ollama models for efficient, private embedding.
Class: OllamaEmbeddings
Initialization
def __init__(self, model: str = "nomic-embed-text", endpoint: str = None)
| Parameter | Description |
| model | Ollama model name (default: "nomic-embed-text") |
| endpoint | Custom embedding endpoint (defaults to OLLAMA_EMBEDDING_URL from .env or http://localhost:11434/api/embed) |
Method: embed_documents()
def embed_documents(self, texts: List[str]) -> List[List[float]]
| Purpose | Embeds a list of text chunks for use in vector search |
| Input | List of strings (text chunks) |
| Output | List of vector embeddings (each embedding is a list of floats) |
| Logging | Logs the total number of documents being embedded |
- Internally calls _embed() on each string.
Method: embed_query()
def embed_query(self, text: str) -> List[float]
| Purpose | Embeds a single query string |
| Input | Single string |
| Output | A single embedding vector (list of floats) |
- Useful for semantic similarity search against embedded chunks.
Internal Method: _embed()
def _embed(self, text: str) -> List[float]
| Purpose | Core API call to Ollama’s embedding endpoint |
| Request | Sends a POST request with |
| Response | Expects JSON response with "embedding" field |
| Error Handling | Logs error and returns empty list if the request fails |
Environment Variables
Summary Table
| Method | Role |
| init() | Configure model and endpoint |
| embed_documents() | Embed a batch of text chunks |
| embed_query() | Embed a single query string |
| _embed() | Perform actual API request to Ollama |