Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Generative AI on Google Cloud with LangChain
Generative AI on Google Cloud with LangChain

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

Arrow left icon
Profile Icon Leonid Kuligin Profile Icon Jorge Zaldívar Profile Icon Maximilian Tschochohei
Arrow right icon
$44.99
Paperback Dec 2024 306 pages 1st Edition
eBook
$24.99 $35.99
Paperback
$44.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Leonid Kuligin Profile Icon Jorge Zaldívar Profile Icon Maximilian Tschochohei
Arrow right icon
$44.99
Paperback Dec 2024 306 pages 1st Edition
eBook
$24.99 $35.99
Paperback
$44.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$24.99 $35.99
Paperback
$44.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Generative AI on Google Cloud with LangChain

Using LangChain with Google Cloud

We, as authors, are so happy that you are interested in generative AI and you have decided to read this book. We wrote this book for engineers and practitioners who want to develop their first generative AI application with LangChain, a great open source framework.

In this chapter, you will install the LangChain framework and experiment with its basic primitives using LangChain Expression Language (LCEL).

We will cover the following main topics:

  • The LangChain framework and the company behind it
  • LangChain primitives - chains and runnables
  • Main LangChain building blocks
  • LangChain with Google Cloud
  • LangChain integration with Google

Technical requirements

To be able to work with the code samples in this chapter, install the langchain-core library by running pip install langchain-core in your terminal.

LangChain

Large language models (LLMs) are the fundamental components required to develop generative AI applications. However, as we will discuss in this book, these applications needed to be extended with state management, retrieval of supplementary data, interactions with the API, and so on. Such extended systems are often referred to as LLM-augmented autonomous agents (LAAs), which are computer programs capable of interacting with their environment and solving complex tasks with their underlying LLM by leveraging past observations and actions [1]. LAAs need orchestration, which is a process through which the LAA controls the underlying LLMs and coordinates the planning and step-by-step execution of a task.

LangChain is one of the most popular open source frameworks for LLM orchestration. Harrison Chase launched it as an open source project in 2022, and it attracted a lot of attention and quickly grew in popularity [2]. In February 2024, the LangChain team raised a Series A round...

LangChain primitives – chains and runnables

As we have discussed, LangChain is a framework for orchestrating your generative AI applications. Let’s look at two key LangChain primitives: runnables and chains.

The most basic LangChain abstract interface is langchain_core/runnables/base.Runnable, and it makes it easy to compose different pieces together for the orchestration [4]. A Runnable defines three main methods (there are some more, mainly for asynchronous applications; you can take a look at the full documentation if you’re interested [4]):

  • invoke calls Runnable on an input. It’s an abstract method that should be defined by the specific implementation.
  • stream calls Runnable on an input and streams chunks of the response. With a default implementation, it just calls the invoke method and yields the output, but if you need proper streaming support, your Runnable should implement this method.
  • batch calls Runnable on a list of inputs ...

Main LangChain building blocks

Let’s have a high-level look at key LangChain building blocks. If you need a refresher on terms such as vectorstore, or embeddings are new to you, feel free to check the Index where you can find more details and links to relevant chapters with more detailed explanations.

Data structures

First, let’s explore some key data structures that you might encounter while developing applications. LangChain uses Pydantic, a Python data validation library; therefore, almost all LangChain data structures are Pydantic base models [7]. For those of you who are new to Pydantic, it’s a data validation Python library that allows you to define your data schemas, and if you haven’t heard about it, then you probably used an alternative schema definition library – dataclasses.

Note that LangChain has also its own langchain_core.load.Serializable class that inherits from Basemodel and adds additional control over serialization of...

LangChain with Google Cloud

To get started, you need to have a Google Cloud project with billing and enable the Vertex AI API on it. If you are new to Google Cloud or you’re unfamiliar with some of the Google Cloud services that we mention during this section, take a look at Chapter 4.

You must authenticate yourself on Google Cloud. Most probably, you’ll be trying things out with a Jupyter notebook (and we’ll look into deploying your application to production in Chapter 16). In that case, you have a few options:

  • If you’re running a Jupyter notebook locally, you can use your local credentials. All you need to do is run the following in the terminal:
    gcloud auth application-default login
  • If you’re running a Colab notebook, you can authenticate with your own credentials (but you’ll need to refresh them each time after your instance is stopped):
    from google.colab import auth
    auth.authenticate_user()

If you’re using a Jupyter...

LangChain integration with Google

What is an integration? LangChain exposes a set of interfaces, and different libraries or vendors can integrate with LangChain through their own implementation of these interfaces (of course, they might support additional parameters or features that other providers don’t have). The most obvious example of an interface is an interface for an LLM itself (we’ll discuss this in more detail in Chapter 3). You can think about your LangChain code as being a stateless transformation, and everything that requires I/O or state preservation (e.g., working with your data or storing the history of a chat conversation) will connect to different providers of such services (LLMs, databases, document storage, etc.) through an integration.

