Attempting to move API key to os

This commit is contained in:
unknown 2023-03-14 18:49:44 +01:00
parent 6106e2987d
commit 04e469bd7a

View File

@ -1,4 +1,5 @@
import openai
import os
from redbot.core import Config, commands
class ReginaldCog(commands.Cog):
@ -6,7 +7,6 @@ class ReginaldCog(commands.Cog):
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"
)
@ -18,7 +18,9 @@ class ReginaldCog(commands.Cog):
if prompt is None:
prompt = "Hey,"
try:
api_key = await self.config.openai_api_key()
api_key = os.environ.get('OPENAI_API_KEY')
if api_key is None:
raise ValueError('OPENAI_API_KEY environment variable not set')
model = await self.config.openai_model()
openai.api_key = api_key
max_tokens = min(len(prompt) * 2, 2048)
@ -32,8 +34,9 @@ class ReginaldCog(commands.Cog):
)
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}")
import traceback
traceback.print_exc()
await ctx.send(f"I apologize, sir, but I am unable to generate a response at this time. Error message: {str(e)}")
def setup(bot):
cog = ReginaldCog(bot)