|
| 1 | +# Copyright 2023 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] |
0 commit comments