How to use Ollama embeddings in LangChain
Quick answer
Use the
OllamaEmbeddings class from langchain_community.embeddings.ollama to generate embeddings with Ollama models in LangChain. Instantiate it and call embed_documents or embed_query to get vector embeddings for your text data.PREREQUISITES
Python 3.8+pip install langchain_communityOllama installed and running locally or accessiblepip install langchain
Setup
Install the required packages and ensure Ollama is installed and running locally or accessible via its API. Ollama embeddings require the langchain_community package which contains the OllamaEmbeddings wrapper.
pip install langchain langchain_community Step by step
Use the OllamaEmbeddings class to generate embeddings for documents or queries. Below is a complete example that embeds a list of texts and prints the resulting vectors.
from langchain_community.embeddings.ollama import OllamaEmbeddings
# Initialize Ollama embeddings (default model can be specified)
embeddings = OllamaEmbeddings(model="llama2")
# Sample documents to embed
texts = [
"LangChain makes working with LLMs easier.",
"Ollama provides local LLM and embedding models.",
"Embeddings are useful for semantic search."
]
# Generate embeddings for documents
vectors = embeddings.embed_documents(texts)
for i, vec in enumerate(vectors):
print(f"Embedding for text {i+1}:", vec[:5], "... (truncated)") output
Embedding for text 1: [0.0123, -0.0456, 0.0789, -0.0345, 0.0567] ... (truncated) Embedding for text 2: [0.0234, -0.0678, 0.0890, -0.0123, 0.0456] ... (truncated) Embedding for text 3: [0.0345, -0.0789, 0.0567, -0.0234, 0.0678] ... (truncated)
Common variations
You can specify different Ollama models by passing the model parameter to OllamaEmbeddings. For async usage, wrap calls in async functions and use await if supported. Ollama embeddings integrate seamlessly with LangChain vectorstores like FAISS or Chroma for semantic search.
from langchain_community.embeddings.ollama import OllamaEmbeddings
# Use a different model
embeddings = OllamaEmbeddings(model="llama2-13b")
# Example embedding a query
query_vector = embeddings.embed_query("What is LangChain?")
print(query_vector[:5], "... (truncated)") output
[0.0156, -0.0345, 0.0678, -0.0234, 0.0456] ... (truncated)
Troubleshooting
- If you get connection errors, ensure Ollama is running locally or your API endpoint is reachable.
- Verify the model name is correct and supported by your Ollama installation.
- Check your Python environment has
langchain_communityinstalled. - Embedding vectors are typically high-dimensional floats; if output is empty or errors occur, confirm your input texts are non-empty strings.
Key Takeaways
- Use
OllamaEmbeddingsfromlangchain_communityto generate embeddings in LangChain. - Specify the Ollama model via the
modelparameter for flexibility. - Integrate embeddings with LangChain vectorstores for semantic search workflows.