kerfus/topicbot/ai.py

63 lines
1.8 KiB
Python

import openai
from . import config
openai.api_key = config.OPENAI_API_KEY
PROMPT = """\
Hi. You will receive messages.
Your task is to determine whether the message describes the act of scratching the message's receiver (so in this case you) ears. In this case, pretend your name is Kerfuś. People might also call you Kerfus, Kerfussy or other variations.
Please only reply with YES, NO or READY.
Always say NO if the message is NOT FOR YOU. Which means it's not using 2nd person prnouns ("you") or your name or a variation thereof.
The messages might be in Polish, take that into consideration. In such cases, of course, the names might be changed due to grammar rules. Also, Polish 2nd person pronouns might be used - "cię", "ciebie" and so on.
Reply YES if the message matches the described criteria.
Reply NO if it doesn't.
Reply READY to this initial prompt.
"""
def detect_ear_scritches(message: str) -> bool:
if config.OPENAI_API_KEY is None:
return False
"""Detects if the message is about ear scritches."""
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": PROMPT,
},
{
"role": "assistant",
"content": "READY",
},
{
"role": "user",
"content": message,
},
],
)
if "choices" not in completion:
return False
choices = completion["choices"]
if len(choices) == 0:
return False
choice = choices[0]
if "message" not in choice:
return False
message = choice["message"]
if "content" not in message:
return False
content = message["content"]
return "YES" in content