本頁說明如何使用 public 和 google_ml 命名空間的函式叫用預測。google_ml_integration 擴充功能包含這些命名空間的預測函式。
您可以在 public 架構中使用 ml_predict_row() 函式,搭配 Gemini Enterprise Agent Platform 中託管的任何泛型模型,不必註冊端點。您可以在 google_ml 結構定義中使用 google_ml.predict_row() 函式,搭配已向模型端點管理服務註冊的任何模型。
如要叫用預測功能,請選取下列其中一個結構定義。
事前準備
如要允許 AlloyDB 叫用預測,請按照下列步驟操作:
- 您可以在可以使用 Agent Platform 上的生成式 AI 的區域叫用預測功能。如需地區清單,請參閱「部署作業和端點」。
- 使用
psql或 AlloyDB for PostgreSQL Studio 連線至資料庫。 - 確認已安裝
google_ml_integration擴充功能。 - 確認
google_ml_integration.enable_model_support旗標已設為on。 - 您必須先設定 AlloyDB,才能從 AlloyDB 資料庫叫用 Agent Platform 的預測功能。詳情請參閱「將資料庫與 Agent Platform 整合」。
- 您必須擁有有效的 Agent Platform 模型和有效端點,並具備存取 Identity and Access Management (IAM) 的權限。AlloyDB 不支援使用私人端點取得線上預測結果。
授予資料庫使用者執行預測函式的權限,以便叫用預測:
\c DB_NAME; GRANT EXECUTE ON FUNCTION ml_predict_row TO USER_NAME;更改下列內容:
DB_NAME:應授予權限的資料庫名稱
USER_NAME:要授予權限的使用者名稱
叫用線上預測
使用 ml_predict_row() SQL 函式,對資料叫用線上預測。
函式初始引數的格式取決於您要使用的機器學習模型是在 Model Garden 中,還是在 Google Cloud 專案中執行的端點。
在 Model Garden 中使用模型
如要使用在 Model Garden 中執行的機器學習模型叫用線上預測,請使用 google_ml.predict_row() SQL 函式的下列語法:
SELECT ml_predict_row('projects/PROJECT_ID/locations/REGION_ID/publishers/google/models/MODEL_ID', '{ CONTENTS }');
更改下列內容:
PROJECT_ID:您 Google Cloud 專案的 IDREGION_ID:模型所在的 Google Cloud 區域 ID,例如 gemini-pro 的us-central1MODEL_ID:要使用的 ML 模型 ID,例如 gemini-proCONTENTS:預測呼叫的輸入內容,格式為 JSON
如果 ML 模型與 AlloyDB 叢集位於相同專案和區域,則可以縮寫這個函式的第一個引數:
SELECT ml_predict_row('publishers/google/models/MODEL_ID', '{ CONTENTS }');
如要瞭解模型的 JSON 回覆訊息,請參閱生成式 AI 基礎模型參考資料。
如需範例,請參閱「呼叫範例」。
使用 Agent Platform 模型端點
如要使用 Agent Platform 模型端點叫用線上預測,請使用 ml_predict_row() SQL 函式的下列語法:
SELECT ml_predict_row('projects/PROJECT_ID/locations/REGION_ID/endpoints/ENDPOINT_ID', '{ CONTENTS }');
更改下列內容:
PROJECT_ID:模型所在 Google Cloud 專案的 IDREGION_ID:模型所在 Google Cloud 區域的 ID,例如us-central1ENDPOINT_ID:模型端點的 IDCONTENTS:預測呼叫的輸入內容,格式為 JSON
如果端點與 AlloyDB 叢集位於相同專案和區域,則可以縮寫這個函式的第一個引數:
SELECT ml_predict_row('endpoints/ENDPOINT_ID', '{ CONTENTS }');
如要瞭解模型的 JSON 回應訊息,請參閱 PredictResponse。
叫用範例
下列範例使用 Model Garden 中提供的 gemini-pro,根據以字面值引數形式提供給 ml_predict_row() 的簡短提示生成文字:
SELECT
json_array_elements(ml_predict_row('publishers/google/models/gemini-1.5-pro:streamGenerateContent',
'{ "contents": [ { "role": "user", "parts": [ { "text": "For TPCH database schema as mentioned here https://www.tpc.org/TPC_Documents_Current_Versions/pdf/TPC-H_v3.0.1.pdf , generate a SQL query to find all supplier names which are located in the India nation."
} ] } ] }'))-> 'candidates' -> 0 -> 'content' -> 'parts' -> 0 -> 'text';
回應為 JSON 物件。如要進一步瞭解物件格式,請參閱「回應本文」。
下列範例會以以下方式修改先前的範例:
這個範例會使用目前資料庫的
messages.message資料欄內容做為輸入內容。這個範例會示範如何使用
json_build_object()函式,協助格式化函式參數。
select ml_predict_row('projects/PROJECT_ID/locations/us-central1/publishers/google/models/gemini-1.5-pro:generateContent', json_build_object('contents', json_build_object('text', message))) from messages;
傳回的 JSON 物件現在的 predictions 陣列中,會為 messages 表格的每個資料列包含一個項目。
由於回應是 JSON 物件,您可以使用 PostgreSQL 箭頭運算子從中提取特定欄位:
select ml_predict_row('projects/PROJECT_ID/locations/us-central1/publishers/google/models/gemini-1.5-pro:generateContent', json_build_object('contents', json_build_object('text', message)))->'predictions'->0->'content' FROM messages;
如需 ml_predict_row() 的更多引數範例,請參閱「使用 Agent Platform API 快速入門」。