Introduction
In our previous article, we explored several Snowflake LLM functions, including SENTIMENT and EXTRACT_ANSWER, to gain insight into their capabilities and use cases.
In this article, we continue our journey by exploring additional LLM functions, highlighting their practical applications and how they can enhance data-driven workflows within Snowflake.
Link to article Part 1: https://www.c-sharpcorner.com/article/transforming-data-insights-with-snowflakes-llm-functions-part-1/
Lab Setup
This lab setup is identical to the one we used in the previous article.
Let’s set up the environment before using the LLM function.
Step 1. Set the Role.
USE ROLE accountadmin;
Step 2. Set the Warehouse.
USE WAREHOUSE compute_wh;
ALTER ACCOUNT
SET CORTEX_ENABLED_CROSS_REGION = 'us-east-1';
Step 3. Create a database.
CREATE DATABASE IF NOT EXISTS llmtest_db;
Step 4. Create a schema in a database.
CREATE SCHEMA IF NOT EXISTS llmtest_db.data;
Step 5. Create a stage to hold data for sentiment analysis.
CREATE STAGE IF NOT EXISTS llmtest_db.data.reviews
DIRECTORY = (
ENABLE = TRUE,
REFRESH_ON_CREATE = TRUE
);
Step 6. Switch to the schema that was created in the previous steps.
USE SCHEMA llmtest_db.data;
1. TRANSLATE
The TRANSLATE function in Snowflake Cortex utilizes an integrated large language model (LLM) to perform multilingual text translation directly within the Snowflake environment. This eliminates the need for external translation services or APIs (e.g., Google Translate), enabling low-latency, in-database language processing at scale.
It can be used for various purposes, such as enabling multi-language analytics and translating user inputs into another language.
Syntax
TRANSLATE(input_text, source_lang, target_lang)
- Input_text: The original text to translate.
- Source_lang: The language code of the input (e.g., 'en').
- Target_lang: The language code to translate to (e.g., 'fr').
Let's understand this concept with an example, where we will translate text from English to French and vice versa.
Set text strings for English and French to variables.
SET VAR_TRANSLATE_TEXT_EN = 'Good Morning, Have a nice day';
SET VAR_TRANSLATE_TEXT_FR = 'Bonjour, comment ça va?';
Verify that the variables are set correctly.
SHOW VARIABLES LIKE '%TRANSLATE%';
![Variables]()
Let’s translate English text to French using the SQL query below.
SELECT
snowflake.cortex.TRANSLATE($VAR_TRANSLATE_TEXT_EN, 'en', 'fr') AS Text_French;
Output
![SQL Server]()
Now, let's translate the French text to English using the SQL query below.
SELECT
snowflake.cortex.TRANSLATE($VAR_TRANSLATE_TEXT_FR, 'fr', 'en') AS Text_English;
Output
![French Text]()
Now, let's translate the French text into Italian.
SELECT
snowflake.cortex.TRANSLATE($VAR_TRANSLATE_TEXT_FR, 'fr', 'it') AS Text_Italian;
![Text into Italian]()
You can also create a generic table with multiple language codes that can be leveraged to convert input text to various languages in an effortless manner.
Create a temporary table for language codes.
CREATE OR REPLACE TEMPORARY TABLE temp_language (
language VARCHAR,
code VARCHAR
);
Let’s insert a few language codes and their names.
INSERT INTO temp_language (LANGUAGE, CODE) VALUES
('English', 'en'),
('French', 'fr'),
('German', 'de'),
('Italian', 'it');
Verify the table contents.
SELECT * FROM temp_language;
![Table contents]()
Now, we can translate English text into multiple languages using the SQL query below.
SELECT
language,
code,
snowflake.cortex.TRANSLATE($VAR_TRANSLATE_TEXT_EN, 'en', code) AS translation_text
FROM
temp_language;
![Multiple languages]()
2. SUMMARIZE
The SUMMARIZE function in Snowflake Cortex is a task-specific, fully managed SQL function that leverages integrated large language models (LLMs) to generate concise summaries of input text directly within the Snowflake environment.
It can be leveraged for use cases such as summarizing reviews, meeting transcripts, and large amounts of unstructured text.
SYNTAX
SNOWFLAKE.CORTEX.SUMMARIZE(input_text);
input_text: A string containing the English-language input text to be summarized.
Let's understand it better with an example below, where a paragraph is provided as input text to summarize.
Example
SELECT snowflake.cortex.SUMMARIZE(
'A data mesh is an architectural framework that solves advanced data security challenges
through distributed, decentralized ownership. Organizations have multiple data sources
from different lines of business that must be integrated for analytics. A data mesh
architecture effectively unites the disparate data sources and links them together through
centrally managed data sharing and governance guidelines. Business functions can maintain
control over how shared data is accessed, who accesses it, and in what formats it’s accessed.
A data mesh adds complexities to architecture but also brings efficiency by improving data
access, security, and scalability.'
) AS summary;
Output
![Output]()
Text Output
A data mesh is an architectural framework that addresses advanced data security challenges through decentralized ownership. It unites disparate data sources for analytics in organizations and allows business functions to manage access and formats. It adds complexity but improves data access, security, and scalability.
Summary
Snowflake Cortex AI functions can be seamlessly integrated into SQL workflows to enhance data processing and analytical capabilities. By leveraging built-in large language models (LLMs), users can perform advanced natural language tasks—such as summarization, translation, sentiment analysis, and classification—directly within the Snowflake environment. This enables scalable, low-latency, and secure AI-powered data enrichment without the need for external services or infrastructure, streamlining complex analytics and accelerating insights across diverse datasets.