Categories:

String & binary functions (Large Language Model)

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> ] )
Copy

Arguments¶

model

A string specifying the model to be used. For text only inputs, you can use one of the following models:

  • 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

For image inputs, you can use one of the following models:
  • claude-4-opus

  • claude-4-sonnet

  • claude-3-7-sonnet

  • claude-3-5-sonnet

  • llama-4-maverick

  • llama-4-scout

  • pixtral-large

Supported models might have different costs.

prompt

A 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 to temperature. The difference is that top_p restricts the set of possible tokens that the model outputs, while temperature influences 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. Either TRUE or FALSE. The default value is FALSE.

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
FROM image_table;
Copy

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));
Copy

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;
Copy

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;
Copy

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);
Copy

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;
Copy

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 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

    • .webp

    • The pixtral and llama4 models also support .bmp.

  • The maximum image size is 10 MB for most models, and 3.75 MB for claude models. claude models 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.