The open source community and Google continuously work together on bringing the LangChain experience to Google Cloud services:

  • langchain-google-vertexai – Contains integrations that use Vertex AI (a...

Summary

In this chapter, we look into the landscape of LangChain products and libraries. We explained that LangChain is an open source framework for developing generative AI applications, and how they integrate with service providers. We also briefly looked at LangChain itself, the start-up behind this great framework.

Then, we discussed key LangChain primitives – the data structures that are based on Pydantic models and the main interfaces that are used in chains. We looked into what a chain is and at key primitives of LCEL – a declarative language to develop new chains. Now, you should be able to easily read LangChain code and understand what’s happening behind the scenes.

Finally, we looked at the integrations available with Google products, and we’ll explore their practical usage throughout the book.

Having the big picture in front of us, let’s start to dive deeper. In the next chapter, we’ll look into the basics of how to use...

References

  1. BOLAA: Benchmarking and Orchestrating LLM-augmented Autonomous Agents, Z. Liu et al., 2023:

    https://arxiv.org/abs/2308.05960

  2. LangChain, Wikipedia:

    https://en.wikipedia.org/wiki/LangChain

  3. Announcing the General Availability of LangSmith and Our Series A Led By Sequoia Capital, Langchain blog:

    https://blog.langchain.dev/langsmith-ga/

  4. LangChain Expression Language Interface, Langchain:

    https://python.langchain.com/docs/expression_language/interface

  5. Evaluation order, Python Language Reference:

    https://docs.python.org/3/reference/expressions.html#evaluation-order

  6. Service accounts overview, Google Cloud documentation:

    https://cloud.google.com/iam/docs/service-account-overview

  7. Pydantic, product documentation:

    https://docs.pydantic.dev/latest/

  8. Gemma: Introducing new state-of-the-art open models, J. Banks, T. Warkentin:

    https://blog.google/technology/developers/gemma-open-models/

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Solve real-world business problems with hands-on examples of GenAI applications on Google Cloud
  • Learn repeatable design patterns for Gen AI on Google Cloud with a focus on architecture and AI ethics
  • Build and implement GenAI agents and workflows, such as RAG and NL2SQL, using LangChain and Vertex AI
  • Purchase of the print or Kindle book includes a free PDF eBook

Description

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.

Who is this book for?

If you’re an application developer or ML engineer eager to dive into GenAI, this book is for you. Whether you're new to LangChain or Google Cloud, you'll learn how to use these tools to build scalable AI solutions. This book is ideal for developers familiar with Python and machine learning basics looking to apply their skills in GenAI. Professionals who want to explore Google Cloud's powerful suite of enterprise-grade GenAI products and their implementation will also find this book useful.

What you will learn

  • Build enterprise-ready applications with LangChain and Google Cloud
  • Navigate and select the right Google Cloud generative AI tools
  • Apply modern design patterns for generative AI applications
  • Plan and execute proof-of-concepts for enterprise AI solutions
  • Gain hands-on experience with LangChain's and Google Cloud's AI products
  • Implement advanced techniques for text generation and summarization
  • Leverage Vertex AI Search and other tools for scalable AI solutions
Estimated delivery fee Deliver to South Africa

Standard delivery 10 - 13 business days

$12.95

Premium delivery 3 - 6 business days

$34.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Dec 20, 2024
Length: 306 pages
Edition : 1st
Language : English
ISBN-13 : 9781835889329
Category :
Concepts :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to South Africa

Standard delivery 10 - 13 business days

$12.95

Premium delivery 3 - 6 business days

$34.95
(Includes tracking information)

Product Details

Publication date : Dec 20, 2024
Length: 306 pages
Edition : 1st
Language : English
ISBN-13 : 9781835889329
Category :
Concepts :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Table of Contents

19 Chapters
Part 1: Intro to LangChain and Generative AI on Google Cloud Chevron down icon Chevron up icon
Chapter 1: Using LangChain with Google Cloud Chevron down icon Chevron up icon
Chapter 2: Foundational Models on Google Cloud Chevron down icon Chevron up icon
Part 2: Hallucinations and Grounding Responses Chevron down icon Chevron up icon
Chapter 3: Grounding Responses Chevron down icon Chevron up icon
Chapter 4: Vector Search on Google Cloud Chevron down icon Chevron up icon
Chapter 5: Ingesting Documents Chevron down icon Chevron up icon
Chapter 6: Multimodality Chevron down icon Chevron up icon
Part 3: Common Generative AI Architectures Chevron down icon Chevron up icon
Chapter 7: Working with Long Context Chevron down icon Chevron up icon
Chapter 8: Building Chatbots Chevron down icon Chevron up icon
Chapter 9: Tools and Function Calling Chevron down icon Chevron up icon
Chapter 10: Agents Chevron down icon Chevron up icon
Chapter 11: Agentic Workflows Chevron down icon Chevron up icon
Part 4: Designing Generative AI Applications Chevron down icon Chevron up icon
Chapter 12: Evaluating GenAI Applications Chevron down icon Chevron up icon
Chapter 13: Generative AI System Design Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela