|
| 1 | +# Understanding Vertex AI Search Grounding |
| 2 | + |
| 3 | +[Vertex AI Search Grounding tool](../tools/built-in-tools.md#vertex-ai-search) is a powerful feature in the Agent Development Kit (ADK) that enables AI agents to access information from your private enterprise documents and data repositories. By connecting your agents to indexed enterprise content, you can provide users with answers grounded in your organization's knowledge base. |
| 4 | + |
| 5 | +This feature is particularly valuable for enterprise-specific queries requiring information from internal documentation, policies, research papers, or any proprietary content that has been indexed in your [Vertex AI Search](https://cloud.google.com/enterprise-search) datastore. When your agent determines that information from your knowledge base is needed, it automatically searches your indexed documents and incorporates the results into its response with proper attribution. |
| 6 | + |
| 7 | +## What You'll Learn |
| 8 | + |
| 9 | +In this guide, you'll discover: |
| 10 | + |
| 11 | +- **Quick Setup**: How to create and run a Vertex AI Search-enabled agent from scratch |
| 12 | +- **Grounding Architecture**: The data flow and technical process behind enterprise document grounding |
| 13 | +- **Response Structure**: How to interpret grounded responses and their metadata |
| 14 | +- **Best Practices**: Guidelines for displaying citations and document references to users |
| 15 | + |
| 16 | +## Vertex AI Search Grounding Quickstart |
| 17 | + |
| 18 | +This quickstart guides you through creating an ADK agent with Vertex AI Search grounding feature. This quickstart assumes a local IDE (VS Code or PyCharm, etc.) with Python 3.9+ and terminal access. |
| 19 | + |
| 20 | +### 1. Prepare Vertex AI Search |
| 21 | + |
| 22 | +If you already have a Vertex AI Search Data Store and its Data Store ID, you can skip this section. If not, follow the instruction in the [Get started with custom search](https://cloud.google.com/generative-ai-app-builder/docs/try-enterprise-search#unstructured-data) until the end of [Create a data store](https://cloud.google.com/generative-ai-app-builder/docs/try-enterprise-search#create_a_data_store), with selecting the `Unstructured data` tab. With this instruction, you will build a sample Data Store with earning report PDFs from the [Alphabet invector site](https://abc.xyz/). |
| 23 | + |
| 24 | +After finishing the Create a data store section, open the [Data Stores](https://console.cloud.google.com/gen-app-builder/data-stores/) and select the data store you created, and find the `Data store ID`: |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +Note this `Data store ID` as we will use this later. |
| 29 | + |
| 30 | +### 2. Set up Environment & Install ADK {#venv-install} |
| 31 | + |
| 32 | +Create & Activate Virtual Environment: |
| 33 | + |
| 34 | +```bash |
| 35 | +# Create |
| 36 | +python -m venv .venv |
| 37 | + |
| 38 | +# Activate (each new terminal) |
| 39 | +# macOS/Linux: source .venv/bin/activate |
| 40 | +# Windows CMD: .venv\Scripts\activate.bat |
| 41 | +# Windows PowerShell: .venv\Scripts\Activate.ps1 |
| 42 | +``` |
| 43 | + |
| 44 | +Install ADK: |
| 45 | + |
| 46 | +```bash |
| 47 | +pip install google-adk==1.5.0 |
| 48 | +``` |
| 49 | + |
| 50 | +### 3. Create Agent Project {#create-agent-project} |
| 51 | + |
| 52 | +Under a project directory, run the following commands: |
| 53 | + |
| 54 | +```bash |
| 55 | +# Step 1: Create a new directory for your agent |
| 56 | +mkdir vertex_search_agent |
| 57 | + |
| 58 | +# Step 2: Create __init__.py for the agent |
| 59 | +echo "from . import agent" > vertex_search_agent/__init__.py |
| 60 | + |
| 61 | +# Step 3: Create an agent.py (the agent definition) and .env (authentication config) |
| 62 | +touch vertex_search_agent/agent.py .env |
| 63 | +``` |
| 64 | + |
| 65 | +#### Edit `agent.py` |
| 66 | + |
| 67 | +Copy and paste the following code into `agent.py`, and replace `YOUR_PROJECT_ID` and `YOUR_DATASTORE_ID` at the `Configuration` part with your project ID and Data Store ID accordingly: |
| 68 | + |
| 69 | +```python title="vertex_search_agent/agent.py" |
| 70 | +from google.adk.agents import Agent |
| 71 | +from google.adk.tools import VertexAiSearchTool |
| 72 | + |
| 73 | +# Configuration |
| 74 | +DATASTORE_ID = "projects/YOUR_PROJECT_ID/locations/global/collections/default_collection/dataStores/YOUR_DATASTORE_ID" |
| 75 | + |
| 76 | +root_agent = Agent( |
| 77 | + name="vertex_search_agent", |
| 78 | + model="gemini-2.5-flash", |
| 79 | + instruction="Answer questions using Vertex AI Search to find information from internal documents. Always cite sources when available.", |
| 80 | + description="Enterprise document search assistant with Vertex AI Search capabilities", |
| 81 | + tools=[VertexAiSearchTool(data_store_id=DATASTORE_ID)] |
| 82 | +) |
| 83 | +``` |
| 84 | + |
| 85 | +Now you would have the following directory structure: |
| 86 | + |
| 87 | +```console |
| 88 | +my_project/ |
| 89 | + vertex_search_agent/ |
| 90 | + __init__.py |
| 91 | + agent.py |
| 92 | + .env |
| 93 | +``` |
| 94 | + |
| 95 | +### 4. Authentication Setup {#choose-a-platform} |
| 96 | + |
| 97 | +**Note: Vertex AI Search requires Google Cloud Platform (Vertex AI) authentication. Google AI Studio is not supported for this tool.** |
| 98 | + |
| 99 | + * Set up the [gcloud CLI](https://cloud.google.com/vertex-ai/generative-ai/docs/start/quickstarts/quickstart-multimodal#setup-local) |
| 100 | + * Authenticate to Google Cloud, from the terminal by running `gcloud auth login`. |
| 101 | + * Open the **`.env`** file and copy-paste the following code and update the project ID and location. |
| 102 | + |
| 103 | + ```env title=".env" |
| 104 | + GOOGLE_GENAI_USE_VERTEXAI=TRUE |
| 105 | + GOOGLE_CLOUD_PROJECT=YOUR_PROJECT_ID |
| 106 | + GOOGLE_CLOUD_LOCATION=LOCATION |
| 107 | + ``` |
| 108 | +
|
| 109 | +
|
| 110 | +### 5. Run Your Agent {#run-your-agent} |
| 111 | +
|
| 112 | +There are multiple ways to interact with your agent: |
| 113 | +
|
| 114 | +=== "Dev UI (adk web)" |
| 115 | + Run the following command to launch the **dev UI**. |
| 116 | +
|
| 117 | + ```shell |
| 118 | + adk web |
| 119 | + ``` |
| 120 | + |
| 121 | + !!!info "Note for Windows users" |
| 122 | +
|
| 123 | + When hitting the `_make_subprocess_transport NotImplementedError`, consider using `adk web --no-reload` instead. |
| 124 | +
|
| 125 | +
|
| 126 | + **Step 1:** Open the URL provided (usually `http://localhost:8000` or |
| 127 | + `http://127.0.0.1:8000`) directly in your browser. |
| 128 | +
|
| 129 | + **Step 2.** In the top-left corner of the UI, you can select your agent in |
| 130 | + the dropdown. Select "vertex_search_agent". |
| 131 | +
|
| 132 | + !!!note "Troubleshooting" |
| 133 | +
|
| 134 | + If you do not see "vertex_search_agent" in the dropdown menu, make sure you |
| 135 | + are running `adk web` in the **parent folder** of your agent folder |
| 136 | + (i.e. the parent folder of vertex_search_agent). |
| 137 | +
|
| 138 | + **Step 3.** Now you can chat with your agent using the textbox. |
| 139 | +
|
| 140 | +=== "Terminal (adk run)" |
| 141 | +
|
| 142 | + Run the following command, to chat with your Vertex AI Search agent. |
| 143 | +
|
| 144 | + ``` |
| 145 | + adk run vertex_search_agent |
| 146 | + ``` |
| 147 | + To exit, use Cmd/Ctrl+C. |
| 148 | +
|
| 149 | +### 📝 Example prompts to try |
| 150 | +
|
| 151 | +With those questions, you can confirm that the agent is actually calling Vertex AI Search |
| 152 | +to get information from the Alphabet reports: |
| 153 | +
|
| 154 | +* What is the revenue of Google Cloud in 2022 Q1? |
| 155 | +* What about YouTube? |
| 156 | +
|
| 157 | + |
| 158 | +
|
| 159 | +You've successfully created and interacted with your Vertex AI Search agent using ADK! |
| 160 | +
|
| 161 | +## How grounding with Vertex AI Search works |
| 162 | +
|
| 163 | +Grounding with Vertex AI Search is the process that connects your agent to your organization's indexed documents and data, allowing it to generate accurate responses based on private enterprise content. When a user's prompt requires information from your internal knowledge base, the agent's underlying LLM intelligently decides to invoke the `VertexAiSearchTool` to find relevant facts from your indexed documents. |
| 164 | +
|
| 165 | +### **Data Flow Diagram** |
| 166 | +
|
| 167 | +This diagram illustrates the step-by-step process of how a user query results in a grounded response. |
| 168 | +
|
| 169 | + |
| 170 | +
|
| 171 | +### **Detailed Description** |
| 172 | +
|
| 173 | +The grounding agent uses the data flow described in the diagram to retrieve, process, and incorporate enterprise information into the final answer presented to the user. |
| 174 | +
|
| 175 | +1. **User Query**: An end-user interacts with your agent by asking a question about internal documents or enterprise data. |
| 176 | +
|
| 177 | +2. **ADK Orchestration**: The Agent Development Kit orchestrates the agent's behavior and passes the user's message to the core of your agent. |
| 178 | +
|
| 179 | +3. **LLM Analysis and Tool-Calling**: The agent's LLM (e.g., a Gemini model) analyzes the prompt. If it determines that information from your indexed documents is required, it triggers the grounding mechanism by calling the VertexAiSearchTool. This is ideal for answering queries about company policies, technical documentation, or proprietary research. |
| 180 | +
|
| 181 | +4. **Vertex AI Search Service Interaction**: The VertexAiSearchTool interacts with your configured Vertex AI Search datastore, which contains your indexed enterprise documents. The service formulates and executes search queries against your private content. |
| 182 | +
|
| 183 | +5. **Document Retrieval & Ranking**: Vertex AI Search retrieves and ranks the most relevant document chunks from your datastore based on semantic similarity and relevance scoring. |
| 184 | +
|
| 185 | +6. **Context Injection**: The search service integrates the retrieved document snippets into the model's context before the final response is generated. This crucial step allows the model to "reason" over your organization's factual data. |
| 186 | +
|
| 187 | +7. **Grounded Response Generation**: The LLM, now informed by relevant enterprise content, generates a response that incorporates the retrieved information from your documents. |
| 188 | +
|
| 189 | +8. **Response Presentation with Sources**: The ADK receives the final grounded response, which includes the necessary source document references and groundingMetadata, and presents it to the user with attribution. This allows end-users to verify the information against your enterprise sources. |
| 190 | +
|
| 191 | +## Understanding grounding with Vertex AI Search response |
| 192 | +
|
| 193 | +When the agent uses Vertex AI Search to ground a response, it returns detailed information that includes the final text answer and metadata about the documents used to generate that answer. This metadata is crucial for verifying the response and providing attribution to your enterprise sources. |
| 194 | +
|
| 195 | +### Example of a Grounded Response |
| 196 | +
|
| 197 | +The following is an example of the content object returned by the model after a grounded query against enterprise documents. |
| 198 | +
|
| 199 | +**Final Answer Text:** |
| 200 | +
|
| 201 | +``` |
| 202 | +"Developing models for a medical scribe presents several significant challenges, primarily due to the complex nature of medical documentation, the sensitive data involved, and the demanding requirements of clinical workflows. Key challenges include: **Accuracy and Reliability:** Medical documentation requires extremely high levels of accuracy, as errors can lead to misdiagnoses, incorrect treatments, and legal repercussions. Ensuring that AI models can reliably capture nuanced medical language, distinguish between subjective and objective information, and accurately transcribe physician-patient interactions is a major hurdle. **Natural Language Understanding (NLU) and Speech Recognition:** Medical conversations are often rapid, involve highly specialized jargon, acronyms, and abbreviations, and can be spoken by individuals with diverse accents or speech patterns... [response continues with detailed analysis of privacy, integration, and technical challenges]" |
| 203 | +``` |
| 204 | +
|
| 205 | +**Grounding Metadata Snippet:** |
| 206 | +
|
| 207 | +This is the grounding metadata you will receive. On `adk web`, you can find this on the `Response` tab: |
| 208 | +
|
| 209 | +```json |
| 210 | +{ |
| 211 | + "groundingMetadata": { |
| 212 | + "groundingChunks": [ |
| 213 | + { |
| 214 | + "document": { |
| 215 | + "title": "AI in Medical Scribing: Technical Challenges", |
| 216 | + "uri": "projects/your-project/locations/global/dataStores/your-datastore-id/documents/doc-medical-scribe-ai-tech-challenges", |
| 217 | + "id": "doc-medical-scribe-ai-tech-challenges" |
| 218 | + } |
| 219 | + }, |
| 220 | + { |
| 221 | + "document": { |
| 222 | + "title": "Regulatory and Ethical Hurdles for AI in Healthcare", |
| 223 | + "uri": "projects/your-project/locations/global/dataStores/your-datastore-id/documents/doc-ai-healthcare-ethics", |
| 224 | + "id": "doc-ai-healthcare-ethics" |
| 225 | + } |
| 226 | + } |
| 227 | + // ... additional documents |
| 228 | + ], |
| 229 | + "groundingSupports": [ |
| 230 | + { |
| 231 | + "groundingChunkIndices": [0, 1], |
| 232 | + "segment": { |
| 233 | + "endIndex": 637, |
| 234 | + "startIndex": 433, |
| 235 | + "text": "Ensuring that AI models can reliably capture nuanced medical language..." |
| 236 | + } |
| 237 | + } |
| 238 | + // ... additional supports linking text segments to source documents |
| 239 | + ], |
| 240 | + "retrievalQueries": [ |
| 241 | + "challenges in natural language processing medical domain", |
| 242 | + "AI medical scribe challenges", |
| 243 | + "difficulties in developing AI for medical scribes" |
| 244 | + // ... additional search queries executed |
| 245 | + ] |
| 246 | + } |
| 247 | +} |
| 248 | +``` |
| 249 | + |
| 250 | +### How to Interpret the Response |
| 251 | + |
| 252 | +The metadata provides a link between the text generated by the model and the enterprise documents that support it. Here is a step-by-step breakdown: |
| 253 | + |
| 254 | +- **groundingChunks**: This is a list of the enterprise documents the model consulted. Each chunk contains the document title, uri (document path), and id. |
| 255 | + |
| 256 | +- **groundingSupports**: This list connects specific sentences in the final answer back to the `groundingChunks`. |
| 257 | + |
| 258 | +- **segment**: This object identifies a specific portion of the final text answer, defined by its `startIndex`, `endIndex`, and the `text` itself. |
| 259 | + |
| 260 | +- **groundingChunkIndices**: This array contains the index numbers that correspond to the sources listed in the `groundingChunks`. For example, the text about "HIPAA compliance" is supported by information from `groundingChunks` at index 1 (the "Regulatory and Ethical Hurdles" document). |
| 261 | + |
| 262 | +- **retrievalQueries**: This array shows the specific search queries that were executed against your datastore to find relevant information. |
| 263 | + |
| 264 | +## How to display grounding responses with Vertex AI Search |
| 265 | + |
| 266 | +Unlike Google Search grounding, Vertex AI Search grounding does not require specific display components. However, displaying citations and document references builds trust and allows users to verify information against your organization's authoritative sources. |
| 267 | + |
| 268 | +### Optional Citation Display |
| 269 | + |
| 270 | +Since grounding metadata is provided, you can choose to implement citation displays based on your application needs: |
| 271 | + |
| 272 | +**Simple Text Display (Minimal Implementation):** |
| 273 | + |
| 274 | +```python |
| 275 | +for event in events: |
| 276 | + if event.is_final_response(): |
| 277 | + print(event.content.parts[0].text) |
| 278 | + |
| 279 | + # Optional: Show source count |
| 280 | + if event.grounding_metadata: |
| 281 | + print(f"\nBased on {len(event.grounding_metadata.grounding_chunks)} documents") |
| 282 | +``` |
| 283 | + |
| 284 | +**Enhanced Citation Display (Optional):** You can implement interactive citations that show which documents support each statement. The grounding metadata provides all necessary information to map text segments to source documents. |
| 285 | + |
| 286 | +### Implementation Considerations |
| 287 | + |
| 288 | +When implementing Vertex AI Search grounding displays: |
| 289 | + |
| 290 | +1. **Document Access**: Verify user permissions for referenced documents |
| 291 | +2. **Simple Integration**: Basic text output requires no additional display logic |
| 292 | +3. **Optional Enhancements**: Add citations only if your use case benefits from source attribution |
| 293 | +4. **Document Links**: Convert document URIs to accessible internal links when needed |
| 294 | +5. **Search Queries**: The retrievalQueries array shows what searches were performed against your datastore |
| 295 | + |
| 296 | +## Summary |
| 297 | + |
| 298 | +Vertex AI Search Grounding transforms AI agents from general-purpose assistants into enterprise-specific knowledge systems capable of providing accurate, source-attributed information from your organization's private documents. By integrating this feature into your ADK agents, you enable them to: |
| 299 | + |
| 300 | +- Access proprietary information from your indexed document repositories |
| 301 | +- Provide source attribution for transparency and trust |
| 302 | +- Deliver comprehensive answers with verifiable enterprise facts |
| 303 | +- Maintain data privacy within your Google Cloud environment |
| 304 | + |
| 305 | +The grounding process seamlessly connects user queries to your organization's knowledge base, enriching responses with relevant context from your private documents while maintaining the conversational flow. With proper implementation, your agents become powerful tools for enterprise information discovery and decision-making. |
0 commit comments