Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(2671): Move retry logic from infer_type_name to wizard #2673

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ indenter = "0.3.3"
derive_more = { workspace = true }
enum_dispatch = "0.3.13"
strum = "0.26.2"
tokio-retry = "0.3.0"

[dev-dependencies]
tailcall-prettier = { path = "tailcall-prettier" }
Expand Down
66 changes: 28 additions & 38 deletions src/cli/llm/infer_type_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,47 +102,37 @@
.collect(),
};

let mut delay = 3;
loop {
let answer = wizard.ask(question.clone()).await;
match answer {
Ok(answer) => {
let name = &answer.suggestions.join(", ");
for name in answer.suggestions {
if config.types.contains_key(&name)
|| new_name_mappings.contains_key(&name)
{
continue;
}
new_name_mappings.insert(name, type_name.to_owned());
break;
let answer = wizard.ask(question).await;
match answer {
Ok(answer) => {
let name = &answer.suggestions.join(", ");
for name in answer.suggestions {
if config.types.contains_key(&name) || new_name_mappings.contains_key(&name)

Check warning on line 110 in src/cli/llm/infer_type_name.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/llm/infer_type_name.rs#L105-L110

Added lines #L105 - L110 were not covered by tests
{
continue;

Check warning on line 112 in src/cli/llm/infer_type_name.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/llm/infer_type_name.rs#L112

Added line #L112 was not covered by tests
}
tracing::info!(
"Suggestions for {}: [{}] - {}/{}",
type_name,
name,
i + 1,
total
);

// TODO: case where suggested names are already used, then extend the base
// question with `suggest different names, we have already used following
// names: [names list]`
new_name_mappings.insert(name, type_name.to_owned());

Check warning on line 114 in src/cli/llm/infer_type_name.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/llm/infer_type_name.rs#L114

Added line #L114 was not covered by tests
break;
}
Err(e) => {
// TODO: log errors after certain number of retries.
if let Error::GenAI(_) = e {
// TODO: retry only when it's required.
tracing::warn!(
"Unable to retrieve a name for the type '{}'. Retrying in {}s",
type_name,
delay
);
tokio::time::sleep(tokio::time::Duration::from_secs(delay)).await;
delay *= std::cmp::min(delay * 2, 60);
}
}
tracing::info!(
"Suggestions for {}: [{}] - {}/{}",
type_name,
name,
i + 1,

Check warning on line 121 in src/cli/llm/infer_type_name.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/llm/infer_type_name.rs#L117-L121

Added lines #L117 - L121 were not covered by tests
total
);

// TODO: case where suggested names are already used, then extend the base
// question with `suggest different names, we have already used following
// names: [names list]`
break;

Check warning on line 128 in src/cli/llm/infer_type_name.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/llm/infer_type_name.rs#L128

Added line #L128 was not covered by tests
}
Err(e) => {
tracing::warn!(
"Unable to retrieve a name for the type '{}', skipping with error {}",

Check warning on line 132 in src/cli/llm/infer_type_name.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/llm/infer_type_name.rs#L130-L132

Added lines #L130 - L132 were not covered by tests
type_name,
e
);
}
}
}
Expand Down
24 changes: 18 additions & 6 deletions src/cli/llm/wizard.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::time::Duration;

use derive_setters::Setters;
use genai::adapter::AdapterKind;
use genai::chat::{ChatOptions, ChatRequest, ChatResponse};
use genai::resolver::AuthResolver;
use genai::Client;
use tokio_retry::strategy::ExponentialBackoff;

use super::Result;
use crate::cli::llm::model::Model;
Expand Down Expand Up @@ -41,13 +44,22 @@

pub async fn ask(&self, q: Q) -> Result<A>
where
Q: TryInto<ChatRequest, Error = super::Error>,
Q: TryInto<ChatRequest, Error = super::Error> + Clone,

Check warning on line 47 in src/cli/llm/wizard.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/llm/wizard.rs#L47

Added line #L47 was not covered by tests
A: TryFrom<ChatResponse, Error = super::Error>,
{
let response = self
.client
.exec_chat(self.model.as_str(), q.try_into()?, None)
.await?;
A::try_from(response)
let retry = ExponentialBackoff::from_millis(1000)
.max_delay(Duration::from_secs(60))
.map(tokio_retry::strategy::jitter)
.take(10);

tokio_retry::Retry::spawn(retry, || async {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retry should only happen incase of a 429 status code.

let response = self
.client
.exec_chat(self.model.as_str(), q.clone().try_into()?, None)
.await?;

A::try_from(response)
})
.await

Check warning on line 63 in src/cli/llm/wizard.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/llm/wizard.rs#L50-L63

Added lines #L50 - L63 were not covered by tests
}
}
Loading