|
| 1 | +# Copyright 2024 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 | +# http://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 | +# [START genappbuilder_create_data_store] |
| 17 | + |
| 18 | +from google.api_core.client_options import ClientOptions |
| 19 | +from google.cloud import discoveryengine |
| 20 | + |
| 21 | +# TODO(developer): Uncomment these variables before running the sample. |
| 22 | +# project_id = "YOUR_PROJECT_ID" |
| 23 | +# location = "YOUR_LOCATION" # Values: "global" |
| 24 | +# data_store_id = "YOUR_DATA_STORE_ID" |
| 25 | + |
| 26 | + |
| 27 | +def create_data_store_sample( |
| 28 | + project_id: str, |
| 29 | + location: str, |
| 30 | + data_store_id: str, |
| 31 | +) -> str: |
| 32 | + # For more information, refer to: |
| 33 | + # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store |
| 34 | + client_options = ( |
| 35 | + ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com") |
| 36 | + if location != "global" |
| 37 | + else None |
| 38 | + ) |
| 39 | + |
| 40 | + # Create a client |
| 41 | + client = discoveryengine.DataStoreServiceClient(client_options=client_options) |
| 42 | + |
| 43 | + # The full resource name of the collection |
| 44 | + # e.g. projects/{project}/locations/{location}/collections/default_collection |
| 45 | + parent = client.collection_path( |
| 46 | + project=project_id, |
| 47 | + location=location, |
| 48 | + collection="default_collection", |
| 49 | + ) |
| 50 | + |
| 51 | + data_store = discoveryengine.DataStore( |
| 52 | + display_name="My Data Store", |
| 53 | + # Options: GENERIC, MEDIA, HEALTHCARE_FHIR |
| 54 | + industry_vertical=discoveryengine.IndustryVertical.GENERIC, |
| 55 | + # Options: SOLUTION_TYPE_RECOMMENDATION, SOLUTION_TYPE_SEARCH, SOLUTION_TYPE_CHAT, SOLUTION_TYPE_GENERATIVE_CHAT |
| 56 | + solution_types=[discoveryengine.SolutionType.SOLUTION_TYPE_SEARCH], |
| 57 | + # Options: NO_CONTENT, CONTENT_REQUIRED, PUBLIC_WEBSITE |
| 58 | + content_config=discoveryengine.DataStore.ContentConfig.PUBLIC_WEBSITE, |
| 59 | + ) |
| 60 | + |
| 61 | + request = discoveryengine.CreateDataStoreRequest( |
| 62 | + parent=parent, |
| 63 | + data_store_id=data_store_id, |
| 64 | + data_store=data_store, |
| 65 | + # Optional: For Advanced Site Search Only |
| 66 | + # create_advanced_site_search=True, |
| 67 | + ) |
| 68 | + |
| 69 | + # Make the request |
| 70 | + operation = client.create_data_store(request=request) |
| 71 | + |
| 72 | + print(f"Waiting for operation to complete: {operation.operation.name}") |
| 73 | + response = operation.result() |
| 74 | + |
| 75 | + # Once the operation is complete, |
| 76 | + # get information from operation metadata |
| 77 | + metadata = discoveryengine.CreateDataStoreMetadata(operation.metadata) |
| 78 | + |
| 79 | + # Handle the response |
| 80 | + print(response) |
| 81 | + print(metadata) |
| 82 | + |
| 83 | + return operation.operation.name |
| 84 | + |
| 85 | + |
| 86 | +# [END genappbuilder_create_data_store] |
0 commit comments