- 类别:
:doc:`/sql-reference/functions-string`(大型语言模型)
AI_COMPLETE(单字符串)¶
备注
AI_COMPLETE 是 COMPLETE (SNOWFLAKE.CORTEX) 的更新版本。要获得最新功能,请使用 AI_COMPLETE。
使用支持的语言模型,根据文本提示生成响应(补全)。
语法¶
该函数包含两个必需实参和四个可选实参。该函数可以与位置或命名实参语法一起使用。
将 AI_COMPLETE 与单个字符串输入一起使用
AI_COMPLETE(
<model>, <prompt> [ , <model_parameters>, <response_format>, <show_details> ] )
实参¶
model
指定要使用模型的字符串。指定以下模型之一:
claude-4-opus
claude-4-sonnet
claude-3-7-sonnet
claude-3-5-sonnet
deepseek-r1
gemma-7b
jamba-1.5-mini
jamba-1.5-large
jamba-instruct
llama2-70b-chat
llama3-8b
llama3-70b
llama3.1-8b
llama3.1-70b
llama3.1-405b
llama3.2-1b
llama3.2-3b
llama3.3-70b
llama4-maverick
llama4-scout
mistral-large
mistral-large2
mistral-7b
mixtral-8x7b
openai-gpt-4.1
openai-o4-mini
reka-core
reka-flash
snowflake-arctic
snowflake-llama-3.1-405b
snowflake-llama-3.3-70b
受支持的模型可能会产生不同的 费用。
prompt
字符串提示符
model_parameters
一个 对象,包含下列影响模型超参数的零个或多个选项。请参阅 LLM 设置 (https://www.promptingguide.ai/introduction/settings)。
temperature
:一个从 0 到 1(含)的值,用于控制语言模型输出的随机性。温度越高(例如 0.7),输出就越多样和随机,而温度越低(例如 0.2),输出就越确定和集中。默认值:0
top_p
:一个从 0 到 1(含)的值,用于控制语言模型的随机性和多样性,t通常用作temperature
的替代项。不同的是,top_p
限制模型输出的可能令牌集,而temperature
影响每个步骤选择哪些令牌。默认值:0
max_tokens
:设置响应中输出令牌的最大数量。数值小会导致响应截断。默认值:4096。最大允许值:8192
guardrails
:使用 Cortex Guard 筛选来自语言模型的潜在不安全和有害的响应。TRUE 或 FALSE。默认:FALSE
response_format
响应应遵循的 JSON 架构 (https://json-schema.org/)。这是 SQL 子对象,不是字符串。如果未指定
response_format
,则响应是包含响应的字符串,或包含响应及其相关信息的序列化 JSON 对象。有关更多信息,请参阅 AI_COMPLETE 结构化输出。
show_details
一个布尔标志,指示是否返回包含响应及其相关信息的序列化 JSON 对象。
返回¶
如果未指定 show_details
实参或将其设置为 FALSE,且未指定 response_format
或将其设置为 NULL,则返回包含响应的字符串。
如果未指定 show_details
实参或将其设置为 FALSE,且已指定 response_format
,则返回遵循所提供的响应格式的对象。
如果 show_details
实参设置为 TRUE,且未指定 response_format
,则返回一个包含以下键的 JSON 对象。
"choices"
:模型响应的数组。(目前只提供一个响应。)每个响应都是一个包含"messages"
键的对象,其值为模型对最新提示的响应。"created"
:生成响应时的 UNIX 时间戳(自 1970 年 1 月 1 日午夜以来的秒数)。"model"
:创建响应的模型的名称。"usage"
:记录此补全所消耗和生成的令牌数的对象。包括以下子键:"completion_tokens"
:生成的响应中的令牌数。"prompt_tokens"
:提示中的令牌数。"total_tokens"
:消耗的令牌总数,这是其他两个值的总和。
如果 show_details
实参设置为 TRUE,且指定了 response_format
,则返回一个包含以下键的 JSON 对象
"structured_output"
:遵循指定响应格式的 json 对象。"created"
:生成响应时的 UNIX 时间戳(自 1970 年 1 月 1 日午夜以来的秒数)。"model"
:创建响应的模型的名称。"usage"
:记录此补全所消耗和生成的令牌数的对象。包括以下子键:"completion_tokens"
:生成的响应中的令牌数。"prompt_tokens"
:提示中的令牌数。"total_tokens"
:消耗的令牌总数,这是其他两个值的总和。
示例¶
单个响应¶
生成单个响应:
SELECT AI_COMPLETE('snowflake-arctic', 'What are large language models?');
来自表列的响应¶
以下示例使用 content
列作为输入,为 reviews
表中的每一行生成响应。每个查询结果都包含对相应评论的评论。
SELECT AI_COMPLETE(
'mistral-large',
CONCAT('Critique this review in bullet points: <review>', content, '</review>')
) FROM reviews LIMIT 10;
小技巧
如本例所示,您可以使用提示符中的标记来控制生成的响应类型。有关技巧,请参阅 提示 LLaMA 2 指南 (https://replicate.com/blog/how-to-prompt-llama)。
控制模型参数¶
以下示例指定了用于提供响应的 model_parameters
。
SELECT AI_COMPLETE(
model => 'llama2-70b-chat',
prompt => 'how does a snowflake get its unique pattern?',
model_parameters => {
'temperature': 0.7,
'max_tokens': 10
}
);
响应是包含来自语言模型的消息和其他信息的字符串。请注意,响应按照 model_parameters
实参的指示截断。
"The unique pattern on a snowflake is"
详细输出¶
以下示例显示如何使用 show_details
实参返回额外的推理细节。
SELECT AI_COMPLETE(
model => 'llama2-70b-chat',
prompt => 'how does a snowflake get its unique pattern?',
model_parameters => {
'temperature': 0.7,
'max_tokens': 10
},
show_details => true
);
响应是一个包含模型消息和相关详细信息的 JSON 对象。options
实参用于截断输出。
{
"choices": [
{
"messages": " The unique pattern on a snowflake is"
}
],
"created": 1708536426,
"model": "llama2-70b-chat",
"usage": {
"completion_tokens": 10,
"prompt_tokens": 22,
"guardrail_tokens": 0,
"total_tokens": 32
}
}
指定 JSON 响应格式¶
此示例说明如何使用函数的 response_format
实参返回结构化响应
SELECT AI_COMPLETE(
model => 'llama2-70b-chat',
prompt => 'Extract structured data from this customer interaction note: Customer Sarah Jones complained about the mobile app crashing during checkout. She tried to purchase 3 items: a red XL jacket ($89.99), blue running shoes ($129.50), and a fitness tracker ($199.00). The app crashed after she entered her shipping address at 123 Main St, Portland OR, 97201. She has been a premium member since January 2024.',
model_parameters => {
'temperature': 0,
'max_tokens': 4096
},
response_format => {
'type':'json',
'schema':{'type' : 'object','properties' : {'sentiment_categories':{'type':'array','items':{'type':'object','properties':
{'food_quality' : {'type' : 'string'},'food_taste': {'type':'string'}, 'wait_time': {'type':'string'}, 'food_cost': {'type':'string'}},'required':['food_quality','food_taste' ,'wait_time','food_cost']}}}}
}
);
响应是遵循结构化响应格式的 json 对象。
响应:
{
"sentiment_categories": [
{
"food_cost": "negative",
"food_quality": "positive",
"food_taste": "positive",
"wait_time": "neutral"
}
]
}
指定带有详细输出的 JSON 响应格式¶
此示例说明如何使用函数的 response_format
实参返回结构化响应,并结合使用 show_details
来获取额外的推理信息
SELECT AI_COMPLETE(
model => 'llama2-70b-chat',
prompt => 'Extract structured data from this customer interaction note: Customer Sarah Jones complained about the mobile app crashing during checkout. She tried to purchase 3 items: a red XL jacket ($89.99), blue running shoes ($129.50), and a fitness tracker ($199.00). The app crashed after she entered her shipping address at 123 Main St, Portland OR, 97201. She has been a premium member since January 2024.',
model_parameters => {
'temperature': 0,
'max_tokens': 4096
},
response_format => {
'type':'json',
'schema':{'type' : 'object','properties' : {'sentiment_categories':{'type':'array','items':{'type':'object','properties':
{'food_quality' : {'type' : 'string'},'food_taste': {'type':'string'}, 'wait_time': {'type':'string'}, 'food_cost': {'type':'string'}},'required':['food_quality','food_taste' ,'wait_time','food_cost']}}}}
},
show_details => true
);
响应是一个 json 对象,其中包含结构化响应和额外的推理元数据。
{
"created": 1738683744,
"model": "mistral-large2",
"structured_output": [
{
"raw_message": {
"sentiment_categories": [
{
"food_cost": "negative",
"food_quality": "positive",
"food_taste": "positive",
"wait_time": "neutral"
}
]
},
"type": "json"
}
],
"usage": {
"completion_tokens": 60,
"prompt_tokens": 94,
"total_tokens": 154
}
}