Leanpub Header

Skip to main content

LangChain and LlamaIndex Projects Lab Book: Hooking Large Language Models Up to the Real World

Using local models using Ollama andHugging Face. Notes provided for switching to commercial inference providers like OpenAI, Anthropic, Google, etc.

This book is 100% completeLast updated on 2026-08-04

Running LLMs with your own data allows you to quickly build personalized applications.

Minimum price

Free!

$12.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
About

About

About the Book

Note: as of July 2026 this book ihas been almost completely rewritten to cover only the latest open source LangChain, LangGraph, and LlamaIndex libraries.

Share this book

License

Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License

Author

About the Author

Mark Watson

Mark Watson is a consultant specializing in LLMs, deep learning, machine learning, knowledge graphs, and general artificial intelligence software development. He uses Common Lisp, Clojure, Python, Java, Haskell, and Ruby for development.

He is the author of 20+ published books on Artificial Intelligence, Deep Learning, Java, Ruby, Machine Learning, Common LISP, Clojure, JavaScript, Semantic Web, NLP, C++, Linux, and Scheme. He has 55 US Patents.

Mark's consulting customer list includes: Google, Capital One, Olive AI, CompassLabs, Disney, Sitescout.com, Embed.ly, and Webmind Corporation.

Mark wrote ten traditional published books for McGraw Hill, Springer Verlag, J Wiley, and Morgan Kaufman publishers before adopting the LeanPub self-publishing platform.

The Leanpub Podcast

Podcast

Podcast Episode

Contents

Table of Contents

Preface

  1. Who this book is for
  2. What this book covers, and what it deliberately does not
  3. What you need to install
  4. Where the code lives
  5. Requests from the Author
  6. About the Author
  7. Book Cover
  8. Acknowledgements

The Stack We’re Building On

  1. The whole book, in one dependency list
  2. What we are deliberately not installing
  3. A note on uv and pyproject.toml
  4. The one-time setup

Large Language Model Overview

  1. Technological Change is Increasing at an Exponential Rate
  2. What LLMs Are and What They Are Not
  3. Big Tech Businesses vs. Small Startups Using Large Language Models
  4. Part I: LangChain 1.0 and LangGraph 1.0

LangChain 1.0 in One Hour

  1. The chapter directory layout
  2. Your first chat model call
  3. Swapping in a hosted model
  4. Two more Runnable methods .stream() and .batch()
  5. Prompt templates and the LCEL pipe
  6. Output parsers and structured output
  7. Tool binding
  8. What we covered

Swapping Between Local and Commercial Models

  1. Switching providers in LangChain
  2. Switching providers in LlamaIndex
  3. Wrap up

RAG Patterns with LangChain

  1. A shared corpus loader
  2. Pattern 1: naive RAG
  3. Pattern 2: retrieve wide, rerank narrow
  4. Pattern 3: hybrid dense + sparse retrieval
  5. Pattern 4: multi-query rewriting
  6. Which pattern for which situation
  7. What we covered

Running Local LLMs Using Ollama

  1. Simple Use of a Local Model Using LangChain
  2. Minimal Example Using Ollama for Retrieval Augmented Queries Against Local Documents
  3. Wrap Up for Running Local LLMs Using Ollama

Extraction of Facts and Relationships from Text Data

  1. Key Capabilities of LLMs for Fact and Relationship Extraction
  2. Techniques and Approaches
  3. Benefits
  4. Applications
  5. Example Prompts for Getting Information About a Person from Text and Generating JSON
  6. From One Record to Many: CSV to JSON
  7. What we covered

Using LLMs to Summarize Text

  1. Example Prompt
  2. Code Example

LangGraph 1.0 Fundamentals

  1. What LangGraph is, and is not
  2. Why a graph at all
  3. Setup
  4. Example 1: hello graph
  5. Example 2: state fields with reducers
  6. Example 3: conditional routing
  7. Example 4: an LLM in a node
  8. What we covered

