-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(models-http-api): use rate_limit mod in embedding
- Loading branch information
Showing
3 changed files
with
37 additions
and
27 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use async_trait::async_trait; | ||
use ratelimit::Ratelimiter; | ||
use tabby_inference::Embedding; | ||
use tokio; | ||
|
||
pub struct RateLimitedEmbedding { | ||
embedding: Box<dyn Embedding>, | ||
rate_limiter: Ratelimiter, | ||
} | ||
|
||
impl RateLimitedEmbedding { | ||
pub fn new(embedding: Box<dyn Embedding>, rate_limiter: Ratelimiter) -> Self { | ||
Self { | ||
embedding, | ||
rate_limiter, | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Embedding for RateLimitedEmbedding { | ||
async fn embed(&self, prompt: &str) -> anyhow::Result<Vec<f32>> { | ||
for _ in 0..5 { | ||
if let Err(sleep) = self.rate_limiter.try_wait() { | ||
tokio::time::sleep(sleep).await; | ||
continue; | ||
} | ||
|
||
return self.embedding.embed(prompt).await; | ||
} | ||
|
||
anyhow::bail!("Rate limit exceeded for embedding computation"); | ||
} | ||
} |