Skip to content

Commit

Permalink
fix: groq delay fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
evilsocket committed Jun 23, 2024
1 parent 424b13b commit 59c6395
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ serde_yaml = "0.9.34"
simple-home-dir = "0.3.5"
tokio = "1.38.0"
xml-rs = "0.8.20"
duration-string = { version = "0.4.0", optional = true }

[features]
default = ["ollama", "groq", "openai"]

ollama = ["dep:ollama-rs"]
groq = ["dep:groq-api-rs"]
groq = ["dep:groq-api-rs", "dep:duration-string"]
openai = ["dep:openai_api_rust"]


Expand Down
26 changes: 20 additions & 6 deletions src/agent/generator/groq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::time::Duration;

use async_trait::async_trait;
use colored::Colorize;
use duration_string::DurationString;
use groq_api_rs::completion::{client::Groq, request::builder, response::ErrorResponse};
use lazy_static::lazy_static;
use regex::Regex;
Expand All @@ -11,7 +12,8 @@ use crate::agent::generator::Message;
use super::{Client, Options};

lazy_static! {
static ref RETRY_TIME_PARSER: Regex = Regex::new(r"(?m)^.+try again in (.+)s\..*").unwrap();
static ref RETRY_TIME_PARSER: Regex =
Regex::new(r"(?m)^.+try again in (.+)\. Visit.*").unwrap();
}

pub struct GroqClient {
Expand Down Expand Up @@ -114,21 +116,33 @@ impl Client for GroqClient {
.as_str()
.clone_into(&mut retry_time_str);

if let Ok(retry_time) = retry_time_str.parse::<f32>() {
// DurationString can't handle decimals like Xm3.838383s
if retry_time_str.contains('.') {
let (val, _) = retry_time_str.split_once('.').unwrap();
retry_time_str = format!("{}s", val);
}

if let Ok(retry_time) = retry_time_str.parse::<DurationString>() {
println!(
"{}: rate limit reached for this model, retrying in {}s ...\n",
"{}: rate limit reached for this model, retrying in {} ...\n",
"WARNING".bold().yellow(),
retry_time,
);

tokio::time::sleep(Duration::from_millis(
((retry_time + 1.0) * 1000.0) as u64,
))
tokio::time::sleep(
retry_time.checked_add(Duration::from_millis(1000)).unwrap(),
)
.await;

return self.chat(options).await;
} else {
eprintln!("can't parse '{}'", &retry_time_str);
}
} else {
eprintln!("cap len wrong");
}
} else {
eprintln!("regex failed");
}

eprintln!(
Expand Down

0 comments on commit 59c6395

Please sign in to comment.