Updating to chat completion v.0+
This commit is contained in:
parent
501c2927fa
commit
ba10dc76a2
@ -88,11 +88,11 @@ class ReginaldCog(commands.Cog):
|
|||||||
await ctx.send(response_text[:2000]) # Discord character limit safeguard
|
await ctx.send(response_text[:2000]) # Discord character limit safeguard
|
||||||
|
|
||||||
async def generate_response(self, api_key, messages):
|
async def generate_response(self, api_key, messages):
|
||||||
"""✅ Generates a response using OpenAI's async API client (corrected version)."""
|
"""✅ Generates a response using OpenAI's new async API client (OpenAI v1.0+)."""
|
||||||
model = await self.config.openai_model()
|
model = await self.config.openai_model()
|
||||||
try:
|
try:
|
||||||
openai.api_key = api_key # ✅ Correct API key handling
|
client = openai.AsyncOpenAI(api_key=api_key) # ✅ Correct API usage
|
||||||
response = await openai.ChatCompletion.acreate(
|
response = await client.chat.completions.create(
|
||||||
model=model,
|
model=model,
|
||||||
messages=messages,
|
messages=messages,
|
||||||
max_tokens=1024,
|
max_tokens=1024,
|
||||||
@ -104,21 +104,15 @@ class ReginaldCog(commands.Cog):
|
|||||||
if not response.choices:
|
if not response.choices:
|
||||||
return "I fear I have no words to offer at this time."
|
return "I fear I have no words to offer at this time."
|
||||||
|
|
||||||
return response.choices[0].message["content"].strip()
|
return response.choices[0].message.content.strip()
|
||||||
|
|
||||||
except OpenAIError as e:
|
except OpenAIError as e:
|
||||||
error_trace = traceback.format_exc() # Get full traceback
|
error_message = f"OpenAI Error: {e}"
|
||||||
error_message = str(e)
|
|
||||||
|
|
||||||
# ✅ Log the full error for debugging (but not reveal the whole traceback to users)
|
|
||||||
print(f"⚠️ OpenAI Error: {error_trace}")
|
|
||||||
|
|
||||||
# ✅ Reginald will present the error in-character
|
|
||||||
reginald_responses = [
|
reginald_responses = [
|
||||||
f"Regrettably, I must inform you that I have encountered a bureaucratic obstruction:\n\n```{error_message}```\nI shall endeavor to resolve this at the earliest convenience.",
|
f"Regrettably, I must inform you that I have encountered a bureaucratic obstruction:\n\n```{error_message}```",
|
||||||
f"It would seem that a most unfortunate technical hiccup has befallen my faculties:\n\n```{error_message}```\nPerhaps a cup of tea and a moment of patience will remedy the situation.",
|
f"It would seem that a most unfortunate technical hiccup has befallen my faculties:\n\n```{error_message}```",
|
||||||
f"Ah, it appears I have received an urgent memorandum stating:\n\n```{error_message}```\nI shall investigate this matter forthwith, sir.",
|
f"Ah, it appears I have received an urgent memorandum stating:\n\n```{error_message}```",
|
||||||
f"I regret to inform you that my usual eloquence is presently obstructed by an unforeseen complication:\n\n```{error_message}```\nRest assured, I shall recover momentarily."
|
f"I regret to inform you that my usual eloquence is presently obstructed by an unforeseen complication:\n\n```{error_message}```"
|
||||||
]
|
]
|
||||||
return random.choice(reginald_responses)
|
return random.choice(reginald_responses)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user