列出 Vertex LLM 的调整模型(生成式 AI)

此代码示例演示了如何获取 Vertex LLM 的调整后模型列表

深入探索

如需查看包含此代码示例的详细文档,请参阅以下内容:

代码示例

Java

在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Java 设置说明执行操作。如需了解详情,请参阅 Vertex AI Java API 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


import com.google.cloud.aiplatform.v1.ListModelsRequest;
import com.google.cloud.aiplatform.v1.LocationName;
import com.google.cloud.aiplatform.v1.Model;
import com.google.cloud.aiplatform.v1.ModelServiceClient;
import com.google.cloud.aiplatform.v1.ModelServiceClient.ListModelsPagedResponse;
import com.google.cloud.aiplatform.v1.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

在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Python 设置说明执行操作。如需了解详情,请参阅 Vertex AI Python API 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

import vertexai

from vertexai.language_models import TextGenerationModel

# TODO(developer): Update values for project_id & location
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)

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器