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
Free Learning
Arrow right icon
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 Maximilian Tschochohei Profile Icon Jorge Zaldívar
Arrow right icon
Coming Soon Coming Soon Publishing in Dec 2024
€18.99 per month
eBook Dec 2024 306 pages 1st Edition
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Leonid Kuligin Profile Icon Maximilian Tschochohei Profile Icon Jorge Zaldívar
Arrow right icon
Coming Soon Coming Soon Publishing in Dec 2024
€18.99 per month
eBook Dec 2024 306 pages 1st Edition
Subscription
Free Trial
Renews at €18.99p/m
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Info icon
You can access this book only when it is published in Dec 2024
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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

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 : 9781835889336
Category :
Concepts :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Info icon
You can access this book only when it is published in Dec 2024
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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
€264.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
Banner background image

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 included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.