Generative AI on Google Cloud with LangChain - Leonid Kuligin - E-Book

Generative AI on Google Cloud with LangChain E-Book

Leonid Kuligin

0,0
32,39 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

The rapid transformation and enterprise adoption of GenAI has created an urgent demand for developers to quickly build and deploy AI applications that deliver real value. Written by three distinguished Google AI engineers and LangChain contributors who have shaped Google Cloud’s integration with LangChain and implemented AI solutions for Fortune 500 companies, this book bridges the gap between concept and implementation, exploring LangChain and Google Cloud’s enterprise-ready tools for scalable AI solutions.
You'll start by exploring the fundamentals of large language models (LLMs) and how LangChain simplifies the development of AI workflows by connecting LLMs with external data and services. This book guides you through using essential tools like the Gemini and PaLM 2 APIs, Vertex AI, and Vertex AI Search to create sophisticated, production-ready GenAI applications. You'll also overcome the context limitations of LLMs by mastering advanced techniques like Retrieval-Augmented Generation (RAG) and external memory layers.
Through practical patterns and real-world examples, you’ll gain everything you need to harness Google Cloud’s AI ecosystem, reducing the time to market while ensuring enterprise scalability. You’ll have the expertise to build robust GenAI applications that can be tailored to solve real-world business challenges.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 367

Veröffentlichungsjahr: 2024

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Generative AI on Google Cloud with LangChain

Design scalable generative AI solutions with Python, LangChain, and Vertex AI on Google Cloud

Leonid Kuligin | Jorge Zaldívar | Maximilian Tschochohei

Generative AI on Google Cloud with LangChain

Copyright © 2024 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Group Product Manager: Niranjan Naikwadi

Publishing Product Manager: Nitin Nainani

Book Project Manager: Aparna Nair

Lead Editor: Sushma Reddy

Technical Editor: Rahul Limbachiya

Copy Editor: Safis Editing

Proofreader: Sushma Reddy

Indexer: Tejal Soni

Production Designer: Ponraj Dhandapani

DevRel Marketing Coordinator: Nivedita Singh

First published: Dec 2024

Production reference: 1221124

Published by Packt Publishing Ltd.

Grosvenor House 11 St Paul’s SquareBirmingham B3 1RB, UK

ISBN 978-1-83588-932-9

www.packtpub.com

To my wife, Ksenia, whose unwavering love and optimism have been my constant support over all these years; to my mother-in-law, Tatyana, whose belief in me, even in my most crazy endeavors, has been an incredible source of strength; and to my kids, Matvey and Milena, I hope you’ll read it one day.

– Leonid Kuligin

To my wife, Lucia, and son, Jaime, thank you for your endless support and patience during the journey of writing this book. You are my inspiration and my joy.

– Jorge Zaldivar

For Jacqueline, my partner and my rock. This book wouldn’t exist without your love and support.

– Maximilian Tschochohei

Foreword

I started LangChain as a side project, an open source Python package, in late October of 2022. This was after Stable Diffusion but before the launch of ChatGPT. I was going to a bunch of meetups and hackathons, saw people building with early LLMs, and saw some common abstractions. Those abstractions became LangChain.

That was over two years ago, and the space has evolved rapidly since then. LangChain has both grown and evolved. The amount of people building applications on top of LLMs has exploded, and LangChain has served as an entry point for a lot of them. The functionality inside LangChain has had to evolve as the underlying technology did so as well.

LangChain aims to empower developers, engineers, and architects to leverage the potential of generative AI in their projects. The rapid evolution of this technology has opened up unprecedented opportunities for innovation and creativity, allowing us to transform industries in ways we never imagined. In recent years, we have witnessed a remarkable shift in how we interact with technology. Generative AI stands at the forefront of this revolution, enabling machines to create content, assist in decision-making, and enhance user experiences in ways we could only dream of a decade ago. However, navigating this landscape can be daunting, especially for those who are new to the field or transitioning from traditional development practices. This book serves as a comprehensive guide for those looking to harness the power of generative AI using LangChain.

This book is tailored for a diverse audience, from generative AI enthusiasts to seasoned engineers exploring the intricacies of this technology. It assumes a foundational knowledge of Python and basic machine learning principles, making it accessible yet rich in content for both novices and experienced professionals. The practical examples and hands-on approach will empower readers to design and implement enterprise-ready architectures on Google Cloud, leveraging the full potential of LangChain and its integrations.

In this book, you will learn not only about the foundational models available on Google Cloud, such as PaLM 2 and Gemini, but also how to build robust applications that address real-world challenges. The structured journey through the book will guide you from understanding the core concepts of generative AI to deploying your solutions effectively in a cloud environment.

Moreover, the insights provided on common architectures, grounding responses, and advanced techniques will equip you with the knowledge to tackle the complexities of generative AI applications. You will gain the confidence to experiment, innovate, and ultimately contribute to the ever-evolving landscape of AI-driven solutions.

I encourage you to embrace this learning journey with an open mind and a spirit of curiosity. Above all else, I believe that it is still incredibly early on in this technological revolution. The possibilities are limitless, and with the right tools and knowledge, you can become a pioneer in this exciting field. Thank you for choosing this book as your guide, and I look forward to seeing the incredible applications you create with LangChain and Google Cloud.

Harrison Chase, CEO, LangChain

Contributors

About the authors

Leonid Kuligin is a staff AI engineer at Google Cloud, working on generative AI and classical machine learning solutions (such as demand forecasting or optimization problems). Leonid is one of the key maintainers of Google Cloud integrations on LangChain, and a visiting lecturer at CDTM (TUM and LMU). Prior to Google, Leonid gained more than 20 years of experience in building B2C and B2B applications based on complex machine learning and data processing solutions such as search, maps, and investment management in German, Russian, and US technological, financial, and retail companies.

I want to thank all my colleagues who helped me not only with this book but also with making Google integrations a first-class citizen on LangChain. I want to express my gratitude to all LangChain folks and the community, who helped to build such an incredible framework we all love working with. I’d like to say a special thank you to Pavel Gulin, who inspired me to write this book with his optimism and enthusiasm, and Slava Rubaev, my first manager and my lifetime mentor, who taught me the basics of entrepreneurship, technology, and critical thinking.

Jorge Zaldívar is an AI engineer at Google and a contributor to LangChain’s integrations with Google. He has a decade of experience building complex machine learning applications and products applied to the energy and financial industries.

Maximilian Tschochohei leads AI engineering at Google Cloud Consulting EMEA. He implements LangChain applications together with Google Cloud customers. Before Google, he worked in strategy and technology consulting with Boston Consulting Group.

I want to thank my partner, my family, and my friends: thank you for your endless patience and unwavering support. I also want to thank my colleagues and my team: thank you for letting me learn from you. And special thanks to Leonid for coming up with the idea for this book, and for letting me be a part of it.

About the reviewers

Nuno Campos is a founding software engineer at LangChain, Inc. Nuno has more than 9 years of experience as a Python and JavaScript software engineer, architect, and open-source maintainer. Nuno has been a maintainer of various popular open-source projects, including LangGraph, LangChain.js, and Enzyme. Previously, he worked for various tech start-ups in software engineering and data science roles. He holds an MSc in finance.

Georgii Danshchin is the head of data science at HeadHunter Group, where he played a key role in shaping and leading the development of crucial components such as recommendations and search ranking based on machine learning. With a background in software development starting in 2012, he studied data science at the Higher School of Economics and Yandex School of Data Analysis. He has applied machine learning to real-world business tasks for nearly a decade. Georgii has also successfully participated in machine learning competitions, holds the Kaggle Master title, and has earned several gold medals. He is passionate about the opportunities that large language models (LLMs) offer to engineers and product managers.

Lucio Floretta leads a team of AI experts within Google Cloud’s Professional Services organization, where he empowers enterprise customers to harness the transformative power of artificial intelligence. While his recent focus has been on generative AI, Lucio has been guiding customers across EMEA in building robust MLOps systems and ML solutions on Google Cloud since 2017.

Table of Contents

Preface

Part 1: Intro to LangChain and Generative AI on Google Cloud

1

Using LangChain with Google Cloud

Technical requirements

LangChain

LangChain primitives – chains and runnables

LCEL

RunnablePassThrough and itemgetter

Main LangChain building blocks

Data structures

Interfaces

LangChain with Google Cloud

LangChain integration with Google

Summary

References

2

Foundational Models on Google Cloud

Technical requirements

What is Vertex AI?

Google Cloud console

Using LangChain

Prompt templates

Using chat models

Using callbacks

Other models besides Gemini

Vertex AI Model Garden

Open-source models

Partner models

Prompt engineering for foundational models

Summary

References

Part 2: Hallucinations and Grounding Responses

3

Grounding Responses

Technical requirements

Overcoming hallucinations in LLMs

Understanding RAG applications – closed-book versus open-book question-answering

Vertex AI Agent Builder

Creating a data store and indexing documents

Querying Vertex AI Agent Builder

Using Vertex AI Agent Builder

Building a custom RAG application

Customizing the RAG application further

Query expansion

Filtering documents

Further methods

Summary

References

4

Vector Search on Google Cloud

Technical requirements

What is vector search?

Overview of vector search architecture

LangChain interfaces – embeddings and vector stores

Embeddings

Vector stores

Vector store with Vertex AI Vector Search

