- Categories:
String & binary functions (AI Functions)
AI_COMPLETE (Prompt object)¶
Note
AI_COMPLETE is the updated version of COMPLETE (SNOWFLAKE.CORTEX). For the latest functionality, use AI_COMPLETE.
Generates a response (completion) for a prompt object. The prompt object references one or more columns containing text or image data.
Syntax¶
The function can be used with either positional or named argument syntax.
AI_COMPLETE(
<model>, <prompt> [ , <model_parameters> ] )
Arguments¶
modelA string specifying the model to be used. For text only inputs, you can use one of the following models:
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
- For image inputs, you can use one of the following models:
claude-4-opusclaude-4-sonnetclaude-3-7-sonnetclaude-3-5-sonnetllama4-maverickllama4-scoutopenai-o4-miniopenai-gpt-4.1pixtral-large
Supported models might have different costs.
promptA prompt object containing text and/or images
model_parameters
An object containing zero or more of the following options that affect the model’s
hyperparameters. See LLM Settings.
temperature: A value from 0 to 1 (inclusive) that controls the randomness of the output of the language model. A higher temperature (for example, 0.7) results in more diverse and random output, while a lower temperature (such as 0.2) makes the output more deterministic and focused.Default: 0
top_p: A value from 0 to 1 (inclusive) that controls the randomness and diversity of the language model, generally used as an alternative totemperature. The difference is thattop_prestricts the set of possible tokens that the model outputs, whiletemperatureinfluences which tokens are chosen at each step.Default: 0
max_tokens: Sets the maximum number of output tokens in the response. Small values can result in truncated responses.Default: 4096 Maximum allowed value: 8192
guardrails: Filters potentially unsafe and harmful responses from a language model using Cortex Guard. EitherTRUEorFALSE. The default value isFALSE.
Important
If you’re using AI_COMPLETE with a prompt object, you can’t provide a JSON schema to get a structured output as a response.
To get a structured output as the response, use the response_format parameter with AI_COMPLETE (Single string). For more information using structured outputs, see AI_COMPLETE structured outputs.
Example¶
Passing multiple images as the input¶
The following example compares two images by passing both as input to the AI_COMPLETE function and asking whether both are pictures of cats:
SELECT AI_COMPLETE('claude-3-5-sonnet',
PROMPT('Are both image {0} and image {1} pictures of cats?',
TO_FILE('@myimages', 'sleepingcat.png'), TO_FILE('@myimages', 'jumpingcat.png'))) AS image_classification;
Batch processing images from a directory or table¶
For batch processing of multiple images, performing the same operation on each, store the image files in the same stage. Apply the AI_COMPLETE function to each row of the table.
Note
The stage must have a directory table to retrieve the paths to its files.
First, create the table by retrieving the image locations from the directory, convert these to FILE objects, and storing the resulting FILE objects in a column in a table. Use SQL like the following:
CREATE TABLE image_table AS
(SELECT TO_FILE('@myimages', RELATIVE_PATH) AS img FROM DIRECTORY(@myimages));
Then, apply the AI_COMPLETE function to the column containing the FILE objects. The following example classifies each image in the table:
SELECT AI_COMPLETE('claude-3-5-sonnet',
PROMPT('Classify the input image {0} in no more than 2 words. Respond in JSON', img_file)) AS image_classification
FROM image_table;
Response:
{ "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 AI_COMPLETE('claude-3-5-sonnet',
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;
You can also retrieve the images to be processed directly from a stage’s directory, as shown here:
SELECT AI_COMPLETE('claude-3-5-sonnet',
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);
Providing images and prompts in a table¶
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.
AI_COMPLETE('claude-3-5-sonnet',
PROMPT('Given the input image {0}, {1}. Respond in JSON',
TO_FILE('@myimages', img_path), prompt) as image_result)
FROM image_table;
Usage notes for processing images¶
To process multiple images, specify a prompt object in the function call that defines a prompt template and the associated image files. You can use the PROMPT function to create this object. The prompt template can contain numbered placeholders (
{0},{1}, etc.) that correspond to the images in the prompt object.Only text and images are supported. Video and audio files are not supported.
Supported image formats:
.jpg.jpeg.png.gif.webpThe
pixtralandllama4models also support.bmp.
The maximum image size is 10 MB for most models, and 3.75 MB for
claudemodels.claudemodels do not support images with resolutions above 8000x8000.The stage containing the images must have server-side encryption enabled. Client-side encrypted stages are not supported.
The function does not support custom network policies.
Stage names are case-insensitive; paths are case-sensitive.