From: Georgios Atheridis Date: Mon, 22 May 2023 22:59:56 +0000 (+0100) Subject: Add OPENAI Command Maker X-Git-Url: https://git.atheridis.org/?a=commitdiff_plain;h=4687f6efede3bfacb269907e68ed51494c7b28a7;p=ihaspeks%2Faptbot-ihaspeks.git Add OPENAI Command Maker Automatically generates an output for nonexistent commands --- diff --git a/ihaspeks/analyze_command.py b/ihaspeks/analyze_command.py index 76ba314..a2ec24b 100644 --- a/ihaspeks/analyze_command.py +++ b/ihaspeks/analyze_command.py @@ -12,7 +12,7 @@ PATH = os.path.dirname(os.path.realpath(__file__)) logger.debug(f"PATH set to: {PATH}") -def do_command(bot: Bot, message: Message, command_modules: dict): +def do_command(bot: Bot, message: Message, command_modules: dict) -> bool: db_name_database = "database.db" conn = sqlite3.connect(os.path.join(PATH, db_name_database)) c = conn.cursor() @@ -73,7 +73,7 @@ def do_command(bot: Bot, message: Message, command_modules: dict): fetched = c.fetchall() if not fetched: conn.close() - return + return False ( _, @@ -90,7 +90,7 @@ def do_command(bot: Bot, message: Message, command_modules: dict): Please wait {int(avail_time - message_timestamp) + 1} seconds.", ) conn.close() - return + return False c.execute( "REPLACE INTO cooldowns VALUES (?, ?, ?)", @@ -113,3 +113,5 @@ def do_command(bot: Bot, message: Message, command_modules: dict): command_modules[command].main(bot, message) else: tools.smart_privmsg.send(bot, message, value) + + return True diff --git a/ihaspeks/main.py b/ihaspeks/main.py index 9551034..277be70 100644 --- a/ihaspeks/main.py +++ b/ihaspeks/main.py @@ -16,6 +16,7 @@ import scripts.crylaugh import scripts.clean_queue import scripts.discord_start_stream import scripts.will_be_missed +import scripts.command_generator import database_manager import analyze_auto_message import time @@ -36,6 +37,7 @@ reload(scripts.crylaugh) reload(scripts.clean_queue) reload(scripts.discord_start_stream) reload(scripts.will_be_missed) +reload(scripts.command_generator) reload(database_manager) reload(analyze_auto_message) @@ -140,7 +142,8 @@ def main(bot: Bot, message: Message): if message.command == Commands.PRIVMSG: database_manager.add_message_to_chat_history(message) if message.value[0] in {"?", "\\"}: - analyze_command.do_command(bot, message, commands_modules) + if not analyze_command.do_command(bot, message, commands_modules): + scripts.command_generator.generate(bot, message) scripts.unit_converter.send_metric(bot, message) scripts.alwase.alwase(bot, message) scripts.alwase.no_horny(bot, message) diff --git a/ihaspeks/scripts/command_generator.py b/ihaspeks/scripts/command_generator.py new file mode 100644 index 0000000..3be736a --- /dev/null +++ b/ihaspeks/scripts/command_generator.py @@ -0,0 +1,54 @@ +from aptbot.bot import Bot, Message, Commands +import os +import tools.smart_privmsg +import requests + +openai_api_key = os.getenv("OPENAI_API") + +SYSTEM_CONTENT = """You act like a twitch.tv command. The user gives you a word and you say something that would be expected to be said as that word being a command. All words are valid commands. + +Do not comment about what's going to happen on stream. Do not add type of text. + +All commands start with a question mark. Every word is a valid command. Come up with something funny. + +Some information: The name of the streamer is Peks. His avatar is a shiba inu doge. He likes playing League of Legends and Overwatch 2. He mains Twitch in League of Legends and Genji in Overwatch 2. He's born in England, but has lived in France since he was 4, so Peks is French. + +Try to come up with funny and troll answers. Stuff that would annoy viewers.""" + + +def generate(bot: Bot, message: Message): + replied_message = message.tags.get("reply-parent-msg-body", None) + if replied_message: + msg = replied_message + replied_msg_id = message.tags["reply-parent-msg-id"] + else: + msg = " ".join(message.value.split(" ")[1:]) + replied_msg_id = message.tags["id"] + + response = requests.post( + "https://api.openai.com/v1/chat/completions", + headers={ + "Content-Type": "application/json", + "Authorization": f"Bearer {openai_api_key}", + }, + data={ + "model": "gpt-3.5-turbo", + "messages": [ + { + "role": "system", + "content": SYSTEM_CONTENT, + }, + { + "role": "user", + "content": msg, + } + ], + }, + ) + print(response) + if response.status_code != 200: + msg = "Sowwy, theres a minor issue right now :(" + else: + msg = response.json()["choices"][0]["message"]["content"] + + tools.smart_privmsg.send_safe(bot, message.channel, msg, reply=replied_msg_id)