
Generative AI with LangChain
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
- Apply enterprise-grade practices for testing, observability, and monitoring
- Build specialized agents for software development and data analysis
- Purchase of the print or Kindle book includes a free PDF eBook
Book DescriptionThis second edition tackles the biggest challenge facing companies in AI today: moving from prototypes to production. Fully updated to reflect the latest developments in the LangChain ecosystem, it captures how modern AI systems are developed, deployed, and scaled in enterprise environments. This edition places a strong focus on multi-agent architectures, robust LangGraph workflows, and advanced retrieval-augmented generation (RAG) pipelines. You'll explore design patterns for building agentic systems, with practical implementations of multi-agent setups for complex tasks. The book guides you through reasoning techniques such as Tree-of -Thoughts, structured generation, and agent handoffs-complete with error handling examples. Expanded chapters on testing, evaluation, and deployment address the demands of modern LLM applications, showing you how to design secure, compliant AI systems with built-in safeguards and responsible development principles. This edition also expands RAG coverage with guidance on hybrid search, re-ranking, and fact-checking pipelines to enhance output accuracy. Whether you're extending existing workflows or architecting multi-agent systems from scratch, this book provides the technical depth and practical instruction needed to design LLM applications ready for success in production environments.What you will learn - Design and implement multi-agent systems using LangGraph
- Implement testing strategies that identify issues before deployment
- Deploy observability and monitoring solutions for production environments
- Build agentic RAG systems with re-ranking capabilities
- Architect scalable, production-ready AI agents using LangGraph and MCP
- Work with the latest LLMs and providers like Google Gemini, Anthropic, Mistral, DeepSeek, and OpenAI's o3-mini
- Design secure, compliant AI systems aligned with modern ethical practices
Who this book is forThis book is for developers, researchers, and anyone looking to learn more about LangChain and LangGraph. With a strong emphasis on enterprise deployment patterns, it's especially valuable for teams implementing LLM solutions at scale. While the first edition focused on individual developers, this updated edition expands its reach to support engineering teams and decision-makers working on enterprise-scale LLM strategies. A basic understanding of Python is required, and familiarity with machine learning will help you get the most out of this book.
All prices
More details
Content
- Cover
- Title Page
- Copyright Page
- Contributors
- Table of Contents
- Preface
- Chapter 1: The Rise of Generative AI: From Language Models to Agents
- The modern LLM landscape
- Model comparison
- LLM provider landscape
- Licensing
- From models to agentic applications
- Limitations of traditional LLMs
- Understanding LLM applications
- Understanding AI agents
- Introducing LangChain
- Challenges with raw LLMs
- How LangChain enables agent development
- Exploring the LangChain architecture
- Ecosystem
- Modular design and dependency management
- LangGraph, LangSmith, and companion tools
- Third-party applications and visual tools
- Summary
- Questions
- Chapter 2: First Steps with LangChain
- Setting up dependencies for this book
- API key setup
- Exploring LangChain's building blocks
- Model interfaces
- LLM interaction patterns
- Development testing
- Working with chat models
- Reasoning models
- Controlling model behavior
- Choosing parameters for applications
- Prompts and templates
- Chat prompt templates
- LangChain Expression Language (LCEL)
- Simple workflows with LCEL
- Complex chain example
- Running local models
- Getting started with Ollama
- Working with Hugging Face models locally
- Tips for local models
- Multimodal AI applications
- Text-to-image
- Using DALL-E through OpenAI
- Using Stable Diffusion
- Image understanding
- Using Gemini 1.5 Pro
- Using GPT-4 Vision
- Summary
- Review questions
- Chapter 3: Building Workflows with LangGraph
- LangGraph fundamentals
- State management
- Reducers
- Making graphs configurable
- Controlled output generation
- Output parsing
- Error handling
- Prompt engineering
- Prompt templates
- Zero-shot vs. few-shot prompting
- Chaining prompts together
- Dynamic few-shot prompting
- Chain of Thought
- Self-consistency
- Working with short context windows
- Summarizing long video
- Understanding memory mechanisms
- Trimming chat history
- Saving history to a database
- LangGraph checkpoints
- Summary
- Questions
- Chapter 4: Building Intelligent RAG Systems
- From indexes to intelligent retrieval
- Components of a RAG system
- When to implement RAG
- From embeddings to search
- Embeddings
- Vector stores
- Vector stores comparison
- Hardware considerations for vector stores
- Vector store interface in LangChain
- Vector indexing strategies
- Breaking down the RAG pipeline
- Document processing
- Chunking strategies
- Retrieval
- Advanced RAG techniques
- Hybrid retrieval: Combining semantic and keyword search
- Re-ranking
- Query transformation: Improving retrieval through better queries
- Context processing: maximizing retrieved information value
- Response enhancement: Improving generator output
- Corrective RAG
- Agentic RAG
- Choosing the right techniques
- Developing a corporate documentation chatbot
- Document loading
- Language model setup
- Document retrieval
- Designing the state graph
- Integrating with Streamlit for a user interface
- Evaluation and performance considerations
- Troubleshooting RAG systems
- Summary
- Questions
- Chapter 5: Building Intelligent Agents
- What is a tool?
- Tools in LangChain
- ReACT
- Defining tools
- Built-in LangChain tools
- Custom tools
- Wrapping a Python function as a tool
- Creating a tool from a Runnable
- Subclass StructuredTool or BaseTool
- Error handling
- Advanced tool-calling capabilities
- Incorporating tools into workflows
- Controlled generation
- Controlled generation provided by the vendor
- ToolNode
- Tool-calling paradigm
- What are agents?
- Plan-and-solve agent
- Summary
- Questions
- Chapter 6: Advanced Applications and Multi-Agent Systems
- Agentic architectures
- Agentic RAG
- Multi-agent architectures
- Agent roles and specialization
- Consensus mechanism
- Communication protocols
- Semantic router
- Organizing interactions
- LangGraph streaming
- Handoffs
- Communication via a shared messages list
- LangGraph platform
- Building adaptive systems
- Dynamic behavior adjustment
- Human-in-the-loop
- Exploring reasoning paths
- Tree of Thoughts
- Trimming ToT with MCTS
- Agent memory
- Cache
- Store
- Summary
- Questions
- Chapter 7: Software Development and Data Analysis Agents
- LLMs in software development
- The future of development
- Implementation considerations
- Evolution of code LLMs
- Benchmarks for code LLMs
- LLM-based software engineering approaches
- Security and risk mitigation
- Validation framework for LLM-generated code
- LangChain integrations
- Writing code with LLMs
- Google generative AI
- Hugging Face
- Anthropic
- Agentic approach
- Documentation RAG
- Repository RAG
- Applying LLM agents for data science
- Training an ML model
- Setting up a Python-capable agent
- Asking the agent to build a neural network
- Agent execution and results
- Analyzing a dataset
- Creating a pandas DataFrame agent
- Asking questions about the dataset
- Summary
- Questions
- Chapter 8: Evaluation and Testing
- Why evaluation matters
- Safety and alignment
- Performance and efficiency
- User and stakeholder value
- Building consensus for LLM evaluation
- What we evaluate: core agent capabilities
- Task performance evaluation
- Tool usage evaluation
- RAG evaluation
- Planning and reasoning evaluation
- How we evaluate: methodologies and approaches
- Automated evaluation approaches
- Human-in-the-loop evaluation
- System-level evaluation
- Evaluating LLM agents in practice
- Evaluating the correctness of results
- Evaluating tone and conciseness
- Evaluating the output format
- Evaluating agent trajectory
- Evaluating CoT reasoning
- Offline evaluation
- Evaluating RAG systems
- Evaluating a benchmark in LangSmith
- Evaluating a benchmark with HF datasets and Evaluate
- Evaluating email extraction
- Summary
- Questions
- Chapter 9: Production-Ready LLM Deployment and Observability
- Security considerations for LLM applications
- Deploying LLM apps
- Web framework deployment with FastAPI
- Scalable deployment with Ray Serve
- Building the index
- Serving the index
- Running the application
- Deployment considerations for LangChain applications
- LangGraph platform
- Local development with the LangGraph CLI
- Serverless deployment options
- UI frameworks
- Model Context Protocol
- Infrastructure considerations
- How to choose your deployment model
- Model serving infrastructure
- How to observe LLM apps
- Operational metrics for LLM applications
- Tracking responses
- Hallucination detection
- Bias detection and monitoring
- LangSmith
- Observability strategy
- Continuous improvement for LLM applications
- Cost management for LangChain applications
- Model selection strategies in LangChain
- Tiered model selection
- Cascading model approach
- Output token optimization
- Other strategies
- Monitoring and cost analysis
- Summary
- Questions
- Chapter 10: The Future of Generative Models: Beyond Scaling
- The current state of generative AI
- The limitations of scaling and emerging alternatives
- The scaling hypothesis challenged
- Big tech vs. small enterprises
- Emerging alternatives to pure scaling
- Scaling up (traditional approach)
- Scaling down (efficiency innovations)
- Scaling out (distributed approaches)
- Evolution of training data quality
- Democratization through technical advances
- New scaling laws for post-training phases
- Economic and industry transformation
- Industry-specific transformations and competitive dynamics
- Job evolution and skills implications
- Near-term impacts (2025-2035)
- Medium-term impacts (2035-2045)
- Long-term shifts (2045 and beyond)
- Economic distribution and equity considerations
- Societal implications
- Misinformation and cybersecurity
- Copyright and attribution challenges
- Regulations and implementation challenges
- Summary
- Appendix
- OpenAI
- Hugging Face
- 1. Google AI platform
- 2. Google Cloud Vertex AI
- Other providers
- Summarizing long videos
- Packt Page
- Other Books You May Enjoy
- Index
1
The Rise of Generative AI: From Language Models to Agents
The gap between experimental and production-ready agents is stark. According to LangChain's State of Agents report, performance quality is the #1 concern among 51% of companies using agents, yet only 39.8% have implemented proper evaluation systems. Our book bridges this gap on two fronts: first, by demonstrating how LangChain and LangSmith provide robust testing and observability solutions; second, by showing how LangGraph's state management enables complex, reliable multi-agent systems. You'll find production-tested code patterns that leverage each tool's strengths for enterprise-scale implementation and extend basic RAG into robust knowledge systems.
LangChain accelerates time-to-market with readily available building blocks, unified vendor APIs, and detailed tutorials. Furthermore, LangChain and LangSmith debugging and tracing functionalities simplify the analysis of complex agent behavior. Finally, LangGraph has excelled in executing its philosophy behind agentic AI - it allows a developer to give a large language model (LLM) partial control flow over the workflow (and to manage the level of how much control an LLM should have), while still making agentic workflows reliable and well-performant.
In this chapter, we'll explore how LLMs have evolved into the foundation for agentic AI systems and how frameworks like LangChain and LangGraph transform these models into production-ready applications. We'll also examine the modern LLM landscape, understand the limitations of raw LLMs, and introduce the core concepts of agentic applications that form the basis for the hands-on development we'll tackle throughout this book.
In a nutshell, the following topics will be covered in this book:
- The modern LLM landscape
- From models to agentic applications
- Introducing LangChain
The modern LLM landscape
Artificial intelligence (AI) has long been a subject of fascination and research, but recent advancements in generative AI have propelled it into mainstream adoption. Unlike traditional AI systems that classify data or make predictions, generative AI can create new content-text, images, code, and more-by leveraging vast amounts of training data.
The generative AI revolution was catalyzed by the 2017 introduction of the transformer architecture, which enabled models to process text with unprecedented understanding of context and relationships. As researchers scaled these models from millions to billions of parameters, they discovered something remarkable: larger models didn't just perform incrementally better-they exhibited entirely new emergent capabilities like few-shot learning, complex reasoning, and creative generation that weren't explicitly programmed. Eventually, the release of ChatGPT in 2022 marked a turning point, demonstrating these capabilities to the public and sparking widespread adoption.
The landscape shifted again with the open-source revolution led by models like Llama and Mistral, democratizing access to powerful AI beyond the major tech companies. However, these advanced capabilities came with significant limitations-models couldn't reliably use tools, reason through complex problems, or maintain context across interactions. This gap between raw model power and practical utility created the need for specialized frameworks like LangChain that transform these models from impressive text generators into functional, production-ready agents capable of solving real-world problems.
Key terminologies
Tools: External utilities or functions that AI models can use to interact with the world. Tools allow agents to perform actions like searching the web, calculating values, or accessing databases to overcome LLMs' inherent limitations.
Memory: Systems that allow AI applications to store and retrieve information across interactions. Memory enables contextual awareness in conversations and complex workflows by tracking previous inputs, outputs, and important information.
Reinforcement learning from human feedback (RLHF): A training technique where AI models learn from direct human feedback, optimizing their performance to align with human preferences. RLHF helps create models that are more helpful, safe, and aligned with human values.
Agents: AI systems that can perceive their environment, make decisions, and take actions to accomplish goals. In LangChain, agents use LLMs to interpret tasks, choose appropriate tools, and execute multi-step processes with minimal human intervention.
Year
Development
Key Features
1990s
IBM Alignment Models
Statistical machine translation
2000s
Web-scale datasets
Large-scale statistical models
2009
Statistical models dominate
Large-scale text ingestion
2012
Deep learning gains traction
Neural networks outperform statistical models
2016
Neural Machine Translation (NMT)
Seq2seq deep LSTMs replace statistical methods
2017
Transformer architecture
Self-attention revolutionizes NLP
2018
BERT and GPT-1
Transformer-based language understanding and generation
2019
GPT-2
Large-scale text generation, public awareness increases
2020
GPT-3
API-based access, state-of-the-art performance
2022
ChatGPT
Mainstream adoption of LLMs
2023
Large Multimodal Models (LMMs)
AI models process text, images, and audio
2024
OpenAI o1
Stronger reasoning capabilities
2025
DeepSeek R1
Open-weight, large-scale AI model
Table 1.1: A timeline of major developments in language models
The field of LLMs is rapidly evolving, with multiple models competing in terms of performance, capabilities, and accessibility. Each provider brings distinct advantages, from OpenAI's advanced general-purpose AI to Mistral's open-weight, high-efficiency models. Understanding the differences between these models helps practitioners make informed decisions when integrating LLMs into their applications.
Model comparison
The following points outline key factors to consider when comparing different LLMs, focusing on their accessibility, size, capabilities, and specialization:
- Open-source vs. closed-source models: Open-source models like Mistral and LLaMA provide transparency and the ability to run locally, while closed-source models like GPT-4 and Claude are accessible through APIs. Open-source LLMs can be downloaded and modified, enabling developers and...
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.
File format: ePUB
Copy protection: without DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Use a reader that can handle the file format ePUB, such as Adobe Digital Editions or FBReader – both free (see eBook Help).
- Tablet/Smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePUB works well for novels and non-fiction books – i.e., 'flowing' text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook does not use copy protection or Digital Rights Management
For more information, see our eBook Help page.