使用受管理 AI 函式執行語意分析
本教學課程說明如何使用 BigQuery ML 管理的 AI 函式,對顧客意見回饋執行語意分析。
目標
在本教學課程中,您將執行下列作業:
- 建立資料集,並將情緒資料載入資料表
- 建立 Cloud 資源連線
- 使用下列 AI 函式執行語意分析:
AI.IF: 使用自然語言條件篩選資料AI.SCORE: 依情緒評估輸入內容AI.CLASSIFY: 將輸入內容分類至使用者定義的類別
費用
本教學課程使用 Google Cloud的計費元件,包括:
- BigQuery
- BigQuery ML
如要進一步瞭解 BigQuery 費用,請參閱 BigQuery 定價頁面。
如要進一步瞭解 BigQuery ML 費用,請參閱 BigQuery ML 定價。
事前準備
- 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.
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
如要使用現有專案進行本指南中的操作,請確認您具有所需的權限。如果您建立新專案,則已具備必要權限。
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
如要使用現有專案進行本指南中的操作,請確認您具有所需的權限。如果您建立新專案,則已具備必要權限。
-
Enable the BigQuery API and BigQuery Connection API APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.新專案會自動啟用 BigQuery API。
- 選用: 為專案啟用計費功能。如果您不想啟用帳單或提供信用卡,仍可按照本文中的步驟操作。BigQuery 提供沙箱,方便您執行這些步驟。詳情請參閱「啟用 BigQuery 沙箱」一文。
-
執行查詢工作和載入工作:
BigQuery 工作使用者 (
roles/bigquery.jobUser) -
建立連線:
BigQuery 連線管理員 (
roles/bigquery.connectionAdmin) -
建立資料集、建立資料表、將資料載入資料表,以及查詢資料表:
BigQuery 資料編輯器 (
roles/bigquery.dataEditor) 前往「BigQuery」頁面
點選左側窗格中的 「Explorer」:

