KaniumCogs/reginaldCog/reginald.py
2023-03-14 17:59:57 +01:00

40 lines
1.4 KiB
Python

import openai
from redbot.core import Config, commands
class ReginaldCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.config = Config.get_conf(self, identifier=71717171171717)
self.config.register_global(
openai_api_key="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",
openai_model="text-davinci-002"
)
@commands.guild_only()
@commands.has_permissions(manage_guild=True)
@commands.command()
async def reginald(self, ctx, *, prompt=None):
"""Ask Reginald a question"""
if prompt is None:
prompt = "Hey,"
try:
api_key = await self.config.openai_api_key()
model = await self.config.openai_model()
openai.api_key = api_key
max_tokens = min(len(prompt) * 2, 2048)
response = openai.Completion.create(
model=model,
prompt=prompt,
max_tokens=max_tokens,
n=1,
stop=None,
temperature=0.5,
)
await ctx.send(response.choices[0].text.strip())
except openai.error.OpenAIError as e:
await ctx.send("I apologize, sir, but I am unable to generate a response at this time.")
print(f"OpenAI API Error: {e}")
def setup(bot):
cog = ReginaldCog(bot)
bot.add_cog(cog)