Fix
authorGeorgios Atheridis <georgios@atheridis.org>
Mon, 22 May 2023 23:16:12 +0000 (00:16 +0100)
committerGeorgios Atheridis <georgios@atheridis.org>
Mon, 22 May 2023 23:16:12 +0000 (00:16 +0100)
ihaspeks/scripts/command_generator.py

index 3be736a2f3e560a7f6e1c560619a52642aea1a15..122a2574212be401bf5578a23c139c9aceda7eb7 100644 (file)
@@ -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)