LangChain Ollama Integration

1.1.0 · active · verified Fri Apr 10

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

Install

Imports

Quickstart

This quickstart demonstrates how to set up a `ChatOllama` instance to interact with a local LLM (e.g., Llama 3) via the Ollama server. It includes a basic chat prompt template and uses `StrOutputParser` for clean output. It also shows how to instantiate `OllamaEmbeddings` to generate vector embeddings. Before running, ensure the Ollama server is installed and running, and the specified model (e.g., 'llama3') has been pulled using `ollama pull <model_name>`.

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)}")

view raw JSON →