使用 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

您必須先為 Google Cloud 專案啟用 Healthcare Natural Language API,才能使用這項 API。您可以使用 Healthcare Natural Language API,而不啟用或使用 Cloud Healthcare API 的功能。

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

  1. 登入 Google Cloud 帳戶。如果您未曾使用過 Google Cloud,歡迎建立帳戶,親自體驗實際使用我們產品的成效。新客戶還能獲得價值 $300 美元的免費抵免額,用於執行、測試及部署工作負載。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Create a service account:

    1. Ensure that you have the Create Service Accounts IAM role (roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles.
    2. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. 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.

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

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

    7. Click Continue.
    8. 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.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  8. Verify that billing is enabled for your Google Cloud project.

  9. Create a service account:

    1. Ensure that you have the Create Service Accounts IAM role (roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles.
    2. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. 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.

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

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

    7. Click Continue.
    8. 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. 啟用 Cloud Healthcare API (如果尚未啟用)。

    啟用 API 所需的角色

    如要啟用 API,您必須具備 serviceusage.services.enable 權限。如果您建立了專案,可能已透過擁有者角色 (roles/owner) 取得這項權限。否則,您可以透過 Service Usage 管理員角色 (roles/serviceusage.serviceUsageAdmin) 取得這項權限。瞭解如何授予角色。

    啟用 API

  13. 安裝 Google Cloud CLI。

  14. 若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。

  15. 執行下列指令,初始化 gcloud CLI:

    gcloud init

設定權限

如要使用本指南中的功能,您必須具備 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_US 和 ICD10CM 授權詞彙的詞彙代碼。輸出內容如下,要求的授權詞彙代碼會以粗體顯示:

將輸出內容擷取為 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 Bundle 資源的 fhirBundle 鍵。FHIR 組合包含所有實體、實體提及和關係,格式為 JSON。