|
| 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_train_custom_model] |
| 17 | + |
| 18 | +from google.api_core.client_options import ClientOptions |
| 19 | +from google.api_core.operation import Operation |
| 20 | +from google.cloud import discoveryengine |
| 21 | + |
| 22 | +# TODO(developer): Uncomment these variables before running the sample. |
| 23 | +# project_id = "YOUR_PROJECT_ID" |
| 24 | +# location = "YOUR_LOCATION" # Values: "global" |
| 25 | +# data_store_id = "YOUR_DATA_STORE_ID" |
| 26 | +# corpus_data_path = "gs://my-bucket/corpus.jsonl" |
| 27 | +# query_data_path = "gs://my-bucket/query.jsonl" |
| 28 | +# train_data_path = "gs://my-bucket/train.tsv" |
| 29 | +# test_data_path = "gs://my-bucket/test.tsv" |
| 30 | + |
| 31 | + |
| 32 | +def train_custom_model_sample( |
| 33 | + project_id: str, |
| 34 | + location: str, |
| 35 | + data_store_id: str, |
| 36 | + corpus_data_path: str, |
| 37 | + query_data_path: str, |
| 38 | + train_data_path: str, |
| 39 | + test_data_path: str, |
| 40 | +) -> Operation: |
| 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 | + client_options = ( |
| 44 | + ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com") |
| 45 | + if location != "global" |
| 46 | + else None |
| 47 | + ) |
| 48 | + # Create a client |
| 49 | + client = discoveryengine.SearchTuningServiceClient(client_options=client_options) |
| 50 | + |
| 51 | + # The full resource name of the data store |
| 52 | + data_store = f"projects/{project_id}/locations/{location}/collections/default_collection/dataStores/{data_store_id}" |
| 53 | + |
| 54 | + # Make the request |
| 55 | + operation = client.train_custom_model( |
| 56 | + request=discoveryengine.TrainCustomModelRequest( |
| 57 | + gcs_training_input=discoveryengine.TrainCustomModelRequest.GcsTrainingInput( |
| 58 | + corpus_data_path=corpus_data_path, |
| 59 | + query_data_path=query_data_path, |
| 60 | + train_data_path=train_data_path, |
| 61 | + test_data_path=test_data_path, |
| 62 | + ), |
| 63 | + data_store=data_store, |
| 64 | + model_type="search-tuning", |
| 65 | + ) |
| 66 | + ) |
| 67 | + |
| 68 | + # Optional: Wait for training to complete |
| 69 | + # print(f"Waiting for operation to complete: {operation.operation.name}") |
| 70 | + # response = operation.result() |
| 71 | + |
| 72 | + # After the operation is complete, |
| 73 | + # get information from operation metadata |
| 74 | + # metadata = discoveryengine.TrainCustomModelMetadata(operation.metadata) |
| 75 | + |
| 76 | + # Handle the response |
| 77 | + # print(response) |
| 78 | + # print(metadata) |
| 79 | + print(operation) |
| 80 | + |
| 81 | + return operation |
| 82 | + |
| 83 | + |
| 84 | +# [END genappbuilder_train_custom_model] |
0 commit comments