- Categories:
String & binary functions (AI Functions)
AI_ SENTIMENT¶
AI_SENTIMENT provides sentiment classification across diverse markets and languages. It uses a custom Snowflake large language model to return overall sentiment for a piece of text and, optionally, aspect-based (also called category-based or entity-based) sentiment for specific topics that you specify.
Common use cases include:
- Social media monitoring
- Detailed product analysis
- Comprehensive brand perception studies
- Advanced market intelligence
- Employee engagement analysis
- Customer experience journey mapping
- Content performance analysis
- Customer support optimization
Syntax¶
Arguments¶
Required:
textA string containing the text in which sentiment is detected.
Optional:
categoriesAn array containing up to ten categories (also called entities or aspects) for which sentiment should be extracted. Each category is a string. For example, if extracting sentiment from a restaurant review, you might specify
['cost', 'quality', 'service', 'wait time']as the categories. Each category may be a maximum of 30 characters long.If you do not provide this argument, AI_SENTIMENT returns only the overall sentiment.
return_error_detailsA BOOLEAN flag that indicates whether to return error details in case of error. When set to TRUE, the function returns an OBJECT that contains the value and the error message, one of which is NULL depending on whether the function succeeded or failed. See Error behavior for details.
Returns¶
An OBJECT value containing a categories field. categories is an array of category records. Each category includes these fields:
name: The name of the category. The category names match the categories specified in thecategoriesargument.sentiment: The sentiment of the category. Each sentiment result is one of the following strings.unknown: The category was not mentioned in the text.positive: The category was mentioned positively in the text.negative: The category was mentioned negatively in the text.neutral: The category was mentioned in the text, but neither positively nor negatively.mixed: The category was mentioned both positively and negatively in the text.
The overall category record is always included and contains the overall sentiment of the text.
Example:
Error behavior¶
By default, if AI_SENTIMENT can’t process the input, the function returns NULL. If the query processes multiple rows, rows with errors return NULL and don’t prevent the query from completing.
The return value on error depends on the return_error_details
argument. The following table shows the return value based on the return_error_details argument:
return_error_detailsReturn value Description FALSE Not passed NULL TRUE OBJECT with valueanderrorfieldsvalue: An OBJECT containing the sentiment analysis result, or NULL if an error occurred.error: A VARCHAR value that contains the error message if an error occurred, or NULL if the function succeeded.
For more information about error handling for AI functions, see Snowflake Cortex AI Function: Multirow error handling improvements.
Access control requirements¶
Users must use a role that has been granted the SNOWFLAKE.CORTEX_USER database role. See Cortex LLM privileges for more information on this role.
Usage notes¶
- Supported languages: AI_SENTIMENT can analyze sentiment in English, French, German, Hindi, Italian, Spanish, and Portuguese. Aspects can be specified in the language of the text or in English.
- Context window: AI_SENTIMENT is optimized for text inputs of 2,048 tokens (roughly 1,600 words). Inputs that exceed the context window return an error; outputs that would exceed the context window are truncated.
- Aspect limits: You can specify a maximum of ten aspects, each no longer than 30 characters.
| Function | Context window (tokens) | Maximum number of entity labels |
|---|---|---|
| AI_SENTIMENT | 2,048 | 10 |
Examples¶
Get the overall sentiment of text¶
The following example uses AI_SENTIMENT to get the overall sentiment of a product review.
Return value:
Get aspect-based sentiment¶
Specify one or more aspects to get aspect-based sentiment in addition to the overall sentiment. The following example uses AI_SENTIMENT to get the sentiment classification for specific aspects of a restaurant review.
Return value:
If some aspects that you specify do not apply to the text you provide, AI_SENTIMENT returns unknown for those aspects,
as shown for Professionalism and Brand in the following example.
Return value:
Analyze multiple reviews from a table¶
In this example, a table named reviews contains a column named review_content containing the text of movie reviews
submitted by users. The query returns the sentiment of several facets of up to ten reviews.
Multilingual sentiment analysis¶
AI_SENTIMENT can analyze sentiment in multiple languages, so you don’t need to translate the text and risk losing an essential part of its meaning. You do not need to specify the language of the text. Aspects can be specified in the language of the text or in English.
The following example has both text and labels in Spanish:
Return value:
The following example has text in German and labels in English:
Return value:
Regional availability¶
AI_SENTIMENT is available in the following regions:
| Function (Model) | AWS US West 2 (Oregon) | AWS US East 1 (N. Virginia) | AWS Europe Central 1 (Frankfurt) | AWS Europe West 1 (Ireland) | AWS AP Southeast 2 (Sydney) | AWS AP Northeast 1 (Tokyo) | Azure East US 2 (Virginia) | Azure West Europe (Netherlands) | AWS (Cross-Region) |
|---|---|---|---|---|---|---|---|---|---|
| AI_SENTIMENT | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
Note
AI_SENTIMENT is the updated version of ENTITY_SENTIMENT. For the latest functionality, use AI_SENTIMENT.
Legal notices¶
Refer to Snowflake AI and ML.