Here's a simple example where LangChain uses Ollama to summarize text from a PDF document locally.
Prerequisites
-
Install Ollama and download a model (like
llama2ormistral):# Install Ollama brew install ollama # Download Mistral model ollama pull mistral -
Install required Python packages:
pip install langchain pypdf
Code Example
from langchain.document_loaders import PyPDFLoader
from langchain.llms import Ollama
from langchain.chains.summarize import load_summarize_chain
# Step 1: Load the PDF document
pdf_path = "sample.pdf" # Replace with your file path
loader = PyPDFLoader(pdf_path)
documents = loader.load()
# Step 2: Initialize the Ollama LLM
llm = Ollama(model="mistral") # Use your downloaded model
# Step 3: Create the Summarization Chain
summarize_chain = load_summarize_chain(llm, chain_type="map_reduce")
# Step 4: Generate the Summary
summary = summarize_chain.run(documents)
print("Summary:")
print(summary)
How This Works
- PyPDFLoader reads and splits the PDF into smaller text chunks.
- The map_reduce chain first summarizes each chunk independently (map phase).
- Then it combines those smaller summaries into one final summary (reduce phase).
- LangChain sends each chunk to the Ollama API for text generation.
🔑 Advantages
- Local processing = 100% privacy.
- Works offline.
- No external API keys needed.
- Fast inference on consumer hardware.
Bonus Tip 💡
If you want streaming summaries (token-by-token output), modify the LLM call like this:
for chunk in llm.stream("Summarize the following text: " + documents[0].page_content):
print(chunk, end="")
Comments
Post a Comment