|
| 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