类别:

字符串和二进制函数 (AI Functions)

AI_COMPLETE(单字符串)

备注

AI_COMPLETE 是 COMPLETE (SNOWFLAKE.CORTEX) 的更新版本。要获得最新功能,请使用 AI_COMPLETE。

使用支持的语言模型,根据文本提示生成响应(补全)。

语法

该函数包含两个必需实参和四个可选实参。该函数可以与位置或命名实参语法一起使用。

将 AI_COMPLETE 与单个字符串输入一起使用

AI_COMPLETE(
    <model>, <prompt> [ , <model_parameters>, <response_format>, <show_details> ] )
Copy

实参

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 对象。

  • SQL 类型的定义,以 TYPE 关键字开头。此类型必须使用 OBJECT 作为顶层容器类型,其 OBJECT 中的字段映射到相应的 JSON 字段及其值。

有关更多信息,请参阅 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":消耗的令牌总数,这是其他两个值的总和。

示例

单个响应

生成单个响应:

SELECT AI_COMPLETE('snowflake-arctic', 'What are large language models?');
Copy

来自表列的响应

以下示例使用 content 列作为输入,为 reviews 表中的每一行生成响应。每个查询结果都包含对相应评论的评论。

SELECT AI_COMPLETE(
    'mistral-large',
        CONCAT('Critique this review in bullet points: <review>', content, '</review>')
) FROM reviews LIMIT 10;
Copy

小技巧

如本例所示,您可以使用提示符中的标记来控制生成的响应类型。有关技巧,请参阅 提示 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
    }
);
Copy

响应是包含来自语言模型的消息和其他信息的字符串。请注意,响应按照 model_parameters 实参的指示截断。

"The unique pattern on a snowflake is"
Copy

详细输出

以下示例显示如何使用 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
);
Copy

响应是一个包含模型消息和相关详细信息的 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
    }
}
Copy

指定 JSON 响应格式

This example illustrates the use of the function's response_format argument to return a structured response by providing a type literal.

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 OBJECT(note OBJECT(items_count NUMBER, price ARRAY(STRING), address STRING, member_date STRING)),
);
Copy

响应是遵循结构化响应格式的 json 对象。

响应:

{
    "note": {
        "address": "123 Main St, Portland OR, 97201",
        "items_count": 3,
        "member_date": "January 2024",
        "price": [
        "$89.99",
        "$129.50",
        "$199.00"
        ]
    }
}

Specifying a JSON response format with details, using a type literal

This example illustrates the use of the function's response_format argument to return a structured response combined with show_details to get additional inference information, using a type literal.

SELECT AI_COMPLETE(
    model => 'llama3.3-70b',
    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.',
    response_format => TYPE OBJECT(note OBJECT(items_count NUMBER, price ARRAY(STRING), address STRING, member_date STRING)),
    show_details => TRUE
);
Copy

The response is a JSON object containing structured response with additional inference metadata.

{
  "created": 1758755328,
  "model": "llama3.3-70b",
  "structured_output": [
    {
      "raw_message": {
        "note": {
          "items_count": 3,
          "price": [
            "$89.99",
            "$129.50",
            "$199.00"
          ]
        }
      },
      "type": "json"
    }
  ],
  "usage": {
    "completion_tokens": 49,
    "prompt_tokens": 100,
    "total_tokens": 149
  }
}
Copy

Specifying a JSON response format with details, using a JSON schema

This example illustrates the use of the function's response_format argument to return a structured response combined with show_details to get additional inference information, using a JSON schema.

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' : {'note':{'type':'object','properties':
            {'items_count' : {'type' : 'number'},'price': {'type':'array','items':{'type':'string'}}, 'address': {'type':'string'}, 'member_date': {'type':'string'}},'required':['items_count','price' ,'address', 'member_date']}}}
    },
    show_details => true
);
Copy

响应是一个 json 对象,其中包含结构化响应和额外的推理元数据。

{
    "created": 1758057115,
    "model": "mistral-large2",
    "structured_output": [
        {
        "raw_message": {
            "note": {
            "address": "123 Main St, Portland OR, 97201",
            "items_count": 3,
            "member_date": "January 2024",
            "price": [
                "$89.99",
                "$129.50",
                "$199.00"
            ]
            }
        },
        "type": "json"
        }
    ],
    "usage": {
        "completion_tokens": 76,
        "prompt_tokens": 100,
        "total_tokens": 176
    }
}
语言: 中文