From: Georgios Atheridis Date: Mon, 22 May 2023 23:16:12 +0000 (+0100) Subject: Fix X-Git-Url: https://git.atheridis.org/?a=commitdiff_plain;h=cf581f5846ce314da0dc73bac349b285fbc8c2c7;p=ihaspeks%2Faptbot-ihaspeks.git Fix --- diff --git a/ihaspeks/scripts/command_generator.py b/ihaspeks/scripts/command_generator.py index 3be736a..122a257 100644 --- a/ihaspeks/scripts/command_generator.py +++ b/ihaspeks/scripts/command_generator.py @@ -1,9 +1,10 @@ from aptbot.bot import Bot, Message, Commands import os import tools.smart_privmsg -import requests +import openai +from openai.error import RateLimitError -openai_api_key = os.getenv("OPENAI_API") +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. @@ -25,30 +26,29 @@ def generate(bot: Bot, message: Message): 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, - } + try: + response = openai.ChatCompletion.create( + model="gpt-3.5-turbo", + messages=[ + {"role": "system", "content": SYSTEM_CONTENT}, + {"role": "user", "content": msg}, ], - }, - ) + ) + except RateLimitError: + tools.smart_privmsg.send_safe( + bot, + message.channel, + "UwU Sowwy. The sevwews awe ovewloaded :( Twy again in a few minutes OwO", + reply=replied_msg_id, + ) + return + print(response) - if response.status_code != 200: - msg = "Sowwy, theres a minor issue right now :(" + if response: + msg = response.choices[0].message + # msg = msg[:msg.find("#")] else: - msg = response.json()["choices"][0]["message"]["content"] + msg = "Sadge nothing was returned" tools.smart_privmsg.send_safe(bot, message.channel, msg, reply=replied_msg_id) + # bot.send_privmsg(message.channel, msg, reply=replied_msg_id)