Bring-your-own model for AI_ COMPLETE¶
Generates a response (completion) using a model that you deploy as a Snowpark Container Services (SPCS) model inference service, instead of a Snowflake-hosted Cortex model. The prompt can be a text string, a text string with a single FILE object, or a prompt object that contains text and FILE objects. FILE objects can contain images or text documents.
To use this feature, set the model argument to the name of an SPCS model inference service that hosts a text generation model or a vision-language (image-text-to-text) model.
Prerequisites¶
Before you can use this feature, log a text generation or vision-language model from Hugging Face using Hugging Face pipeline, then deploy it as an SPCS model inference service using the Snowsight UI or the Python API. For more examples on creating SPCS inference services, see Example workflows.
To process files, store them on an internal stage with server-side encryption enabled. To process all files on a stage in a single query, also enable a directory table on the stage:
Syntax¶
The function accepts the same arguments as AI_COMPLETE (Single string), AI_COMPLETE (Single image), and AI_COMPLETE (Prompt object). The only change is that the model argument is the name of an SPCS service instead of the name of a Cortex model.
The function can be used with either positional or named argument syntax.
Using AI_COMPLETE with a single string input:
Using AI_COMPLETE with a single file input:
Using AI_COMPLETE with a prompt object:
Arguments¶
modelA string specifying the name of an SPCS model inference service that hosts a text generation or vision-language model from Hugging Face.
To process images, the service must host a vision-language model that accepts image input, such as a Qwen3-VL model.
promptA string prompt, or a prompt object created with the PROMPT function. The prompt object’s template can contain numbered placeholders (
{0},{1}, and so on) that correspond to text strings or FILE objects created with TO_FILE.predicateFor the single file input, a string prompt. When you use named argument syntax, the argument name is
predicate, notprompt.fileFor the single file input, a FILE object created with TO_FILE, or a column of type FILE.
model_parametersAn object containing zero or more options that affect the model’s hyperparameters, such as
temperature,top_p, andmax_tokens. For descriptions of these options, see Arguments.The
guardrailsoption isn’t supported.response_formatThe format that the response should follow, specified as a JSON schema or a SQL type literal. For more information, see Arguments.
show_detailsA boolean flag that indicates whether to return a serialized JSON object containing the response and information about it. For more information, see Arguments.
Returns¶
By default, returns a string containing the model’s response.
When the response_format argument is specified, returns an object that follows the provided format. When the show_details argument is set to TRUE, returns a JSON object containing the response and inference metadata. For full details on each return shape, see Returns.
Error behavior¶
The function follows the same error behavior as the AI_COMPLETE function. For the return values for the return_error_details argument, see Error behavior.
Specifying the guardrails option in the model_parameters argument causes the entire query to fail with an error, regardless of the return_error_details setting.
Examples¶
The examples in this section set a SQL variable to the name of the SPCS service:
To confirm that the service exists, describe it:
The responses shown in these examples were generated by a Qwen3-VL-8B model. Responses vary by model and between runs.
Single response¶
To generate a single response:
Responses from a table column¶
The following example generates a response for each row in the prompts table, using the q column as input:
Controlling model parameters¶
The following example specifies the model_parameters used to provide a response:
Using a prompt template¶
The following example uses a prompt object with text arguments:
Visual question answering with a single image¶
The following example passes one image with a text prompt, using the single file input:
Response:
With named argument syntax, the prompt argument is named predicate:
Entity extraction from an image¶
The following example extracts the objects in an image and returns the results in JSON format:
Response:
Passing multiple images¶
To pass more than one file in the same prompt, use multiple numbered placeholders in the prompt template:
Combining text and file arguments¶
A prompt object can combine text strings and FILE objects:
Response:
Extracting information from a text document¶
The following example lists the statistics in a text file:
Response:
You can ask questions about CSV, JSON, and HTML files the same way:
Batch processing files from a directory or table¶
The following example classifies every file on a stage by retrieving the file paths from the stage’s directory table. PDF files aren’t supported, so the query excludes them:
Response:
If you have a table with a column of type FILE, you can pass the column in the single file input or in a prompt object:
Joining with a table¶
The following example builds a prompt from the columns of each row in the customers table:
Usage notes for processing images¶
-
To process images, the SPCS service must host a vision-language model. A text-only model can’t interpret image content.
-
Supported image formats:
.jpg.png.gif.webp
-
To process multiple images, specify a prompt object that contains a numbered placeholder for each image. Use the TO_FILE function to specify the image files.
-
The stage containing the images must have server-side encryption enabled. Client-side encrypted stages are not supported.
-
Stage names are case-insensitive; paths are case-sensitive.
Usage notes for processing documents¶
-
Supported document formats:
.txt.csv.json.html
-
PDF files are not supported. When you process all files on a stage, exclude PDF files, as shown in Batch processing files from a directory or table.
-
The stage containing the documents must have server-side encryption enabled. Client-side encrypted stages are not supported.
-
Stage names are case-insensitive; paths are case-sensitive.
Limitations¶
The guardrails option (Cortex Guard) in the model_parameters argument isn’t supported. A query that specifies guardrails fails with an error.
Access control requirements¶
The user must have the USAGE privilege on the SPCS service and on its inference function. To process staged files, the user must also have the READ privilege on the stage that contains the files.
Legal notices¶
Refer to Snowflake Cortex AI Functions (including LLM functions).