使用 Healthcare Natural Language API

本頁說明如何啟用 Healthcare Natural Language API、設定權限,以及呼叫 analyzeEntities 方法,從醫療文字中擷取醫療洞察資料。

總覽

Healthcare Natural Language API 提供機器學習解決方案,可從醫療文字中獲取深入分析資訊。Healthcare Natural Language API 是 Cloud Healthcare API 的一部分。如要瞭解 Healthcare Natural Language API 總覽,請參閱 Healthcare Natural Language API 概念說明文件

Healthcare Natural Language API 會剖析非結構化醫療文字,例如病歷或保險理賠申請。接著,系統會產生這些資料來源中儲存的醫療知識實體的結構化資料表示法,以供後續分析和自動化作業使用。例如,您可以:

  • 擷取疾病、藥物、醫療器材、醫療程序等醫療概念的相關資訊,以及臨床相關屬性
  • 將醫療概念對應至標準醫療詞彙,例如 RxNorm、ICD-10、MeSH 和 SNOMED CT (僅限美國和英國使用者)
  • 從文字產生醫療深入分析結果,並與 Google Cloud中的資料分析產品整合

服務地區

Healthcare Natural Language API 支援下列地區:

地點名稱 地點說明
asia-south1 印度孟買
australia-southeast1 澳洲雪梨
europe-west2 英國倫敦
europe-west4 荷蘭
northamerica-northeast1 加拿大蒙特婁
us-central1 美國愛荷華州

啟用 Healthcare Natural Language API

開始使用 Healthcare Natural Language API 前,請先為專案啟用 API。 Google Cloud 您可以使用 Healthcare Natural Language API,不必啟用或使用 Cloud Healthcare API 的功能。

