Skip to content

Commit 0e71656

Browse files
committed
Add OpenAI API retry functionality and backoff crate.
- Add retry logic to the OpenAI API configuration - Implement a new setting for OpenAI API retries
1 parent 865f8d3 commit 0e71656

File tree

5 files changed

+15
-1
lines changed

5 files changed

+15
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ anyhow = "1.0.69"
2121
async-openai = "0.8.0"
2222
async-std = "1.12.0"
2323
async-trait = "0.1.64"
24+
backoff = "0.4.0"
2425
clap = { version = "4.1.8", features = ["derive"] }
2526
colored = "2.0.0"
2627
config = { version = "0.13.3", features = ["toml"] }

src/llms/openai.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ impl OpenAIClient {
3232
bail!("No OpenAI model configured.")
3333
}
3434

35-
let client = Client::new().with_api_key(&api_key);
35+
let mut client = Client::new().with_api_key(&api_key);
36+
37+
if settings.retries.unwrap_or_default() > 0 {
38+
let backoff = backoff::ExponentialBackoffBuilder::new()
39+
.with_max_elapsed_time(Some(std::time::Duration::from_secs(60)))
40+
.build();
41+
client = client.with_backoff(backoff);
42+
}
3643
Ok(Self { model, client })
3744
}
3845

src/settings.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ impl<'de> serde::Deserialize<'de> for ModelProvider {
7373
pub(crate) struct OpenAISettings {
7474
pub api_key: Option<String>,
7575
pub model: Option<String>,
76+
pub retries: Option<u16>,
7677
}
7778

7879
// implement the trait `From<OpenAISettings>` for `ValueKind`
@@ -81,6 +82,7 @@ impl From<OpenAISettings> for config::ValueKind {
8182
let mut properties = HashMap::new();
8283
properties.insert("api_key".to_string(), config::Value::from(settings.api_key));
8384
properties.insert("model".to_string(), config::Value::from(settings.model));
85+
properties.insert("retries".to_string(), config::Value::from(settings.retries));
8486
Self::Table(properties)
8587
}
8688
}
@@ -198,6 +200,7 @@ impl Settings {
198200
Some(OpenAISettings {
199201
api_key: None,
200202
model: Some(DEFAULT_OPENAI_MODEL.to_string()),
203+
retries: Some(2),
201204
}),
202205
)?
203206
.set_default(

src/toml.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ the-force = { value = "surrounds-you" }
8787
"model_provider",
8888
"openai.api_key",
8989
"openai.model",
90+
"openai.retries",
9091
"output.lang",
9192
"prompt.commit_summary",
9293
"prompt.commit_title",
@@ -108,6 +109,7 @@ the-force = { value = "surrounds-you" }
108109
"model_provider",
109110
"openai.api_key",
110111
"openai.model",
112+
"openai.retries",
111113
"output.lang",
112114
"prompt.commit_summary",
113115
"prompt.commit_title",

0 commit comments

Comments
 (0)