How to build a data agent with BigQuery and CloudSQL

Google Cloud Tech · Beginner ·🔍 RAG & Vector Search ·5mo ago

Key Takeaways

The video demonstrates how to build a data agent using BigQuery, CloudSQL, and other Google Cloud tools, transforming unstructured data into structured knowledge for strategic analysis. It guides viewers through the process of creating a champion's greatest feat table, generating vector embeddings, and performing vector search.

Full Transcript

Welcome back to the Agentverse. We're a crew of developers, data engineers, platform engineers, and architects. Each completing our own missions to defeat our enemies like procrastination and ambivalence. But ultimately, we must all work together to defeat the final boss, aka build and deploy a powerful, secure, and intelligent application. Check out the first video for developers linked in the description below to start from the beginning of the journey. The mission today is for the data engineers, the scholar. We need to take some chaotic unstructured text and battle scrolls and transform it into a powerful structured knowledge engine. Then we can build and deploy a wise rag agent that can provide nuanced contextaware answers to ultimately help us defeat our enemies. Picture this. Over centuries, battle reports have been recorded to document the ways that adventurers past have been victorious against monsters. These reports are written out in log files that live in a Google Cloud Storage bucket. First, we'll use BQML and Gemini to turn those unstructured reports into structured tables for strategic analysis. After that, we'll build our first rag pipeline by taking that same unstructured text, chunking it, and turning it into vector embeddings. This will unlock a deeper semantic meaning that we can then query in BigQuery. Then we'll switch gears a little bit. BigQuery is awesome, but when we're in battle, we're going to need something a little faster to defeat our enemies. So, we're going to switch to an operational database to create a spell book that we can reference during battle. Then, we'll build out a pipeline so that the latest and greatest information is at our fingertips. Finally, we'll bring all our work together to craft a master rag agent, leveraging all the tools we put together to generate that final grounded answer. I'm not going to show every step, but the lab with all of the directions is linked in the description. Are you ready? Let's go. First up, making some sense of these unstructured battle reports. The easiest way for us to do this is to leave those reports right where they are in Google Cloud Storage. Huh? Fret not. We'll set up a magic lens in the form of an external table that lets us peer from BigQuery into the storage bucket like a giant telescope. This is an in database AI powered form of ELT. Extract, load, and transform. The external table is the extract and load. It'll apply a schema on red allowing BigQuery to directly query raw text files in cloud storage. Now we can see our battle reports in a new raw intel content table. At this point, we'll have made a connection between BigQuery and other services on our project and a data set called BC data where all of our new tables and models will live. Then we'll create a BQML remote model that we can use with the MLG generate text function. This function works by calling our Gemini model directly from a SQL query and transforming our data into a JSON object. Then it's put into a new structured beastiary table with information about monsters, battles, and adventurers. And all of this happens in a single query without writing or managing a separate processing pipeline. There's our JSON. Chef's kiss. Now, JSON is Jbomb, but let's be real. We need to parse, clean, and normalize this puppy. So, this script will read the raw nested JSON from our staging table, cleanse and parse it to get to the core data, and then scribe the relevant pieces into the final table for monsters. We'll do the same for adventurers and battles. And the tables will look a little something like this. Now we have our three tables looking all fine. But if we want to do some real analysis and get strategic insights to use the true power of structured data tables, we're going to need to join them. Exactly. You're smart. Let's go ahead and do that. To create a champion's greatest feat table, now I want to know what's the name of the most powerful monster that each adventurer defeated, and how long did it take them? This is a complex question that covers information across our three tables. I'll run this in the BigQuery editor to get my final table. And there we go. We have the information for the biggest foe that each adventurer defeated and the time it took. Looks like perfectionism is no match for Ara. And now we completed the first part of our mission. turning unstructured battle reports into structured, useful data that you can get insights from. Next up, we want to get some deeper semantic meaning from our data. Structured tables are great, but if I ask a question about a specific monster or its attack, I could get back an entire battle report that might not be exactly what I need. Imagine searching a database for expenses and not getting back a relevant article that mentions expenditure reports. So let's build a rag data preparation pipeline to do some more precise vector search in BigQuery. Rag consists of a few different parts. First, chunking. In order to make it easier to query our data, we need to chunk it into smaller pieces that make sense. If they don't make sense, then it won't be helpful. And this is why there are a few different types of chunking strategies. We'll use the split function to do chunking at every period and then save those sentences into a new chunked Intel table. You can get much fancier with recursive or contentaware chunking, but this is good for what we need. Next, we take each of those chunks and turn them into vector embeddings. Why? Because embeddings are a mathematical representation of our data stored in a vector format. They help us translate the semantic meaning of our data into a format that the computer can understand and compare. Computers are great at math but not so great at human language nuances. So the model does the part of understanding the meaning of the text itself and then encoding it into the computer's language to enable vector search. We'll once again call upon BQML and our embedding model to help us generate these embeddings. This time using the MLG generate embedding function. This function will read every row from our chunked intel table, send the text to the model, and store the resulting vector embeddings in a new embedded intel table. This new table within BigQuery is considered our knowledge base that we can use to do our vector search. And later on, we'll switch to a different vector database, and we'll discuss why. So now, we can start asking questions to do semantic search. something like what are the tactics against a foe that causes paralysis? But before we can get an output, Gemini will turn this prompt into an embedding as it comes in using the same BQML function as before. So now we have our text chunks from our embedded intel table as embeddings and our prompt as an embedding. With these numerical coordinates, BigQuery can perform a vector search and measure the distance between these two embeddings, find the ones that are the closest, giving us our semantic search results as the output. A quick note from our sponsor, this distance that I speak of. There are two main types of distances that are used in vector search, cosine and uklitian. We're using cosine distance, and all you need to know is that it's fantastic for semantic search. It finds texts with a similar meaning, even if the words are different. The smaller the distance or the angle between the vectors, the more aligned their meanings are. Back to our output. As you can see, this is so much more powerful than a basic keyword search. A keyword search would only return scrolls that contain the exact words, tactic, or paralysis. But the top result for our semantic search demonstrates the understanding of the deeper meaning of tactics against paralysis with words that are close like shattered and paralyzing. Moving on, why are we switching databases? BigQuery is an amazing analytics data warehouse. It's designed to answer big complex questions by scanning massive amounts of data like a research library. But when our agent is in the heat of battle, we need a spellbook that it can reference quickly, not an entire library. Something designed to serve live applications by handling many small, fast, and frequent requests. Enter cloud SQL for Postgress and the PG vector extension. Here we have our Arcane Wisdom database and in the query editor of SQL Studio, I can run this query to add the PG vector and Google ML integration extensions to my database. The PG vector extension adds the vector data type to the database and provides the function cosign distance needed to perform similarity search. Without it, Cloud SQL wouldn't know how to store or search the embeddings. The Google ML integration extension provides the embedding function, a powerful feature that we'll use in a little bit that allows us to call the Vertex AI embedding model directly from our SQL query. Once that's done, we can run this command in the editor. It'll create an ancient scrolls table to store both the original battle reports text and their corresponding vector embeddings, making it a perfect spellbook for our rag agent. Quick tip. Before building a complex pipeline, run a manual test. This makes debugging way easier by letting us test our connection and logic in isolation. If everything is working, you'll get data in your database. And it'll look something like this. Our final step before the pipeline is the addition of an index. The a hierarchical navigable small world hnssw index to be exact. This will pre-organize the data and build a multi-layered graph of the vectors starting generally at the top and getting progressively more detailed in its search to pinpoint the exact neighbors at an exceptional speed. Without an index, our search has to do a sequential scan. which is slow. Watch the execution time here. Now we'll create an HNSW index. Let's run the exact same search again. Look at the execution time now. Boom. That's the power of a vector index. All this talk of a pipeline and the time is finally here. We're going to build out our dataf flow pipeline to turn that simple manual process that we just did into a massively scalable, automated, and robust assembly line of scribes. This will process all our existing scrolls and any new ones we add later in a large batch job whenever we run the pipeline. Imagine if we had millions of scrolls. This would be very helpful. We'll build this pipeline using dataf flow, Google cloud's managed service for Apache Beam. This lets us turn our manual script into an automated job. We'll assemble our pipeline right in our code. Now, there are two main components that we'll be using. Embed text batch, which takes a batch of raw text files, calls the Gemini text embedding model in Vert.ex AI, and generates a vector embedding for each one. and write essence to spellillbook, a custom function that connects to our cloud SQL database and inserts each scrolls text and its new vector embedding into the ancient scrolls table. Here's how the pipeline flows. We'll read the files, send them in batches to an embed text batch function to get embeddings and then pass the results to a write essence to spellbook function to save them to our database. Now we can trigger the scheduled jobs for our batch pipeline with a terminal command and check the progress right in dataf flow. Once all the jobs are finished running, we can run a select query in Cloud SQL to see that our scrolls have all been pulled in and have their embeddings. Finally, we have a spellbook that is a true knowledge engine and it's ready to be queried for meaning. How will it be queried, you ask? Ah, this is where our rag agent officially comes in. It's built to perform that full retrieve augment generate loop that we discussed. Let's take a peek at the code. It's a pretty traditional agent where the model, instructions, and tools are all defined. And the first step in the instructions is consult the scrolls with the grimoire lookup tool. The tool is the retrieve part where Cloud SQL is consulted for information about a specific monster. The function takes the monster's name from our questions and converts it into an embedding. It then searches the ancient scrolls table in cloud SQL for the most similar embeddings and returns the top three scrolls which are used to augment the initial question and then generate the final answer. Our agent is ready. Now we just need to fire it up and test it out. If we ask it about the hydra of scope creep, it'll follow this entire process and return us useful contextaware information on how to defeat it. Turns out to avoid scope creep, we need to stop saying yes to more undefined work. I feel personally attacked. Finally, you can deploy your agent to Cloud Run to show that you've completed the mission and then test out your own knowledge of building this rag agent with the boss fight. Remember to clean up your resources to avoid any unwanted costs. As a data engineer, our primary weapon is knowledge. We transform raw chaotic data into actionable intelligence. Much like an alchemist transforms raw chaotic lead into structured, valuable gold. Along the way, we've made crucial architectural decisions to craft our knowledge engine and provide our final rack agent with the power to be victorious. Go to the description to check out the lab link for yourself and find a lot more resources like a detailed blog post and a more detailed video with step-by-step instructions. I encourage you to explore the other paths and see how your skills as a data engineer connect with the developer, the platform engineer, and the architect. Together, we are unstoppable. Scholar out. Wow.

