-
Notifications
You must be signed in to change notification settings - Fork 259
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 and implement tokio_retry #2674
Closed
onyedikachi-david
wants to merge
15
commits into
tailcallhq:main
from
onyedikachi-david:refactor/implement-tokio-retry-in-wizard
Closed
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
376d75c
refactor(2671): implement tokio_retry for API rate limiting
onyedikachi-david 463373c
lint fix
onyedikachi-david a86526f
Refine retry logic in Wizard's ask method
onyedikachi-david aaac082
Merge branch 'main' into refactor/implement-tokio-retry-in-wizard
onyedikachi-david dc811b8
Merge branch 'main' into refactor/implement-tokio-retry-in-wizard
tusharmath f2f4aed
Merge branch 'main' into refactor/implement-tokio-retry-in-wizard
onyedikachi-david fc68ada
Merge branch 'main' into refactor/implement-tokio-retry-in-wizard
onyedikachi-david 2a1e1d7
using match arm instead of string comparison
onyedikachi-david 0feb36b
Merge branch 'main' into refactor/implement-tokio-retry-in-wizard
onyedikachi-david 0be7933
Update src/cli/llm/wizard.rs
onyedikachi-david 5ee7b1a
Merge branch 'main' into refactor/implement-tokio-retry-in-wizard
onyedikachi-david 49863dd
fix(wizard): use RetryIf for more specific error handling
onyedikachi-david 34a994b
Merge branch 'main' into refactor/implement-tokio-retry-in-wizard
onyedikachi-david d27ae73
Merge branch 'main' into refactor/implement-tokio-retry-in-wizard
tusharmath e3d9235
Merge branch 'main' into refactor/implement-tokio-retry-in-wizard
onyedikachi-david File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,46 @@ | ||
use derive_more::From; | ||
use strum_macros::Display; | ||
use reqwest::StatusCode; | ||
use thiserror::Error; | ||
|
||
#[derive(Debug, From, Display, thiserror::Error)] | ||
#[derive(Debug, Error)] | ||
pub enum WebcError { | ||
#[error("Response failed with status {status}: {body}")] | ||
ResponseFailedStatus { status: StatusCode, body: String }, | ||
#[error("Reqwest error: {0}")] | ||
Reqwest(#[from] reqwest::Error), | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
pub enum Error { | ||
#[error("GenAI error: {0}")] | ||
GenAI(genai::Error), | ||
#[error("Webc error: {0}")] | ||
Webc(WebcError), | ||
#[error("Empty response")] | ||
EmptyResponse, | ||
Serde(serde_json::Error), | ||
#[error("Serde error: {0}")] | ||
Serde(#[from] serde_json::Error), | ||
} | ||
|
||
impl From<genai::Error> for Error { | ||
fn from(err: genai::Error) -> Self { | ||
if let genai::Error::WebModelCall { webc_error, .. } = &err { | ||
let error_str = webc_error.to_string(); | ||
if error_str.contains("ResponseFailedStatus") { | ||
// Extract status and body from the error message | ||
let parts: Vec<&str> = error_str.splitn(3, ": ").collect(); | ||
if parts.len() >= 3 { | ||
if let Ok(status) = parts[1].parse::<u16>() { | ||
return Error::Webc(WebcError::ResponseFailedStatus { | ||
status: StatusCode::from_u16(status) | ||
.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR), | ||
body: parts[2].to_string(), | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
Error::GenAI(err) | ||
} | ||
} | ||
onyedikachi-david marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
pub type Result<A> = std::result::Result<A, Error>; | ||
pub type Result<T> = std::result::Result<T, Error>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
// use std::borrow::Borrow; | ||
onyedikachi-david marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
use derive_setters::Setters; | ||
use genai::adapter::AdapterKind; | ||
use genai::chat::{ChatOptions, ChatRequest, ChatResponse}; | ||
use genai::resolver::AuthResolver; | ||
use genai::Client; | ||
use reqwest::StatusCode; | ||
use tokio_retry::strategy::{jitter, ExponentialBackoff}; | ||
use tokio_retry::Retry; | ||
|
||
use super::Result; | ||
use super::error::{Error, Result, WebcError}; | ||
use crate::cli::llm::model::Model; | ||
|
||
#[derive(Setters, Clone)] | ||
|
@@ -41,13 +46,32 @@ impl<Q, A> Wizard<Q, A> { | |
|
||
pub async fn ask(&self, q: Q) -> Result<A> | ||
where | ||
Q: TryInto<ChatRequest, Error = super::Error>, | ||
A: TryFrom<ChatResponse, Error = super::Error>, | ||
Q: TryInto<ChatRequest, Error = Error> + Clone, | ||
A: TryFrom<ChatResponse, Error = Error>, | ||
{ | ||
let response = self | ||
.client | ||
.exec_chat(self.model.as_str(), q.try_into()?, None) | ||
.await?; | ||
A::try_from(response) | ||
let retry_strategy = ExponentialBackoff::from_millis(1000).map(jitter).take(5); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the base for the strategy is too high, please, check the doc, it'll be Consider:
|
||
|
||
Retry::spawn(retry_strategy, || async { | ||
onyedikachi-david marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let request = q.clone().try_into()?; | ||
match self | ||
.client | ||
.exec_chat(self.model.as_str(), request, None) | ||
.await | ||
{ | ||
Ok(response) => Ok(A::try_from(response)?), | ||
Err(err) => { | ||
let error = Error::from(err); | ||
match &error { | ||
Error::Webc(WebcError::ResponseFailedStatus { status, .. }) | ||
if *status == StatusCode::TOO_MANY_REQUESTS => | ||
{ | ||
Err(error) // Propagate the error to trigger a retry | ||
} | ||
_ => Ok(Err(error)?), // Other errors are returned without retrying | ||
} | ||
} | ||
} | ||
}) | ||
.await | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might not need so much code since this is merged — jeremychone/rust-genai@736bbec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, great.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tusharmath Pls, why is this repo: https://github.dev/laststylebender14/rust-genai being used instead of the main genai repo: https://github.com/jeremychone/rust-genai
jeremychone's latest lib.rs:
laststylebender14's latest commit hash lib.rs:
So the webc is still private.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@laststylebender14 can you update your Repo with the latest changes? Also can you transfer ownership to
tailcallhq
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@onyedikachi-david I have taken the latest changes here — https://github.com/tailcallhq/rust-genai
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please apply discussed changes: