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 |
是 |
- |
模型名称,如 |
|
message |
TEXT |
是 |
- |
发送给模型的提示词。 |
|
input_file |
FILE |
否 |
- |
输入文件,仅多模态重载支持。通过 |
|
response_format |
JSON |
是 |
- |
输出格式定义,支持 |
|
on_error |
TEXT |
否 |
'capture' |
行级异常处理策略,仅治理模型推理阶段的错误(如模型返回非法 JSON)。取值如下:
批量处理场景建议使用 说明
以下使用层面的错误不受 on_error 控制,会始终直接抛出异常:模型名不存在;输入文件不存在(OSS 404);SQL 语法或逻辑错误。 |
|
params |
JSON |
否 |
'{}' |
额外参数,以 JSON 格式传入,如 |
返回值
返回 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等参数正常生效。