借助 Google Distributed Cloud (GDC) 网闸隔离配置中的 Vertex AI 客户端库,您可以通过编程方式访问光学字符识别 (OCR)、Speech-to-Text、Vertex AI Translation 和 Vertex AI Platform API,而无需编写原始服务器请求。直接从 tar 文件中提取并安装客户端库,或在 JupyterLab 笔记本中导入该库。如需详细了解笔记本环境,请参阅管理笔记本。
本文档适用于应用运维团队中负责配置应用和开发环境以启用 AI 功能的应用开发者。如需了解详情,请参阅 GDC 网闸隔离配置文档的受众群体。
Vertex AI 客户端库
Vertex AI 为 CentOS 和 Ubuntu 操作系统提供不同版本的客户端库。
tar 文件中 Vertex AI 客户端库的命名惯例基于操作系统、服务名称和版本。文件名遵循以下格式:
OS-google-cloud-SERVICE-VERSION.tar.gz
替换以下内容:
OS:您要安装客户端库的操作系统的名称。允许的值包括centos和ubuntu。SERVICE:要从中下载客户端库的 Vertex AI 服务的名称。允许的值如下:aiplatform:Vertex AI Platform 客户端库。speech:Speech-to-Text 客户端库。translate:Vertex AI Translation 客户端库。vision:OCR 客户端库。
VERSION:客户端库的版本号,例如3.8.0。
下表列出了 Distributed Cloud 支持的 Vertex AI 客户端库:
| Vertex AI 服务 | 操作系统 | 文件名 |
|---|---|---|
| OCR | Centos | centos-google-cloud-vision-3.0.0.tar.gz |
| Ubuntu | ubuntu-google-cloud-vision-3.0.0.tar.gz |
|
| Speech-to-Text | Centos | centos-google-cloud-speech-2.15.0.tar.gz |
| Ubuntu | ubuntu-google-cloud-speech-2.15.0.tar.gz |
|
| Vertex AI Translation | Centos | centos-google-cloud-translate-3.8.0.tar.gz |
| Ubuntu | ubuntu-google-cloud-translate-3.8.0.tar.gz |
|
| Vertex AI Platform | Centos | centos-google-cloud-aiplatform-1.34.0.tar.gz |
| Ubuntu | ubuntu-google-cloud-aiplatform-1.34.0.tar.gz |
准备工作
在下载 tar 文件并解压缩客户端库之前,请按以下步骤操作:
使用 gdcloud CLI 进行身份验证:
gdcloud auth login如需详细了解如何使用配置的身份提供方进行身份验证,请参阅 gdcloud CLI 身份验证。
验证您是否已安装 Python 3.7 至 3.10。客户端库 tarball 包含
cp37到cp310的 wheel。
安装客户端库
完成前提条件后,请按照以下步骤下载 tar 文件,并使用 tar 文件安装客户端库:
下载要安装的客户端库:
wget https://GDC_URL/.well-known/static/client-libraries/CLIENT_LIBRARY替换以下内容:
GDC_URL:贵组织的 GDC 控制台网址。CLIENT_LIBRARY:您要下载的客户端库的文件名。
解压库文件:
tar -zxf CLIENT_LIBRARY提取
OS-google-cloud-SERVICE-VERSION.tar.gz(例如centos-google-cloud-speech-2.15.0.tar.gz)会创建LIBRARY_NAME目录(例如google-cloud-speech,不带OS-前缀或-VERSION后缀)。在 Distributed Cloud 中安装客户端库:
pip install -r LIBRARY_NAME/requirements.txt --no-index --find-links LIBRARY_NAME将
LIBRARY_NAME替换为解压后的库目录名称,例如google-cloud-speech。如果您安装了 Speech-to-Text 客户端库,请验证安装和版本 (
2.15.0):from google.cloud import speech_v1p1beta1 as speech print(speech.__version__)输出会显示已安装的版本:
2.15.0使用 Python 脚本导入客户端库。以下示例展示了导入 Vertex AI Translation 客户端库的 Python 脚本的代码段,以说明导入库的过程:
from google.cloud import translate from google.api_core.client_options import ClientOptions import google.auth from google.auth.transport import requests creds, _ = google.auth.default() creds = creds.with_gdch_audience("https://ENDPOINT:443") creds.refresh(requests.Request()) client = translate.TranslationServiceClient( credentials=creds, client_options=ClientOptions(api_endpoint="ENDPOINT:443")) response = client.translate_text(request={ "parent": "projects/PROJECT_ID", "target_language_code": "ru", "mime_type": "text/plain", "contents": ["Hello"]}) print(response.translations[0].translated_text)替换以下内容:
ENDPOINT:您为组织使用的 Vertex AI Translation 端点,不含https://前缀或:443端口。如需了解详情,请查看服务状态和端点。PROJECT_ID:您的项目 ID。
将 Python 脚本保存为某个名称,例如
translation-service.py。运行 Python 脚本:
python SCRIPT_NAME将
SCRIPT_NAME替换为您为 Python 脚本指定的名称,例如translation-service.py。