Building a ReAct Agent with LangGraph + Ollama

  1. The shape of a ReAct loop
  2. The tools the agent will use
  3. Version 1: create_agent
  4. Version 2: the same agent, built explicitly
  5. When to reach for which version
  6. Watching each step with .stream()
  7. Two failure modes worth knowing
  8. What we covered

Durable, Restart-Safe Agents

  1. Three flavors of checkpointer
  2. The shared graph
  3. Example 1: MemorySaver and thread_id
  4. Example 2: SqliteSaver, spread across two processes
  5. Example 3: inspecting checkpoint history
  6. Two design points worth internalizing
  7. What we covered

Human-in-the-Loop Patterns

  1. Example 1: the minimum viable interrupt
  2. Example 2: a tool-approval gate
  3. Example 3: pause after a node, edit the checkpoint, resume
  4. Two mechanisms, when to reach for each
  5. What we covered

Multi-Agent Supervisor Pattern

  1. The example
  2. The specialists
  3. The supervisor and the graph
  4. Running it
  5. Watching the routing
  6. When multi-agent is worth it
  7. What we covered

Natural-Language SQLite

  1. The sample database
  2. The four SQL tools
  3. Building the agent
  4. Running it
  5. Watching the SQL get written
  6. Where to take this next
  7. What we covered

DBpedia and Wikidata as Agent Tools

  1. The example
  2. The DBpedia agent
  3. The Wikidata agent
  4. DBpedia versus Wikidata
  5. Where to take this next
  6. What we covered

A Perplexity-Style Local Search Agent

  1. The pipeline
  2. The graph
  3. Running a complete example
  4. Watching each stage
  5. Extending this
  6. Wrapping up Part I
  7. Part II: LlamaIndex 0.14 and the Workflows API

LlamaIndex

  1. What changed since the previous edition
  2. The four primitives
  3. Setup
  4. Your first LlamaIndex program
  5. Swapping in a hosted model
  6. Persist an index, reload it later
  7. Retrievers, without the LLM
  8. What we covered

Local Documents and Local Embeddings

  1. SimpleDirectoryReader deep dive
  2. Building Documents by hand
  3. Comparing embedding models
  4. Chunking with the ingestion pipeline
  5. What we covered

Choosing an Index Type

  1. SummaryIndex: every query touches every Node
  2. SimpleKeywordTableIndex: retrieval by exact keyword match
  3. QueryFusionRetriever: the practical hybrid pattern
  4. Decision tree
  5. What we covered

RAG with Reranking

  1. Baseline: no reranker
  2. Same query, with a reranker
  3. What the reranker actually costs
  4. What we covered

The Workflows API

  1. The four primitives
  2. Setup
  3. Hello workflow
  4. Chaining steps with a custom event
  5. Branching by event type
  6. A three-step LLM workflow
  7. Workflows vs LangGraph
  8. What we covered

Building an Agent as a Workflow

  1. The Tools
  2. Version 1: FunctionAgent
  3. Version 2: same agent, built explicitly
  4. When to Reach for Which Framework
  5. What we covered

Multi-Index Query Pipelines

  1. RouterQueryEngine: pick one index
  2. SubQuestionQueryEngine: decompose and combine
  3. When to reach for which
  4. What we covered

Structured Extraction

  1. One-shot extraction
  2. Batch extraction
  3. When to reach for this vs a chat model with tools
  4. What we covered

Serving a Workflow with FastAPI

  1. The moving parts
  2. The workflow being served
  3. Terminal 1: the server
  4. Terminal 2: the client
  5. Scaling from here
  6. What we covered

Using LLMs To Organize Information in Our Google Drives

  1. Setting Up Requirements
  2. Write Utility To Fetch All Text Files From Top Level Google Drive Folder
  3. Generate Vector Indices for Files in Specific Google Drive Directories
  4. Google Drive Example Wrap Up

Examples Using Hugging Face Open Source Models

  1. Using LangChain as a Wrapper for a Local Hugging Face Pipeline
  2. Creating a Custom LlamaIndex Hugging Face LLM Wrapper Class That Runs on Your Laptop

Book Wrap Up

  1. Where the book falls short
  2. Thank you

Also by the Author

Also by the Author

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub