Bases: Module
An agent that uses the ChatCompletion module
to analyze the sentiment of input messages. The sentiment is classified into
three categories: positive, neutral, or negative.
Examples:
>>> import os
>>>
>>> import afnio
>>> import afnio.utils.agents as agents
>>> from afnio.models.openai import AsyncOpenAI
>>>
>>> os.environ["OPENAI_API_KEY"] = "sk-..." # Replace with your actual key
>>>
>>> fwd_model = AsyncOpenAI()
>>> agent = agents.SentimentAnalyzer()
>>>
>>> response = agent(
... fwd_model,
... inputs={"message": "I've been a satisfied client of ProCare for a year"},
... model="gpt-4.1-nano",
... temperature=0.0,
... )
>>> print(response.data)
{"sentiment":"positive"}
Source code in afnio/utils/agents/sentiment_analyzer.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 | class SentimentAnalyzer(cog.Module):
"""
An agent that uses the [`ChatCompletion`][afnio.cognitive.ChatCompletion] module
to analyze the sentiment of input messages. The sentiment is classified into
three categories: positive, neutral, or negative.
Examples:
>>> import os
>>>
>>> import afnio
>>> import afnio.utils.agents as agents
>>> from afnio.models.openai import AsyncOpenAI
>>>
>>> os.environ["OPENAI_API_KEY"] = "sk-..." # Replace with your actual key
>>>
>>> fwd_model = AsyncOpenAI()
>>> agent = agents.SentimentAnalyzer()
>>>
>>> response = agent(
... fwd_model,
... inputs={"message": "I've been a satisfied client of ProCare for a year"},
... model="gpt-4.1-nano",
... temperature=0.0,
... )
>>> print(response.data)
{"sentiment":"positive"}
""" # noqa: E501
def __init__(self):
super().__init__()
self.sentiment_task = cog.Parameter(
data="Read the provided message and determine the sentiment.",
role="system prompt for sentiment classification",
requires_grad=True,
)
self.sentiment_user = afnio.Variable(
data="**Message:**\n\n{message}\n\n",
role="input template to sentiment classifier",
)
self.sentiment_classifier = cog.ChatCompletion()
def forward(
self,
fwd_model: Optional[ChatCompletionModel],
inputs: Optional[Dict[str, Union[str, Variable]]] = None,
**completion_args,
):
sentiment_messages = [
{"role": "system", "content": [self.sentiment_task]},
{"role": "user", "content": [self.sentiment_user]},
]
return self.sentiment_classifier(
fwd_model,
sentiment_messages,
inputs=inputs,
response_format=SENTIMENT_RESPONSE_FORMAT,
**completion_args,
)
|