AI_COMPLETE

AI_COMPLETE
AI_COMPLETE
是云器 Lakehouse 平台中用于生成式 AI 任务的核心标量函数,允许用户在 SQL 环境中直接调用大语言模型(LLM),根据文本提示或多模态输入生成响应,从而完成文本补全、翻译、情感分析、代码生成及复杂推理等任务。

云器将 AI 计算下沉至存储层与执行引擎,数据在平台内部即可完成智能处理,无需流转至外部环境,在保障数据安全的同时大幅降低任务延迟。


语法

AI_COMPLETE
AI_COMPLETE
支持两种调用形式:

文本模式

ai_complete(<model>, <prompt> [, json '{}'])

图像模式

ai_complete(<model>, (<prompt> AS prompt, <image_url> AS image) [, json '{}'])


参数说明

model(必需)

指定要调用的语言模型,支持两种来源:

来源一:API Gateway Endpoint(推荐)

平台管理员在 API Gateway 中预先配置模型服务(包含供应商、版本、认证凭据),普通用户通过

endpoint:
endpoint:
前缀引用,无需关心底层连接细节。

'endpoint:<endpoint名称>' -- 示例 'endpoint:qwen3-max-preview' 'endpoint:qwen3.5-plus' 'endpoint:doubao-seed-2-0-pro-260215'

来源二:API Connection 连接对象

用户通过

CREATE API CONNECTION
CREATE API CONNECTION
自行创建连接对象,适用于需要自定义服务地址、认证密钥或对接私有化部署模型的场景。

-- 创建连接对象 CREATE API CONNECTION conn_bailian TYPE ai_function PROVIDER = 'bailian' BASE_URL = 'https://dashscope.aliyuncs.com/api/v1' API_KEY = 'sk-xxxxxxxxxxxxxxxxxxxxxxxx'; -- 引用时使用 connection: 前缀 SELECT ai_complete('conn_bailian:qwen3.5-plus', '请简要介绍量子计算的基本原理。');

CREATE API CONNECTION
CREATE API CONNECTION
各字段说明:

字段说明
TYPE
TYPE
固定为
ai_function
ai_function
PROVIDER
PROVIDER
模型供应商标识,如
'bailian'
'bailian'
'openai'
'openai'
'anthropic'
'anthropic'
BASE_URL
BASE_URL
模型服务的 API 基础地址
API_KEY
API_KEY
调用服务所需的认证密钥

prompt(必需)

待发送给模型的输入内容,为字符串类型。

文本模式:直接传入字符串:

SELECT ai_complete('endpoint:qwen3-max-preview', '用一句话解释什么是向量数据库');

支持通过

CONCAT
CONCAT
||
||
拼接动态内容:

SELECT ai_complete( 'endpoint:qwen3-max-preview', CONCAT('用20字总结以下文本:', content) ) AS summary FROM articles;

图像模式:使用具名元组语法,同时传入文本提示和图像 URL:

SELECT ai_complete( 'endpoint:doubao-seed-2-0-pro-260215', ('图片中有什么?' AS prompt, GET_PRESIGNED_URL(USER VOLUME, 'images/product.jpg', 36000) AS image) );


options(可选)

使用

json '{}'
json '{}'
字面量语法传入,控制执行行为和模型参数:

参数键类型说明
model.params.temperature
model.params.temperature
FLOAT输出随机性,范围 [0, 2],越低越确定
model.params.max_tokens
model.params.max_tokens
INT最大输出 token 数
model.params.top_p
model.params.top_p
FLOAT核采样概率,范围 (0, 1]
model.params.enable_thinking
model.params.enable_thinking
BOOL是否开启 thinking 模式,批量处理建议设为
false
false
response.timeout
response.timeout
STRING单次请求超时时间(秒),如
"60"
"60"
task.concurrency
task.concurrency
STRING批量处理并发度,如
"5"
"5"

-- 关闭 thinking 模式 SELECT ai_complete( 'endpoint:qwen3-max-preview', '什么是人工智能?用一句话回答', json '{"model.params":{"enable_thinking":false}}' ) AS result; -- 多参数组合 SELECT ai_complete( 'endpoint:qwen3-max-preview', question, json '{"model.params":{"enable_thinking":false},"task.concurrency":"5"}' ) AS answer FROM questions;


