|
16 | 16 | # [START genappbuilder_search] |
17 | 17 | from typing import List |
18 | 18 |
|
| 19 | +from google.api_core.client_options import ClientOptions |
19 | 20 | from google.cloud import discoveryengine |
20 | 21 |
|
21 | 22 | # TODO(developer): Uncomment these variables before running the sample. |
22 | 23 | # 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" |
26 | 26 | # search_query = "YOUR_SEARCH_QUERY" |
27 | 27 |
|
28 | 28 |
|
29 | 29 | def search_sample( |
30 | 30 | project_id: str, |
31 | 31 | location: str, |
32 | | - search_engine_id: str, |
33 | | - serving_config_id: str, |
| 32 | + data_store_id: str, |
34 | 33 | search_query: str, |
35 | 34 | ) -> 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 | + |
36 | 43 | # Create a client |
37 | | - client = discoveryengine.SearchServiceClient() |
| 44 | + client = discoveryengine.SearchServiceClient(client_options=client_options) |
38 | 45 |
|
39 | 46 | # 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} |
41 | 48 | serving_config = client.serving_config_path( |
42 | 49 | project=project_id, |
43 | 50 | 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", |
46 | 53 | ) |
47 | 54 |
|
| 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 |
48 | 57 | request = discoveryengine.SearchRequest( |
49 | 58 | serving_config=serving_config, query=search_query, page_size=10 |
50 | 59 | ) |
|
0 commit comments