Created command to set API key

This commit is contained in:
unknown 2023-03-14 18:58:49 +01:00
parent 04e469bd7a
commit e316016fc7

View File

@ -9,18 +9,28 @@ class ReginaldCog(commands.Cog):
self.config.register_global( self.config.register_global(
openai_model="text-davinci-002" openai_model="text-davinci-002"
) )
self.config.register_guild(
openai_api_key=None
)
@commands.guild_only() @commands.guild_only()
@commands.has_permissions(manage_guild=True) @commands.has_permissions(manage_guild=True)
@commands.command() @commands.command()
async def setreginaldcogapi(self, ctx, api_key):
"""Set the OpenAI API key"""
await self.config.guild(ctx.guild).openai_api_key.set(api_key)
await ctx.send("OpenAI API key set successfully.")
@commands.guild_only()
@commands.command()
async def reginald(self, ctx, *, prompt=None): async def reginald(self, ctx, *, prompt=None):
"""Ask Reginald a question""" """Ask Reginald a question"""
if prompt is None: if prompt is None:
prompt = "Hey," prompt = "Hey,"
try: try:
api_key = os.environ.get('OPENAI_API_KEY') api_key = await self.config.guild(ctx.guild).openai_api_key()
if api_key is None: if api_key is None:
raise ValueError('OPENAI_API_KEY environment variable not set') raise ValueError('OpenAI API key not set. Please use the "setreginaldcogapi" command to set the key.')
model = await self.config.openai_model() model = await self.config.openai_model()
openai.api_key = api_key openai.api_key = api_key
max_tokens = min(len(prompt) * 2, 2048) max_tokens = min(len(prompt) * 2, 2048)