LangChain Ollama Integration
langchain-ollama is an integration package that connects LangChain, a framework for developing applications powered by language models, with Ollama, a platform for running open-source large language models (LLMs) locally. This allows developers to build AI applications that leverage local LLMs for enhanced privacy, reduced costs, and offline capabilities. The library is actively maintained with frequent releases, currently at version 1.1.0, requiring Python versions >=3.10.0 and <4.0.0.
Warnings
- breaking The primary import paths for `ChatOllama`, `OllamaLLM`, and `OllamaEmbeddings` have shifted from `langchain_community` to `langchain_ollama` (e.g., `from langchain_ollama import ChatOllama`). Older code relying on `langchain_community` imports may break or use deprecated versions.
- gotcha The `ollama` server application must be installed and running, and the desired LLM model (e.g., 'llama3') must be pulled using `ollama pull <model_name>` via the command line before `langchain-ollama` can connect to it. Failure to do so will result in connection or model not found errors.
- gotcha Ollama's native tool calling support is less mature compared to providers like OpenAI. When implementing agents that use tools, `langchain-ollama` often relies on JSON-based agent workarounds, which might have a lower success rate for complex tasks than native function calling.
- gotcha Connecting to an Ollama instance not running on the default `http://localhost:11434` requires explicitly setting the `base_url` parameter during instantiation (e.g., `ChatOllama(base_url='http://your-ollama-host:port')`). Incorrect configuration can lead to connection errors.
- deprecated The `OllamaEmbeddings` class in `langchain_community` was officially deprecated in favor of `langchain_ollama.embeddings.OllamaEmbeddings` around version `0.3.1` of `langchain-community`, with removal planned for `1.0.0` of `langchain-community`.
Install
-
pip install langchain-ollama -
curl -fsSL https://ollama.com/install.sh | sh ollama pull llama3
Imports
- ChatOllama
from langchain_ollama import ChatOllama
- OllamaLLM
from langchain_ollama.llms import OllamaLLM
- OllamaEmbeddings
from langchain_ollama.embeddings import OllamaEmbeddings
Quickstart
import os
from langchain_ollama import ChatOllama
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
# Ensure Ollama server is running and 'llama3' model is pulled (e.g., `ollama pull llama3`)
# You can check Ollama status at http://localhost:11434/
# Instantiate the ChatOllama model
# You can specify base_url if Ollama is not on default localhost:11434
llm = ChatOllama(model="llama3", base_url=os.environ.get("OLLAMA_BASE_URL", "http://localhost:11434"))
# Define a prompt template
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful AI assistant. Answer the user's questions truthfully."),
("user", "{question}")
])
# Create a simple chain
chain = prompt | llm | StrOutputParser()
# Invoke the chain
response = chain.invoke({"question": "What is the capital of France?"})
print(response)
# Example for embeddings
from langchain_ollama.embeddings import OllamaEmbeddings
embeddings_model = OllamaEmbeddings(model="llama3", base_url=os.environ.get("OLLAMA_BASE_URL", "http://localhost:11434"))
query_vector = embeddings_model.embed_query("What is the largest city in France?")
print(f"Embedding vector length: {len(query_vector)}")