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.
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)