Skip to content

Commit ec2af86

Browse files
authored
docs(samples): Added Vertex AI Search "lite" method sample (#12767)
* docs(samples): Added Vertex AI Search "lite" method sample https://cloud.google.com/generative-ai-app-builder/docs/preview-search-results#web-api-key * Update copyright date * Add search lite sample test * Update typing in Search Sample and simplify tests to save processing time * Update library version * Change printing of search results
1 parent 61f707d commit ec2af86

File tree

5 files changed

+123
-8
lines changed

5 files changed

+123
-8
lines changed

discoveryengine/requirements.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
google-cloud-discoveryengine==0.12.3
2-
google-api-core==2.21.0
1+
google-cloud-discoveryengine==0.13.4
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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_search_lite]
17+
18+
from google.api_core.client_options import ClientOptions
19+
from google.cloud import discoveryengine_v1 as discoveryengine
20+
21+
# TODO(developer): Uncomment these variables before running the sample.
22+
# project_id = "YOUR_PROJECT_ID"
23+
# location = "YOUR_LOCATION" # Values: "global", "us", "eu"
24+
# engine_id = "YOUR_APP_ID"
25+
# api_key = "YOUR_API_KEY"
26+
# search_query = "YOUR_SEARCH_QUERY"
27+
28+
29+
def search_lite_sample(
30+
project_id: str,
31+
location: str,
32+
engine_id: str,
33+
api_key: str,
34+
search_query: str,
35+
) -> discoveryengine.services.search_service.pagers.SearchLitePager:
36+
37+
client_options = ClientOptions(
38+
# For information on API Keys, refer to:
39+
# https://cloud.google.com/generative-ai-app-builder/docs/migrate-from-cse#api-key-deploy
40+
api_key=api_key,
41+
# For more information, refer to:
42+
# https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
43+
api_endpoint=(
44+
f"{location}-discoveryengine.googleapis.com"
45+
if location != "global"
46+
else None
47+
),
48+
)
49+
50+
# Create a client
51+
client = discoveryengine.SearchServiceClient(client_options=client_options)
52+
53+
# The full resource name of the search app serving config
54+
serving_config = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/servingConfigs/default_config"
55+
56+
# Refer to the `SearchRequest` reference for all supported fields:
57+
# https://cloud.google.com/python/docs/reference/discoveryengine/latest/google.cloud.discoveryengine_v1.types.SearchRequest
58+
request = discoveryengine.SearchRequest(
59+
serving_config=serving_config,
60+
query=search_query,
61+
)
62+
63+
page_result = client.search_lite(request)
64+
65+
# Handle the response
66+
for response in page_result:
67+
print(response)
68+
69+
return page_result
70+
71+
72+
# [END genappbuilder_search_lite]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
18+
from discoveryengine import search_lite_sample
19+
20+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
21+
api_key = os.environ["VERTEX_AI_SEARCH_API_KEY"]
22+
search_query = "Google"
23+
24+
25+
def test_search_lite():
26+
location = "global"
27+
engine_id = "test-search-engine_1689960780551"
28+
response = search_lite_sample.search_lite_sample(
29+
project_id=project_id,
30+
location=location,
31+
engine_id=engine_id,
32+
api_key=api_key,
33+
search_query=search_query,
34+
)
35+
36+
assert response
37+
assert response.results
38+
39+
for result in response.results:
40+
assert result.document.name
41+
break

discoveryengine/search_sample.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#
1515

1616
# [START genappbuilder_search]
17-
from typing import List
18-
1917
from google.api_core.client_options import ClientOptions
2018
from google.cloud import discoveryengine_v1 as discoveryengine
2119

@@ -31,7 +29,7 @@ def search_sample(
3129
location: str,
3230
engine_id: str,
3331
search_query: str,
34-
) -> List[discoveryengine.SearchResponse]:
32+
) -> discoveryengine.services.search_service.pagers.SearchPager:
3533
# For more information, refer to:
3634
# https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
3735
client_options = (
@@ -86,10 +84,13 @@ def search_sample(
8684
),
8785
)
8886

89-
response = client.search(request)
90-
print(response)
87+
page_result = client.search(request)
88+
89+
# Handle the response
90+
for response in page_result:
91+
print(response)
9192

92-
return response
93+
return page_result
9394

9495

9596
# [END genappbuilder_search]

discoveryengine/search_sample_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def test_search():
3636

3737
for result in response.results:
3838
assert result.document.name
39+
break
3940

4041

4142
def test_search_eu_endpoint():
@@ -55,3 +56,4 @@ def test_search_eu_endpoint():
5556
for result in response.results:
5657
assert result.document
5758
assert result.document.name
59+
break

0 commit comments

Comments
 (0)