Created
April 15, 2023 09:22
-
-
Save kagesenshi/82fa02e989ed0e39e19e1a4ec25c5d1b to your computer and use it in GitHub Desktop.
ChatGPT Sentiment Analyzer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import openai | |
class SentimentAnalyzer(object): | |
def __init__(self, language): | |
self._language = language | |
def _prompt(self): | |
prompt = """ | |
The following sentence is in %s language. | |
Categorize the sentence in to good, bad, neutral or sarcastic sentiment. | |
Provide output in the form of JSONL without any wrapping text. Valid | |
return values are "positive", "negative", "neutral", "sarcastic". Also include | |
the sentence in the json. Do not provide any explanation so that the result can | |
be parsed by json parser. | |
""" % self._language | |
return prompt | |
def evaluate(self, messages): | |
prompt = self._prompt() + "\n".join(messages) | |
completion = openai.ChatCompletion.create(model='gpt-3.5-turbo', messages=[{'role': 'user', 'content': prompt}]) | |
return completion.choices[0].message.content | |
analyzer = SentimentAnalyzer('malay') | |
print(analyzer.evaluate(messages=['tak boleh blah'])) | |
print(analyzer.evaluate(messages=['tak suka makan', 'ye la tu, suka la sangat', 'jom makan'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
seems to handle sarcasm ok