如果沒有看到左側窗格,請按一下「展開左側窗格」圖示 開啟窗格。
在「Explorer」窗格中展開專案名稱,然後按一下「Connections」。
在「Connections」(連線) 頁面中,按一下「Create connection」(建立連線)。
在「連線類型」中,選擇「Vertex AI 遠端模型、遠端函式、BigLake 和 Spanner (Cloud 資源)」。
在「連線 ID」欄位中,輸入連線名稱。
在「位置類型」部分,選取連線位置。連線應與資料集等其他資源位於同一位置。
點選「建立連線」。
點選「前往連線」。
在「連線資訊」窗格中,複製服務帳戶 ID,以便在後續步驟中使用。
在指令列環境中建立連線:
bq mk --connection --location=REGION --project_id=PROJECT_ID \ --connection_type=CLOUD_RESOURCE CONNECTION_ID
--project_id參數會覆寫預設專案。更改下列內容:
REGION:您的連線區域PROJECT_ID:您的 Google Cloud 專案 IDCONNECTION_ID:連線的 ID
建立連線資源時,BigQuery 會建立專屬的系統服務帳戶,並將其與連線建立關聯。
疑難排解:如果收到下列連線錯誤訊息,請更新 Google Cloud SDK:
Flags parsing error: flag --connection_type=CLOUD_RESOURCE: value should be one of...
擷取並複製服務帳戶 ID,以供後續步驟使用:
bq show --connection PROJECT_ID.REGION.CONNECTION_ID
輸出結果會與下列內容相似:
name properties 1234.REGION.CONNECTION_ID {"serviceAccountId": "connection-1234-9u56h9@gcp-sa-bigquery-condel.iam.gserviceaccount.com"}- 啟動 Cloud Shell。
-
設定要套用 Terraform 設定的預設 Google Cloud 專案。
每個專案只需要執行一次這個指令,而且可以在任何目錄中執行。
export GOOGLE_CLOUD_PROJECT=PROJECT_ID
如果您在 Terraform 設定檔中設定明確值,環境變數就會遭到覆寫。
-
在 Cloud Shell 中建立目錄,並在該目錄中建立新檔案。檔案名稱的副檔名必須是
.tf,例如main.tf。在本教學課程中,這個檔案稱為main.tf。mkdir DIRECTORY && cd DIRECTORY && touch main.tf
-
如果您正在學習教學課程,可以複製每個章節或步驟中的範例程式碼。
將範例程式碼複製到新建立的
main.tf中。視需要從 GitHub 複製程式碼。如果 Terraform 代码片段是端對端解決方案的一部分,建議您使用這個方法。
- 查看並修改範例參數,套用至您的環境。
- 儲存變更。
-
初始化 Terraform。每個目錄只需執行一次這項操作。
terraform init
如要使用最新版 Google 供應商,請加入
-upgrade選項:terraform init -upgrade
-
檢查設定,確認 Terraform 即將建立或更新的資源符合您的預期:
terraform plan
視需要修正設定。
-
執行下列指令,然後在提示中輸入
yes,套用 Terraform 設定:terraform apply
等待 Terraform 顯示「Apply complete!」訊息。
- 開啟 Google Cloud 專案即可查看結果。在 Google Cloud 控制台中,前往 UI 中的資源,確認 Terraform 已建立或更新這些資源。
前往「IAM & Admin」(IAM 與管理) 頁面。
按一下「 授予存取權」。
在「新增主體」欄位,輸入先前複製的服務帳戶 ID。
在「選取角色」欄位中,選擇「Vertex AI」,然後選取「Vertex AI 使用者」角色。
按一下 [儲存]。
- 根據評論評估顧客滿意度。
- 監控社群媒體上的品牌觀感。
- 根據使用者的不滿程度,決定支援單的優先順序。
- 找出顧客意見回饋中常見的主題。
- 依主題整理文件。
- 依主題轉送支援單。
- 找出重複或幾乎重複的項目。
- 將類似的意見回饋歸為一組。
- 支援語意搜尋應用程式。
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
必要的角色
如要取得使用 AI 函數所需的權限,請要求管理員在專案中授予您下列 IAM 角色:
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
建立樣本資料
如要為本教學課程建立名為 my_dataset 的資料集,請執行下列查詢。
CREATE SCHEMA my_dataset OPTIONS (location = 'LOCATION');
接著,建立名為 customer_feedback 的資料表,其中包含裝置的顧客評論範例:
CREATE TABLE my_dataset.customer_feedback AS (
SELECT
*
FROM
UNNEST( [STRUCT<review_id INT64, review_text STRING>
(1, "The battery life is incredible, and the screen is gorgeous! Best phone I've ever had. Totally worth the price."),
(2, "Customer support was a nightmare. It took three weeks for my order to arrive, and when it did, the box was damaged. Very frustrating!"),
(3, "The product does exactly what it says on the box. No complaints, but not exciting either."),
(4, "I'm so happy with this purchase! It arrived early and exceeded all my expectations. The quality is top-notch, although the setup was a bit tricky."),
(5, "The price is a bit too high for what you get. The material feels cheap and I'm worried it won't last. Service was okay."),
(6, "Absolutely furious! The item arrived broken, and getting a refund is proving impossible. I will never buy from them again."),
(7, "This new feature for account access is confusing. I can't find where to update my profile. Please fix this bug!"),
(8, "The shipping was delayed, but the support team was very helpful and kept me informed. The product itself is great, especially for the price.")
])
);
建立連線
建立 Cloud 資源連線,並取得連線的服務帳戶。
選取下列選項之一:
控制台
bq
Terraform
使用 google_bigquery_connection 資源。
如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證作業」。
下列範例會在 US 地區中建立名為 my_cloud_resource_connection 的 Cloud 資源連線:
如要在 Google Cloud 專案中套用 Terraform 設定,請完成下列各節的步驟。
準備 Cloud Shell
準備目錄
每個 Terraform 設定檔都必須有自己的目錄 (也稱為根模組)。
套用變更
將權限授予連線的服務帳戶
為連線的服務帳戶授予 Vertex AI 使用者角色。您必須在「開始前」一節中建立或選取的專案中,授予這項角色。在其他專案中授予角色會導致 bqcx-1234567890-xxxx@gcp-sa-bigquery-condel.iam.gserviceaccount.com does not have the permission to access resource 錯誤。
如要授予角色,請按照下列步驟操作:
分類整體觀感
擷取文字表達的整體情緒感受,有助於支援下列用途:
下列查詢說明如何使用 AI.CLASSIFY 函式,將 customer_feedback 表格中的評論分類為正面、負面或中性:
SELECT
review_id,
review_text,
AI.CLASSIFY(
review_text,
categories => ['positive', 'negative', 'neutral'],
connection_id => "CONNECTION_ID") AS sentiment
FROM
my_dataset.customer_feedback;
傳回的結果看起來類似下列內容:
+-----------+------------------------------------------+-----------+ | review_id | review_text | sentiment | +-----------+------------------------------------------+-----------+ | 7 | This new feature for account access is | negative | | | confusing. I can't find where to update | | | | my profile. Please fix this bug! | | +-----------+------------------------------------------+-----------+ | 4 | "I'm so happy with this purchase! It | positive | | | arrived early and exceeded all my | | | | expectations. The quality is top-notch, | | | | although the setup was a bit tricky." | | +-----------+------------------------------------------+-----------+ | 2 | "Customer support was a nightmare. It | negative | | | took three weeks for my order to | | | | arrive, and when it did, the box was | | | | damaged. Very frustrating!" | | +-----------+------------------------------------------+-----------+ | 1 | "The battery life is incredible, and | positive | | | the screen is gorgeous! Best phone I've | | | | ever had. Totally worth the price." | | +-----------+------------------------------------------+-----------+ | 8 | "The shipping was delayed, but the | positive | | | support team was very helpful and kept | | | | me informed. The product itself is | | | | great, especially for the price." | | +-----------+------------------------------------------+-----------+ | 5 | The price is a bit too high for what | negative | | | you get. The material feels cheap and | | | | I'm worried it won't last. Service was | | | | okay. | | +-----------+------------------------------------------+-----------+ | 3 | "The product does exactly what it says | neutral | | | on the box. No complaints, but not | | | | exciting either." | | +-----------+------------------------------------------+-----------+ | 6 | "Absolutely furious! The item arrived | negative | | | broken, and getting a refund is proving | | | | impossible. I will never buy from them | | | | again." | | +-----------+------------------------------------------+-----------+
分析以面向為基礎的情緒
如果正面或負面等整體情緒不足以滿足您的用途,您可以分析文字意義的特定面向。舉例來說,您可能想瞭解使用者對產品品質的態度,但不在意他們對價格的想法。您甚至可以要求自訂值,指出特定層面不適用。
以下範例說明如何使用 AI.SCORE 函式,根據 customer_feedback 資料表中每則評論對價格、客戶服務和品質的評價,將使用者情緒評等從 1 分到 10 分。如果評論中未提及任何方面,函式會傳回自訂值 -1,方便您稍後篩除這些評論。
SELECT
review_id,
review_text,
AI.SCORE(
("Score 0.0 to 10 on positive sentiment about PRICE for review: ", review_text,
"If price is not mentioned, return -1.0"),
connection_id => "CONNECTION_ID") AS price_score,
AI.SCORE(
("Score 0.0 to 10 on positive sentiment about CUSTOMER SERVICE for review: ", review_text,
"If customer service is not mentioned, return -1.0"),
connection_id => "CONNECTION_ID") AS service_score,
AI.SCORE(
("Score 0.0 to 10 on positive sentiment about QUALITY for review: ", review_text,
"If quality is not mentioned, return -1.0"),
connection_id => "CONNECTION_ID") AS quality_score
FROM
my_dataset.customer_feedback
LIMIT 3;
傳回的結果看起來類似下列內容:
+-----------+------------------------------------------+--------------+---------------+---------------+ | review_id | review_text | price_score | service_score | quality_score | +-----------+------------------------------------------+--------------+---------------+---------------+ | 4 | "I'm so happy with this purchase! It | -1.0 | -1.0 | 9.5 | | | arrived early and exceeded all my | | | | | | expectations. The quality is top-notch, | | | | | | although the setup was a bit tricky." | | | | +-----------+------------------------------------------+--------------+---------------+---------------+ | 8 | "The shipping was delayed, but the | 9.0 | 8.5 | 9.0 | | | support team was very helpful and kept | | | | | | me informed. The product itself is | | | | | | great, especially for the price." | | | | +-----------+------------------------------------------+--------------+---------------+---------------+ | 6 | "Absolutely furious! The item arrived | -1.0 | 1.0 | 0.0 | | | broken, and getting a refund is proving | | | | | | impossible. I will never buy from them | | | | | | again." | | | | +-----------+------------------------------------------+--------------+---------------+---------------+
偵測情緒
除了正面或負面情緒,您也可以根據選取的特定情緒分類文字。這項功能有助於深入瞭解使用者回應,或標記情緒激動的意見回饋以供審查。
SELECT
review_id,
review_text,
AI.CLASSIFY(
review_text,
categories => ['joy', 'anger', 'sadness', 'surprise', 'fear', 'disgust', 'neutral', 'other'],
connection_id => "CONNECTION_ID"
) AS emotion
FROM
my_dataset.customer_feedback;
傳回的結果看起來類似下列內容:
+-----------+------------------------------------------+---------+ | review_id | review_text | emotion | +-----------+------------------------------------------+---------+ | 2 | "Customer support was a nightmare. It | anger | | | took three weeks for my order to | | | | arrive, and when it did, the box was | | | | damaged. Very frustrating!" | | +-----------+------------------------------------------+---------+ | 7 | This new feature for account access is | anger | | | confusing. I can't find where to update | | | | my profile. Please fix this bug! | | +-----------+------------------------------------------+---------+ | 4 | "I'm so happy with this purchase! It | joy | | | arrived early and exceeded all my | | | | expectations. The quality is top-notch, | | | | although the setup was a bit tricky." | | +-----------+------------------------------------------+---------+ | 1 | "The battery life is incredible, and | joy | | | the screen is gorgeous! Best phone I've | | | | ever had. Totally worth the price." | | +-----------+------------------------------------------+---------+ | 8 | "The shipping was delayed, but the | joy | | | support team was very helpful and kept | | | | me informed. The product itself is | | | | great, especially for the price." | | +-----------+------------------------------------------+---------+ | 5 | The price is a bit too high for what | sadness | | | you get. The material feels cheap and | | | | I'm worried it won't last. Service was | | | | okay. | | +-----------+------------------------------------------+---------+ | 3 | "The product does exactly what it says | neutral | | | on the box. No complaints, but not | | | | exciting either." | | +-----------+------------------------------------------+---------+ | 6 | "Absolutely furious! The item arrived | anger | | | broken, and getting a refund is proving | | | | impossible. I will never buy from them | | | | again." | | +-----------+------------------------------------------+---------+
依主題分類評論
你可以使用 AI.CLASSIFY 函式,將評論歸入預先定義的主題。
例如,您可以執行以下操作:
以下範例說明如何將顧客意見回饋分類為各種類型,例如帳單問題或帳戶存取權,然後計算每個類別的評論數量:
SELECT
AI.CLASSIFY(
review_text,
categories => ['Billing Issue', 'Account Access',
'Product Bug', 'Feature Request',
'Shipping Delay', 'Other'],
connection_id => "CONNECTION_ID") AS topic,
COUNT(*) AS number_of_reviews,
FROM
my_dataset.customer_feedback
GROUP BY topic
ORDER BY number_of_reviews DESC;
傳回的結果看起來類似下列內容:
+----------------+-------------------+ | topic | number_of_reviews | +----------------+-------------------+ | Other | 5 | | Shipping Delay | 2 | | Product Bug | 1 | +----------------+-------------------+
找出語意相似的評論
您可以使用 AI.SCORE 函式,要求評估兩段文字的語意相似程度,藉此判斷兩者是否相似。這項功能可協助你完成下列工作:
以下查詢會找出討論產品設定難度的評論:
SELECT
review_id,
review_text,
AI.SCORE(
(
"""How similar is the review to the concept of 'difficulty in setting up the product'?
A higher score indicates more similarity. Review: """,
review_text),
connection_id => "CONNECTION_ID") AS setup_difficulty
FROM my_dataset.customer_feedback
ORDER BY setup_difficulty DESC
LIMIT 2;
傳回的結果看起來類似下列內容:
+-----------+------------------------------------------+------------------+ | review_id | review_text | setup_difficulty | +-----------+------------------------------------------+------------------+ | 4 | "I'm so happy with this purchase! It | 3 | | | arrived early and exceeded all my | | | | expectations. The quality is top-notch, | | | | although the setup was a bit tricky." | | +-----------+------------------------------------------+------------------+ | 7 | This new feature for account access is | 1 | | | confusing. I can't find where to update | | | | my profile. Please fix this bug! | | +-----------+------------------------------------------+------------------+
您也可以使用 AI.IF 函式,找出與文字相關的評論:
SELECT
review_id,
review_text
FROM my_dataset.customer_feedback
WHERE
AI.IF(
(
"Does this review discuss difficulty setting up the product? Review: ",
review_text),
connection_id => "CONNECTION_ID");
合併函式
建議您在單一查詢中合併使用這些函式。舉例來說,下列查詢會先篩選出負面情緒的評論,然後依挫折感類型分類:
SELECT
review_id,
review_text,
AI.CLASSIFY(
review_text,
categories => [
'Poor Quality', 'Bad Customer Service', 'High Price', 'Other Negative'],
connection_id => "CONNECTION_ID") AS negative_topic
FROM my_dataset.customer_feedback
WHERE
AI.IF(
("Does this review express a negative sentiment? Review: ", review_text),
connection_id => "CONNECTION_ID");
建立可重複使用的提示 UDF
如要確保查詢內容清晰易讀,您可以建立使用者定義函式,重複使用提示邏輯。下列查詢會建立函式,並透過自訂提示呼叫 AI.IF,偵測負面情緒。然後呼叫該函式,依負面評論篩選。
CREATE OR REPLACE FUNCTION my_dataset.is_negative_sentiment(review_text STRING)
RETURNS BOOL
AS (
AI.IF(
("Does this review express a negative sentiment? Review: ", review_text),
connection_id => "CONNECTION_ID")
);
SELECT
review_id,
review_text
FROM my_dataset.customer_feedback
WHERE my_dataset.is_negative_sentiment(review_text);
清除所用資源
如要避免產生費用,您可以刪除含有您所建立資源的專案,或保留專案並刪除個別資源。
刪除專案
如要刪除專案,請進行以下操作:
刪除資料集
如要刪除資料集和其中包含的所有資源 (包括所有資料表和函式),請執行下列查詢:
DROP SCHEMA my_dataset CASCADE;