Using SQL to automatically generate object descriptions¶
The Cortex Powered Object Descriptions feature allows you to use the
Snowflake Cortex COMPLETE function to automatically generate descriptions for
tables, views, and columns. This feature leverages Snowflake-hosted large language models (LLMs) to evaluate object metadata and, if
desired, sample data to generate a description.
This topic describes how to use a stored procedure to generate descriptions programmatically. For
information about using Snowsight to generate the descriptions, see Generate descriptions with Snowflake Cortex.
The AI_GENERATE_TABLE_DESC stored procedure automatically generates a description for a table and
view. It can also generate descriptions for the columns of that table or view.
The AI_GENERATE_TABLE_DESC stored procedure accepts two arguments:
The name of the table or view that you want to generate a description for.
An optional configuration object that allows you to do the following:
Generate descriptions for the columns of the specified table or view.
Use sample data from the table or view to potentially improve the accuracy of the column descriptions.
Example: Generate a table description
CALLAI_GENERATE_TABLE_DESC('my_table');
Example: Generate table and column descriptions without using sample data
The AI_GENERATE_TABLE_DESC stored procedure returns a JSON object that contains the generated descriptions along with general
information about the table and columns. Within this object, the description field contains the generated description.
Suppose you created the following table:
CREATEORREPLACETABLE mydb.sch1.hr_data (fname VARCHAR, age INTEGER);INSERTINTO hr_data (fname, age)VALUES('Thomas',44),('Katherine',29),('Lisa',29);
Given this table, the following is an example of the JSON object returned by AI_GENERATE_TABLE_DESC:
{"COLUMNS":[{"database_name":"mydb","description":"The first name of the employee.","name":"FNAME","schema_name":"sch1","table_name":"hr_data"}{"database_name":"mydb","description":"A column holding data of type DecimalType representing age values.","name":"AGE","schema_name":"sch1","table_name":"hr_data"},],"TABLE":[{"database_name":"mydb","description":" The table contains records of employee data, specifically demographic information. Each record includes an employee's age and name.","name":"hr_data","schema_name":"sch1"}]}
For more information about each JSON field, see Returns.
To set a generated description as a comment on a table, view, or column, you must manually execute a SQL statement that includes the
SET COMMENT parameter. For example, to save a generated description for a table t1, execute
ALTER TABLE t1 SET COMMENT = 'ai generated description';.
You can write custom code to automatically generate and save descriptions. For examples of stored procedures that do this, see
Examples.
Your region must support the LLM used by Snowflake Cortex (like Mistral-7b and Llama 3.1-8b) to generate the descriptions. Check the
availability of the COMPLETE function. If the COMPLETE function is not supported in your region, you
must enable cross-region inference to use the feature.
When generating a description for a column, you can rely only on metadata, or you can choose to use sample data to
improve the Snowflake Cortex Powered Description. Sample data refers to data within a particular column that is evaluated when you
use Snowflake Cortex to generate descriptions. If you choose to use sample data, Snowflake uses a portion of the sample data to generate the
description, which leads to more accurate descriptions. Sample data is not stored by Snowflake as Usage Data.
Generating descriptions incurs the following costs:
Credits consumed by the warehouse in use.
Credits charged for the use of Snowflake Cortex with smaller LLMs like Mistral-7b and Llama 3.1-8b. These charges appear on a bill as
AI-Services, which includes all uses of Snowflake Cortex.
This feature relies on the COMPLETE function to generate a recommended object description. When the user initiates the description
generation, Usage Data may be collected through the COMPLETE function.
The generated description is not retained by Snowflake until it is saved by the user.