A FastAPI RAG (Retrieval Augmented Generation) tutorial using LangChain v1-style agents, Chroma, OpenAI embeddings, and optional conversation memory.
| Feature | Description |
|---|---|
| 🤖 Agent-Based RAG | Uses create_agent instead of chains - LLM decides when to retrieve |
| 🏠 Local Model Support | Ollama integration for free, local inference (no API key needed) |
| 📄 Advanced PDF Processing | Tables (PDFPlumber) + Diagrams (GPT-4o Vision) |
| 💬 Conversation Memory | Multi-turn chat with InMemorySaver |
| 🌊 Streaming Responses | Real-time token streaming for better UX |
| 🛠️ Multi-Tool Agents | Easily extend with custom tools |
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
cp .env.example .env
# Fill OPENAI_API_KEY in .env
uvicorn main:app --host 127.0.0.1 --port 8000pip install langchain langchain-ollama langchain-chroma langgraph
# Install Ollama: https://ollama.com/download
ollama pull llama3.2
ollama pull nomic-embed-textThen use the notebook's Ollama path for local experimentation.
pip install -qU \
langchain>=1.2.4 \
langchain-openai \
langchain-ollama \
langchain-chroma \
langchain-community \
langgraph>=1.0.0 \
chromadb \
pypdf \
pdfplumber \
pymupdf \
pillowThese checks do not require an OpenAI API key:
PYTHONPYCACHEPREFIX=/private/tmp/langchain-rag-pycache python3 -m compileall main.py rag_agent.py knowledge_base.py env_utils.py test_session_isolation.py
pytest -q| Variable | Purpose |
|---|---|
OPENAI_API_KEY |
Required for /chat, /chat/conversation, and clearing conversation sessions |
OPENAI_MODEL |
Chat model name, default gpt-4o-mini |
DATABASE_URL |
Optional PostgreSQL checkpoint persistence |
ALLOWED_ORIGINS |
Comma-separated explicit HTTP(S) CORS origins, defaults to localhost only; wildcard origins are ignored |
ENABLE_DEBUG_ENDPOINTS |
Enables /debug/langsmith and /chat/sessions when true |
DEBUG_ERRORS |
Returns redacted, truncated exception details when true; keep false in production |
- Health checks and local tests run without
OPENAI_API_KEYor LangChain imports. /documentsreturns the built-in tutorial corpus without initializing the RAG agent or requiring provider credentials.- RAG endpoints return
503untilOPENAI_API_KEYis configured. - Backend/provider exceptions are hidden by default; set
DEBUG_ERRORS=trueonly in trusted local debugging. Debug details are still redacted and truncated. - Conversation
session_idvalues are bounded and limited to letters, numbers, dots, underscores, colons, and hyphens. - Debug/admin endpoints stay hidden unless
ENABLE_DEBUG_ENDPOINTS=true.
# Agent Creation (NEW - replaces create_retrieval_chain)
from langchain.agents import create_agent
from langchain_core.tools import create_retriever_tool
from langgraph.checkpoint.memory import InMemorySaver
agent = create_agent(
model=llm, # ChatOpenAI or ChatOllama instance
tools=[retriever_tool],
system_prompt="You are...", # NOT state_modifier!
checkpointer=InMemorySaver() # For conversation memory
)
result = agent.invoke({"messages": [HumanMessage(content="question")]})
answer = result["messages"][-1].content| Chain-Based RAG | Agent-Based RAG |
|---|---|
| Always queries database | LLM decides when to retrieve |
| Wastes tokens on "hi", "thanks" | Answers simple prompts directly |
| Fixed pipeline | Flexible tool selection |
| Hard to extend | Easy to add more tools |
main.py- FastAPI app with lazy agent loadingknowledge_base.py- Built-in tutorial corpus shared by the API and RAG agentrag_agent.py- LangChain RAG agent implementationtest_session_isolation.py- Local API tests that run without OpenAI credentialslangchain_rag_tutorial_updated.ipynb- Tutorial notebook
- Python 3.12
- LangChain 1.2.4
- LangGraph 1.0.6
- OpenAI GPT-4o-mini / GPT-4o
- Ollama llama3.2 / llava
MIT
Updated January 2026 for LangChain v1.2.4