|
| 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] |
0 commit comments