Sentiment Analysis Task¶
openaivec.task.nlp.sentiment_analysis ¶
Sentiment analysis task for OpenAI API.
This module provides a predefined task for sentiment analysis that analyzes sentiment and emotions in text using OpenAI's language models.
Example
Basic usage with BatchResponses:
from openai import OpenAI
from openaivec.responses import BatchResponses
from openaivec.task import nlp
client = OpenAI()
analyzer = BatchResponses.of_task(
client=client,
model_name="gpt-4o-mini",
task=nlp.SENTIMENT_ANALYSIS
)
texts = ["I love this product!", "This is terrible and disappointing."]
analyses = analyzer.parse(texts)
for analysis in analyses:
print(f"Sentiment: {analysis.sentiment}")
print(f"Confidence: {analysis.confidence}")
print(f"Emotions: {analysis.emotions}")
With pandas integration:
import pandas as pd
from openaivec import pandas_ext # Required for .ai accessor
from openaivec.task import nlp
df = pd.DataFrame({"text": ["I love this product!", "This is terrible and disappointing."]})
df["sentiment"] = df["text"].ai.task(nlp.SENTIMENT_ANALYSIS)
# Extract sentiment components
extracted_df = df.ai.extract("sentiment")
print(extracted_df[["text", "sentiment_sentiment", "sentiment_confidence", "sentiment_polarity"]])
Attributes:
Name | Type | Description |
---|---|---|
SENTIMENT_ANALYSIS |
PreparedTask
|
A prepared task instance configured for sentiment analysis with temperature=0.0 and top_p=1.0 for deterministic output. |