Generative AI with LangChain

Book description

2024 Edition – Get to grips with the LangChain framework to develop production-ready applications, including agents and personal assistants. The 2024 edition features updated code examples and an improved GitHub repository. Purchase of the print or Kindle book includes a free PDF eBook.

Key Features

  • Learn how to leverage LangChain to work around LLMs’ inherent weaknesses
  • Delve into LLMs with LangChain and explore their fundamentals, ethical dimensions, and application challenges
  • Get better at using ChatGPT and GPT models, from heuristics and training to scalable deployment, empowering you to transform ideas into reality

Book Description

ChatGPT and the GPT models by OpenAI have brought about a revolution not only in how we write and research but also in how we can process information. This book discusses the functioning, capabilities, and limitations of LLMs underlying chat systems, including ChatGPT and Gemini. It demonstrates, in a series of practical examples, how to use the LangChain framework to build production-ready and responsive LLM applications for tasks ranging from customer support to software development assistance and data analysis – illustrating the expansive utility of LLMs in real-world applications.

Unlock the full potential of LLMs within your projects as you navigate through guidance on fine-tuning, prompt engineering, and best practices for deployment and monitoring in production environments. Whether you're building creative writing tools, developing sophisticated chatbots, or crafting cutting-edge software development aids, this book will be your roadmap to mastering the transformative power of generative AI with confidence and creativity.

What you will learn

  • Create LLM apps with LangChain, like question-answering systems and chatbots
  • Understand transformer models and attention mechanisms
  • Automate data analysis and visualization using pandas and Python
  • Grasp prompt engineering to improve performance
  • Fine-tune LLMs and get to know the tools to unleash their power
  • Deploy LLMs as a service with LangChain and apply evaluation strategies
  • Privately interact with documents using open-source LLMs to prevent data leaks

Who this book is for

The book is for developers, researchers, and anyone interested in learning more about LangChain. Whether you are a beginner or an experienced developer, this book will serve as a valuable resource if you want to get the most out of LLMs using LangChain. Basic knowledge of Python is a prerequisite, while prior exposure to machine learning will help you follow along more easily.

Table of contents

  1. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Work with notebooks and projects
    4. Get in touch
  2. What Is Generative AI?
    1. Introducing generative AI
      1. What are generative models?
      2. Why now?
    2. Understanding LLMs
      1. How do GPT models work?
        1. Transformers
        2. Pre-training
        3. Tokenization
        4. Conditioning
      2. How have GPT models evolved?
        1. Model size
        2. The GPT model series
        3. PaLM and Gemini
        4. Llama and Llama 2
        5. Claude 1–3
        6. Mixture of Experts (MoE)
      3. How to use LLMs
    3. What are text-to-image models?
    4. What can AI do in other domains?
    5. Summary
    6. Questions
  3. LangChain for LLM Apps
    1. Going beyond stochastic parrots
      1. What are the limitations of LLMs?
      2. How can we mitigate LLM limitations?
    2. What is an LLM app?
    3. What is LangChain?
    4. Exploring key components of LangChain
      1. What are chains?
      2. What are agents?
      3. What is memory?
      4. What are tools?
    5. How does LangChain work?
    6. LangChain package structure
    7. Comparing LangChain with other frameworks
    8. Summary
    9. Questions
  4. Getting Started with LangChain
    1. How to set up the dependencies for this book
    2. Exploring cloud integrations
      1. Environment setup and API keys
        1. OpenAI
        2. Hugging Face
        3. Google
    3. Building blocks for LLM interaction
      1. LLMs
        1. Fake LLM
      2. Chat models
      3. Prompts
      4. Chains
      5. LangChain Expression Language
      6. Text-to-Image
        1. Dall-E
        2. Replicate
      7. Image understanding
    4. Running local models
      1. Hugging Face Transformers
      2. llama.cpp
      3. GPT4All
    5. Prototyping an application for customer service
      1. Sentiment analysis
      2. Text classification
      3. Document summarization
    6. Applying map-reduce
    7. Monitoring token usage
    8. Summary
    9. Questions
  5. Building Capable Assistants
    1. Answering questions with tools
      1. Tool use
      2. Defining custom tools
        1. Tool decorator
        2. Subclassing BaseTool
        3. StructuredTool dataclass
      3. Error handling
    2. Implementing a research assistant with tools
      1. Building a visual interface
      2. Exploring agent architectures
    3. Extracting structured information from documents
    4. Mitigating hallucinations through fact-checking
    5. Summary
    6. Questions
  6. Building a Chatbot Like ChatGPT
    1. What is a chatbot?
    2. From vectors to RAG
      1. Vector embeddings
        1. Embeddings in LangChain
      2. Vector storage
        1. Vector indexing
        2. Vector libraries
        3. Vector databases
      3. Document loaders
      4. Retrievers in LangChain
        1. kNN retriever
        2. PubMed retriever
        3. Custom retrievers
    3. Implementing a chatbot with a retriever
      1. Document loaders
      2. Vector storage
      3. Conversation Memory: Preserving Context
        1. ConversationBufferMemory
        2. ConversationBufferWindowMemory
        3. ConversationSummaryMemory
        4. ConversationKGMemory
        5. CombinedMemory
        6. Long-term persistence
    4. Moderating responses
      1. Guardrails
    5. Summary
    6. Questions
  7. Developing Software with Generative AI
    1. Software development and AI
      1. Code LLMs
    2. Writing code with LLMs
      1. Vertex AI
      2. StarCoder
      3. StarChat
      4. Llama 2
      5. Small local model
    3. Automating software development
      1. Implementing a feedback loop
      2. Tool use
        1. Error handling
      3. Finishing touches to our developer
    4. Summary
    5. Questions
  8. LLMs for Data Science
    1. The impact of generative models on data science
    2. Automated data science
      1. Data collection
      2. Visualization and EDA
      3. Preprocessing and feature extraction
      4. AutoML
    3. Using agents to answer data science questions
    4. Data exploration with LLMs
    5. Summary
    6. Questions
  9. Customizing LLMs and Their Output
    1. Conditioning LLMs
    2. Methods for conditioning
      1. Reinforcement learning with human feedback
      2. Low-rank adaptation
      3. Inference-time conditioning
    3. Fine-tuning
      1. Setup for fine-tuning
      2. Open-source models
      3. Commercial models
    4. Prompt engineering
      1. Prompt techniques
        1. Zero-shot prompting
        2. Few-shot learning
        3. CoT prompting
        4. Self-consistency
        5. ToT
    5. Summary
    6. Questions
  10. Generative AI in Production
    1. How to get LLM apps ready for production
    2. How to evaluate LLM apps
    3. Comparing two outputs
      1. Comparing against criteria
      2. String and semantic comparisons
      3. Running evaluations against datasets
    4. How to deploy LLM apps
      1. FastAPI web server
      2. Ray
    5. How to observe LLM apps
      1. Tracking responses
      2. Observability tools
      3. LangSmith
      4. PromptWatch
    6. Summary
    7. Questions
  11. The Future of Generative Models
    1. The current state of generative AI
      1. Challenges
      2. Trends in model development
      3. Big Tech vs. small enterprises
      4. Artificial General Intelligence
    2. Economic consequences
      1. Creative industries
      2. Education
      3. Law
      4. Manufacturing
      5. Medicine
      6. Military
    3. Societal implications
      1. Misinformation and cybersecurity
      2. Regulations and implementation challenges
    4. The road ahead
  12. Other Books You May Enjoy
  13. Index

Product information

  • Title: Generative AI with LangChain
  • Author(s): Ben Auffarth
  • Release date: December 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781835083468