Skip to content

Commit 7368358

Browse files
authored
fix(discoveryengine): Update the samples after GA Launch (GoogleCloudPlatform#10635)
* fix: Add API Endpoint to Samples - Change `search_engine_id` to `data_store_id` * fix: Fixes to document samples - Use correct client (copy/paste mishap) - Remove erroneous import of annotation * Add explicit optionals
1 parent 8a3a38e commit 7368358

File tree

8 files changed

+74
-41
lines changed

8 files changed

+74
-41
lines changed

discoveryengine/documents_sample_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
2222
location = "global"
23-
search_engine_id = "test-structured-data-engine"
23+
data_store_id = "test-structured-data-engine"
2424
gcs_uri = "gs://cloud-samples-data/gen-app-builder/search/empty.json"
2525

2626
# Empty Dataset
@@ -32,7 +32,7 @@ def test_import_documents_gcs():
3232
operation_name = import_documents_sample.import_documents_sample(
3333
project_id=project_id,
3434
location=location,
35-
search_engine_id=search_engine_id,
35+
data_store_id=data_store_id,
3636
gcs_uri=gcs_uri,
3737
)
3838

@@ -43,7 +43,7 @@ def test_import_documents_bigquery():
4343
operation_name = import_documents_sample.import_documents_sample(
4444
project_id=project_id,
4545
location=location,
46-
search_engine_id=search_engine_id,
46+
data_store_id=data_store_id,
4747
bigquery_dataset=bigquery_dataset,
4848
bigquery_table=bigquery_table,
4949
)
@@ -55,7 +55,7 @@ def test_list_documents():
5555
response = list_documents_sample.list_documents_sample(
5656
project_id=project_id,
5757
location=location,
58-
search_engine_id=search_engine_id,
58+
data_store_id=data_store_id,
5959
)
6060

6161
assert response

discoveryengine/get_operation_sample.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
# [START genappbuilder_get_operation]
1717
from google.cloud import discoveryengine
18-
1918
from google.longrunning import operations_pb2
2019

2120
# TODO(developer): Uncomment these variables before running the sample.

discoveryengine/import_documents_sample.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
#
1515

1616
# [START genappbuilder_import_documents]
17-
from __future__ import annotations
18-
17+
from typing import Optional
1918

19+
from google.api_core.client_options import ClientOptions
2020
from google.cloud import discoveryengine
2121

2222
# TODO(developer): Uncomment these variables before running the sample.
2323
# project_id = "YOUR_PROJECT_ID"
2424
# location = "YOUR_LOCATION" # Values: "global"
25-
# search_engine_id = "YOUR_SEARCH_ENGINE_ID"
25+
# data_store_id = "YOUR_DATA_STORE_ID"
2626

2727
# Must specify either `gcs_uri` or (`bigquery_dataset` and `bigquery_table`)
2828
# Format: `gs://bucket/directory/object.json` or `gs://bucket/directory/*.json`
@@ -34,20 +34,28 @@
3434
def import_documents_sample(
3535
project_id: str,
3636
location: str,
37-
search_engine_id: str,
38-
gcs_uri: str | None = None,
39-
bigquery_dataset: str | None = None,
40-
bigquery_table: str | None = None,
37+
data_store_id: str,
38+
gcs_uri: Optional[str] = None,
39+
bigquery_dataset: Optional[str] = None,
40+
bigquery_table: Optional[str] = None,
4141
) -> str:
42+
# For more information, refer to:
43+
# https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
44+
client_options = (
45+
ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
46+
if location != "global"
47+
else None
48+
)
49+
4250
# Create a client
43-
client = discoveryengine.DocumentServiceClient()
51+
client = discoveryengine.DocumentServiceClient(client_options=client_options)
4452

4553
# The full resource name of the search engine branch.
46-
# e.g. projects/{project}/locations/{location}/dataStores/{data_store}/branches/{branch}
54+
# e.g. projects/{project}/locations/{location}/dataStores/{data_store_id}/branches/{branch}
4755
parent = client.branch_path(
4856
project=project_id,
4957
location=location,
50-
data_store=search_engine_id,
58+
data_store=data_store_id,
5159
branch="default_branch",
5260
)
5361

discoveryengine/list_documents_sample.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,39 @@
1515
# [START genappbuilder_list_documents]
1616
from typing import Any
1717

18+
from google.api_core.client_options import ClientOptions
1819
from google.cloud import discoveryengine
1920

2021
# TODO(developer): Uncomment these variables before running the sample.
2122
# project_id = "YOUR_PROJECT_ID"
22-
# location = "YOUR_LOCATION" # Values: "global"
23-
# search_engine_id = "YOUR_SEARCH_ENGINE_ID"
23+
# location = "YOUR_LOCATION" # Values: "global", "us", "eu"
24+
# data_store_id = "YOUR_DATA_STORE_ID"
2425

2526

26-
def list_documents_sample(project_id: str, location: str, search_engine_id: str) -> Any:
27+
def list_documents_sample(project_id: str, location: str, data_store_id: str) -> Any:
28+
# For more information, refer to:
29+
# https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
30+
client_options = (
31+
ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
32+
if location != "global"
33+
else None
34+
)
35+
2736
# Create a client
28-
client = discoveryengine.DocumentServiceClient()
37+
client = discoveryengine.DocumentServiceClient(client_options=client_options)
2938

3039
# The full resource name of the search engine branch.
31-
# e.g. projects/{project}/locations/{location}/dataStores/{data_store}/branches/{branch}
40+
# e.g. projects/{project}/locations/{location}/dataStores/{data_store_id}/branches/{branch}
3241
parent = client.branch_path(
3342
project=project_id,
3443
location=location,
35-
data_store=search_engine_id,
44+
data_store=data_store_id,
3645
branch="default_branch",
3746
)
3847

3948
response = client.list_documents(parent=parent)
4049

41-
print(f"Documents in {search_engine_id}:")
50+
print(f"Documents in {data_store_id}:")
4251
for result in response:
4352
print(result)
4453

discoveryengine/list_operations_sample.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
# limitations under the License.
1414

1515
# [START genappbuilder_list_operations]
16-
from __future__ import annotations
17-
16+
from typing import Optional
1817

1918
from google.cloud import discoveryengine
20-
2119
from google.longrunning import operations_pb2
2220

2321
# TODO(developer): Uncomment these variables before running the sample.
@@ -33,7 +31,7 @@ def list_operations_sample(
3331
project_id: str,
3432
location: str,
3533
search_engine_id: str,
36-
operations_filter: str | None = None,
34+
operations_filter: Optional[str] = None,
3735
) -> operations_pb2.ListOperationsResponse:
3836
# Create a client
3937
client = discoveryengine.DocumentServiceClient()

discoveryengine/poll_operation_sample.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from time import sleep
1818

1919
from google.cloud import discoveryengine
20-
2120
from google.longrunning import operations_pb2
2221

2322
# TODO(developer): Uncomment these variables before running the sample.

discoveryengine/search_sample.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,44 @@
1616
# [START genappbuilder_search]
1717
from typing import List
1818

19+
from google.api_core.client_options import ClientOptions
1920
from google.cloud import discoveryengine
2021

2122
# TODO(developer): Uncomment these variables before running the sample.
2223
# project_id = "YOUR_PROJECT_ID"
23-
# location = "YOUR_LOCATION" # Values: "global"
24-
# search_engine_id = "YOUR_SEARCH_ENGINE_ID"
25-
# serving_config_id = "default_config" # Values: "default_config"
24+
# location = "YOUR_LOCATION" # Values: "global", "us", "eu"
25+
# data_store_id = "YOUR_DATA_STORE_ID"
2626
# search_query = "YOUR_SEARCH_QUERY"
2727

2828

2929
def search_sample(
3030
project_id: str,
3131
location: str,
32-
search_engine_id: str,
33-
serving_config_id: str,
32+
data_store_id: str,
3433
search_query: str,
3534
) -> List[discoveryengine.SearchResponse.SearchResult]:
35+
# For more information, refer to:
36+
# https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
37+
client_options = (
38+
ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
39+
if location != "global"
40+
else None
41+
)
42+
3643
# Create a client
37-
client = discoveryengine.SearchServiceClient()
44+
client = discoveryengine.SearchServiceClient(client_options=client_options)
3845

3946
# The full resource name of the search engine serving config
40-
# e.g. projects/{project_id}/locations/{location}
47+
# e.g. projects/{project_id}/locations/{location}/dataStores/{data_store_id}/servingConfigs/{serving_config_id}
4148
serving_config = client.serving_config_path(
4249
project=project_id,
4350
location=location,
44-
data_store=search_engine_id,
45-
serving_config=serving_config_id,
51+
data_store=data_store_id,
52+
serving_config="default_config",
4653
)
4754

55+
# Refer to the SearchRequest reference for all supported fields:
56+
# https://cloud.google.com/python/docs/reference/discoveryengine/latest/google.cloud.discoveryengine_v1.types.SearchRequest
4857
request = discoveryengine.SearchRequest(
4958
serving_config=serving_config, query=search_query, page_size=10
5059
)

discoveryengine/search_sample_test.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,29 @@
1818
from discoveryengine import search_sample
1919

2020
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
21-
location = "global"
22-
search_engine_id = "test-search-engine_1689960780551"
23-
serving_config_id = "default_config"
2421
search_query = "Google"
2522

2623

2724
def test_search():
25+
location = "global"
26+
data_store_id = "test-search-engine_1689960780551"
2827
response = search_sample.search_sample(
2928
project_id=project_id,
3029
location=location,
31-
search_engine_id=search_engine_id,
32-
serving_config_id=serving_config_id,
30+
data_store_id=data_store_id,
31+
search_query=search_query,
32+
)
33+
34+
assert response
35+
36+
37+
def test_search_eu_endpoint():
38+
location = "eu"
39+
data_store_id = "alphabet-earnings-reports-eu"
40+
response = search_sample.search_sample(
41+
project_id=project_id,
42+
location=location,
43+
data_store_id=data_store_id,
3344
search_query=search_query,
3445
)
3546

0 commit comments

Comments
 (0)