返回值

返回 STRING 类型,为模型生成的响应文本。

  • prompt 为
    NULL
    NULL
    或空字符串时,返回
    NULL
    NULL
  • endpoint 不存在时,报错
    CZLH-67000: No available endpoints found
    CZLH-67000: No available endpoints found
  • model 格式错误(无
    endpoint:
    endpoint:
    connection:
    connection:
    前缀)时,报错
    CZLH-65000: Invalid model coordinates
    CZLH-65000: Invalid model coordinates

使用示例

基础文本补全

SELECT ai_complete('endpoint:qwen3-max-preview', '中国的首都在哪里?') AS result;

批量处理表中数据

SELECT id, ai_complete( 'endpoint:qwen3-max-preview', question, json '{"model.params":{"enable_thinking":false},"task.concurrency":"5"}' ) AS answer FROM questions;

CONCAT 动态拼接 prompt

SELECT product_id, ai_complete( 'endpoint:qwen3-max-preview', CONCAT('请为以下商品写一段30字以内的卖点描述:', product_name) ) AS selling_point FROM products;

图像描述

SELECT ai_complete( 'endpoint:doubao-seed-2-0-pro-260215', ('图片中有什么?' AS prompt, GET_PRESIGNED_URL(USER VOLUME, 'images/product.jpg', 36000) AS image) ) AS result;

图像 + 具体问题

SELECT ai_complete( 'endpoint:doubao-seed-2-0-pro-260215', ('这个商品的价格是多少?请用一句话回答。' AS prompt, GET_PRESIGNED_URL(USER VOLUME, 'images/product.jpg', 36000) AS image) ) AS result;

批量图像处理

SELECT relative_path, ai_complete( 'endpoint:doubao-seed-2-0-pro-260215', ('用一句话描述图片中的商品。' AS prompt, GET_PRESIGNED_URL(USER VOLUME, relative_path, 36000) AS image), json '{"model.params":{"enable_thinking":false},"task.concurrency":"3"}' ) AS description FROM (SHOW USER VOLUME DIRECTORY SUBDIRECTORY 'images/products') LIMIT 10;

与其他 AI 函数组合

-- AI_COMPLETE 生成文本 → AI_CLASSIFY 分类 SELECT ai_classify( 'endpoint:qwen3.5-plus', ai_complete('endpoint:qwen3-max-preview', '写一条关于科技的新闻标题'), ARRAY('科技', '体育', '金融', '娱乐') ) AS category;


注意事项

  • 模型选择:图像模式必须使用支持多模态的模型(如
    doubao-seed-2-0-pro-260215
    doubao-seed-2-0-pro-260215
    )。
    qwen3-max-preview
    qwen3-max-preview
    等纯文本模型不支持图像输入,传入图像时不报错但会忽略图片内容,仅根据文本 prompt 生成响应。
  • Thinking 模式:qwen3 系列模型默认开启 thinking 模式,会增加延迟和 token 消耗。批量处理场景建议通过 options 关闭:
    json '{"model.params":{"enable_thinking":false}}'
    json '{"model.params":{"enable_thinking":false}}'
  • 图像字段不可为 NULL:图像模式中
    image
    image
    字段传入 NULL 会报错
    invalid type of image field: void
    invalid type of image field: void
    ,需确保
    GET_PRESIGNED_URL()
    GET_PRESIGNED_URL()
    返回有效 URL。
  • NULL 与空字符串:prompt 为
    NULL
    NULL
    或空字符串时均返回
    NULL
    NULL
    ,不报错。
  • 错误码说明:endpoint 不存在报
    CZLH-67000
    CZLH-67000
    ;model 格式错误(缺少
    endpoint:
    endpoint:
    前缀)报
    CZLH-65000
    CZLH-65000
  • Token 限制:不同模型有不同的上下文窗口限制,超出限制的输入会被截断或报错,处理长文本时注意控制输入长度。
联系我们
预约咨询
微信咨询
电话咨询