From b0ce40b47f927e1f2fabc9a9355b5fd31b4db9da Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Mon, 13 Nov 2023 20:11:49 +1100 Subject: [PATCH] feat: Add ai command --- ai.py | 35 +++++++++++++++++++++++++++++++++++ bot.py | 6 ++++++ requirements.txt | 3 ++- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 ai.py diff --git a/ai.py b/ai.py new file mode 100644 index 0000000..e83b426 --- /dev/null +++ b/ai.py @@ -0,0 +1,35 @@ +from openai import OpenAI +import os +import dotenv + +# Set environ OPENAI_API_KEY +dotenv.load_dotenv() + + +client = OpenAI( + api_key=os.getenv("OPENAI_API_KEY"), +) + +instructions=""" +Give me a short response to this chat message. +Your name is "Woodburn/" (try to limit using your name unless they ask directly for it) and be a little bit funny. You are a bot in Australia. Pretend drop bears and other Australian myths are real. +""" + +def chat(message): + completion = client.chat.completions.create( + messages=[ + { + "role": "user", + "content": instructions + "\n" + message, + } + ], + model="gpt-3.5-turbo", + max_tokens=200, + ) + return completion.choices[0].message.content + + + +if __name__ == "__main__": + print(chat("What is your name?")) + print(chat("What is a drop bear?")) \ No newline at end of file diff --git a/bot.py b/bot.py index 6b4cae5..e298ebf 100644 --- a/bot.py +++ b/bot.py @@ -13,6 +13,7 @@ from cryptography import x509 from cryptography.hazmat.backends import default_backend import datetime from apscheduler.schedulers.asyncio import AsyncIOScheduler +import ai @@ -446,6 +447,11 @@ async def manualsslcheck(ctx): await checkForSSLExpiry() await ctx.channel.send("SSL check complete",ephemeral=True) +@tree.command(name="ai", description="AI Chat") +async def ai(ctx, message: str): + prompt = "Name: " + str(ctx.user.name) + "\n" + prompt = prompt + "Message: " + message + "\n" + await ctx.response.send_message(ai.chat(prompt)) # When the bot is ready @client.event diff --git a/requirements.txt b/requirements.txt index cb404ca..fb90560 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,5 @@ requests dnspython markdownify cryptography -apscheduler \ No newline at end of file +apscheduler +openai \ No newline at end of file