Using Cortex AI Functions with Python and Snow CLI¶
You can call Cortex AI Functions from Python (via the Snowpark Python API or Snowflake ML) and from the Snowflake CLI. Use one of these entry points when you want to integrate AI Functions into application code or scripts rather than running them as SQL statements.
Call Cortex AI Functions in Snowpark Python¶
You can use Snowflake Cortex AI Functions in the Snowpark Python API. These functions include the following. Note that the functions in Snowpark Python have names in Pythonic “snake_case” format, with words separated by underscores and all letters in lowercase.
ai_agg example¶
The ai_agg function aggregates a column of text using natural language instructions in a similar manner to how you would ask an analyst to summarize or extract findings from grouped or ungrouped data.
The following example summarizes customer reviews for each product using the ai_agg function. The function takes a column of text and a natural language instruction to summarize the reviews.
Note
Use task descriptions that are detailed and centered on the use case. For example, “Summarize the customer feedback for an investor report”.
Classify text with ai_classify¶
The ai_classify function takes a string or image and classifies it into the categories that you define.
The following example classifies travel reviews into categories such as “travel” and “cooking”. The function takes a column of text and a list of categories to classify the text into.
Filter rows with ai_filter¶
The ai_filter function evaluates a natural language condition and returns True or False. You can use it to filter or tag rows.
Note
You can filter on both strings and files. For dynamic prompts, use the prompt function.
For more information, see
Snowpark Python reference.
Call Cortex AI Functions in Snowflake ML¶
Snowflake ML contains the older AI Functions, those with names that don’t begin with “AI”. These functions are supported in version 1.1.2 and later of Snowflake ML. The names are rendered in Pythonic “snake_case” format, with words separated by underscores and all letters in lowercase.
If you run your Python script outside of Snowflake, you must create a Snowpark session to use these functions. See Connecting to Snowflake for instructions.
Process single values¶
The following Python example illustrates calling Snowflake Cortex AI functions on single values:
Pass hyperparameter options¶
You can pass options that affect the model’s hyperparameters when using the complete function. The following
Python example illustrates modifying the maximum number of output tokens that the model can generate:
Call functions on table columns¶
You can call an AI function on a table column, as shown below. This example requires a session object (stored in
session) and a table articles containing a text column abstract_text, and creates a new column
abstract_summary containing a summary of the abstract.
Note
The advanced chat-style (multi-message) form of COMPLETE is not currently supported in Snowflake ML Python.
Using Snowflake Cortex AI functions with Snowflake CLI¶
Snowflake Cortex AI Functions are available in Snowflake CLI version 2.4.0 and later. See Introducing Snowflake CLI for more information about using Snowflake CLI. The functions are the old-style functions, those with names that don’t begin with “AI”.
The following examples illustrate using the snow cortex commands on single values. The -c parameter specifies which connection to use.
Note
The advanced chat-style (multi-message) form of COMPLETE is not currently supported in Snowflake CLI.
You can also use files that contain the text you want to use for the commands. For this example, assume that the file about_cortex.txt contains the following content:
You can then execute the snow cortex summarize command by passing in the filename using the --file parameter, as shown:
For more information about these commands, see snow cortex commands.