mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-29 01:03:17 +00:00
* refactor(rust): align the cache crates with Python and activate every backend The cache port had drifted: lifecycle and Redis-only operations sat on `BaseCache`, counters were pinned to `f64`, each semantic backend defined its own embedder and prompt handling, and only the in-memory backend could be selected natively. - Split `disconnect` and `test_connection` out of `BaseCache` into optional capabilities, implemented only where the Python class defines them, and give every Redis-only operation its own capability trait. - Decouple counters from the stored value type, so one backend can serve both responses and counters as Python's `RedisCache` does. - Share one `Embedder` and prompt contract in `litellm_cache::semantic`, and make the Redis and Valkey semantic backends generic over their codec. - Port the Python operations that were missing: `async_refresh_ttl`, `async_rpush_and_trim`, `async_set_cache_pipeline_with_ttls`, the DualCache pipeline, sadd, bulk delete and TTL reads, and the semantic-similarity write-back. - Take the HTTP client from the host pool in the GCS, S3 and Azure backends. - Activate all nine backends through the Rust catalog, whose rules all stay `PYTHON_ONLY`, and route the `Cache` facade's storage calls to the native runtime when one is selected. - Give every crate the same layout, move all tests to `tests/` on rstest, and add the shared `litellm-cache-testing` contract suite. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix: freeze native cache request kwargs and batch entries for type discipline Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix: declare semantic lookup methods in the native stub Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * refactor(rust): align the cache crates with Python and activate every backend The cache port had drifted: lifecycle and Redis-only operations sat on `BaseCache`, counters were pinned to `f64`, each semantic backend defined its own embedder and prompt handling, and only the in-memory backend could be selected natively. - Split `disconnect` and `test_connection` out of `BaseCache` into optional capabilities, implemented only where the Python class defines them, and give every Redis-only operation its own capability trait. - Decouple counters from the stored value type, so one backend can serve both responses and counters as Python's `RedisCache` does. - Share one `Embedder` and prompt contract in `litellm_cache::semantic`, and make the Redis and Valkey semantic backends generic over their codec. - Port the Python operations that were missing: `async_refresh_ttl`, `async_rpush_and_trim`, `async_set_cache_pipeline_with_ttls`, the DualCache pipeline, sadd, bulk delete and TTL reads, and the semantic-similarity write-back. - Take the HTTP client from the host pool in the GCS, S3 and Azure backends. - Activate all nine backends through the Rust catalog, whose rules all stay `PYTHON_ONLY`, and route the `Cache` facade's storage calls to the native runtime when one is selected. - Give every crate the same layout, move all tests to `tests/` on rstest, and add the shared `litellm-cache-testing` contract suite. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix: freeze native cache request kwargs and batch entries for type discipline Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix: declare semantic lookup methods in the native stub Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(rust): opt the native Messages and tokenizer suites into Rust explicitly #42517 made the Messages, token counter and tokenizer routes Python-only, so tests/test_litellm_rust silently exercised the Python path or failed outright. Each suite now prepends a RUST_OPT_IN rule for its route, keeping native coverage without changing the shipped default. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(rust): pop one at a time in the Redis 6 lpop pipeline and drop explanatory comments Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Yujong Lee <yujong@berri.ai> Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
160 lines
4.7 KiB
Rust
160 lines
4.7 KiB
Rust
use std::{future::Future, pin::Pin, time::Duration};
|
|
|
|
use litellm_cache::{
|
|
BaseCache, BatchCache, CacheConnectionResult, ConnectionCache, Error, ExactCacheContext,
|
|
FlushCache,
|
|
};
|
|
use serde_json::Value;
|
|
|
|
use crate::{CacheEntry, PartialHits, ResponseCache, ResponseCacheRequest};
|
|
|
|
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
|
|
|
|
/// Object-safe view of a `ResponseCache` over an exact-match backend, so hosts can hold every
|
|
/// exact backend behind one pointer without erasing which backend it is elsewhere.
|
|
pub trait ExactResponseCache: Send + Sync {
|
|
fn default_ttl(&self) -> Option<Duration>;
|
|
|
|
fn lookup(&self, request: &ResponseCacheRequest, now: Duration)
|
|
-> Result<Option<Value>, Error>;
|
|
|
|
fn store(
|
|
&self,
|
|
request: &ResponseCacheRequest,
|
|
response: Value,
|
|
now: Duration,
|
|
) -> Result<(), Error>;
|
|
|
|
fn lookup_batch(
|
|
&self,
|
|
requests: &[ResponseCacheRequest],
|
|
now: Duration,
|
|
) -> Result<PartialHits, Error>;
|
|
|
|
fn async_lookup<'a>(
|
|
&'a self,
|
|
request: &'a ResponseCacheRequest,
|
|
now: Duration,
|
|
) -> BoxFuture<'a, Result<Option<Value>, Error>>;
|
|
|
|
fn async_store<'a>(
|
|
&'a self,
|
|
request: &'a ResponseCacheRequest,
|
|
response: Value,
|
|
now: Duration,
|
|
) -> BoxFuture<'a, Result<(), Error>>;
|
|
|
|
fn async_lookup_batch<'a>(
|
|
&'a self,
|
|
requests: &'a [ResponseCacheRequest],
|
|
now: Duration,
|
|
) -> BoxFuture<'a, Result<PartialHits, Error>>;
|
|
|
|
fn async_store_batch<'a>(
|
|
&'a self,
|
|
entries: Vec<(ResponseCacheRequest, Value)>,
|
|
now: Duration,
|
|
) -> BoxFuture<'a, Result<(), Error>>;
|
|
|
|
fn async_store_entries<'a>(
|
|
&'a self,
|
|
entries: Vec<(ResponseCacheRequest, Value, Duration)>,
|
|
) -> BoxFuture<'a, Result<(), Error>>;
|
|
|
|
fn async_flush<'a>(&'a self) -> BoxFuture<'a, Result<(), Error>>;
|
|
}
|
|
|
|
/// Object-safe `test_connection` for the exact backends whose Python class defines it. Hosts hold
|
|
/// one next to their `ExactResponseCache` when the backend has it, and report the operation as
|
|
/// unsupported otherwise, as Python's `BaseCache.test_connection` does.
|
|
pub trait ConnectionProbe: Send + Sync {
|
|
fn test_connection<'a>(&'a self) -> BoxFuture<'a, Result<CacheConnectionResult, Error>>;
|
|
}
|
|
|
|
impl<B> ConnectionProbe for ResponseCache<B>
|
|
where
|
|
B: ConnectionCache<Value = CacheEntry>,
|
|
B::Context: Default + PartialEq,
|
|
{
|
|
fn test_connection<'a>(&'a self) -> BoxFuture<'a, Result<CacheConnectionResult, Error>> {
|
|
Box::pin(ResponseCache::test_connection(self))
|
|
}
|
|
}
|
|
|
|
impl<B> ExactResponseCache for ResponseCache<B>
|
|
where
|
|
B: BaseCache<Value = CacheEntry, Context = ExactCacheContext> + BatchCache + FlushCache,
|
|
{
|
|
fn default_ttl(&self) -> Option<Duration> {
|
|
ResponseCache::default_ttl(self)
|
|
}
|
|
|
|
fn lookup(
|
|
&self,
|
|
request: &ResponseCacheRequest,
|
|
now: Duration,
|
|
) -> Result<Option<Value>, Error> {
|
|
ResponseCache::lookup(self, request, now)
|
|
}
|
|
|
|
fn store(
|
|
&self,
|
|
request: &ResponseCacheRequest,
|
|
response: Value,
|
|
now: Duration,
|
|
) -> Result<(), Error> {
|
|
ResponseCache::store(self, request, response, now)
|
|
}
|
|
|
|
fn lookup_batch(
|
|
&self,
|
|
requests: &[ResponseCacheRequest],
|
|
now: Duration,
|
|
) -> Result<PartialHits, Error> {
|
|
ResponseCache::lookup_batch(self, requests, now)
|
|
}
|
|
|
|
fn async_lookup<'a>(
|
|
&'a self,
|
|
request: &'a ResponseCacheRequest,
|
|
now: Duration,
|
|
) -> BoxFuture<'a, Result<Option<Value>, Error>> {
|
|
Box::pin(ResponseCache::async_lookup(self, request, now))
|
|
}
|
|
|
|
fn async_store<'a>(
|
|
&'a self,
|
|
request: &'a ResponseCacheRequest,
|
|
response: Value,
|
|
now: Duration,
|
|
) -> BoxFuture<'a, Result<(), Error>> {
|
|
Box::pin(ResponseCache::async_store(self, request, response, now))
|
|
}
|
|
|
|
fn async_lookup_batch<'a>(
|
|
&'a self,
|
|
requests: &'a [ResponseCacheRequest],
|
|
now: Duration,
|
|
) -> BoxFuture<'a, Result<PartialHits, Error>> {
|
|
Box::pin(ResponseCache::async_lookup_batch(self, requests, now))
|
|
}
|
|
|
|
fn async_store_batch<'a>(
|
|
&'a self,
|
|
entries: Vec<(ResponseCacheRequest, Value)>,
|
|
now: Duration,
|
|
) -> BoxFuture<'a, Result<(), Error>> {
|
|
Box::pin(ResponseCache::async_store_batch(self, entries, now))
|
|
}
|
|
|
|
fn async_store_entries<'a>(
|
|
&'a self,
|
|
entries: Vec<(ResponseCacheRequest, Value, Duration)>,
|
|
) -> BoxFuture<'a, Result<(), Error>> {
|
|
Box::pin(ResponseCache::async_store_entries(self, entries))
|
|
}
|
|
|
|
fn async_flush<'a>(&'a self) -> BoxFuture<'a, Result<(), Error>> {
|
|
Box::pin(ResponseCache::async_flush(self))
|
|
}
|
|
}
|