Original Description

Agentverse - Building Knowledge Engines with RAG → https://goo.gle/4pSijGw First video for Developers → https://goo.gle/4jY30uw Discover how to build a powerful data agent using ADK, BigQuery and CloudSQL. This video guides you through transforming unstructured data into structured knowledge, enabling intelligent applications. Watch along and learn how to create RAG pipelines, leverage Gemini for vector embeddings, and automate processes with Dataflow to achieve nuanced, context aware insights. Chapters: 0:00 - Intro 0:34 - The data engineering mission 2:13 - Unstructured to structured data with BQML and Gemini 5:06 - Use RAG to perform vector search 8:43 - Accelerating search with CloudSQL and HNSW indexes 11:20 - Automating with a Dataflow and Apache Beam pipeline 13:15 - How the RAG agent works 14:51 - Key takeaways 15:06 - Get started today 🔔 Subscribe to Google Cloud Tech → https://goo.gle/GoogleCloudTech #Gemini #GoogleCloud #Agentverse Speaker: Debi Cabrera Products Mentioned: BigQuery, CloudSQL, Agentverse, Gemini, Agent Development Kit
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Google Cloud Tech · Google Cloud Tech · 0 of 60

← Previous Next →
1 I’m going for it #GoogleCloudCertified
I’m going for it #GoogleCloudCertified
Google Cloud Tech
2 I had to get #GoogleCloudCertified
I had to get #GoogleCloudCertified
Google Cloud Tech
3 Be better overall at what you do #GoogleCloudCertified
Be better overall at what you do #GoogleCloudCertified
Google Cloud Tech
4 Cloud Monitoring on our radar #Analysis #Uptime
Cloud Monitoring on our radar #Analysis #Uptime
Google Cloud Tech
5 Introduction to Generative AI Studio
Introduction to Generative AI Studio
Google Cloud Tech
6 How to use Github Actions with Google's Workload Identity Federation
How to use Github Actions with Google's Workload Identity Federation
Google Cloud Tech
7 Introduction to Responsible AI
Introduction to Responsible AI
Google Cloud Tech
8 Networking updates and CDMC-certified architecture
Networking updates and CDMC-certified architecture
Google Cloud Tech
9 Create and use a Cloud Storage bucket
Create and use a Cloud Storage bucket
Google Cloud Tech
10 How to digitize text from documents
How to digitize text from documents
Google Cloud Tech
11 Faster analytical queries with AlloyDB
Faster analytical queries with AlloyDB
Google Cloud Tech
12 Next ‘23 sessions and FaaS Wave
Next ‘23 sessions and FaaS Wave
Google Cloud Tech
13 Introduction to Assured Open Source Software
Introduction to Assured Open Source Software
Google Cloud Tech
14 BigQuery Cost Optimization: Storage
BigQuery Cost Optimization: Storage
Google Cloud Tech
15 BigQuery Cost Optimization: Compute
BigQuery Cost Optimization: Compute
Google Cloud Tech
16 BigQuery Cost Optimization: Select Queries
BigQuery Cost Optimization: Select Queries
Google Cloud Tech
17 Remote Field Equipment Management with Manufacturing Data Engine
Remote Field Equipment Management with Manufacturing Data Engine
Google Cloud Tech
18 Supercharging your applications with Cloud SQL Enterprise Plus
Supercharging your applications with Cloud SQL Enterprise Plus
Google Cloud Tech
19 Vector Support on our radar #GenAI
Vector Support on our radar #GenAI
Google Cloud Tech
20 Architecting a blockchain startup with Google Cloud
Architecting a blockchain startup with Google Cloud
Google Cloud Tech
21 Kubernetes and multitasking updates!
Kubernetes and multitasking updates!
Google Cloud Tech
22 GKE: Using Kubernetes Events
GKE: Using Kubernetes Events
Google Cloud Tech
23 How to configure firewall rules for Cloud Composer
How to configure firewall rules for Cloud Composer
Google Cloud Tech
24 Vertex AI Embeddings API + Matching Engine: Grounding LLMs made easy
Vertex AI Embeddings API + Matching Engine: Grounding LLMs made easy
Google Cloud Tech
25 Geospatial analytics on our radar #EarthEngine #BigQuery
Geospatial analytics on our radar #EarthEngine #BigQuery
Google Cloud Tech
26 Ensuring requests are set in Kubernetes
Ensuring requests are set in Kubernetes
Google Cloud Tech
27 Cloud Next 2023, Google research program, and more!
Cloud Next 2023, Google research program, and more!
Google Cloud Tech
28 How to migrate projects between organizations with Resource Manager
How to migrate projects between organizations with Resource Manager
Google Cloud Tech
29 How to run #MySQL in Google Cloud
How to run #MySQL in Google Cloud
Google Cloud Tech
30 #GenerativeAI for enterprises and #Next2023
#GenerativeAI for enterprises and #Next2023
Google Cloud Tech
31 How Google Photos scales to store 4 trillion photos and videos
How Google Photos scales to store 4 trillion photos and videos
Google Cloud Tech
32 Google Cross-Cloud Interconnect (Demo 2)
Google Cross-Cloud Interconnect (Demo 2)
Google Cloud Tech
33 GKE Cost Optimization Golden Signals: Introduction
GKE Cost Optimization Golden Signals: Introduction
Google Cloud Tech
34 GKE Cost Optimization Golden Signals: Workload Rightsizing
GKE Cost Optimization Golden Signals: Workload Rightsizing
Google Cloud Tech
35 GKE Load Balancing: Overview
GKE Load Balancing: Overview
Google Cloud Tech
36 GKE Load Balancing: Best Practices
GKE Load Balancing: Best Practices
Google Cloud Tech
37 Disaster Recovery in GKE
Disaster Recovery in GKE
Google Cloud Tech
38 How to configure IP masquerade agent in GKE Standard clusters
How to configure IP masquerade agent in GKE Standard clusters
Google Cloud Tech
39 Enable and use GKE Control plane logs
Enable and use GKE Control plane logs
Google Cloud Tech
40 Compliance in Australia with Assured Workloads
Compliance in Australia with Assured Workloads
Google Cloud Tech
41 Creating budgets and budget alerts in Google Cloud #FinOps
Creating budgets and budget alerts in Google Cloud #FinOps
Google Cloud Tech
42 Cloud SQL Enterprise Plus on our radar #mySQL
Cloud SQL Enterprise Plus on our radar #mySQL
Google Cloud Tech
43 What's Next for Google Cloud?
What's Next for Google Cloud?
Google Cloud Tech
44 How Loveholidays scaled with Contact Center AI
How Loveholidays scaled with Contact Center AI
Google Cloud Tech
45 What is fleet team management in GKE?
What is fleet team management in GKE?
Google Cloud Tech
46 Troubleshoot VPC Network Peering
Troubleshoot VPC Network Peering
Google Cloud Tech
47 Introduction to DocAI and Contact Center AI
Introduction to DocAI and Contact Center AI
Google Cloud Tech
48 Cloud Run Direct VPC egress explained
Cloud Run Direct VPC egress explained
Google Cloud Tech
49 Database deployment options in GKE
Database deployment options in GKE
Google Cloud Tech
50 Analyze cloud billing data with #BigQuery
Analyze cloud billing data with #BigQuery
Google Cloud Tech
51 Tips to becoming a world-class Prompt Engineer
Tips to becoming a world-class Prompt Engineer
Google Cloud Tech
52 Serverless is simple. Do I need CI/CD?
Serverless is simple. Do I need CI/CD?
Google Cloud Tech
53 Accelerating model deployment with MLOps
Accelerating model deployment with MLOps
Google Cloud Tech
54 How Hawaii's Department of Human Services scaled with CCAI
How Hawaii's Department of Human Services scaled with CCAI
Google Cloud Tech
55 Pricing API on our #Radar
Pricing API on our #Radar
Google Cloud Tech
56 How Recommendations AI for Media can boost customer retention
How Recommendations AI for Media can boost customer retention
Google Cloud Tech
57 Troubleshooting: Node Not Ready Status
Troubleshooting: Node Not Ready Status
Google Cloud Tech
58 One weekend until Cloud Next 2023!
One weekend until Cloud Next 2023!
Google Cloud Tech
59 #GoogleCloudNext starts tomorrow!
#GoogleCloudNext starts tomorrow!
Google Cloud Tech
60 #GoogleCloudNext will be demand!
#GoogleCloudNext will be demand!
Google Cloud Tech

