Generative AI with LangChain: Build large language model (LLM) apps with Python, ChatGPT, and other LLMs
Ben Auffarth
Table of Contents
Preface xv
Chapter 1: What Is Generative AI? 1
Introducing generative AI ………………. 2
What are generative models? • 4
Why now? • 8
Understanding LLMs ……………………. 11
What is a GPT? • 13
Other LLMs • 16
Major players • 18
How do GPT models work? • 20
Pre-training • 23
Tokenization • 24
Scaling • 25
Conditioning • 26
How to try out these models • 27
What are text-to-image models? ……. 27
What can AI do in other domains? …. 33
Summary …………………………………… 34
Questions …………………………………… 35
Chapter 2: LangChain for LLM Apps 37
Going beyond stochastic parrots ……. 38
What are the limitations of LLMs? • 38
How can we mitigate LLM limitations? • 42
What is an LLM app? • 43
What is LangChain? …………………….. 46
Exploring key components of LangChain ………………………… 50
What are chains? • 50
What are agents? • 52
What is memory? • 54
What are tools? • 55
How does LangChain work? ………….. 57
Comparing LangChain with other frameworks ………………… 60
Summary …………………………………… 61
Questions …………………………………… 62
Chapter 3: Getting Started with LangChain 65
How to set up the dependencies for this book …………………… 65
pip • 67
Poetry • 68
Conda • 68
Docker • 68
Exploring API model integrations ….. 69
Fake LLM • 72
OpenAI • 73
Hugging Face • 75
Google Cloud Platform • 77
Jina AI • 80
Replicate • 82
Others • 84
Azure • 84
Anthropic • 85
Exploring local models ………………… 85
Hugging Face Transformers • 86
llama.cpp • 87
GPT4All • 88
Building an application for customer service ……………………. 89
Summary …………………………………… 96
Questions …………………………………… 96
Chapter 4: Building Capable Assistants 99
Mitigating hallucinations through fact-checking ……………. 100
Summarizing information ………….. 103
Basic prompting • 103
Prompt templates • 104
Chain of density • 105
Map-Reduce pipelines • 107
Monitoring token usage • 109
Extracting information from documents ………………………… 112
Answering questions with tools ……. 116
Information retrieval with tools • 116
Building a visual interface • 118
Exploring reasoning strategies ……… 121
Summary ………………………………….. 129
Questions …………………………………. 130
Chapter 5: Building a Chatbot like ChatGPT 131
What is a chatbot? ………………………. 132
Understanding retrieval and vectors ………………………………. 134
Embeddings • 135
Vector storage • 139
Vector indexing • 140
Vector libraries • 141
Vector databases • 143
Loading and retrieving in LangChain …………………………….. 148
Document loaders • 149
Retrievers in LangChain • 150
kNN retriever • 151
PubMed retriever • 152
Custom retrievers • 153
Implementing a chatbot ……………… 153
Document loader • 154
Vector storage • 155
Memory • 160
Conversation buffers • 161
Remembering conversation summaries • 164
Storing knowledge graphs • 164
Combining several memory mechanisms • 165
Long-term persistence • 166
Moderating responses …………………. 167
Summary …………………………………. 170
Questions ………………………………….. 171
Chapter 6: Developing Software with Generative AI 173
Software development and AI ………. 174
Code LLMs • 175
Writing code with LLMs ……………… 179
StarCoder • 179
StarChat • 184
Llama 2 • 186
Small local model • 187
Automating software development ……………………………….. 189
Summary …………………………………. 201
Questions …………………………………. 201
Chapter 7: LLMs for Data Science 203
The impact of generative models on data science ……………. 204
Automated data science ……………… 207
Data collection • 209
Visualization and EDA • 210
Preprocessing and feature extraction • 210
AutoML • 211
Using agents to answer data science questions ………………… 213
Data exploration with LLMs ………… 217
Summary …………………………………. 222
Questions …………………………………. 223
Chapter 8: Customizing LLMs and Their Output 225
Conditioning LLMs ……………………. 226
Methods for conditioning • 227
Reinforcement learning with human feedback • 228
Low-rank adaptation • 229
Inference-time conditioning • 230
Fine-tuning ………………………………. 232
Setup for fine-tuning • 233
Open-source models • 236
Commercial models • 241
Prompt engineering …………………… 242
Prompt techniques • 244
Zero-shot prompting • 246
Few-shot learning • 246
Chain-of-thought prompting • 248
Self-consistency • 249
Tree-of-thought • 251
Summary …………………………………. 255
Questions …………………………………. 255
Chapter 9: Generative AI in Production 257
How to get LLM apps ready for production …………………….. 258
Terminology • 260
How to evaluate LLM apps …………… 261
Comparing two outputs • 264
Comparing against criteria • 265
String and semantic comparisons • 267
Running evaluations against datasets • 268
How to deploy LLM apps …………….. 273
FastAPI web server • 276
Ray • 280
How to observe LLM apps …………… 284
Tracking responses • 287
Observability tools • 289
LangSmith • 291
PromptWatch • 294
Summary …………………………………. 295
Questions …………………………………. 296
Chapter 10: The Future of Generative Models 299
The current state of generative AI ………………………………… 300
Challenges • 302
Trends in model development • 304
Big Tech vs. small enterprises • 307
Artificial General Intelligence • 309
Economic consequences …………….. 310
Creative industries and advertising • 313
Education • 315
Law • 315
Manufacturing • 316
Medicine • 316
Military • 316
Societal implications ………………….. 317
Misinformation and cybersecurity • 318
Regulations and implementation challenges • 319
The road ahead ………………………….. 321
Other Books You May Enjoy 325
Index 329