- 类别:
:doc:`/sql-reference/functions-string`(AI 函数)
AI_COMPLETE(单字符串)¶
备注
AI_COMPLETE 是 COMPLETE (SNOWFLAKE.CORTEX) 的更新版本。要获得最新功能,请使用 AI_COMPLETE。
使用支持的语言模型,根据文本提示生成响应(补全)。
语法¶
该函数包含两个必需实参和四个可选实参。该函数可以与位置或命名实参语法一起使用。
将 AI_COMPLETE 与单个字符串输入一起使用
实参¶
model指定要使用模型的字符串。指定以下模型之一:
claude-4-opusclaude-4-sonnetclaude-3-7-sonnetclaude-3-5-sonnetdeepseek-r1llama3-8bllama3-70bllama3.1-8bllama3.1-70bllama3.1-405bllama3.3-70bllama4-maverickllama4-scoutmistral-largemistral-large2mistral-7bmixtral-8x7bopenai-gpt-4.1openai-o4-minisnowflake-arcticsnowflake-llama-3.1-405bsnowflake-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_formatThe format that the response should follow. You can specify the response format as:
A JSON schema (https://json-schema.org/) that the response should follow. This is a SQL sub-object, not a string.
A SQL type literal beginning with the TYPE keyword. The defined type must use an OBJECT as its top-level container, and fields of this OBJECT are mapped to corresponding JSON fields and values.
If
response_formatis not specified, the response is a string containing either the response or a serialized JSON object containing the response and information about it.有关更多信息,请参阅 AI_COMPLETE structured outputs。
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":消耗的令牌总数,这是其他两个值的总和。
示例¶
单个响应¶
生成单个响应:
来自表列的响应¶
以下示例使用 content 列作为输入,为 reviews 表中的每一行生成响应。每个查询结果都包含对相应评论的评论。
小技巧
如本例所示,您可以使用提示符中的标记来控制生成的响应类型。有关技巧,请参阅 提示 LLaMA 2 指南 (https://replicate.com/blog/how-to-prompt-llama)。
控制模型参数¶
以下示例指定了用于提供响应的 model_parameters。
响应是包含来自语言模型的消息和其他信息的字符串。请注意,响应按照 model_parameters 实参的指示截断。
详细输出¶
以下示例显示如何使用 show_details 实参返回额外的推理细节。
响应是一个包含模型消息和相关详细信息的 JSON 对象。options 实参用于截断输出。
指定 JSON 响应格式¶
此示例演示如何使用函数的 response_format 参数,通过提供类型字面量返回结构化响应。
The response is a JSON object following the structured response format.
响应:
使用类型字面量指定包含详细信息的 JSON 响应格式。¶
This example illustrates the use of response_format argument to return a structured response combined with show_details to get additional inference information, using a type literal.
响应是一个 JSON 对象,其中包含结构化响应和额外的推理元数据。
使用 JSON 架构指定包含详细信息的 JSON 响应格式。¶
此示例演示如何使用函数的 response_format 参数,通过 JSON 架构返回结构化响应,并结合 show_details 获取额外的推理信息。
响应是一个 json 对象,其中包含结构化响应和额外的推理元数据。