Skip to content

Commit

Permalink
handle prompt failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Medowhill committed Jul 20, 2024
1 parent 3973cfc commit 02c1552
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/llm_client/openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
time::{Duration, Instant},
};

use async_openai::{types::*, Client};
use async_openai::{error::OpenAIError, types::*, Client};
use async_trait::async_trait;
use etrace::some_or;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -73,6 +73,8 @@ impl HasElapsed for CacheVal {
}
}

const ERROR_MESSAGE: &str = "Sorry! We've encountered an issue with repetitive patterns in your prompt. Please try again with a different prompt.";

pub struct OpenAIClient {
inner: Option<Client>,
model: Option<String>,
Expand Down Expand Up @@ -123,7 +125,7 @@ impl OpenAIClient {

tracing::info!("send_request START");
let (mut response, elapsed) = loop {
assert!(i < 10);
assert!(i < 10, "{:?}", msgs);
let mut request = CreateChatCompletionRequestArgs::default();
request
.model(model)
Expand Down Expand Up @@ -175,6 +177,32 @@ impl OpenAIClient {
elapsed,
err
);
if i == 9 {
if let OpenAIError::ApiError(err) = err {
if err.message == ERROR_MESSAGE {
let response = CreateChatCompletionResponse {
id: String::new(),
object: String::new(),
created: 0,
model: String::new(),
choices: vec![ChatChoice {
index: 0,
message: ChatCompletionResponseMessage {
role: Role::Assistant,
content: String::new(),
},
finish_reason: None,
}],
usage: Some(Usage {
prompt_tokens: 0,
completion_tokens: 0,
total_tokens: 0,
}),
};
break (response, elapsed);
}
}
}
msgs.first_mut().unwrap().content += " ";
i += 1;
}
Expand Down

0 comments on commit 02c1552

Please sign in to comment.