安裝 Vertex AI 用戶端程式庫

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 檔案並擷取用戶端程式庫之前,請先按照下列步驟操作:

  1. 設定 Vertex AI 專案。

  2. 使用 gdcloud CLI 進行驗證:

    gdcloud auth login
    

    如要進一步瞭解如何使用設定的身分提供者進行驗證,請參閱「gdcloud CLI 驗證」。

  3. 確認已安裝 Python 3.7 至 3.10。用戶端程式庫 tarball 包含 cp37 至 cp310 的 wheel。

安裝用戶端程式庫

完成必要條件後,請按照下列步驟下載 tar 檔案,並使用 tar 檔案安裝用戶端程式庫:

  1. 下載要安裝的用戶端程式庫:

    wget https://GDC_URL/.well-known/static/client-libraries/CLIENT_LIBRARY
    

    更改下列內容:

    • GDC_URL:貴組織的 GDC 控制台網址。
    • CLIENT_LIBRARY:要下載的用戶端程式庫檔案名稱。
  2. 解壓縮程式庫檔案:

    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 後置字元)。

  3. 在 Distributed Cloud 安裝用戶端程式庫:

    pip install -r LIBRARY_NAME/requirements.txt --no-index --find-links LIBRARY_NAME
    

    將 LIBRARY_NAME 替換為解壓縮的程式庫目錄名稱,例如 google-cloud-speech。

  4. 如果您已安裝 Speech-to-Text 用戶端程式庫,請驗證安裝和版本 (2.15.0):

    from google.cloud import speech_v1p1beta1 as speech
    
    print(speech.__version__)
    

    輸出內容會顯示已安裝的版本:

    2.15.0
    
  5. 使用 Python 指令碼匯入用戶端程式庫。以下範例顯示 Python 指令碼的程式碼片段,該指令碼會匯入 Vertex AI Translation 用戶端程式庫,說明匯入程式庫的方式:

    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。

    如要進一步瞭解如何提出 Vertex AI Translation 要求,請參閱翻譯功能或翻譯文字。

  6. 將 Python 指令碼儲存為 translation-service.py 等名稱。

  7. 執行 Python 指令碼:

    python SCRIPT_NAME
    

    將 SCRIPT_NAME 替換為您為 Python 指令碼指定的名稱,例如 translation-service.py。