Skip to content

Commit 89eee6d

Browse files
committed
docs(genai): Add Grounding Sample for Vertex AI Search
1 parent 46a0c73 commit 89eee6d

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

genai/tools/test_tools_examples.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import tools_func_def_with_txt
2424
import tools_func_desc_with_txt
2525
import tools_google_search_with_txt
26+
import tools_vais_with_txt
2627

2728
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
2829
os.environ["GOOGLE_CLOUD_LOCATION"] = "us-central1"
@@ -53,3 +54,10 @@ def test_tools_func_desc_with_txt() -> None:
5354
def test_tools_google_search_with_txt() -> None:
5455
response = tools_google_search_with_txt.generate_content()
5556
assert response
57+
58+
59+
def test_tools_vais_with_txt() -> None:
60+
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT")
61+
datastore = f"projects/{PROJECT_ID}/locations/global/collections/default_collection/dataStores/grounding-test-datastore"
62+
response = tools_vais_with_txt.generate_content(datastore)
63+
assert response

genai/tools/tools_vais_with_txt.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def generate_content(datastore: str) -> str:
17+
# [START googlegenaisdk_tools_vais_with_txt]
18+
from google import genai
19+
from google.genai.types import (
20+
GenerateContentConfig,
21+
HttpOptions,
22+
Retrieval,
23+
Tool,
24+
VertexAISearch,
25+
)
26+
27+
client = genai.Client(http_options=HttpOptions(api_version="v1"))
28+
29+
# Load Data Store ID from Vertex AI Search
30+
# datastore = "projects/111111111111/locations/global/collections/default_collection/dataStores/data-store-id"
31+
32+
response = client.models.generate_content(
33+
model="gemini-2.0-flash-001",
34+
contents="How do I make an appointment to renew my driver's license?",
35+
config=GenerateContentConfig(
36+
tools=[
37+
# Use Vertex AI Search Tool
38+
Tool(
39+
retrieval=Retrieval(
40+
vertex_ai_search=VertexAISearch(
41+
datastore=datastore,
42+
)
43+
)
44+
)
45+
],
46+
),
47+
)
48+
49+
print(response.text)
50+
# Example response:
51+
# 'The process for making an appointment to renew your driver's license varies depending on your location. To provide you with the most accurate instructions...'
52+
# [END googlegenaisdk_tools_vais_with_txt]
53+
return response.text
54+
55+
56+
if __name__ == "__main__":
57+
datastore = input("Data Store ID: ")
58+
generate_content(datastore)

0 commit comments

Comments
 (0)