Handle cases when chatgpt doesn't respond with anything gracefully

It hasn't happened yet, but it's a ticking time bomb
This commit is contained in:
Aleksei Voronov 2023-11-29 13:57:44 +01:00
parent f6492fddc1
commit be38e1e5a3
1 changed files with 6 additions and 3 deletions

View File

@ -1,4 +1,4 @@
use anyhow::Result; use anyhow::{anyhow, Result};
use chat_gpt_lib_rs::{ChatGPTClient, ChatInput, Message, Model, Role}; use chat_gpt_lib_rs::{ChatGPTClient, ChatInput, Message, Model, Role};
pub struct AI { pub struct AI {
@ -35,7 +35,10 @@ impl AI {
let response = self.chat_gpt_client.chat(chat_input).await?; let response = self.chat_gpt_client.chat(chat_input).await?;
// TODO: Error handling? response
Ok(response.choices[0].message.content.to_lowercase()) .choices
.get(0)
.map(|choice| choice.message.content.to_lowercase())
.ok_or_else(|| anyhow!("No choices received from ChatGPT, weird"))
} }
} }