List the tuned models for Vertex LLMs (Generative AI)

A code sample demonstrating how to get a list of the tuned models for Vertex LLMs

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

Java

Before trying this sample, follow the Java setup instructions in the Vertex AI quickstart using client libraries. For more information, see the Vertex AI Java API reference documentation.

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


import com.google.cloud.aiplatform.v1beta1.ListModelsRequest;
import com.google.cloud.aiplatform.v1beta1.LocationName;
import com.google.cloud.aiplatform.v1beta1.Model;
import com.google.cloud.aiplatform.v1beta1.ModelServiceClient;
import com.google.cloud.aiplatform.v1beta1.ModelServiceClient.ListModelsPagedResponse;
import com.google.cloud.aiplatform.v1beta1.ModelServiceSettings;
import java.io.IOException;

public class ListTunedModelsSample {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace this variable before running the sample.
    String project = "YOUR_PROJECT_ID";

    String location = "us-central1";
    String model = "text-bison@001";

    listTunedModelsSample(project, location, model);
  }

  // List tuned models for a large language model
  public static void listTunedModelsSample(String project, String location, String model)
      throws IOException {
    final String endpoint = String.format("%s-aiplatform.googleapis.com:443", location);
    ModelServiceSettings modelServiceSettings =
        ModelServiceSettings.newBuilder().setEndpoint(endpoint).build();

    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (ModelServiceClient modelServiceClient = ModelServiceClient.create(modelServiceSettings)) {
      final String parent = LocationName.of(project, location).toString();
      final String filter =
          String.format("labels.google-vertex-llm-tuning-base-model-id=%s", model);
      ListModelsRequest request =
          ListModelsRequest.newBuilder().setParent(parent).setFilter(filter).build();

      ListModelsPagedResponse listModelsPagedResponse = modelServiceClient.listModels(request);
      System.out.println("List Tuned Models response");
      for (Model element : listModelsPagedResponse.iterateAll()) {
        System.out.format("\tModel Name: %s\n", element.getName());
        System.out.format("\tModel Display Name: %s\n", element.getDisplayName());
      }
    }
  }
}

Python

Before trying this sample, follow the Python setup instructions in the Vertex AI quickstart using client libraries. For more information, see the Vertex AI Python API reference documentation.

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


import vertexai
from vertexai.language_models import TextGenerationModel


def list_tuned_models(
    project_id: str,
    location: str,
) -> None:
    """List tuned models."""

    vertexai.init(project=project_id, location=location)
    model = TextGenerationModel.from_pretrained("text-bison@002")
    tuned_model_names = model.list_tuned_model_names()
    print(tuned_model_names)

    return tuned_model_names

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.