Tune language foundation model (Generative AI)
Stay organized with collections
Save and categorize content based on your preferences.
Tune language foundation models with a tuning dataset.
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["Tune language foundation models with a tuning dataset.\n\nCode sample \n\nJava\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[Vertex AI quickstart using\nclient libraries](/vertex-ai/docs/start/client-libraries).\n\n\nFor more information, see the\n[Vertex AI Java API\nreference documentation](/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1).\n\n\nTo authenticate to Vertex AI, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import com.google.cloud.aiplatform.v1.https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.CreatePipelineJobRequest.html;\n import com.google.cloud.aiplatform.v1.https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.LocationName.html;\n import com.google.cloud.aiplatform.v1.https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PipelineJob.html;\n import com.google.cloud.aiplatform.v1.https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PipelineJob.html.https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PipelineJob.RuntimeConfig.html;\n import com.google.cloud.aiplatform.v1.https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PipelineServiceClient.html;\n import com.google.cloud.aiplatform.v1.https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PipelineServiceSettings.html;\n import com.google.protobuf.https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.Value.html;\n import java.io.IOException;\n import java.util.HashMap;\n import java.util.Map;\n\n public class CreatePipelineJobModelTuningSample {\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n String project = \"PROJECT\";\n String location = \"europe-west4\"; // europe-west4 and us-central1 are the supported regions\n String pipelineJobDisplayName = \"PIPELINE_JOB_DISPLAY_NAME\";\n String modelDisplayName = \"MODEL_DISPLAY_NAME\";\n String outputDir = \"OUTPUT_DIR\";\n String datasetUri = \"DATASET_URI\";\n int trainingSteps = 300;\n\n createPipelineJobModelTuningSample(\n project,\n location,\n pipelineJobDisplayName,\n modelDisplayName,\n outputDir,\n datasetUri,\n trainingSteps);\n }\n\n // Create a model tuning job\n public static void createPipelineJobModelTuningSample(\n String project,\n String location,\n String pipelineJobDisplayName,\n String modelDisplayName,\n String outputDir,\n String datasetUri,\n int trainingSteps)\n throws IOException {\n final String endpoint = String.format(\"%s-aiplatform.googleapis.com:443\", location);\n https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PipelineServiceSettings.html pipelineServiceSettings =\n https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PipelineServiceSettings.html.newBuilder().setEndpoint(endpoint).build();\n\n // Initialize client that will be used to send requests. This client only needs to be created\n // once, and can be reused for multiple requests.\n try (https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PipelineServiceClient.html client = https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PipelineServiceClient.html.create(pipelineServiceSettings)) {\n Map\u003cString, Value\u003e parameterValues = new HashMap\u003c\u003e();\n parameterValues.put(\"project\", stringToValue(project));\n parameterValues.put(\"model_display_name\", stringToValue(modelDisplayName));\n parameterValues.put(\"dataset_uri\", stringToValue(datasetUri));\n parameterValues.put(\n \"location\",\n stringToValue(\n \"us-central1\")); // Deployment is only supported in us-central1 for Public Preview\n parameterValues.put(\"large_model_reference\", stringToValue(\"text-bison@001\"));\n parameterValues.put(\"train_steps\", numberToValue(trainingSteps));\n parameterValues.put(\"accelerator_type\", stringToValue(\"GPU\")); // Optional: GPU or TPU\n\n https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PipelineJob.RuntimeConfig.html runtimeConfig =\n https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PipelineJob.RuntimeConfig.html.newBuilder()\n .setGcsOutputDirectory(outputDir)\n .https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PipelineJob.RuntimeConfig.Builder.html#com_google_cloud_aiplatform_v1_PipelineJob_RuntimeConfig_Builder_putAllParameterValues_java_util_Map_java_lang_String_com_google_protobuf_Value__(parameterValues)\n .build();\n\n https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PipelineJob.html pipelineJob =\n https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PipelineJob.html.newBuilder()\n .https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PipelineJob.Builder.html#com_google_cloud_aiplatform_v1_PipelineJob_Builder_setTemplateUri_java_lang_String_(\n \"https://us-kfp.pkg.dev/ml-pipeline/large-language-model-pipelines/tune-large-model/v2.0.0\")\n .setDisplayName(pipelineJobDisplayName)\n .https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PipelineJob.Builder.html#com_google_cloud_aiplatform_v1_PipelineJob_Builder_setRuntimeConfig_com_google_cloud_aiplatform_v1_PipelineJob_RuntimeConfig_(runtimeConfig)\n .build();\n\n https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.LocationName.html parent = https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.LocationName.html.of(project, location);\n https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.CreatePipelineJobRequest.html request =\n https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.CreatePipelineJobRequest.html.newBuilder()\n .setParent(parent.https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.LocationName.html#com_google_cloud_aiplatform_v1_LocationName_toString__())\n .setPipelineJob(pipelineJob)\n .build();\n\n https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PipelineJob.html response = client.createPipelineJob(request);\n System.out.format(\"response: %s\\n\", response);\n System.out.format(\"Name: %s\\n\", response.https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PipelineJob.html#com_google_cloud_aiplatform_v1_PipelineJob_getName__());\n }\n }\n\n static https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.Value.html stringToValue(String str) {\n return https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.Value.html.newBuilder().setStringValue(str).build();\n }\n\n static https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.Value.html numberToValue(int n) {\n return https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.Value.html.newBuilder().setNumberValue(n).build();\n }\n }\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=aiplatform)."]]