Categories:

String & binary functions (AI Functions)

COMPLETE (SNOWFLAKE.CORTEX) (multimodal)

Note

is the latest version of this function. Use AI_COMPLETE for the latest functionality. You can continue to use COMPLETE (SNOWFLAKE.CORTEX).

使用语言模型,为给定图像和提示生成响应(补全)。此函数变体支持图像模型和文本模型,并处理存储在内部 Snowflake 暂存区或外部暂存区中的图像。COMPLETE 可用于批量处理单个图像、多个图像、对每张图像应用相同或不同的提示,或者在单个操作(例如,比较)中应用多个图像。

语法

使用下列之一:

SNOWFLAKE.CORTEX.COMPLETE(
    '<model>', '<prompt>', <file_object>)
FROM <table>
SNOWFLAKE.CORTEX.COMPLETE(
    '<model>', <prompt_object> )
FROM <table>

实参

model

指定要使用模型的字符串。指定以下模型之一:

  • claude-sonnet-4-6
  • pixtral-large

支持的模型可能具有不同的成本和上下文窗口。可能会不时添加新模型。

prompt

A string containing a question about the image and optionally specifying an output format, such as JSON. Either this or the prompt_object argument is required.

prompt_object

A SQL OBJECT containing a string prompt with numbered placeholders ({0}, {1}, and so on) and one or more text or FILE valuse that are inserted into the prompt. The PROMPT function is a convenient way to create an object with the required layout. Either this argument or prompt is required.

file_object

A FILE object that contains an image file to be processed. Use the TO_FILE function to create FILE objects from a stage path. Required when using a string prompt.

FROM table

一个可选表,其中包含图像路径和每个图像的可选提示,允许在一次调用中对图像进行批处理至 COMPLETE。

返回

包含语言模型响应的字符串。

使用说明

  • 超过上下文窗口限制的输入会导致错误。超出上下文窗口限制的输出将被截断。
  • To process multiple images, the prompt must be an object (typically created using the PROMPT function) that specifies a prompt template and the files to be processed.
  • 仅支持文本和图像。不支持视频和音频文件。
  • Images with filename extensions .jpg, .jpeg, .png, .gif, and .webp are supported. pixtral-large also supports .bmp.
  • Maximum image size is 10 MB for pixtral-large and 3.75 MB for claude-sonnet-4-6. Additionally, claude-sonnet-4-6 does not support images with a resolution greater than 8000x8000.
  • 包含图像的暂存区必须启用服务器端加密。不支持客户端加密暂存区。
  • 该函数不支持自定义网络策略。
  • 暂存区名称不区分大小写,但路径区分大小写。

示例

以下示例演示了带图像的 COMPLETE 函数的基本功能。

视觉问答

通货膨胀率图表用于回答有关数据的问题。

Graph of inflation rates in 2023 with estimates for 2024

Comparison between inflation rates in 2023 and in2024 (Statista (https://www.statista.com/))

SELECT SNOWFLAKE.CORTEX.COMPLETE('claude-sonnet-4-6',
    'Which country will observe the largest inflation change in 2024 compared to 2023?',
    TO_FILE('@myimages', 'highest-inflation.png'));

响应:

Looking at the data, Venezuela will experience the largest change in inflation rates between 2023 and 2024.
The inflation rate in Venezuela is projected to decrease significantly from 337.46% in 2023 to 99.98% in 2024,
representing a reduction of approximately 237.48 percentage points. This is the most dramatic change among
all countries shown in the chart, even though Zimbabwe has higher absolute inflation rates.

图像分类

此示例对单个图像中识别的地标进行了分类。

Photograph of the Space Needle in Seattle with a puppy
SELECT SNOWFLAKE.CORTEX.COMPLETE('claude-sonnet-4-6',
    'Classify the landmark identified in this image. Respond in JSON only with the landmark name.',
    TO_FILE('@myimages', 'Seattle.jpg'));

响应:

{"landmark": "Space Needle"}

从图像中提取实体

此示例从图像中提取实体(对象)并以 JSON 格式返回结果。

Photograph of kitchen after remodeling
SELECT SNOWFLAKE.CORTEX.COMPLETE('claude-sonnet-4-6',
    'Extract the kitchen appliances identified in this image. Respond in JSON only with the identified appliances.',
    TO_FILE('@myimages', 'kitchen.png'));

响应:

{
    "appliances": [ "microwave","electric stove","oven","refrigerator" ]
}

批量处理目录或表中的图像

要批量处理多个图像,对每个图像执行相同的操作,将图像文件存储在同一个暂存区。将 COMPLETE 函数应用于表的每一行。

Note

The stage must have a directory table to retrieve the paths to its files.

首先,通过从目录中检索图像位置,将其转换为 FILE 对象,并将生成的 FILE 对象存储在表列中来创建表。使用如下 SQL 语句:

CREATE TABLE image_table AS
    (SELECT TO_FILE('@myimages', RELATIVE_PATH) AS img FROM DIRECTORY(@myimages));

然后,将 COMPLETE 函数应用于包含 FILE 对象的列。以下示例对表中的每张图像进行了分类:

SELECT SNOWFLAKE.CORTEX.COMPLETE('claude-sonnet-4-6',
    PROMPT('Classify the input image {0} in no more than 2 words. Respond in JSON', img_file)) AS image_classification
FROM image_table;

响应:

{ "classification": "Inflation Rates" }
{ "classification": "beverage refrigerator" }
{ "classification": "Space Needle" }
{ "classification": "Modern Kitchen" }
{ "classification": "Pie Chart" }
{ "classification": "Economic Graph" }
{ "classification": "Persian Cat" }
{ "classification": "Labrador Retriever" }
{ "classification": "Jedi Cat" }
{ "classification": "Sleeping cat" }
{ "classification": "Persian Cat" }
{ "classification": "Garden Costume" }
{ "classification": "Floral Fashion" }

If you already have a table with paths to the images, you can use the TO_FILE function to construct the FILE objects within the query:

SELECT SNOWFLAKE.CORTEX.COMPLETE('claude-sonnet-4-6',
    PROMPT('Classify the input image {0} in no more than 2 words. Respond in JSON',
        TO_FILE('@myimages', img_path)) AS image_classification
FROM image_table;

您也可以直接从暂存区的目录中检索要处理的图像,如下所示:

SELECT SNOWFLAKE.CORTEX.COMPLETE('claude-sonnet-4-6',
    PROMPT('Classify the input image {0} in no more than 2 words. Respond in JSON',
        TO_FILE('@myimages', RELATIVE_PATH))) as image_classification
FROM DIRECTORY(@myimages);

在表中提供图像和提示

To perform a different operation on each image in a table, provide the images and their corresponding prompts in a table. In the following example, the table contains the stage path of each image in the img_path column and the prompt in the prompt column.

SNOWFLAKE.CORTEX.COMPLETE('claude-sonnet-4-6',
    PROMPT('Given the input image {0}, {1}. Respond in JSON',
        TO_FILE('@myimages', img_path), prompt) as image_result)
FROM image_table;

法律声明

Refer to Snowflake AI and ML.