[MUSIC PLAYING]
SPEAKER: Hi, are you looking to supercharge
your data-driven applications with new generative AI
capabilities like Google PaLM two?
We're going to walk you through, step
by step, how you can build new AI-powered applications
in Google Cloud using Postgres databases that you already love
and rely on.
We will show you how you can integrate the Postgres
extension pgvector with LangChain and large language
models.
Vector embeddings are a way of representing
data that makes it easy to search for the most
similar or relevant data using approximate nearest neighbor
search algorithms.
This fundamental technology can not only power user
experiences like product recommendations
or in-application searches, but it's also
a critical primitive in more advanced
machine learning use cases, like simulating
long-term memory for LLMs.
Vector embeddings are used in many AI applications,
including some of Google services,
like Google Photos, Google Lens, and YouTube.
You can learn more about the vector embeddings
in the description link below.
Cloud SQL for Postgres and AlloyDB for Postgres
both support the pgvector extension,
which enables storing and querying vector embeddings
in your Postgres database.
This allows you to easily build AI-enabled experiences
in your applications on top of your real-time operational
data.
We're going to dive into a notebook that
will guide you through creating an AI-powered app using
PG vector.
After completing all the steps, you'll
have a good understanding of how to use the PG vector extension.
You're also going to get a hands-on view of using
the LangChain open-source framework to develop
applications powered by LLMs.
And finally, you'll learn about Google Cloud's powerful PaLM
models that we make available through Vertex AI.
This notebook uses an example of an e-commerce company that
runs an online marketplace for buying and selling
children's toys.
The team wants to add new generative AI experiences
in their e-commerce applications for both buyers and sellers.
For buyers, we want to build a new AI-powered hybrid search,
where users can describe their needs in simple text,
as well as applying filters on things, like price.
For sellers, we want to add a new AI-powered
content-generation feature, where they'll
get auto-generated description suggestions for new products
that they want to sell.
The data set for this notebook has about 800 toy products
and is sampled and created from a larger public retail data set
available at Kaggle.com.
We've already done all the pre-work
of installing the required packages into our notebook,
setting up our Google Cloud environment so we can
jump right into the good parts.
You can still see all the setup instructions in the notebook,
though.
Stepping into the notebook now, we
download the data set from a web URL
and store it into a pandas dataframe.
You can see the first 10 rows of the pandas data frame
as an example of the data we will
load into our Postgres table.
The next step is to save this pandas dataframe
in our Postgres table.
We first create a table called "products" and then
copy the pandas dataframe into the table.
Next, we'll generate vector embeddings
using a text-embedding model.
The first step to do that is to split long product description
text into smaller chunks.
We need a split large descriptions into chunks
so they can fit in a single API request.
We use the character text splitter
from the LangChain library to split the description
into chunks of 500 characters each.
The next step is to generate a vector embedding for each chunk
by calling an embedded generation service.
We're using the Vertex AI text-embedding model,
which outputs a 768-dimensional vector for each chunk.
We have loaded the generated vector embeddings
into a dataframe.
And you can see the first five rows here.
Next is to use the pgvector extension
for storing the generated embeddings in the Postgres
database.
The pgvector extension introduces a new vector data
type, which allows us to directly save
a vector embedding through a simple insert
statement in Postgres.
To do this, first we create the extension in the database.
Then we create a table called "product embeddings"
that has a column of type vector.
Using an insert statement, we can store this vector directly
into the Postgres table.
Now we've extracted the semantic knowledge of our product
database and stored it into a vector table using the pgvector
extension.
Now we can show you how you use this semantic knowledge
to answer complex natural language queries from users.
For our first use case, we want to build
an AI-curated hybrid contextual search that
combines the natural language text
with regular relational filters.
For example, a grandparent wants to use the AI-powered search
to find an educational toy for their grandkid
that fits their budget.
They may enter a query like, I want
to buy a fun toy for my grandkid that teaches new skills.
Your application could also add additional filters
based on operational data, like price or real-time inventory,
to improve results.
The first step is to generate the vector embeddings
for that user query.
Here, we call the embeddings model from the application.
And the next step is to use the pgvector extension
to find similar products.
We use the similarity search operator
from the pgvector extension to find
product vectors that are closer in distance to the user query.
Additionally, we also specify regular query filters
on price in the same SQL query.
By using pgvector, we can now integrate
the power of relational databases
with vector search operations.
After running the code snippet, we
found a product that closely matches our user query.
After finding similar products and their descriptions
using pgvector, we will use this context
to generate an informative response from a large language
model for the user query.
Individual product descriptions can be long
and may not fit within the input prompt limit for an LLM model.
So we use the MapReduce chain from the LangChain framework
to generate and combine short summaries of similarly matched
products.
The combined summaries are then used
to build a high-quality prompt for an input in the LLM model.
For our prompt, we instruct the LLM
to include the name, price, and features
of every toy in the summary.
And then we instruct the LLM to answer the user query using
the combined summaries.
We can even have the LLM output the answer in markdown format
with features listed as bullet points.
Let's go ahead and run this now.
There, we have our answer, fully formatted.
For our second use case, we want to add creative content
generation that uses knowledge from our existing data set.
For example, a seller on the retail platform
wants to use the AI-powered content generation
to create a detailed description of their new product.
They can enter a new product description in a few words,
like a bicycle with brand name 'Roadstar bike' for kids
that comes with training wheels and helmet.
Like the first use case, we will use the pgvector similarity
search operator to find an existing product
description that closely matches our new product.
We will use this existing product description
as a creative prompt to our LLM model for generating
a new detailed description.
Amazing.
By leveraging the power of LLMs, we
can generate a detailed description
of an entirely new product using our existing
knowledge of bicycle products.
And that's it.
If you aren't going to keep tinkering,
don't forget to delete your database instance.
We showed you just two examples of powerful features
that you can create by combining LLMs and databases.
Generative AI is a powerful paradigm shift
in application development.
We're excited to see what you build.
Find a link to the colab we used today in the description below.
Thanks for watching.
[MUSIC PLAYING]