Add a try/except to the llm

This commit is contained in:
Luke Robles 2024-06-06 08:03:07 -07:00
parent 375bd7ce4b
commit 84984502e7

View File

@ -158,15 +158,18 @@ async def on_message(ctx):
} }
headers = {"Content-Type": "application/json"} headers = {"Content-Type": "application/json"}
await ctx.channel.trigger_typing() try:
response = requests.post(url, json=payload, headers=headers) await ctx.channel.trigger_typing()
answer = response.json()["choices"][0]["message"]["content"] response = requests.post(url, json=payload, headers=headers)
answer = response.json()["choices"][0]["message"]["content"]
if len(answer) > 2000: if len(answer) > 2000:
await ctx.reply(answer[:2000].replace("<|end_of_turn|>", "")) await ctx.reply(answer[:2000].replace("<|end_of_turn|>", ""))
await ctx.reply(answer[2000:].replace("<|end_of_turn|>", "")) await ctx.reply(answer[2000:].replace("<|end_of_turn|>", ""))
else: else:
await ctx.reply(answer.replace("<|end_of_turn|>", "")) await ctx.reply(answer.replace("<|end_of_turn|>", ""))
except KeyError:
await ctx.reply("Somethings wrong, maybe the LLM crashed")
bot.run(os.getenv("discord_token")) bot.run(os.getenv("discord_token"))