Creating the required infrastructure

LangChain integration

Vectorstore with pgvector on Cloud SQL

Creating a CloudSQL instance and database

Enabling the pgVector extension

LangChain integration

Vectorstore with BigQuery

Creating a BigQuery dataset, table, and vector index

LangChain integration

Summary

References

5

Ingesting Documents

Technical requirements

Ingesting documents with LangChain

Plain text documents

CSV documents

PDF documents

GoogleDriveLoader

Additional types

Loading directories

Integrating with Google Cloud Storage

Document chunking

RecursiveCharacterTextSplitter

Other Text Splitters

Applying text splitters

Parsing documents with Document AI

LangChain usage

Ingesting data with Vertex AI Agent Builder

Website data

Unstructured Data

Structured data

Parsing

Chunking

Summary

Links

6

Multimodality

Technical requirements

Multimodality with Gemini and LangChain

Using Imagen with LangChain

Developing multimodal RAGs

Extracting images from PDF documents

Multimodal context

Parsing an image

Summary

References

Part 3: Common Generative AI Architectures

7

Working with Long Context

Technical requirements

What is summarization?

Summarizing documents

Summarizing text

Using loaders

MapReduce pattern

Summarizing other modalities

Answering questions on long documents

Summary

References

8

Building Chatbots

Technical requirements

Introduction to conversational design

Intent identification

Conversation flow design

Entity extraction

Conversational design workflow

Building chatbots with LangChain

Building a basic chatbot with LangChain messages

Adding memory to your chatbot

Handling memory using Runnables

Automatically handling memory with LangGraph

Optimizing your RAG for memory

Caching memory in context

Detecting intents and routing messages

Custom routing functions

Semantic routing

Summary

References

9

Tools and Function Calling

Technical requirements

Advanced prompt design

Naive controlled generation

Chain-of-Thought (CoT)

Enhancing LLMs’ capabilities with tools

Constructing tools

ReACT pattern

Using ToolConfig

Tools provided by Google

Using Google Search

Search grounding with Gemini

Summary

References

10

Agents

Technical requirements

What is an agent?

Components of an agent

How agents work

Agents in LangChain

Tools

Model

Prompt template

Agent creation

Agent usage

Deploying with Reasoning Engine

Vertex AI Agent Builder

Summary

11

Agentic Workflows

Technical requirements

Workflow state management

Controlled generation

Agentic RAG

NL2SQL

Summary

Part 4: Designing Generative AI Applications

12

Evaluating GenAI Applications

Technical requirements

Evaluating GenAI applications

LangSmith

LangChain Hub for prompt management

Datasets and benchmarks

Pointwise evaluations

Comparison on benchmarks

Evaluation metrics

LangChain interfaces

LangChain pointwise evaluators

Vertex evaluators on LangChain

Pairwise evaluations

Vertex pairwise evaluators

Summary

References

13

Generative AI System Design

Architecture design framework

The business process, or defining your use case

Architecture capabilities

Technical design

Document ingestion and storage

Context retrieval

Model interaction

User-facing components

Enterprise readiness and compliance

RAI

Summary

References

Appendix

Appendix 1

Overview of Generative AI

Introducing generative AI

Exploring LLMs

LLMs can solve tasks described as natural language

How can LLMs solve almost any task?

What is alignment, and how does it enhance LLM capabilities?

Tuning LLMs

Are LLMs as smart as humans?

Is it just hype or something big?

Secrets of successful adoption

Summary

References

Appendix 2

Google Cloud Foundations

Setting up your organization

Adding users and groups

Next steps

Assigning users to groups

Managing administrative users

Understanding IAM roles

Granting roles to groups

Reviewing and customizing default roles

The importance of regular review

Additional tips

Setting up billing in Google Cloud

Billing account types

Associating a billing account

Centralizing logging in Google Cloud

Setting up centralized logging

Securing your Google Cloud environment

Enabling APIs and setting up quotas

Applying organization policies

Setting up your Google Cloud network infrastructure

Creating your initial network configuration

Additional tips

Enabling your Google Cloud environment for AI/machine learning experiments

Deploying LangChain applications with Google Cloud Run

Summary

References

Index

Other Books You May Enjoy

Part 1: Intro to LangChain and Generative AI on Google Cloud

In this part, we will provide an overview of LangChain as a framework and the main generative AI capabilities available on Google Cloud Platform. We’ll also look into the key interfaces of LangChain, making you ready to read LangChain code with ease and start developing applications with this framework. If you’re new to generative AI, we also encourage you to take a look at Appendix 1, where we provide a quick intro to the topic and give you a list of papers to dive into.

This part has the following chapters:

Chapter 1, Using LangChain with Google CloudChapter 2, Foundational Models on Google Cloud