mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-02 01:28:38 +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>
87 lines
2.7 KiB
Rust
87 lines
2.7 KiB
Rust
#![allow(dead_code)]
|
|
|
|
use std::{
|
|
collections::HashMap,
|
|
sync::{Arc, Mutex},
|
|
};
|
|
|
|
use litellm_cache::JsonCodec;
|
|
use litellm_cache_gcs::{GcsCache, GcsConfig, StaticTokenSource, TokenSource};
|
|
use percent_encoding::percent_decode_str;
|
|
use serde_json::Value;
|
|
use wiremock::{Mock, MockServer, Request, Respond, ResponseTemplate, http::Method, matchers::any};
|
|
|
|
pub type JsonGcsCache = GcsCache<JsonCodec<Value>>;
|
|
|
|
pub fn config(server: &MockServer, gcs_path: Option<&str>) -> GcsConfig {
|
|
GcsConfig {
|
|
bucket_name: "bucket".into(),
|
|
gcs_path: gcs_path.map(str::to_string),
|
|
path_service_account: None,
|
|
endpoint: server.uri(),
|
|
}
|
|
}
|
|
|
|
pub fn cache_with_token(
|
|
server: &MockServer,
|
|
gcs_path: Option<&str>,
|
|
token: Arc<dyn TokenSource>,
|
|
) -> JsonGcsCache {
|
|
GcsCache::with_token_source(
|
|
config(server, gcs_path),
|
|
reqwest::Client::new(),
|
|
JsonCodec::new(),
|
|
token,
|
|
)
|
|
}
|
|
|
|
pub fn cache(server: &MockServer, gcs_path: Option<&str>) -> JsonGcsCache {
|
|
cache_with_token(server, gcs_path, Arc::new(StaticTokenSource("tok".into())))
|
|
}
|
|
|
|
/// An in-memory bucket speaking the JSON API's media upload and `alt=media` download.
|
|
#[derive(Clone, Default)]
|
|
pub struct FakeBucket {
|
|
objects: Arc<Mutex<HashMap<String, Vec<u8>>>>,
|
|
}
|
|
|
|
impl FakeBucket {
|
|
pub async fn serve() -> MockServer {
|
|
let server = MockServer::start().await;
|
|
Mock::given(any())
|
|
.respond_with(Self::default())
|
|
.mount(&server)
|
|
.await;
|
|
server
|
|
}
|
|
}
|
|
|
|
impl Respond for FakeBucket {
|
|
fn respond(&self, request: &Request) -> ResponseTemplate {
|
|
let mut objects = self.objects.lock().unwrap();
|
|
match request.method {
|
|
Method::POST => {
|
|
let name = request
|
|
.url
|
|
.query_pairs()
|
|
.find_map(|(key, value)| (key == "name").then(|| value.into_owned()))
|
|
.expect("uploads carry the object name");
|
|
objects.insert(name, request.body.clone());
|
|
ResponseTemplate::new(200)
|
|
}
|
|
Method::GET => {
|
|
let encoded = request
|
|
.url
|
|
.path()
|
|
.strip_prefix("/storage/v1/b/bucket/o/")
|
|
.expect("downloads address an object");
|
|
let name = percent_decode_str(encoded).decode_utf8().unwrap();
|
|
match objects.get(name.as_ref()) {
|
|
Some(body) => ResponseTemplate::new(200).set_body_bytes(body.clone()),
|
|
None => ResponseTemplate::new(404),
|
|
}
|
|
}
|
|
_ => ResponseTemplate::new(405),
|
|
}
|
|
}
|
|
}
|