Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The ai.analyze_sentiment function uses generative AI to detect the emotional state expressed by input text. It uses only a single line of code. It can detect whether the emotional state of the input text is positive, negative, mixed, or neutral. If the function can't determine the sentiment, it leaves the output blank.
AI functions improve data engineering by using the power of large language models in Microsoft Fabric. To learn more, see this overview article.
Important
This feature is in preview, for use in Fabric Runtime 1.3 and later.
- Review the prerequisites in this overview article, including the library installations that are temporarily required to use AI functions.
- By default, the gpt-4o-mini model currently powers AI functions. Learn more about billing and consumption rates.
- Although the underlying model can handle several languages, most of the AI functions are optimized for use on English-language texts.
- During the initial rollout of AI functions, users are temporarily limited to 1,000 requests per minute with the built-in AI endpoint in Fabric.
Use ai.analyze_sentiment with pandas
The ai.analyze_sentiment function extends the pandas Series class. To detect the sentiment of each input row, call the function on a pandas DataFrame text column.
The function returns a pandas Series that contains sentiment labels, which can be stored in a new column of the DataFrame.
Syntax
df["sentiment"] = df["text"].ai.analyze_sentiment()
Parameters
None
Returns
The function returns a pandas Series that contains sentiment labels for each input text row. The sentiment labels include positive, negative, neutral, or mixed. If a sentiment can't be determined, the return value is null.
Example
# This code uses AI. Always review output for mistakes.
# Read terms: https://azure.microsoft.com/support/legal/preview-supplemental-terms/.
df = pd.DataFrame([
"The cleaning spray permanently stained my beautiful kitchen counter. Never again!",
"I used this sunscreen on my vacation to Florida, and I didn't get burned at all. Would recommend.",
"I'm torn about this speaker system. The sound was high quality, though it didn't connect to my roommate's phone.",
"The umbrella is OK, I guess."
], columns=["reviews"])
df["sentiment"] = df["reviews"].ai.analyze_sentiment()
display(df)
Use ai.analyze_sentiment with PySpark
The ai.analyze_sentiment function is also available for Spark DataFrames. You must specify the name of an existing input column as a parameter.
The function returns a new DataFrame, with sentiment labels for each input text row stored in an output column.
Syntax
df.ai.analyze_sentiment(input_col="text", output_col="sentiment")
Parameters
| Name | Description |
|---|---|
input_col Required |
A string that contains the name of an existing column with input text values to analyze for sentiment. |
output_col Optional |
A string that contains the name of a new column to store the sentiment label for each row of input text. If you don't set this parameter, a default name generates for the output column. |
error_col Optional |
A string that contains the name of a new column to store any OpenAI errors that result from processing each row of input text. If you don't set this parameter, a default name generates for the error column. If an input row has no errors, the value in this column is null. |
Returns
The function returns a Spark DataFrame that includes a new column that contains sentiment labels that match each row of text in the input column. The sentiment labels include positive, negative, neutral, or mixed. If a sentiment can't be determined, the return value is null.
Example
# This code uses AI. Always review output for mistakes.
# Read terms: https://azure.microsoft.com/support/legal/preview-supplemental-terms/.
df = spark.createDataFrame([
("The cleaning spray permanently stained my beautiful kitchen counter. Never again!",),
("I used this sunscreen on my vacation to Florida, and I didn't get burned at all. Would recommend.",),
("I'm torn about this speaker system. The sound was high quality, though it didn't connect to my roommate's phone.",),
("The umbrella is OK, I guess.",)
], ["reviews"])
sentiment = df.ai.analyze_sentiment(input_col="reviews", output_col="sentiment")
display(sentiment)
Related content
- Calculate similarity with
ai.similarity. - Categorize text with
ai.classify. - Extract entities with
ai_extract. - Fix grammar with
ai.fix_grammar. - Summarize text with
ai.summarize. - Translate text with
ai.translate. - Answer custom user prompts with
ai.generate_response. - Learn more about the full set of AI functions.
- Customize the configuration of AI functions.
- Did we miss a feature you need? Suggest it on the Fabric Ideas forum.