Cortex AI Functions -- Images¶
使用 Cortex AI Images,您可以完成以下任务:
比较图像
为图像添加字幕
对图像进行分类
从图像中提取实体
生成用于检索系统的嵌入向量
使用图表中的数据回答问题
您可以使用以下功能来完成这些任务:
输入要求¶
COMPLETE 多模态可以处理具有以下特征的图像:
要求 |
值 |
|---|---|
文件扩展名 |
|
暂存区加密 |
服务器端加密 |
数据类型 |
备注
从各个暂存区处理文件目前与自定义网络策略不兼容。
分析图像¶
The COMPLETE function processes a single image or multiple images (for example, extracting differences in entities across various images) stored in a stage. See Create stage for media files for information on creating a suitable stage.
该函数调用指定以下内容:
要使用的多模态模型
提示
图像文件的暂存区路径(通过 FILE 对象)
视觉问答示例¶
以下示例使用 Anthropic 的 Claude Sonnet 3.5 模型来汇总存储在 @myimages 暂存区中的饼图 science-employment-slide.jpeg。
2023 年数学被认为“极为重要”的职业分布¶
SELECT AI_COMPLETE('claude-3-5-sonnet',
'Summarize the insights from this pie chart in 100 words',
TO_FILE('@myimages', 'science-employment-slide.jpeg'));
响应:
This pie chart shows the distribution of occupations where mathematics is considered "extremely important" in 2023.
Data scientists dominate with nearly half (48.7%) of all such positions, followed by operations research analysts
at 29.6%. The remaining positions are distributed among statisticians (7.8%), actuaries (7.2%), physicists (5.1%),
mathematicians (0.6%), and other mathematical science occupations (1.1%). This distribution highlights the growing
importance of data science in mathematics-intensive careers, while traditional mathematics roles represent a smaller
share of the workforce.
比较图像示例¶
备注
目前,只有 Anthropic (claude) 和 Meta (llama) 模型可以在单个提示中引用多张图像。未来版本中可能会提供对其他模型的多图像支持。
使用 :doc:` PROMPT helper 函数 </sql-reference/functions/prompt>` 在一次 COMPLETE 调用中处理多张图像。以下示例使用 Anthropic 的 Claude Sonnet 3.5 模型来比较 @myimages 暂存区上的两种不同的广告创意。
两则电动汽车广告的图像¶
SELECT AI_COMPLETE('claude-3-5-sonnet',
PROMPT('Compare this image {0} to this image {1} and describe the ideal audience for each in two concise bullets no longer than 10 words',
TO_FILE('@myimages', 'adcreative_1.png'),
TO_FILE('@myimages', 'adcreative_2.png')
));
响应:
First image ("Discover a New Energy"):
• Conservative luxury SUV buyers seeking a subtle transition to electrification
Second image ("Electrify Your Drive"):
• Young, tech-savvy urbanites attracted to bold, progressive automotive design
对图像进行分类示例¶
以下示例使用 AI_CLASSIFY 对房地产应用程序的图像进行分类。
以下 SQL 使用 AI_CLASSIFY 函数将图像分类为客厅、厨房、浴室、花园或主卧室的照片。
SELECT AI_CLASSIFY(TO_FILE('@my_images', 'REAL_ESTATE_STAGING.PNG'),
['Living Area', 'Kitchen', 'Bath', 'Garden', 'Master Bedroom']) AS room_classification;
响应:
{ "labels": [ "Living Area" ] }
以下 SQL 将上方图像中的物体分类为沙发、窗户、餐桌、电视或艺术品。
SELECT AI_CLASSIFY (TO_FILE ('@my_images', 'REAL_ESTATE_STAGING.PNG'),
['Couch', 'Window', 'Table', 'Television', 'Art'], {'output_mode': 'multi'} )
AS living_room_objects;
响应:
{
"labels": [
"Art",
"Couch",
"Table",
"Window"
]
}
搜索图像¶
您可以使用 AI_EMBED 来查找与目标图像相似的图像。首先使用 AI_EMBED 函数生成目标图像的嵌入向量,将其视觉特征映射到抽象向量空间中,形成图像特征的数值化表示。随后可使用向量相似度函数,将该嵌入向量与其他图像的嵌入向量进行比对,根据共有的或相似的视觉特征生成相似度评分。该评分可用于根据图像与目标图像的相似度进行分类、排序或筛选。
|
|
例如,针对上方图像,以下 SQL 会为每张图像生成嵌入向量,随后使用余弦相似度进行向量比对。结果为 0.5 左右,表明这些图像具有一定相似性。两张照片均拍摄于城市环境且包含背景人群,但主体内容不同。
WITH ai_image_embeddings as (
SELECT
AI_EMBED('voyage-multimodal-3',
TO_FILE ('@my_images', 'CITY_WALKING1.PNG')) as image1_embeddings,
AI_EMBED('voyage-multimodal-3',
TO_FILE ('@my_images', 'CITY_WALKING2.PNG')) as image2_embeddings
)
SELECT VECTOR_COSINE_SIMILARITY(image1_embeddings,image2_embeddings) as similarity FROM ai_image_embeddings;
0.5359029029
要查找与目标图像相似的图像,可以使用 AI_SIMILARITY。以下示例计算可能数千张图像的相似度评分,并返回与下方摩托车广告最相似的广告创意。
SELECT
TO_FILE('@ad_images', relative_path) as ALL_ADS
FROM DIRECTORY(@ad_images)
WHERE AI_SIMILARITY(TO_FILE('@ad_images', 'image_226.jpg'), ALL_ADS) >= 0.5;
该查询从多模态表中返回相似度评分大于 0.50 的图像。识别出的图像之一 (image_226.jpg) 是我们用作参考的图像。
+-----------------------------------------------------------+
| {} ALL_ADS |
+-----------------------------------------------------------+
| { "CONTENT_TYPE": "image/jpeg", |
| "ETAG": "686897696a7c876b7e", |
| "LAST_MODIFIED": "Wed, 26 Mar 2025 18:11:45 GMT", |
| "RELATIVE_PATH": "image_226.jpg", |
| "SIZE": 39086, |
| "STAGE": "@ad_images" } |
+-----------------------------------------------------------+
| { "CONTENT_TYPE": "image/jpeg", |
| "ETAG": "e7b678c7a696798686", |
| "LAST_MODIFIED": "Wed, 26 Mar 2025 18:11:57 GMT", |
| "RELATIVE_PATH": "image_441.jpg", |
| "SIZE": 12650, |
| "STAGE": "@ad_images" }, |
+-----------------------------------------------------------+
模型限制¶
All models available to Snowflake Cortex have limitations on the total number of input and output tokens, known as the model's context window. The context window size is measured in tokens. Inputs exceeding the context window limit result in an error. Output which would exceed the context window limit is truncated.
对于文本模型,词元通常代表大约四个文本字符,因此与限制相对应的字数小于词元数。
对于图像模型,每张图像的词元数量取决于视觉模型的架构。提示中的词元(例如:“这是什么动物?”)同样会占用模型的上下文窗口容量。
模型 |
上下文窗口(词元) |
文件类型 |
文件大小 |
每个提示的图像 |
|---|---|---|---|---|
|
1,047,576 |
.jpg、.jpeg、.png、.webp、.gif |
10MB |
5 |
|
1,047,576 |
.jpg、.jpeg、.png、.webp、.gif |
10MB |
5 |
|
200,000 |
.jpg、.jpeg、.png、.webp、.gif |
3.75 MB [L1] |
20 |
|
200,000 |
.jpg、.jpeg、.png、.webp、.gif |
3.75 MB [L1] |
20 |
|
200,000 |
.jpg、.jpeg、.png、.webp、.gif |
3.75 MB [L1] |
20 |
|
200,000 |
.jpg、.jpeg、.png、.webp、.gif |
3.75 MB [L1] |
20 |
|
128,000 |
.jpg、.jpeg、.png、.webp、.gif、.bmp |
10 MB |
10 |
|
128,000 |
.jpg、.jpeg、.png、.webp、.gif、.bmp |
10 MB |
10 |
|
128,000 |
.jpg、.jpeg、.png、.webp、.gif、.bmp |
10 MB |
1 |
|
32,768 |
.jpg、.png、.pg、.gif、.bmp |
10 MB |
1 |
成本注意事项¶
计费随处理的词元数量而变化。每张图像的词元数量取决于视觉模型的架构。
Anthropic (
claude) 模型的公式大致为:词元 =(以像素为单位的宽度 x 以像素为单位的高度)/ 750。Mistral (
pixtral) 模型将每张图像分成 16x16 像素的批次,并将每个批次转换为词元。词元总数大致等于(以像素为单位的宽度 / 16)*(以像素为单位的高度 / 16)。Meta (
llama) 模型尝试使用方形图块对图像进行平铺处理。根据图像的纵横比和大小,图块的数量最多可为 16 个,每个图块由大约 153 个词元表示。开源 AI 模型会对图像进行缩放,并使用方形图块进行平铺处理。对于
openai-gpt-4.1,根据图像比例和尺寸的不同,令牌数量可能为 211(适用于 512x512px 及以下图像)、352(长边为 1024px 的非方形图像),或从 630 个令牌(至少 1024x1024px 的方形图像)到 913 个令牌(短边为 1024px 的非方形图像)。对于openai-o4-mini,缩放逻辑更为复杂,令牌数量从 86 (128x512px) 到 1428 (2048x1024px) 不等,且不遵循线性规律。voyage-multimodal-3对图像块阵列进行操作,每个图像块尺寸约为 14x14 像素。图像经过缩放后会被网格覆盖,该网格最少包含 64 个图像块,最多可达 2500 个图像块。添加两个额外的图像令牌,因此输入令牌数量根据图像尺寸和比例的不同,范围在 66 至 2502 之间。
备注
COUNT_TOKENS 函数目前不支持图像输入。
选择视觉模型¶
COMPLETE 函数支持多种不同功能、延迟和成本的模型。为实现最佳的每 credit 性能表现,请根据内容大小和任务复杂性选择适配的模型。
模型 |
MMMU |
Mathvista |
ChartQA |
DocVQA |
VQAv2 |
|---|---|---|---|---|---|
GPT-4o |
68.6 |
64.6 |
85.1 |
88.9 |
77.8 |
|
75.0 |
72.0 |
|||
|
81.6 |
84.3 |
|||
|
68.0 |
64.4 |
87.6 |
90.3 |
70.7 |
|
73.4 |
73.7 |
90 |
94.4 |
|
|
69.4 |
70.7 |
88.8 |
94.4 |
|
|
64.0 |
69.4 |
88.1 |
85.7 |
67 |
基准是:
MMMU:对需要大学水平推理能力的多学科任务中的多模态模型进行评估。
Mathvista:视觉环境中的数学推理基准。
ChartQA:针对图表的复杂推理问题进行评估。
DocVQA 和 VQv2:针对文档的视觉问答的基准测试。
当前仅 voyage-multimodal-3 模型支持多模态嵌入。voyage-multimodal-3 作为先进的多模态嵌入模型,能够同时处理文本与图像的嵌入任务。该模型能从 PDFs 界面截图、幻灯片、表格及图表等来源中提取关键视觉特征,从而减少对复杂文档解析流程的依赖。根据 Voyage AI 内部基准测试,voyage-multimodal-3 模型在性能上超越 OpenAI CLIP Large、Amazon Titan Multimodal 及 Cohere Multimodal v3 等竞品模型。
区域可用性¶
以下 Snowflake 区域的账户原生支持此功能:
模型
|
AWS US 西部 2
(俄勒冈)
|
AWS US 东部 1
(弗吉尼亚北部)
|
AWS 欧洲中部 1
(法兰克福)
|
|---|---|---|---|
|
✔ |
✔ |
|
|
|||
|
|||
|
|||
|
✔ |
✔ |
✔ |
|
✔ |
||
|
✔ |
||
|
AI_COMPLETE 通过 跨区域推理 功能在更多地区开放使用。
错误条件¶
消息 |
解释 |
|---|---|
Request failed for external function SYSTEM$COMPLETE_WITH_IMAGE_INTERNAL with remote service error:400 '"invalid image path" |
模型不接受文件扩展名或文件本身。该消息还可能意味着文件路径不正确;也就是说,文件不存在于指定位置。文件名区分大小写。 |
Error in secure object |
可能表示该暂存区不存在。检查暂存区名称并确保该暂存区存在且可访问。请务必在暂存区路径的开头使用 @ 符号,例如 |
Request failed for external function _COMPLETE_WITH_PROMPT with remote service error:400 '"invalid request parameters: unsupported image format: image/** |
提供给 |
Request failed for external function _COMPLETE_WITH_PROMPT with remote service error:400 '"invalid request parameters:Image data exceeds the limit of 5.00 MB" |
提供给 |
法律声明¶
输入和输出的 Data Classification 如下表所示。
输入 Data Classification |
输出 Data Classification |
名称 |
|---|---|---|
Usage Data |
Customer Data |
正式发布的功能是涵盖的 AI 功能。预览版功能属于预览版 AI 功能。[1] |
有关更多信息,请参阅 Snowflake AI 和 ML。


