How to Build Your First RAG Agent with Agent Development Kit (ADK + Vertex AI RAG Service)

aiwithbrandonAbout 7 min readMay 27, 2025Watch original
THE SUMMARYAI-generated

Key Concepts

  • RAG (Retrieval-Augmented Generation): A framework for enhancing the knowledge of language models by retrieving information from external sources.
  • Agent Development Kit (ADK): Google's tool for building AI agents.
  • Vertex AI: Google Cloud's AI platform.
  • Knowledge Base/Corpus: A collection of documents used as a source of information for the RAG agent.
  • Embedding Model: A model that converts text into numerical vectors (embeddings) for semantic comparison.
  • Vector Store: A database that stores embeddings for efficient retrieval of similar information.
  • Chunking: Dividing documents into smaller segments for better processing and retrieval.
  • Chunk Size: The number of tokens or characters in each chunk.
  • Chunk Overlap: The amount of overlap between adjacent chunks.
  • Top K: The number of most relevant documents to retrieve from the vector store.
  • Distance Threshold: A measure of similarity between embeddings, used to filter retrieved documents.
  • Google Cloud CLI: Command-line interface for interacting with Google Cloud services.
  • State: A mechanism within ADK to maintain information across multiple agent interactions.

1. Introduction to RAG Agent with Google's Agent Development Kit

  • The video demonstrates how to build a RAG agent using Google's Agent Development Kit (ADK) that can answer questions about documents stored in Google Drive.
  • The agent leverages Google's knowledge base service (Vertex AI Rag service) for easier management and retrieval of information.
  • Understanding RAG is crucial for building real-world AI applications.
  • The video provides a step-by-step guide, starting from setting up projects in Google Cloud to connecting agents to the knowledge base.
  • The source code is provided for free.

2. Demonstration of the RAG Agent

  • The agent can identify connected data sources (corpora).
    • Example: "Hey what data sources are you connected to?" returns "test corpus."
  • It can list the documents within a specific corpus.
    • Example: "What data sources do I have in that corpus?" lists PDF and slideshow documents.
  • It can answer questions based on the content of the documents.
    • Example: "What is a lead magnet and why do we need one for our channel?" retrieves relevant information from the slides and provides an answer.
  • New documents can be added to the agent to expand its knowledge.
    • A Google Slide document is added by providing its link.
    • The agent chunks, embeds, and stores the new document in the vector store.
  • The agent can then answer questions about the newly added document.
    • Example: "What do we talk about in week five?" retrieves information from the newly added slides.

3. Setting Up Google Cloud Project

  • Step 1: Create a Google Cloud Project
    • Search for "Google Cloud" and sign up for an account.
    • Navigate to the console (dashboard).
    • Create a new project (e.g., "ADK rag YouTube").
    • Connect a billing account.
      • Costs are associated with token usage (Gemini Flash 2.5 model) and storage (approximately $0.20 per gigabyte per month).
  • Step 2: Enable Vertex AI Services
    • Go to Vertex AI (the AI suite for Google Cloud).
    • Enable all recommended APIs.

4. Setting Up Google Cloud CLI

  • Step 1: Install Google Cloud CLI
    • Follow the installation instructions for your operating system (linked in the readme file).
    • Download the appropriate package (e.g., for Apple Silicon).
    • Unzip the package and locate the installation.sh file.
    • Open a terminal and navigate to the downloads folder.
    • Run the installation script: bash ./google-cloud-sdk/install.sh.
    • Update the path when prompted.
    • Install recommended modules.
    • Verify the installation by running gcloud version.
  • Step 2: Initialize Google Cloud CLI
    • Run gcloud init.
    • Sign in to your Google account in the browser.
    • Allow Google Cloud SDK to access your account.
    • Select the project you created (e.g., "ADK Rag YouTube").
    • Configure a default compute region and zone (e.g., us-central1-a).

