Snowflake Cortex Functions

Snowflake Cortex gives you instant access to industry-leading generative large language models (LLMs), including Meta’s LLaMA 2 model. It also offers models that Snowflake has fine-tuned for specific use cases.

Since these LLMs are fully hosted and managed by Snowflake Cortex, you always have access to them. Your data stays within Snowflake, giving you the performance, scalability, and governance you expect.

Snowflake Cortex features are provided as SQL functions. The available functions are summarized below, and a more detailed description of each function follows.

  • COMPLETE: Given a prompt, returns a response that completes the prompt.

  • SUMMARIZE: Returns a summary of the given text.

  • SENTIMENT: Returns a sentiment score, from -1 to 1, representing the detected overall positive or negative sentiment of the given text.

  • EXTRACT_ANSWER: Given a question and unstructured data, returns the answer to the question if it can be found in the data.

  • TRANSLATE: Translates given text from any supported language to any other.

  • EMBED_TEXT: Creates vectors (embeddings) from text documents, which can be used to evaluate the semantic similarity of two documents.

All Cortex SQL functions are in the SNOWFLAKE.CORTEX schema during the first phase of this preview. They were previously in the SNOWFLAKE.ML schema.

Update your search path to avoid having to specify the schema with every Cortex function call.

COMPLETE

Given a prompt, the instruction-following COMPLETE function generates a response using your choice of the llama2-70b-chat or llama2-7b-chat model.

SNOWFLAKE.CORTEX.COMPLETE(model, prompt) -- returns string
Copy
  • model: A string containing the name of the model to be used, either "llama2-70b-chat" or "llama2-7b-chat".

  • prompt: A string containing the prompt to be used to begin generating the response. In an interactive chat scenario, this would be something the user typed; here, it is a plain-English description of what you want.

For example, to generate a single response:

SELECT SNOWFLAKE.CORTEX.COMPLETE('llama2-7b-chat', 'What are large language models?');
Copy

To generate a response from each of a table (in this example, content is a column from the reviews table):

SELECT SNOWFLAKE.CORTEX.COMPLETE(
    'llama2-7b-chat',
        CONCAT('Critique this review in bullet points: <review>', content, '</review>')
) FROM reviews LIMIT 10;
Copy

As shown in this example, you can use tagging in the prompt to control the kind of response generated. See A guide to prompting LLaMA 2 for tips.

SUMMARIZE

The SUMMARIZE function returns a summary of the given English text.

SNOWFLAKE.CORTEX.SUMMARIZE(text) -- returns string
Copy
  • text: The text to be summarized.

To generate a summary for each row of a table (in this example, review_content is a column from the reviews table):

SELECT SNOWFLAKE.CORTEX.SUMMARIZE(review_content) FROM reviews LIMIT 10;
Copy

EXTRACT_ANSWER

The EXTRACT_ANSWER function extracts an answer to a question given in a text document.

SNOWFLAKE.CORTEX.EXTRACT_ANSWER(from_text, question) -- returns array
Copy
  • from_text: The text that may contain the answer to the question. The text may be a plain-English document or a string representation of a semi-structured data object.

  • question: The plain-English question to be answered. Be as specific as you can.

The result of this function is an array of answers, each an SQL object containing the following keys.

  • answer: The answer extracted by the model.

  • confidence: A floating-point value indicating the confidence level of the model in this answer.

  • id: A unique identifier for the answer.

Answers are ordered with the highest confidence first. It is possible for the answers array to be empty if no answer can be found, or for even the best answer to have a low confidence score.

To extract an answer from each row of a table (in this example, review_content is a column from the reviews table):

SELECT SNOWFLAKE.CORTEX.EXTRACT_ANSWER(review_content,
    'What dishes does this review mention?')
FROM reviews LIMIT 10;
Copy

SENTIMENT

The SENTIMENT function returns sentiment as a score between -1 to 1 (with -1 being the most negative and 1 the most positive, with 0 neutral) for the given English-language input text.

