Retrieval-Augmented Generation (RAG) with Vertex AI - Build agents that answer questions using your own documents.
Blog Post: https://arjunprabhulal.com/adk-builtin-tools-rag/
- What is RAG?
- Prerequisites
- Google Cloud Setup
- Create a RAG Corpus
- Setup Steps
- Running the Agent
- Project Structure
- Next Steps
RAG (Retrieval-Augmented Generation) enhances LLM responses by:
- Retrieving relevant documents from your knowledge base
- Augmenting the prompt with retrieved context
- Generating accurate answers grounded in your data
┌─────────────────────────────────────────────────────────────┐
│ USER QUERY │
│ "What is our refund policy?" │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ RAG RETRIEVAL │
│ │
│ Query → Vector Search → Top-K Documents │
│ │
│ Retrieved: "policy.pdf", "faq.md", "terms.txt" │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ LLM GENERATION │
│ │
│ Context: [Retrieved documents] │
│ Query: "What is our refund policy?" │
│ │
│ Response: "Based on our policy documents, refunds are..." │
└─────────────────────────────────────────────────────────────┘
- Python 3.10+
- Google Cloud Project with billing enabled
gcloudCLI installed and authenticated- Documents to upload (PDF, TXT, HTML, etc.)
gcloud services enable aiplatform.googleapis.com
gcloud services enable discoveryengine.googleapis.com# Replace with your project ID and email
PROJECT_ID="your-project-id"
USER_EMAIL="your-email@example.com"
# Vertex AI User - for using AI Platform
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="user:$USER_EMAIL" \
--role="roles/aiplatform.user"
# Discovery Engine Editor - for RAG operations
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="user:$USER_EMAIL" \
--role="roles/discoveryengine.editor"gcloud auth application-default login- Go to Vertex AI Console
- Navigate to RAG Engine → Corpora
- Click Create Corpus
- Upload your documents
- Copy the corpus resource name
from vertexai.preview import rag
# Create corpus
corpus = rag.create_corpus(display_name="my-knowledge-base")
# Upload documents
rag.upload_file(
corpus_name=corpus.name,
path="path/to/document.pdf",
display_name="document.pdf"
)
print(f"Corpus: {corpus.name}")- Navigate to this module:
cd 08-vertex-ai-rag- Create and activate a virtual environment:
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate- Install dependencies:
pip install -r ../requirements.txt- Configure environment variables in
rag_agent/.env:
GOOGLE_GENAI_USE_VERTEXAI=1
GOOGLE_CLOUD_PROJECT=your-project-id
GOOGLE_CLOUD_LOCATION=us-central1
RAG_CORPUS=projects/your-project-id/locations/us-central1/ragCorpora/your-corpus-id
Replace your-project-id and your-corpus-id with your actual values.
adk webOpen http://127.0.0.1:8000 and select rag_agent.
Test Queries:
- "What documents do you have access to?"
- "Summarize the main topics in my knowledge base"
- "Find information about [your document topic]"
adk run rag_agent08-vertex-ai-rag/
├── README.md
└── rag_agent/
├── __init__.py
├── agent.py # RAG agent with VertexAiRagRetrieval tool
└── .env # Vertex AI configuration
| Parameter | Description | Default |
|---|---|---|
similarity_top_k |
Number of documents to retrieve | 10 |
vector_distance_threshold |
Similarity threshold (0-1) | 0.6 |
rag_resources |
List of corpus resources | Required |
Continue to 09. Vertex AI Search