From 04e469bd7a87464e7d4bc3c81e091a7e725ccef3 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 14 Mar 2023 18:49:44 +0100 Subject: [PATCH] Attempting to move API key to os --- reginaldCog/reginald.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/reginaldCog/reginald.py b/reginaldCog/reginald.py index f02a295..261d82c 100644 --- a/reginaldCog/reginald.py +++ b/reginaldCog/reginald.py @@ -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)