SNOWFLAKE.CORTEX.SENTIMENT(text) -- returns float
Copy
  • text: The text in which sentiment is to be detected.

For example, to score the sentiment of each row of a table (in this example, review_content is a column from the reviews table):

SELECT SNOWFLAKE.CORTEX.SENTIMENT(review_content) FROM reviews LIMIT 10;
Copy

TRANSLATE

The TRANSLATE function translates text from the indicated or detected source language to a target language.

SNOWFLAKE.CORTEX.TRANSLATE(text: string, from_language: enum, to_language: enum)  string
Copy
  • text: The text to be translated.

  • from_language: A language code from the list of supported languages indicating the original language of the text. If you pass an empty string instead of a language code, the TRANSLATE function attempts to detect the source language.

  • to_language: A language code from the the list of supported languages indicating the language to translate the text into.

To translate each row of a table (in this example, review_content is a column from the reviews table):

SELECT SNOWFLAKE.CORTEX.TRANSLATE(review_content, 'en', 'de') FROM reviews LIMIT 10;
Copy

참고

We are actively improving our translation models. Watch for improvements as this preview proceeds.

Supported Languages

The following languages are supported. Use the corresponding language code with the TRANSLATE function.

  • German: de

  • English: en

  • Spanish: es

  • French: fr

  • Italian: it

  • Japanese: ja

  • Korean: ko

  • Russian: ru

  • Polish: pl

  • Portuguese: pt

  • Swedish: sv

EMBED_TEXT

This function returns the vector embeddings for a given English-language text. This vector can be used with the vector comparison functions to determine the semantic similarity of two documents.

SNOWFLAKE.CORTEX.EMBED_TEXT(model, text) -- returns VECTOR
Copy
  • model: The language model to be used to generate the embeddings. Currently this must be 〈e5-base-v2〉.

  • text: The text for which an embedding should be calculated.

You can use other embedding models through Snowpark Container Services. For more information, see Embed Text Container Service.

Model Restrictions

Models used by Snowflake Cortex have limitations on size as described in the table below. Sizes are given in tokens. Tokens generally correspond to words, but not all tokens are words, so the number of words corresponding to a limit is slightly less than the number of tokens. Inputs that exceed the limit are truncated to the permitted size.

Function

Model

Context window (tokens)

COMPLETE

llama2-7b-chat *

4096

llama2-70b-chat *

4096

SUMMARIZE

llama2-7b-chat *

4096

SENTIMENT

Snowflake fine-tuned model (RoBERTa based)

512

EXTRACT_ANSWER

Snowflake fine-tuned model (BERT based)

2048 for text
64 for question

TRANSLATE

Snowflake fine-tuned model (MBart)

1024

EMBED_TEXT

E5-base-v2

512

* Both input and output count toward the token limit, and by default output is limited to 512 tokens. You can change this using the optional max_tokens parameter.

Cost Considerations

During the initial phase of this preview, usage of Snowflake Cortex is free but limited. The per-account daily limits for each function are described in the table below.

Function

Daily Limit

COMPLETE (llama2-70b-chat)

10,000 rows

COMPLETE (llama2-7b-chat)

100,000 rows

SUMMARIZE

100,000 rows

EXTRACT_ANSWER

500,000 rows

SENTIMENT

500,000 rows

TRANSLATE

500,000 rows

EMBED_TEXT

100,000 rows

After the initial phase (duration to be determined), using Snowflake Cortex will incur charges based on usage. The end of free usage will be announced well in advance along with full pricing details.

Upcoming Enhancements

We intend to deliver the following features and others in a future version of this preview. This intention does not, however, constitute a commitment to deliver any feature on any particular schedule, or at all.

  • Python API, allowing the Cortex LLMs to be used from the preferred programming language of machine learning.

  • REST API, allowing the Cortex LLMs to be used from any programming language that can make a RESTful Web services call.

  • A text embedding function, allowing text documents to be compared for semantic similarity (see Vector Similarity).