Leanpub Header

Skip to main content

Building AI Search Systems

A Practical Guide to Production-Ready AI Search & Analytics Pipelines in Python

Build production-ready AI applications from first principles. Using Querybase as a practical case study, this book teaches Python, software architecture, retrieval, and deterministic AI pipelines. Learn to design maintainable, scalable systems without relying on framework magic or black-box abstractions.

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
About

About

About the Book

Artificial intelligence is changing how we build software, but the engineering principles behind reliable systems have not changed. This book teaches those principles through the design and implementation of Querybase, a production-ready AI search and analytics system built entirely in Python. You'll build a pipeline that classifies a question, retrieves relevant evidence through embedding-based search, generates grounded answers, and turns every interaction into insight about what your content is missing. Along the way, the focus stays on the architecture beneath it: separation of concerns, layering, data contracts, validation, evaluation, and testing.

Whether you are an experienced Python developer exploring AI or a software engineer building maintainable LLM applications, this book provides both the practical skills and the architectural mindset needed to design modern AI systems with confidence.

Share this book

Author

About the Author

D. W. Collins

Dan Collins is a systems architect and practitioner focused on building reliable, controllable generative AI systems in real-world environments. His work centers on the design of agentic architectures, memory and state management, and governance mechanisms that mitigate uncertainty, hallucination, and unintended behavior in large language model–based systems. With experience spanning enterprise software, applied AI, and complex decision systems, he emphasizes practical engineering constraints over theoretical idealizations. This book reflects that perspective, translating architectural principles into implementable patterns for high-reliability AI systems.

Contents

Table of Contents

Preface

Technology Stack

The Nature of AI Agents

  1. Part I: Core Python Concepts

Chapter 1: Type Annotations

  1. Labels
  2. Living Documents
  3. Limitations
  4. Looking Ahead

Chapter 2: Enumerations

  1. The Traffic Light Analogy
  2. The enum Module
  3. Creating an Enumeration
  4. String Enums
  5. Modeling Querybase Intents

Chapter 3: Dataclasses, Immutability, and Memory

  1. Dataclasses
  2. Boilerplate for Free
  3. Defaults and Mutable Fields
  4. Immutability

Chapter 4: Runtime Validation

  1. Introducing Pydantic
  2. Dataclasses vs. Pydantic
  3. Part II: Software Architecture Principles

Chapter 5: Separation of Concerns

  1. The Python Hierarchy of Separation
  2. Mapping SoC to the Querybase Files

Chapter 6: Encapsulation and Abstraction

  1. Encapsulation
  2. Abstraction
  3. Public vs. Private Interfaces
  4. Designing Maintainable Modules

Chapter 7: Layering

  1. Querybase Layers
  2. API Layer
  3. Pipeline Layer
  4. Infrastructure Layer

Chapter 8: Pipelines and Flow Contracts

  1. Stages as a chain
  2. Immutable state & flow contracts
  3. Swapping Stages
  4. Part III: Designing Querybase

BEFORE THE BUILD

Chapter 9: Task Decomposition & Workflows

  1. One-Shot Prompt
  2. Decomposing the Querybase Workflow
  3. The Tactical Assignment Matrix
  4. Assigning Responsibilities to Implementations

Chapter 10: Understanding the Question

  1. Structured Understanding
  2. Defining the Classification Contract
  3. Establishing a Shared Vocabulary

RETRIEVAL AND CONTEXT CONSTRUCTION

Chapter 11: Retrieval

  1. What Is Retrieval?
  2. Finding Information
  3. Embedding-Based Retrieval (EBR)
  4. Measuring Similarity
  5. The Three-Step Lifecycle

DESIGN DECISIONS

Chapter 12: Choosing a Retrieval Architecture

  1. The Decision
  2. Crossing the Boundary

MAPPING THE ARCHITECTURE

Chapter 13: Mapping the Retrieval Layer

  1. Part IV: Building Querybase

Chapter 14: Initializing the Project

  1. Development Tools
  2. Creating the Repository
  3. Separating Concerns
  4. Creating our Backend
  5. Initializing the Development Environment
  6. Creating a .gitignore file
  7. Creating an Environment Template
  8. Installing Dependencies
  9. Creating the Project Structure

Chapter 15: Building the Contracts

  1. A Shared Language
  2. Request Contracts
  3. Response Contracts
  4. Structural Contracts
  5. Boundary Validation

Chapter 16: Building the Classifier

Chapter 17: Building the Router

Chapter 18: Building the Search Layer

Chapter 19: Building the Extractor

Chapter 20: Building the Answerer

Chapter 21: Response Generation and Pipeline Orchestration

  1. Building the Pipeline
  2. Routing Requests
  3. Request Handoff
  4. Pipeline Orchestration
  5. Error Handling
  6. Designing Deterministic AI Pipelines

Chapter 22: Testing the Pipeline

  1. Unit-testing each stage in isolation
  2. Mocking the LLM and embedding calls (the dev_smoke_test.py pattern)
  3. End-to-end wiring tests with no live API calls

Decision Register

  1. Comparison Matrix: Retrieval Architectures

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