Sentiment analysis
Sentiment analysis is a common task in natural language processing (NLP) that involves determining the sentiment or emotional tone of a piece of text. This can be useful for various applications, such as social media monitoring, customer feedback analysis, and more.
The following examples, which closely inspired by the Claude documentation, hint at some of the ways you can use structured data extraction.
#| warning: false
import chatlas as ctl
from pydantic import BaseModel, Field
= "The product was okay, but the customer service was terrible. I probably won't buy from them again."
text
class Sentiment(BaseModel):
"""Extract the sentiment scores of a given text. Sentiment scores should sum to 1."""
float = Field(
positive_score: ="Positive sentiment score, ranging from 0.0 to 1.0"
description
)
float = Field(
negative_score: ="Negative sentiment score, ranging from 0.0 to 1.0"
description
)
float = Field(
neutral_score: ="Neutral sentiment score, ranging from 0.0 to 1.0"
description
)
= ctl.ChatOpenAI()
chat =Sentiment) chat.extract_data(text, data_model
{"positive_score": 0.1, "negative_score": 0.7, "neutral_score": 0.2}
The data model does specify that the scores should sum to 1, but this is not enforced by the model. The model will do its best to provide a reasonable output, but it may not always adhere to this constraint.