使用 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 前,您必須為 Google Cloud 專案啟用此 API。您可以使用 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 功能

    以下回應將 Therapeutic Insulin 識別為藥物,該實體在 NCI 術語系統中具有代碼 C581。回應也包含指派給回應的可信度分數。如要進一步瞭解回應欄位,請參閱 analyzeEntities 說明文件。

    加入已授權的詞彙

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

    如果您的要求符合下列規定,您可以在回覆中加入 SNOMED 臨床術語,美國版本 (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 元素。如需其他對應項目清單,請參閱「Healthcare Natural Language API 輸出內容為 FHIR 套件」。

    以下範例說明如何從 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 格式納入所有實體、實體提及和關係。