Attempting to move API key to os
This commit is contained in:
parent
6106e2987d
commit
04e469bd7a
@ -1,4 +1,5 @@
|
|||||||
import openai
|
import openai
|
||||||
|
import os
|
||||||
from redbot.core import Config, commands
|
from redbot.core import Config, commands
|
||||||
|
|
||||||
class ReginaldCog(commands.Cog):
|
class ReginaldCog(commands.Cog):
|
||||||
@ -6,7 +7,6 @@ class ReginaldCog(commands.Cog):
|
|||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.config = Config.get_conf(self, identifier=71717171171717)
|
self.config = Config.get_conf(self, identifier=71717171171717)
|
||||||
self.config.register_global(
|
self.config.register_global(
|
||||||
openai_api_key="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",
|
|
||||||
openai_model="text-davinci-002"
|
openai_model="text-davinci-002"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -18,7 +18,9 @@ class ReginaldCog(commands.Cog):
|
|||||||
if prompt is None:
|
if prompt is None:
|
||||||
prompt = "Hey,"
|
prompt = "Hey,"
|
||||||
try:
|
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()
|
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)
|
||||||
@ -32,8 +34,9 @@ class ReginaldCog(commands.Cog):
|
|||||||
)
|
)
|
||||||
await ctx.send(response.choices[0].text.strip())
|
await ctx.send(response.choices[0].text.strip())
|
||||||
except openai.error.OpenAIError as e:
|
except openai.error.OpenAIError as e:
|
||||||
await ctx.send("I apologize, sir, but I am unable to generate a response at this time.")
|
import traceback
|
||||||
print(f"OpenAI API Error: {e}")
|
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):
|
def setup(bot):
|
||||||
cog = ReginaldCog(bot)
|
cog = ReginaldCog(bot)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user