如何通过Python API访问旧版AI Platform模型而非Vertex AI模型
google-cloud-aiplatform Python SDK是专门为Vertex AI服务设计的官方客户端,和旧版Google AI Platform(即执行gcloud ai-platform系列命令对应的传统机器学习平台服务)是两套独立的服务接口,因此调用aiplatform.Model.list()只会返回Vertex AI下的模型资源,无法获取旧版AI Platform的模型列表。
旧版AI Platform模型列表的Python调用方法
旧版AI Platform没有提供独立的高层封装SDK,可通过Google通用API客户端构建ML Engine v1服务实例调用对应接口,返回结果和gcloud ai-platform models list完全一致。
- 安装依赖包
pip install google-api-python-client google-auth
- 调用示例代码
from googleapiclient.discovery import build from google.auth import default # 自动加载当前环境的认证凭证,与本地gcloud命令行的认证状态互通 creds, project_id = default() # 初始化旧版AI Platform(ML Engine)服务客户端 ml_service = build("ml", "v1", credentials=creds) # 发起模型列表查询请求 response = ml_service.projects().models().list(parent=f"projects/{project_id}").execute() model_list = response.get("models", []) # 遍历输出模型信息 for model in model_list: print(f"模型名:{model.get('name')},默认版本:{model.get('defaultVersion', {}).get('name', '未配置')}")
注意事项
- 上述代码调用的是旧版AI Platform的官方v1生产接口,和gcloud ai-platform命令行的底层调用逻辑完全一致,返回结果不会混入Vertex AI的资源。
- Google目前已经停止对旧版AI Platform的新功能迭代,所有平台新特性仅在Vertex AI上提供,官方推荐将存量模型逐步迁移到Vertex AI,迁移完成后即可直接使用
google-cloud-aiplatformSDK操作所有平台资源。
内容的提问来源于stack exchange,提问作者Robin Nicole
相关产品推荐
相关产品推荐

