Skip to content

Commit 2cc418e

Browse files
authored
samples(discoveryengine): Add Create/Delete Data Store Samples (GoogleCloudPlatform#11870)
1 parent 15f8563 commit 2cc418e

File tree

4 files changed

+181
-2
lines changed

4 files changed

+181
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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]
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
import os
17+
from uuid import uuid4
18+
19+
from discoveryengine import create_data_store_sample, delete_data_store_sample
20+
21+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
22+
location = "global"
23+
data_store_id = f"test-data-store-{str(uuid4())}"
24+
25+
26+
def test_create_data_store():
27+
operation_name = create_data_store_sample.create_data_store_sample(
28+
project_id, location, data_store_id
29+
)
30+
assert operation_name
31+
32+
33+
def test_delete_data_store():
34+
operation_name = delete_data_store_sample.delete_data_store_sample(
35+
project_id, location, data_store_id
36+
)
37+
assert operation_name
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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_delete_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 delete_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+
request = discoveryengine.DeleteDataStoreRequest(
44+
# The full resource name of the data store
45+
name=client.data_store_path(project_id, location, data_store_id)
46+
)
47+
48+
# Make the request
49+
operation = client.delete_data_store(request=request)
50+
51+
print(f"Operation: {operation.operation.name}")
52+
53+
return operation.operation.name
54+
55+
56+
# [END genappbuilder_delete_data_store]

discoveryengine/requirements.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
google-cloud-discoveryengine==0.11.11
2-
google-api-core==2.17.1
1+
google-cloud-discoveryengine==0.11.13
2+
google-api-core==2.19.0

0 commit comments

Comments
 (0)