This video teaches developers how to build a powerful data agent using BigQuery, CloudSQL, and other Google Cloud tools, enabling them to transform unstructured data into structured knowledge for strategic analysis. The data agent can be used to perform vector search, semantic search, and fine-tuning, making it a valuable tool for businesses and organizations. By following the steps outlined in the video, viewers can create their own data agent and start analyzing their data in a more efficient

Key Takeaways
  1. Create an external table in BigQuery to query raw text files in Cloud Storage
  2. Use BQML remote model with MLG generate text function to transform data into a JSON object
  3. Parse, clean, and normalize JSON data to get core information
  4. Join three tables to create a champion's greatest feat table
  5. Run a query in BigQuery to create a champion's greatest feat table
  6. Use the split function to chunk text into smaller pieces
  7. Generate vector embeddings for text chunks using BigQuery ML
  8. Perform vector search in BigQuery using cosine distance
  9. Add the PG vector and Google ML integration extensions to the Cloud SQL database
  10. Create an HNSW index for faster vector search
💡 The video highlights the importance of using retrieval augmented generation and vector search to transform unstructured data into structured knowledge, enabling businesses and organizations to make better decisions and improve their operations.

Related Reads

📰
Optimizing RAG at Scale: Chunking, Retrieval, and the Bayesian Search That Cut Latency 40%
Learn how to optimize RAG at scale using chunking, retrieval, and Bayesian search to reduce latency by 40%
Dev.to · Imus
📰
Optimizing RAG at Scale: Chunking, Retrieval, and the Bayesian Search That Cut Latency 40%
Learn how to optimize RAG at scale using chunking, retrieval, and Bayesian search to reduce latency by 40% and achieve 95% recall@10
Dev.to AI
📰
Optimizing RAG at Scale: Chunking, Retrieval, and the Bayesian Search That Cut Latency 40%
Learn how to optimize RAG at scale using chunking, retrieval, and Bayesian search to reduce latency by 40%
Dev.to · Imus
📰
Optimizing RAG at Scale: Chunking, Retrieval, and the Bayesian Search That Cut Latency 40%
Optimize RAG at scale using chunking, retrieval, and Bayesian search to reduce latency by 40% and achieve 95% recall@10
Dev.to AI

Chapters (9)

Intro
0:34 The data engineering mission
2:13 Unstructured to structured data with BQML and Gemini
5:06 Use RAG to perform vector search
8:43 Accelerating search with CloudSQL and HNSW indexes
11:20 Automating with a Dataflow and Apache Beam pipeline
13:15 How the RAG agent works
14:51 Key takeaways
15:06 Get started today
Up next
Build a Chatbot with RAG in 10 minutes | Python, LangChain, OpenAI
Thomas Janssen
Watch →