5. Code Overview and Setup

  • Step 1: Set Up Local Environment
    • Create a virtual environment: python3 -m venv .venv.
    • Activate the virtual environment:
      • Mac: source .venv/bin/activate
      • Windows: .venv\Scripts\activate
    • Install dependencies from requirements.txt: pip install -r requirements.txt.
  • Step 2: Set Up .env File
    • Create a .env file in the project root.
    • Add the following environment variables:
      • PROJECT_ID: Your Google Cloud project ID.
      • CLOUD_LOCATION: central1.
  • Step 3: Run the Application
    • Navigate to the project root in the terminal.
    • Run adk web to start the server.
    • Access the agent in your browser.

6. Agent Structure and Instructions

  • The agent uses a Gemini 2.5 flash model.
  • The agent's behavior is defined by instructions that provide context, capabilities, and interaction guidelines.
  • Instructions include:
    • Defining the agent as a RAG agent working with a document store.
    • Listing available commands (querying, listing, creating, deleting).
    • Providing instructions on how to interact with the user.
    • Classifying requests and calling appropriate tools.
    • Specifying parameters for each tool.
    • Adding communication guidelines for a better user experience.
  • The agent maintains state to track the current corpus being used.

7. Tool: Create Corpus

  • Allows creating new knowledge stores (corpora) in Vertex AI.
  • Requires a name for the data store (following a naming schema).
  • Uses an embedding model configuration (Google's text embedding model version five).
  • The embedding model converts text to numerical vectors for semantic comparison.
  • The create_corpus function is called with the display name and embedding model configuration.
  • The agent's state is updated to reflect the new corpus.
  • Returns information about the created corpus (display name, project name, ID).

8. Tool: List Corpora

  • Lists all the corpora that have been created.
  • Uses the list_corpora function from the Vertex AI RAG API.
  • Iterates through the list of corpora and extracts relevant information (resource name, display name, creation time).
  • Returns a list of corpus details to the user.

9. Tool: Add Data

  • Adds data (files from Google Drive or Cloud Storage) to a specified corpus.
  • Requires specifying the corpus and the paths to the documents.
  • Supports Google Docs, Sheets, Slides, and files in Google Cloud Storage.
  • Validates the input paths to ensure they are in the correct format.
  • Chunks the documents into smaller pieces using a specified chunk size and overlap.
    • Chunk Size: The number of tokens or characters in each chunk (default: 512).
    • Chunk Overlap: The amount of overlap between adjacent chunks (default: 20%).
  • Uses the import_files function to import the files into the vector store.
  • Resets the current corpus in the agent's state.
  • Returns a message indicating the number of files imported and their location.

10. Tool: Get Corpus Information

  • Retrieves information about the documents within a specific corpus.
  • Requires the corpus name.
  • Uses the list_files function to retrieve a list of files in the corpus.
  • Extracts relevant information about each file (file ID, display name, resource identifier, creation time, last updated time).
  • Returns a list of file details to the user.

11. Tool: Rag Query

  • Answers questions based on the content of the documents in a corpus.
  • Requires the corpus name and the query (question).
  • Uses retrieval parameters to configure the query:
    • Top K: The number of most relevant documents to retrieve (default: 3).
    • Distance Threshold: A measure of similarity between embeddings (default: 0.5).
  • The query is embedded before being passed to the vector store.
  • The query function is called with the corpus name, query, and retrieval parameters.
  • Returns a list of results, including the resource identifier, source name, text, and score (similarity).

12. Tool: Delete Document

  • Deletes a specific document from a corpus.
  • Requires the corpus name and the document ID.
  • Uses the delete_file function to delete the document.
  • If no errors are thrown, it assumes the deletion was successful.

13. Tool: Delete Corpus

  • Deletes an entire corpus.
  • Requires the corpus name and a confirmation.
  • Uses the delete function to delete the corpus.
  • Updates the agent's state to indicate that the corpus no longer exists.

14. Conclusion

  • The video provides a comprehensive guide to building RAG agents using Google's Agent Development Kit and Vertex AI.
  • It covers the essential steps, from setting up the environment to implementing various tools for managing and querying knowledge bases.
  • The RAG agent can answer questions about documents stored in Google Drive, add new documents, and delete existing ones.
  • The video emphasizes the importance of understanding RAG for building real-world AI applications.

AI summaries can miss context or contain errors. Check important details against the original video.

Go a little deeper.

Have a question about this video? Load its transcript to open the video chat.