如要啟用 API,請完成下列步驟:

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create and continue.
    5. Grant the Project > Owner role to the service account.

      To grant the role, find the Select a role list, then select Project > Owner.

    6. Click Continue.
    7. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  5. Create a service account key:

    1. In the Google Cloud console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, and then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.
  6. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again.

  7. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  8. Make sure that billing is enabled for your Google Cloud project.

  9. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create and continue.
    5. Grant the Project > Owner role to the service account.

      To grant the role, find the Select a role list, then select Project > Owner.

    6. Click Continue.
    7. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  10. Create a service account key:

    1. In the Google Cloud console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, and then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.
  11. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again.

  12. Enable the Cloud Healthcare API.

    Enable the API

  13. Install the Google Cloud CLI.

  14. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  15. To initialize the gcloud CLI, run the following command:

    gcloud init
  16. 設定權限

    如要使用本指南中的功能,您必須具備healthcare.nlpservice.analyzeEntities 權限,這項權限包含在healthcare.nlpServiceViewer 角色中。

    如要指派這個角色,請執行 gcloud projects add-iam-policy-binding 指令:

    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member serviceAccount:SERVICE_ACCOUNT_ID \
        --role roles/healthcare.nlpServiceViewer

    擷取實體、關係和情境屬性

    Healthcare Natural Language API 會使用情境感知模型,擷取醫療實體、關係和情境屬性。每個文字實體都會擷取到醫療字典項目中。如要從醫療文字中擷取這個層級的醫療洞察資訊,請使用 projects.locations.services.nlp.analyzeEntities 方法。

    如要在實體提及內容中加入 SNOMED CT 授權詞彙,請參閱「加入授權詞彙」。

    如要使用 Healthcare Natural Language API 從醫療文字中擷取醫療洞察資料,請發出 POST 要求,並在 documentContent 欄位中指定目標文字。醫療文字的大小上限為 20,000 個 Unicode 字元。

    下列範例說明如何使用 analyzeEntities 方法,從醫療文字「Insulin regimen 5 units IV will be administered for diabetes.」擷取醫療洞察。

    REST

    使用任何要求資料之前,請先替換以下項目:

    • PROJECT_ID:您的 Google Cloud 專案 ID
    • LOCATION:資料集位置

    JSON 要求主體:

    {
      "documentContent": "Insulin regimen 5 units IV will be administered for diabetes."
    }
    

    如要傳送要求,請選擇以下其中一個選項:

    curl

    將要求主體儲存在名為 request.json 的檔案中。 在終端機中執行下列指令,在目前目錄中建立或覆寫這個檔案:

    cat > request.json << 'EOF'
    {
      "documentContent": "Insulin regimen 5 units IV will be administered for diabetes."
    }
    EOF

    接著,請執行下列指令來傳送 REST 要求:

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    -d @request.json \
    "https://healthcare.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/services/nlp:analyzeEntities"

    PowerShell

    將要求主體儲存在名為 request.json 的檔案中。 在終端機中執行下列指令,在目前目錄中建立或覆寫這個檔案:

    @'
    {
      "documentContent": "Insulin regimen 5 units IV will be administered for diabetes."
    }
    '@  | Out-File -FilePath request.json -Encoding utf8

    接著,請執行下列指令來傳送 REST 要求:

    $cred = gcloud auth print-access-token
    $headers = @{ "Authorization" = "Bearer $cred" }

    Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json" `
    -InFile request.json `
    -Uri "https://healthcare.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/services/nlp:analyzeEntities" | Select-Object -Expand Content

    如果要求成功,回應會包含下列資訊:

    • 可辨識的醫療知識實體
    • 功能
    • 已辨識實體之間的關係
    • 內容屬性
    • 將醫療知識實體對應至標準術語

    如需支援的實體、屬性和關係類型清單,請參閱 Healthcare Natural Language API 功能

    以下回應會將代碼 C581 的實體「治療性胰島素」識別為藥物,該代碼位於 NCI 術語系統中。回覆中也會包含回覆的信賴度分數。如要進一步瞭解回應欄位,請參閱 analyzeEntities 說明文件。

    加入已獲得授權的詞彙

    根據預設,Healthcare Natural Language API 回應會包含支援的醫療詞彙

    如果要求符合下列規定,您可以在回覆中加入 SNOMED Clinical Terms, US Version (SNOMEDCT_US) 詞彙:

    • API 要求來自美國。
    • 要求主體中的 licensedVocabularies 欄位值為 SNOMEDCT_US

    如果您不需要 SNOMED CT 詞彙,就不會受到這些限制。

    以下範例說明如何在 LicensedVocabularies 物件中納入 SNOMED CT 授權詞彙,從醫療文字「Insulin regimen 5 units IV will be administered for diabetes.」擷取醫療洞察。

    REST

    使用任何要求資料之前,請先替換以下項目:

    • PROJECT_ID:您的 Google Cloud 專案 ID
    • LOCATION:資料集位置

    JSON 要求主體:

    {
      "documentContent": "Insulin regimen 5 units IV will be administered for diabetes.",
      "licensedVocabularies": [
        "SNOMEDCT_US",
        "ICD10CM"
      ]
    }
    

    如要傳送要求,請選擇以下其中一個選項:

    curl

    將要求主體儲存在名為 request.json 的檔案中。 在終端機中執行下列指令,在目前目錄中建立或覆寫這個檔案:

    cat > request.json << 'EOF'
    {
      "documentContent": "Insulin regimen 5 units IV will be administered for diabetes.",
      "licensedVocabularies": [
        "SNOMEDCT_US",
        "ICD10CM"
      ]
    }
    EOF

    接著,請執行下列指令來傳送 REST 要求:

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    -d @request.json \
    "https://healthcare.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/services/nlp:analyzeEntities"

    PowerShell

    將要求主體儲存在名為 request.json 的檔案中。 在終端機中執行下列指令,在目前目錄中建立或覆寫這個檔案:

    @'
    {
      "documentContent": "Insulin regimen 5 units IV will be administered for diabetes.",
      "licensedVocabularies": [
        "SNOMEDCT_US",
        "ICD10CM"
      ]
    }
    '@  | Out-File -FilePath request.json -Encoding utf8

    接著,請執行下列指令來傳送 REST 要求:

    $cred = gcloud auth print-access-token
    $headers = @{ "Authorization" = "Bearer $cred" }

    Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json" `
    -InFile request.json `
    -Uri "https://healthcare.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/services/nlp:analyzeEntities" | Select-Object -Expand Content
    如果要求成功,回應會包含 SNOMEDCT_USICD10CM 授權詞彙的詞彙代碼。輸出內容如下,且要求的授權字彙代碼會以粗體顯示:

    將輸出內容擷取為 FHIR R4 軟體包

    您可以從文字中擷取實體,並將其對應至 FHIR R4 資源和元素。產生的 FHIR R4 組合會以 JSON 格式包含所有實體、實體提及內容和關係。舉例來說,Healthcare Natural Language API 會將基本實體 PROBLEM 對應至 Condition FHIR R4 資源,並將實體 PROBLEM.ANATOMICAL_STRUCTURE 對應至 Condition.bodySite FHIR 元素。如需其他對應項目清單,請參閱「以 FHIR 組合形式輸出 Healthcare Natural Language API」。

    下列範例說明如何從 FHIR R4 組合中的醫療文字「Insulin regimen 5 units IV will be administered for diabetes.」擷取醫療洞察。詳情請參閱 AlternativeOutputFormat 物件。

    REST

    使用任何要求資料之前,請先替換以下項目:

    • PROJECT_ID:您的 Google Cloud 專案 ID
    • LOCATION:資料集位置

    JSON 要求主體:

    {
      "documentContent": "Insulin regimen 5 units IV will be administered for diabetes.",
      "alternativeOutputFormat": "FHIR_BUNDLE"
    }
    

    如要傳送要求,請選擇以下其中一個選項:

    curl

    將要求主體儲存在名為 request.json 的檔案中。 在終端機中執行下列指令,在目前目錄中建立或覆寫這個檔案:

    cat > request.json << 'EOF'
    {
      "documentContent": "Insulin regimen 5 units IV will be administered for diabetes.",
      "alternativeOutputFormat": "FHIR_BUNDLE"
    }
    EOF

    接著,請執行下列指令來傳送 REST 要求:

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    -d @request.json \
    "https://healthcare.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/services/nlp:analyzeEntities"

    PowerShell

    將要求主體儲存在名為 request.json 的檔案中。 在終端機中執行下列指令,在目前目錄中建立或覆寫這個檔案:

    @'
    {
      "documentContent": "Insulin regimen 5 units IV will be administered for diabetes.",
      "alternativeOutputFormat": "FHIR_BUNDLE"
    }
    '@  | Out-File -FilePath request.json -Encoding utf8

    接著,請執行下列指令來傳送 REST 要求:

    $cred = gcloud auth print-access-token
    $headers = @{ "Authorization" = "Bearer $cred" }

    Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json" `
    -InFile request.json `
    -Uri "https://healthcare.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/services/nlp:analyzeEntities" | Select-Object -Expand Content
    如果要求成功,回應會包含具有下列項目的 JSON 物件:
    • 辨識提及的實體,以及實體之間的關係,格式與「 擷取實體、關係和情境屬性」中的輸出內容類似。
    • 包含字串格式 FHIR 套件資源的 fhirBundle 鍵。 FHIR 組合包含所有實體、實體提及項目和關係,格式為 JSON。