全部產品
Search
文件中心

Hologres:AI_GEN_STRUCTURED

更新時間:Jul 28, 2026

ai_gen_structured 是 Hologres 提供的 AI Function,用於產生符合指定 JSON Schema 的結構化 JSON 結果。適用於需要從大模型擷取格式可控、可解析輸出的情境,如從文本中提取結構化實體資訊、從圖片/視頻中提取標註資訊等。

前提條件

  • Hologres 執行個體版本需為 V4.2.4 及以上。

  • 已在 Hologres 控制台部署所需模型,詳情請參見託管模型。

函數文法

文本資訊提取

ai_gen_structured(
    model_name      TEXT,
    message         TEXT,
    response_format JSON,
    on_error        TEXT DEFAULT 'capture',
    params          JSON DEFAULT '{}'::json
) RETURNS JSON

圖片/視頻等多模態資訊提取

ai_gen_structured(
    model_name      TEXT,
    message         TEXT,
    input_file      FILE,
    response_format JSON,
    on_error        TEXT DEFAULT 'capture',
    params          JSON DEFAULT '{}'::json
) RETURNS JSON

參數說明

參數

類型

是否必填

預設值

說明

model_name

TEXT

是

-

模型名稱,如 qwen3.7-max、qwen3.7-plus 等,需提前部署模型,詳情請參見託管模型。

message

TEXT

是

-

發送給模型的提示詞。

input_file

FILE

否

-

輸入檔案,僅多模態重載支援。通過 to_file(uri, endpoint, role_arn) 構造,詳情請參見本文多模態輸入檔案構造。

response_format

JSON

是

-

輸出格式定義,支援 json_schema 和 json_object 兩種模式,詳情請參見本文response_format 格式說明。

on_error

TEXT

否

'capture'

行級異常處理策略,僅治理模型推理階段的錯誤(如模型返回非法 JSON)。取值如下:

  • 'capture'(預設):模型推理出錯時,將錯誤資訊作為 JSON 結果返回,查詢繼續執行。

  • 'skip':模型推理出錯時,該行返回 NULL,查詢繼續執行。

  • 'abort':模型推理出錯時,拋出異常,中斷整條 SQL。

批量處理情境建議使用 'skip' 或 'capture',避免單行錯誤中斷整條查詢。

說明

以下使用層面的錯誤不受 on_error 控制,會始終直接拋出異常:模型名不存在;輸入檔案不存在(OSS 404);SQL 文法或邏輯錯誤。

params

JSON

否

'{}'

額外參數,以 JSON 格式傳入,如 {"temperature": 0.1}。

傳回值

返回 JSON 類型,內容符合 response_format 中定義的 schema 結構。

response_format 格式說明

模式一:json_schema(推薦)

通過標準 JSON Schema 精確定義輸出結構,模型將嚴格按 schema 產生輸出:

{
    "type": "json_schema",
    "schema": {
        "type": "object",
        "properties": {
            "field_name": {"type": "string"},
            "field_age": {"type": "integer"}
        },
        "required": ["field_name", "field_age"],
        "additionalProperties": false
    }
}

Schema 越精確(類型約束、enum 枚舉、description 描述),輸出品質越高。

模式二:json_object

僅保證輸出為合法 JSON 對象,具體欄位由提示詞控制:

{
    "type": "json_object"
}

使用 json_object 時,需要在 message 提示詞中明確說明期望的 JSON 欄位和格式。

使用樣本

樣本 1:從文本中提取結構化實體

SELECT ai_gen_structured(
    model_name => 'qwen3.7-max',
    message => '從以下文本中提取人員資訊:張三是阿里巴巴的進階工程師,男,漢族,1990年3月15日出生,今年35歲',
    response_format => '{
        "type": "json_schema",
        "schema": {
            "type": "object",
            "properties": {
                "name": {"type": "string"},
                "age": {"type": "integer"},
                "sex": {"type": "string", "enum": ["男", "女"]},
                "birth_day": {"type": ["string", "null"], "format": "date"},
                "民族": {"type": "string", "enum": ["漢族", "蒙古族", "回族", "藏族", "維吾爾族"]}
            },
            "required": ["name", "age", "sex", "birth_day", "民族"],
            "additionalProperties": false
        }
    }'::json
);

輸出:

{"name": "張三", "age": 35, "sex": "男", "birth_day": "1990-03-15", "民族": "漢族"}

樣本 2:json_object 模式

SELECT ai_gen_structured(
    model_name => 'qwen3.7-max',
    message => '請以 JSON 格式返回杭州這座城市的資訊,欄位包括:province(所屬省份)、is_municipality(是否直轄市,布爾值)。',
    response_format => '{"type": "json_object"}'::json
);

輸出:

{"province": "浙江省", "is_municipality": false}

樣本 3:多模態 — 從圖片中提取結構化資訊

SELECT ai_gen_structured(
    model_name => 'qwen3.7-plus',
    message => '識別這張駕駛情境圖片,提取情境結構化資訊。',
    input_file => to_file(
        'oss://your-bucket/path/to/image.jpg',
        'oss-cn-hangzhou-internal.aliyuncs.com',
        'acs:ram::your_account_id:role/your_role'
    ),
    response_format => '{
        "type": "json_schema",
        "schema": {
            "type": "object",
            "properties": {
                "scene": {"type": "string", "description": "情境描述"},
                "time_of_day": {"type": "string", "enum": ["白天", "夜晚", "黃昏"]},
                "weather": {"type": "string"},
                "has_vehicle": {"type": "boolean"}
            },
            "required": ["scene", "time_of_day", "weather", "has_vehicle"],
            "additionalProperties": false
        }
    }'::json
);

樣本 4:批量處理表資料

-- 假設 documents 表有 id 和 content 欄位
SELECT id, ai_gen_structured(
    'qwen3.7-max',
    '提取人員資訊:' || content,
    '{"type":"json_schema","schema":{"type":"object","properties":{"name":{"type":"string"},"age":{"type":"integer"},"city":{"type":"string"}},"required":["name","age","city"],"additionalProperties":false}}'::json,
    'skip',
    '{"temperature":0.1}'::json
) AS extracted_info
FROM documents
ORDER BY id;

批量處理時建議使用 on_error => 'skip',避免單行異常中斷整個查詢。

多模態輸入檔案構造

使用 to_file() 函數構造 FILE 型別參數:

to_file(
    object_uri  TEXT,   -- OSS 檔案路徑,如 'oss://bucket/path/file.jpg'
    endpoint    TEXT,   -- OSS endpoint,如 'oss-cn-hangzhou-internal.aliyuncs.com'
    role_arn    TEXT    -- RAM 角色 ARN,如 'acs:ram::account_id:role/role_name'
)

執行個體需要對目標 OSS Bucket 具備對應 Role ARN 的讀取授權。

注意事項

  • Schema 設計:response_format 中的 JSON Schema 越精確,輸出品質越高。建議為每個欄位添加 description,對有限取值使用 enum,可選欄位使用 ["string", "null"] 類型。

  • 批量處理:對錶執行批量提取時,建議使用 on_error => 'skip' 或 'capture',避免單行推理失敗中斷整個查詢。

  • 模型選擇:文本提取推薦 qwen3.7-max(品質優先)或 qwen3.7-plus(性價比);多模態情境需使用支援視覺理解的模型(如 qwen3.7-plus)。

  • 非 JSON 輸出:如果期望輸出非 JSON 格式的文本,請使用 ai_gen 函數。

  • params 參數:結構化輸出模式下,引擎會覆蓋部分模型參數(如 max_tokens)以確保 JSON 輸出完整性,temperature 等參數正常生效。