Skip to content

Commit b1f6a55

Browse files
authored
samples(discoveryengine): Add Create, List, Get, Delete Engine Samples (GoogleCloudPlatform#11880)
* samples(discoveryengine): Add Engine Samples * ContentSearchSpec only supports unstructured data. If structured data is referenced an exception is raised. In the Google Cloud Docs, this is well documented under the [REST tab](https://cloud.google.com/generative-ai-app-builder/docs/preview-search-results#genappbuilder_search-rest). ==> Where it says: `CONTENT_SEARCH_SPEC: optional. For getting snippets, extractive answers, extractive segments, and search summaries. For unstructured data only.`
1 parent 56b73c0 commit b1f6a55

6 files changed

Lines changed: 334 additions & 1 deletion

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
# [START genappbuilder_create_engine]
18+
from typing import List
19+
20+
from google.api_core.client_options import ClientOptions
21+
from google.cloud import discoveryengine_v1 as discoveryengine
22+
23+
# TODO(developer): Uncomment these variables before running the sample.
24+
# project_id = "YOUR_PROJECT_ID"
25+
# location = "YOUR_LOCATION" # Values: "global"
26+
# engine_id = "YOUR_ENGINE_ID"
27+
# data_store_ids = ["YOUR_DATA_STORE_ID"]
28+
29+
30+
def create_engine_sample(
31+
project_id: str, location: str, engine_id: str, data_store_ids: List[str]
32+
) -> str:
33+
# For more information, refer to:
34+
# https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
35+
client_options = (
36+
ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
37+
if location != "global"
38+
else None
39+
)
40+
41+
# Create a client
42+
client = discoveryengine.EngineServiceClient(client_options=client_options)
43+
44+
# The full resource name of the collection
45+
# e.g. projects/{project}/locations/{location}/collections/default_collection
46+
parent = client.collection_path(
47+
project=project_id,
48+
location=location,
49+
collection="default_collection",
50+
)
51+
52+
engine = discoveryengine.Engine(
53+
display_name="Test Engine",
54+
# Options: GENERIC, MEDIA, HEALTHCARE_FHIR
55+
industry_vertical=discoveryengine.IndustryVertical.GENERIC,
56+
# Options: SOLUTION_TYPE_RECOMMENDATION, SOLUTION_TYPE_SEARCH, SOLUTION_TYPE_CHAT, SOLUTION_TYPE_GENERATIVE_CHAT
57+
solution_type=discoveryengine.SolutionType.SOLUTION_TYPE_SEARCH,
58+
# For search apps only
59+
search_engine_config=discoveryengine.Engine.SearchEngineConfig(
60+
# Options: SEARCH_TIER_STANDARD, SEARCH_TIER_ENTERPRISE
61+
search_tier=discoveryengine.SearchTier.SEARCH_TIER_ENTERPRISE,
62+
# Options: SEARCH_ADD_ON_LLM, SEARCH_ADD_ON_UNSPECIFIED
63+
search_add_ons=[discoveryengine.SearchAddOn.SEARCH_ADD_ON_LLM],
64+
),
65+
# For generic recommendation apps only
66+
# similar_documents_config=discoveryengine.Engine.SimilarDocumentsEngineConfig,
67+
data_store_ids=data_store_ids,
68+
)
69+
70+
request = discoveryengine.CreateEngineRequest(
71+
parent=parent,
72+
engine=engine,
73+
engine_id=engine_id,
74+
)
75+
76+
# Make the request
77+
operation = client.create_engine(request=request)
78+
79+
print(f"Waiting for operation to complete: {operation.operation.name}")
80+
response = operation.result()
81+
82+
# Once the operation is complete,
83+
# get information from operation metadata
84+
metadata = discoveryengine.CreateEngineMetadata(operation.metadata)
85+
86+
# Handle the response
87+
print(response)
88+
print(metadata)
89+
90+
return operation.operation.name
91+
92+
93+
# [END genappbuilder_create_engine]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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_engine]
17+
from google.api_core.client_options import ClientOptions
18+
from google.cloud import discoveryengine_v1 as discoveryengine
19+
20+
# TODO(developer): Uncomment these variables before running the sample.
21+
# project_id = "YOUR_PROJECT_ID"
22+
# location = "YOUR_LOCATION" # Values: "global"
23+
# engine_id = "YOUR_ENGINE_ID"
24+
25+
26+
def delete_engine_sample(
27+
project_id: str,
28+
location: str,
29+
engine_id: str,
30+
) -> str:
31+
# For more information, refer to:
32+
# https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
33+
client_options = (
34+
ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
35+
if location != "global"
36+
else None
37+
)
38+
39+
# Create a client
40+
client = discoveryengine.EngineServiceClient(client_options=client_options)
41+
42+
# The full resource name of the engine
43+
# e.g. projects/{project}/locations/{location}/collections/default_collection/engines/{engine_id}
44+
name = client.engine_path(
45+
project=project_id,
46+
location=location,
47+
collection="default_collection",
48+
engine=engine_id,
49+
)
50+
51+
# Make the request
52+
operation = client.delete_engine(name=name)
53+
54+
print(f"Operation: {operation.operation.name}")
55+
56+
return operation.operation.name
57+
58+
59+
# [END genappbuilder_delete_engine]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 (
20+
create_data_store_sample,
21+
create_engine_sample,
22+
delete_data_store_sample,
23+
delete_engine_sample,
24+
get_engine_sample,
25+
list_engines_sample,
26+
)
27+
28+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
29+
location = "global"
30+
engine_id = f"test-engine-{str(uuid4())}"
31+
data_store_id = f"test-data-store-{str(uuid4())}"
32+
33+
34+
def test_create_engine():
35+
create_data_store_sample.create_data_store_sample(
36+
project_id, location, data_store_id
37+
)
38+
operation_name = create_engine_sample.create_engine_sample(
39+
project_id, location, engine_id, data_store_ids=[data_store_id]
40+
)
41+
assert operation_name, operation_name
42+
43+
44+
def test_get_engine():
45+
engine = get_engine_sample.get_engine_sample(project_id, location, engine_id)
46+
assert engine.name, engine.name
47+
48+
49+
def test_list_engines():
50+
response = list_engines_sample.list_engines_sample(
51+
project_id,
52+
location,
53+
)
54+
assert response.engines, response.engines
55+
56+
57+
def test_delete_engine():
58+
operation_name = delete_engine_sample.delete_engine_sample(
59+
project_id, location, engine_id
60+
)
61+
assert operation_name, operation_name
62+
delete_data_store_sample.delete_data_store_sample(
63+
project_id, location, data_store_id
64+
)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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_get_engine]
17+
from google.api_core.client_options import ClientOptions
18+
from google.cloud import discoveryengine_v1 as discoveryengine
19+
20+
# TODO(developer): Uncomment these variables before running the sample.
21+
# project_id = "YOUR_PROJECT_ID"
22+
# location = "YOUR_LOCATION" # Values: "global"
23+
# engine_id = "YOUR_ENGINE_ID"
24+
25+
26+
def get_engine_sample(
27+
project_id: str,
28+
location: str,
29+
engine_id: str,
30+
) -> discoveryengine.Engine:
31+
# For more information, refer to:
32+
# https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
33+
client_options = (
34+
ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
35+
if location != "global"
36+
else None
37+
)
38+
39+
# Create a client
40+
client = discoveryengine.EngineServiceClient(client_options=client_options)
41+
42+
# The full resource name of the engine
43+
# e.g. projects/{project}/locations/{location}/collections/default_collection/engines/{engine_id}
44+
name = client.engine_path(
45+
project=project_id,
46+
location=location,
47+
collection="default_collection",
48+
engine=engine_id,
49+
)
50+
51+
# Make the request
52+
response = client.get_engine(name=name)
53+
54+
# Handle response
55+
print(response)
56+
57+
return response
58+
59+
60+
# [END genappbuilder_get_engine]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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_list_engines]
17+
from google.api_core.client_options import ClientOptions
18+
from google.cloud import discoveryengine_v1 as discoveryengine
19+
20+
# TODO(developer): Uncomment these variables before running the sample.
21+
# project_id = "YOUR_PROJECT_ID"
22+
# location = "YOUR_LOCATION" # Values: "global"
23+
24+
25+
def list_engines_sample(
26+
project_id: str,
27+
location: str,
28+
) -> discoveryengine.ListEnginesResponse:
29+
# For more information, refer to:
30+
# https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
31+
client_options = (
32+
ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
33+
if location != "global"
34+
else None
35+
)
36+
37+
# Create a client
38+
client = discoveryengine.EngineServiceClient(client_options=client_options)
39+
40+
# The full resource name of the parent collection
41+
# e.g. projects/{project}/locations/{location}/collections/default_collection
42+
parent = client.collection_path(
43+
project=project_id,
44+
location=location,
45+
collection="default_collection",
46+
)
47+
48+
# Make the request
49+
response = client.list_engines(parent=parent)
50+
51+
# Handle response
52+
print(response)
53+
54+
return response
55+
56+
57+
# [END genappbuilder_list_engines]

discoveryengine/search_sample.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def search_sample(
4646
# The full resource name of the search app serving config
4747
serving_config = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/servingConfigs/default_config"
4848

49-
# Optional: Configuration options for search
49+
# Optional - only supported for unstructured data: Configuration options for search.
5050
# Refer to the `ContentSearchSpec` reference for all supported fields:
5151
# https://cloud.google.com/python/docs/reference/discoveryengine/latest/google.cloud.discoveryengine_v1.types.SearchRequest.ContentSearchSpec
5252
content_search_spec = discoveryengine.SearchRequest.ContentSearchSpec(

0 commit comments

Comments
 (0)