本教學課程說明如何使用自主嵌入生成和 AI.SEARCH 函式,對圖表資料執行語意搜尋。
目標
本教學課程涵蓋下列工作:- 建立資料表,儲存人員、金融帳戶、帳戶擁有權和帳戶轉移的相關資訊。
- 使用自主嵌入生成功能,簡化嵌入維護工作流程。
- 建立圖表,定義儲存在資料表中的資料之間的關係。
- 使用圖形節點上的
AI.SEARCH函式,對帳戶說明執行語意搜尋。 - 使用圖表邊緣的
AI.SEARCH函式,對帳戶轉移附註執行語意搜尋。
費用
在本文件中,您會使用下列 Cloud de Confiance by S3NS的計費元件:
- BigQuery: You incur costs for the data that you process in BigQuery.
完成本文所述工作後,您可以刪除建立的資源,避免繼續計費,詳情請參閱「清除所用資源」。
事前準備
控制台
-
In the Cloud de Confiance console, on the project selector page, select or create a Cloud de Confiance 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.
-
Verify that billing is enabled for your Cloud de Confiance project.
Enable the BigQuery API.
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 資料編輯者、 專案 IAM 管理員
檢查角色
-
前往 Cloud de Confiance 控制台的「IAM」頁面。
前往「IAM」頁面 - 選取專案。
-
在「主體」欄中,找出所有識別您或您所屬群組的資料列。如要瞭解自己所屬的群組,請與管理員聯絡。
- 針對指定或包含您的所有列,請檢查「角色」欄,確認角色清單是否包含必要角色。
授予角色
-
前往 Cloud de Confiance 控制台的「IAM」頁面。
前往「IAM」頁面 - 選取專案。
- 按一下「Grant access」(授予存取權)。
-
在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是指員工身分集區中使用者的 ID。詳情請參閱「在 IAM 政策中代表工作團隊集區使用者」,或聯絡管理員。
- 按一下「選取角色」,然後搜尋角色。
- 如要授予其他角色,請按一下「Add another role」(新增其他角色),然後新增其他角色。
- 按一下「Save」(儲存)。
-
gcloud
-
安裝 Google Cloud CLI。
-
設定 gcloud CLI,使用您的聯合身分。
詳情請參閱「使用聯合身分登入 gcloud CLI」。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
選取或建立專案所需的角色
- 選取專案:選取專案時,不需要具備特定 IAM 角色,只要您在專案中獲派角色,即可選取該專案。
-
建立專案:如要建立專案,您需要具備專案建立者角色 (
roles/resourcemanager.projectCreator),其中包含resourcemanager.projects.create權限。瞭解如何授予角色。
-
建立 Cloud de Confiance 專案:
gcloud projects create PROJECT_ID
將
PROJECT_ID替換為您要建立的 Cloud de Confiance 專案名稱。 -
選取您建立的 Cloud de Confiance 專案:
gcloud config set project PROJECT_ID
將
PROJECT_ID替換為 Cloud de Confiance 專案名稱。
啟用 BigQuery API:
啟用 API 時所需的角色
如要啟用 API,您需要具備服務使用情形管理員 IAM 角色 (
roles/serviceusage.serviceUsageAdmin),其中包含serviceusage.services.enable權限。瞭解如何授予角色。gcloud services enable bigquery.googleapis.com
-
將角色授予使用者帳戶。針對下列每個 IAM 角色,執行一次下列指令:
roles/bigquery.dataEditor, roles/resourcemanager.projectIamAdmingcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
更改下列內容:
PROJECT_ID:專案 ID。USER_IDENTIFIER:使用者帳戶的 ID。 帳戶。如需範例,請參閱「 在 IAM 政策中代表工作團隊集區使用者」。ROLE:授予使用者帳戶的 IAM 角色。
製作表格
如要儲存在下列範例中建立的資料表和圖表,請建立資料集。以下查詢會建立名為 graph_search 的資料集:
CREATE SCHEMA IF NOT EXISTS graph_search;
下表包含人員和帳戶的相關資訊,以及這些實體之間的關係:
Person:有關人員的資訊。Account:銀行帳戶資訊。PersonOwnAccount:說明哪些帳戶屬於哪些使用者。AccountTransferAccount:帳戶間轉移的相關資訊。
如要建立這些資料表,請執行下列CREATE TABLE陳述式:
CREATE OR REPLACE TABLE graph_search.Person (
id INT64,
name STRING,
PRIMARY KEY (id) NOT ENFORCED
);
CREATE OR REPLACE TABLE graph_search.Account (
id INT64,
create_time TIMESTAMP,
is_blocked BOOL,
description STRING,
description_embedding STRUCT<result ARRAY<FLOAT64>, status STRING>
GENERATED ALWAYS AS (
AI.EMBED(description, model => 'embeddinggemma-300m')
) STORED OPTIONS( asynchronous = TRUE ),
PRIMARY KEY (id) NOT ENFORCED
);
CREATE OR REPLACE TABLE graph_search.PersonOwnAccount (
id INT64 NOT NULL,
account_id INT64 NOT NULL,
create_time TIMESTAMP,
PRIMARY KEY (id, account_id) NOT ENFORCED,
FOREIGN KEY (id) REFERENCES graph_search.Person(id) NOT ENFORCED,
FOREIGN KEY (account_id) REFERENCES graph_search.Account(id) NOT ENFORCED
);
CREATE OR REPLACE TABLE graph_search.AccountTransferAccount (
id INT64 NOT NULL,
to_id INT64 NOT NULL,
amount FLOAT64,
create_time TIMESTAMP NOT NULL,
order_number STRING,
notes STRING,
notes_embedding STRUCT<result ARRAY<FLOAT64>, status STRING>
GENERATED ALWAYS AS (
AI.EMBED(notes, model => 'embeddinggemma-300m')
) STORED OPTIONS( asynchronous = TRUE ),
PRIMARY KEY (id, to_id, create_time) NOT ENFORCED,
FOREIGN KEY (id) REFERENCES graph_search.Account(id) NOT ENFORCED,
FOREIGN KEY (to_id) REFERENCES graph_search.Account(id) NOT ENFORCED
);
Account 和 AccountTransferAccount 資料表會使用自主嵌入生成功能,維護 description 和 notes 資料欄的嵌入。
在本教學課程中,我們使用 embeddinggemma-300m 模型,因為這個模型可在 BigQuery 中執行,且適用於短字串。如為超過 128 個權杖的較長字串,請選擇其他嵌入模型,例如 text-embedding-005。詳情請參閱選擇嵌入模型。
插入資料
下列查詢會將一些範例資料插入資料表。INSERT 陳述式會省略嵌入資料欄,並由 BigQuery 自動填入。
INSERT INTO graph_search.Account
(id, create_time, is_blocked, description)
VALUES
(7,"2020-01-10 06:22:20.222",false,"Fund for a refreshing tropical vacation"),
(16,"2020-01-27 17:55:09.206",true,"Fund for a rainy day!"),
(20,"2020-02-18 05:44:20.655",false,"Saving up for travel");
INSERT INTO graph_search.Person
(id, name)
VALUES
(1,"Alex"),
(2,"Dana"),
(3,"Lee");
INSERT INTO graph_search.AccountTransferAccount
(id, to_id, amount, create_time, order_number, notes)
VALUES
(7,16,300,"2020-08-29 15:28:58.647","304330008004315", "wedding present"),
(7,16,100,"2020-10-04 16:55:05.342","304120005529714", "birthday gift"),
(16,20,300,"2020-09-25 02:36:14.926","103650009791820", "for shared cost of dinner"),
(20,7,500,"2020-10-04 16:55:05.342","304120005529714", "fees for tuition"),
(20,16,200,"2020-10-17 03:59:40.247","302290001484851", "loved the lunch");
INSERT INTO graph_search.PersonOwnAccount
(id, account_id, create_time)
VALUES
(1,7,"2020-01-10 06:22:20.222"),
(2,20,"2020-01-27 17:55:09.206"),
(3,16,"2020-02-18 05:44:20.655");
建立圖表
下列查詢會使用 CREATE PROPERTY GRAPH 陳述式,在 graph_search 資料集中建立名為 FinGraph 的圖形。Account 和 Person 資料表是節點資料表。AccountTransferAccount 和 PersonOwnAccount 資料表是邊緣資料表,代表節點資料表之間的關係。
CREATE OR REPLACE PROPERTY GRAPH graph_search.FinGraph
NODE TABLES (graph_search.Account, graph_search.Person)
EDGE TABLES (
graph_search.PersonOwnAccount
SOURCE KEY (id) REFERENCES Person (id)
DESTINATION KEY (account_id) REFERENCES Account (id)
LABEL Owns,
graph_search.AccountTransferAccount
SOURCE KEY (id) REFERENCES Account (id)
DESTINATION KEY (to_id) REFERENCES Account (id)
LABEL Transfers
);
搜尋節點
下列查詢會顯示休閒旅遊和度假的帳戶擁有者。第一個查詢使用 DECLARE 陳述式建立名為 similar_account 的變數。變數會在 DEFAULT 子句中初始化,並呼叫 AI.SEARCH,找出說明與 accounts for leisure travel and vacation 在語意上最相似的帳戶。查詢會在呼叫 AI.SEARCH 時將 top_k 引數設為 2,藉此限制結果數量。第二個查詢是圖形查詢,會傳回帳戶擁有者的名稱和帳戶說明。
DECLARE similar_account DEFAULT ((
SELECT ARRAY_AGG(base.id)
FROM
AI.SEARCH(
(SELECT * FROM graph_search.Account WHERE description_embedding IS NOT NULL),
'description',
'accounts for leisure travel and vacation',
top_k => 2)
));
GRAPH graph_search.FinGraph
MATCH (p:Person)-[:Owns]->(a:Account)
WHERE a.id IN UNNEST(similar_account)
RETURN p.name, a.description;
結果大致如下:
+------+-----------------------------------------+
| name | description |
+------+-----------------------------------------+
| Dana | Saving up for travel |
| Alex | Fund for a refreshing tropical vacation |
+------+-----------------------------------------+
搜尋邊緣
下列查詢會顯示與食品付款相關的帳戶轉移作業是由誰進行。
第一個查詢會使用 AI.SEARCH 函式填入名為 food_transfers 的變數。這個變數會保留轉移的訂單號碼,而相關聯的附註在語意上與 food 最為相似。查詢會在呼叫 AI.SEARCH 時將 top_k 引數設為 2,藉此限制結果數量。第二個查詢是圖形查詢,會傳回帳戶擁有者的名稱和轉移附註。
DECLARE food_transfers DEFAULT ((
SELECT ARRAY_AGG(base.order_number)
FROM
AI.SEARCH(
(SELECT * FROM graph_search.AccountTransferAccount WHERE notes_embedding IS NOT NULL),
'notes',
'food',
top_k => 2)
));
GRAPH graph_search.FinGraph
MATCH (p:Person)-[:Owns]->(:Account)-[t:Transfers]->(:Account)
WHERE t.order_number IN UNNEST(food_transfers)
RETURN p.name, t.notes;
結果大致如下:
+------+---------------------------+
| name | notes |
+------+---------------------------+
| Dana | loved the lunch |
| Lee | for shared cost of dinner |
+------+---------------------------+
建立向量索引
向量索引可縮短搜尋延遲時間,並降低運算成本。本教學課程中的資料表太小,無法使用向量索引。如果資料表很大 (通常有數百萬列),向量索引就很有用。BigQuery 提供兩種索引類型:IVF 和 TreeAH。如要進一步瞭解如何建立索引及選擇類型,請參閱「管理向量索引」。
清除所用資源
為避免因為本教學課程所用資源,導致系統向 Google Cloud 收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
刪除專案
刪除 Cloud de Confiance 專案:
gcloud projects delete PROJECT_ID
後續步驟
- 進一步瞭解 BigQuery 圖表。
- 瞭解如何建立及查詢圖表。
- 進一步瞭解如何建立向量索引,以及執行語意搜尋和 RAG。