How to Apply RAG Chatbot with LangChain + Ollama
In this post, you'll learn how to build a powerful RAG (Retrieval-Augmented Generation) chatbot using LangChain and Ollama. We'll also show the full flow of how to add documents into your agent dynamically!
Let's go step-by-step.
What is RAG Chatbot?
RAG stands for Retrieval-Augmented Generation. Instead of a chatbot replying only from what it "knows" internally, it first retrieves relevant documents and then generates an intelligent, customized answer.
This approach makes your chatbot:
- More accurate
- Up-to-date with external knowledge
- Better at answering domain-specific questions
Tools We'll Use
- LangChain: Framework for chaining LLMs with retrieval, tools, agents.
- Ollama: Local LLM runner (models like
Mistral
,Llama3
, etc.). - FAISS: Local vector search for fast document retrieval.
Step 1: Setup
First, install the libraries:
pip install langchain ollama faiss-cpu
Make sure you have Ollama installed and running:
- https://ollama.com/
- Download a model (example:
mistral
,llama3
,phi3
, etc.)
Step 2: Basic RAG Chatbot Code
from langchain_community.embeddings import OllamaEmbeddings
from langchain_community.llms import Ollama
from langchain.vectorstores import FAISS
from langchain.chains import RetrievalQA
from langchain.text_splitter import CharacterTextSplitter
from langchain.docstore.document import Document
# 1. Documents
my_docs = [
Document(page_content="Python is a programming language created by Guido van Rossum."),
Document(page_content="LangChain helps build applications powered by language models."),
Document(page_content="The capital of Thailand is Bangkok."),
]
# 2. Split Documents
splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
split_docs = splitter.split_documents(my_docs)
# 3. Embedding
embedder = OllamaEmbeddings(model="nomic-embed-text")
# 4. FAISS Vector Store
vectorstore = FAISS.from_documents(split_docs, embedder)
# 5. Retriever
retriever = vectorstore.as_retriever()
# 6. LLM (Ollama)
llm = Ollama(model="mistral")
# 7. RetrievalQA Chain
rag_chain = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=retriever,
return_source_documents=True
)
# 8. Ask a Question
query = "Who created Python?"
result = rag_chain({"query": query})
print("Answer:", result["result"])
print("Sources:", result["source_documents"])
Step 3: Add New Documents to Your Agent
You can dynamically add documents to your chatbot without restarting everything.
Here is the conceptual workflow in Mermaid.js:
graph TD
A[User Uploads New Document] --> B[Split into Chunks]
B --> C[Embed Chunks]
C --> D[Add Embeddings to Vectorstore]
D --> E[Retriever Automatically Updated]
E --> F[Chatbot Now Uses New Knowledge]
Python Code to Add New Document:
# Suppose you get a new document
new_doc = Document(page_content="Django is a popular Python web framework.")
# 1. Split
new_splits = splitter.split_documents([new_doc])
# 2. Embed
new_vectors = embedder.embed_documents([doc.page_content for doc in new_splits])
# 3. Add to FAISS
vectorstore.add_documents(new_splits)
# Now your retriever has fresh knowledge instantly!
Conclusion
Building a RAG chatbot with LangChain + Ollama is powerful and flexible. You can:
- Control your own models (no external APIs)
- Add new knowledge live
- Build super domain-specific chatbots
In production, you can scale this by connecting to:
- PDF loaders
- Website scrapers
- Database retrievers
The future is open-source, private, and customizable.
Stay tuned for Part 2 where we'll make the chatbot stream responses and keep conversation memory! 🚀
Related Posts
- Building an OCPP 1.6 Central System with Flask async, WebSockets, and MongoDB
- How AI Supercharges Accounting and Inventory in Odoo (with Dev Insights)
- Building a Fullstack E-commerce System with JavaScript
- Building Agentic AI with Python, Langchain, and Ollama for eCommerce & Factory Automation
- Diagnosing the Root Cause of P0420 with Python, OBD-II, and Live Sensor Data
- How to Apply The Mom Test to Validate Your Startup Idea the Right Way
- When to Choose Rasa vs Langchain for Building Chatbots
- Introducing OCR Document Manager: Extract Text from Documents with Ease
- Testing an AI Tool That Finds Winning Products Before They Trend — Interested?
- Your Website Is Losing Leads After Hours — Here’s the Fix
- How Agentic AI is Revolutionizing Smart Farming — And Why Your Farm Needs It Now
- Automating EXFO Instruments with SCPI: A Practical Guide
- Design Patterns That Help Tame Legacy Code (With Python Examples)
- How to Safely Add New Features to Legacy Code — A Developer’s Guide
- Modernizing Legacy Software — Without Breaking Everything
- How OpenSearch Works — Architecture, Internals & Real-Time Search Explained
- Choosing the Right Strategy for Basic vs Premium Features in Django
- Transform Your Custom Furniture Business with a Modern eCommerce Platform
- Introducing simpliPOS: The Smart POS Built on ERPNext
- 🌾 Smart Farming Made Simple: A Tool to Help Farmers Track and Plan Inputs Efficiently
Our Products
Related Posts
- Building an OCPP 1.6 Central System with Flask async, WebSockets, and MongoDB
- How AI Supercharges Accounting and Inventory in Odoo (with Dev Insights)
- Building a Fullstack E-commerce System with JavaScript
- Building Agentic AI with Python, Langchain, and Ollama for eCommerce & Factory Automation
- Diagnosing the Root Cause of P0420 with Python, OBD-II, and Live Sensor Data
- How to Apply The Mom Test to Validate Your Startup Idea the Right Way
- When to Choose Rasa vs Langchain for Building Chatbots
- Introducing OCR Document Manager: Extract Text from Documents with Ease
- Testing an AI Tool That Finds Winning Products Before They Trend — Interested?
- Your Website Is Losing Leads After Hours — Here’s the Fix
- How Agentic AI is Revolutionizing Smart Farming — And Why Your Farm Needs It Now
- Automating EXFO Instruments with SCPI: A Practical Guide
- Design Patterns That Help Tame Legacy Code (With Python Examples)
- How to Safely Add New Features to Legacy Code — A Developer’s Guide
- Modernizing Legacy Software — Without Breaking Everything
- How OpenSearch Works — Architecture, Internals & Real-Time Search Explained
- Choosing the Right Strategy for Basic vs Premium Features in Django
- Transform Your Custom Furniture Business with a Modern eCommerce Platform
- Introducing simpliPOS: The Smart POS Built on ERPNext
- 🌾 Smart Farming Made Simple: A Tool to Help Farmers Track and Plan Inputs Efficiently