Skip to content

Commit a457ca6

Browse files
authored
samples(discoveryengine): Add Website Import (GoogleCloudPlatform#11885)
* samples: Add Website Import * Add delete method * Address review comments * Fix lint errors
1 parent 9aa2fa3 commit a457ca6

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
17+
def create_target_site(
18+
project_id: str,
19+
location: str,
20+
data_store_id: str,
21+
uri_pattern: str,
22+
):
23+
# [START genappbuilder_create_target_site]
24+
from google.api_core.client_options import ClientOptions
25+
26+
from google.cloud import discoveryengine_v1 as discoveryengine
27+
28+
# TODO(developer): Uncomment these variables before running the sample.
29+
# project_id = "YOUR_PROJECT_ID"
30+
# location = "YOUR_LOCATION" # Values: "global"
31+
# data_store_id = "YOUR_DATA_STORE_ID"
32+
# uri_pattern = "https://cloud.google.com/generative-ai-app-builder/docs/*"
33+
34+
# For more information, refer to:
35+
# https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
36+
client_options = (
37+
ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
38+
if location != "global"
39+
else None
40+
)
41+
42+
# Create a client
43+
client = discoveryengine.SiteSearchEngineServiceClient(
44+
client_options=client_options
45+
)
46+
47+
# The full resource name of the data store
48+
# e.g. projects/{project}/locations/{location}/dataStores/{data_store_id}
49+
site_search_engine = client.site_search_engine_path(
50+
project=project_id, location=location, data_store=data_store_id
51+
)
52+
53+
# Target Site to index
54+
target_site = discoveryengine.TargetSite(
55+
provided_uri_pattern=uri_pattern,
56+
# Options: INCLUDE, EXCLUDE
57+
type_=discoveryengine.TargetSite.Type.INCLUDE,
58+
exact_match=False,
59+
)
60+
61+
# Make the request
62+
operation = client.create_target_site(
63+
parent=site_search_engine,
64+
target_site=target_site,
65+
)
66+
67+
print(f"Waiting for operation to complete: {operation.operation.name}")
68+
response = operation.result()
69+
70+
# Once the operation is complete,
71+
# get information from operation metadata
72+
metadata = discoveryengine.CreateTargetSiteMetadata(operation.metadata)
73+
74+
# Handle the response
75+
print(response)
76+
print(metadata)
77+
# [END genappbuilder_create_target_site]
78+
79+
return response
80+
81+
82+
def delete_target_site(
83+
project_id: str,
84+
location: str,
85+
data_store_id: str,
86+
target_site_id: str,
87+
):
88+
# [START genappbuilder_delete_target_site]
89+
from google.api_core.client_options import ClientOptions
90+
91+
from google.cloud import discoveryengine_v1 as discoveryengine
92+
93+
# TODO(developer): Uncomment these variables before running the sample.
94+
# project_id = "YOUR_PROJECT_ID"
95+
# location = "YOUR_LOCATION" # Values: "global"
96+
# data_store_id = "YOUR_DATA_STORE_ID"
97+
# target_site_id = "YOUR_TARGET_SITE_ID"
98+
99+
# For more information, refer to:
100+
# https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
101+
client_options = (
102+
ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
103+
if location != "global"
104+
else None
105+
)
106+
107+
# Create a client
108+
client = discoveryengine.SiteSearchEngineServiceClient(
109+
client_options=client_options
110+
)
111+
112+
# The full resource name of the data store
113+
# e.g. projects/{project}/locations/{location}/collections/{collection}/dataStores/{data_store_id}/siteSearchEngine/targetSites/{target_site}
114+
name = client.target_site_path(
115+
project=project_id,
116+
location=location,
117+
data_store=data_store_id,
118+
target_site=target_site_id,
119+
)
120+
121+
# Make the request
122+
operation = client.delete_target_site(name=name)
123+
124+
print(f"Operation: {operation.operation.name}")
125+
# [END genappbuilder_delete_target_site]
126+
127+
return operation.operation.name
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
import re
18+
19+
from discoveryengine import site_search_engine_sample
20+
21+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
22+
location = "global"
23+
data_store_id = "site-search-data-store"
24+
25+
26+
def test_create_target_site():
27+
response = site_search_engine_sample.create_target_site(
28+
project_id,
29+
location,
30+
data_store_id,
31+
uri_pattern="https://cloud.google.com/generative-ai-app-builder/docs/*",
32+
)
33+
assert response, response
34+
match = re.search(r"\/targetSites\/([^\/]+)", response.name)
35+
36+
if match:
37+
target_site = match.group(1)
38+
site_search_engine_sample.delete_target_site(
39+
project_id=project_id,
40+
location=location,
41+
data_store_id=data_store_id,
42+
target_site_id=target_site,
43+
)

0 commit comments

Comments
 (0)