mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-30 01:08:33 +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>
205 lines
6.3 KiB
Rust
205 lines
6.3 KiB
Rust
use std::sync::OnceLock;
|
|
|
|
use litellm_cache::Error;
|
|
use litellm_cache_redis::connection::ConnectionRef;
|
|
|
|
use crate::reply::{number_value, string_value};
|
|
|
|
pub(crate) const CACHE_KEY_FIELD: &str = "litellm_cache_key";
|
|
pub(crate) const VECTOR_FIELD: &str = "prompt_vector";
|
|
|
|
/// The redisvl `SemanticCache` index, resolved once per cache: the configured name when its
|
|
/// schema fits, else `<name>_isolated`, recreated when that one is stale too.
|
|
pub(crate) struct Index {
|
|
name: String,
|
|
resolved: OnceLock<String>,
|
|
}
|
|
|
|
impl Index {
|
|
pub(crate) fn new(name: String) -> Self {
|
|
Self {
|
|
name,
|
|
resolved: OnceLock::new(),
|
|
}
|
|
}
|
|
|
|
pub(crate) fn name(&self) -> &str {
|
|
&self.name
|
|
}
|
|
|
|
pub(crate) fn ensure(
|
|
&self,
|
|
connection: &mut ConnectionRef<'_>,
|
|
dims: usize,
|
|
) -> Result<String, Error> {
|
|
if let Some(name) = self.resolved.get() {
|
|
return Ok(name.clone());
|
|
}
|
|
let name = match index_compatible(connection, &self.name, dims)? {
|
|
Some(true) => self.name.clone(),
|
|
Some(false) => self.isolated(connection, dims)?,
|
|
None => match create_index(connection, &self.name, dims) {
|
|
Ok(()) => self.name.clone(),
|
|
Err(_) => match index_compatible(connection, &self.name, dims)? {
|
|
Some(true) => self.name.clone(),
|
|
Some(false) => self.isolated(connection, dims)?,
|
|
None => return Err(Error::Unavailable),
|
|
},
|
|
},
|
|
};
|
|
let _ = self.resolved.set(name.clone());
|
|
Ok(name)
|
|
}
|
|
|
|
fn isolated(&self, connection: &mut ConnectionRef<'_>, dims: usize) -> Result<String, Error> {
|
|
let name = format!("{}_isolated", self.name);
|
|
match index_compatible(connection, &name, dims)? {
|
|
Some(true) => Ok(name),
|
|
Some(false) => {
|
|
redis::cmd("FT.DROPINDEX")
|
|
.arg(&name)
|
|
.query::<()>(connection)
|
|
.map_err(|_| Error::Unavailable)?;
|
|
create_index(connection, &name, dims)?;
|
|
Ok(name)
|
|
}
|
|
None => {
|
|
create_index(connection, &name, dims)?;
|
|
Ok(name)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn create_index(connection: &mut ConnectionRef<'_>, name: &str, dims: usize) -> Result<(), Error> {
|
|
redis::cmd("FT.CREATE")
|
|
.arg(name)
|
|
.arg("ON")
|
|
.arg("HASH")
|
|
.arg("PREFIX")
|
|
.arg(1)
|
|
.arg(name)
|
|
.arg("SCORE")
|
|
.arg(1.0)
|
|
.arg("SCHEMA")
|
|
.arg("prompt")
|
|
.arg("TEXT")
|
|
.arg("WEIGHT")
|
|
.arg(1)
|
|
.arg("response")
|
|
.arg("TEXT")
|
|
.arg("WEIGHT")
|
|
.arg(1)
|
|
.arg("inserted_at")
|
|
.arg("NUMERIC")
|
|
.arg("updated_at")
|
|
.arg("NUMERIC")
|
|
.arg(VECTOR_FIELD)
|
|
.arg("VECTOR")
|
|
.arg("FLAT")
|
|
.arg(6)
|
|
.arg("TYPE")
|
|
.arg("FLOAT32")
|
|
.arg("DIM")
|
|
.arg(dims)
|
|
.arg("DISTANCE_METRIC")
|
|
.arg("COSINE")
|
|
.arg(CACHE_KEY_FIELD)
|
|
.arg("TAG")
|
|
.arg("SEPARATOR")
|
|
.arg(",")
|
|
.query::<()>(connection)
|
|
.map_err(|_| Error::Unavailable)
|
|
}
|
|
|
|
fn index_compatible(
|
|
connection: &mut ConnectionRef<'_>,
|
|
name: &str,
|
|
dims: usize,
|
|
) -> Result<Option<bool>, Error> {
|
|
let info = match redis::cmd("FT.INFO")
|
|
.arg(name)
|
|
.query::<redis::Value>(connection)
|
|
{
|
|
Ok(info) => info,
|
|
Err(error) if unknown_index(&error) => return Ok(None),
|
|
Err(_) => return Err(Error::Unavailable),
|
|
};
|
|
Ok(Some(schema_compatible(&info, dims)))
|
|
}
|
|
|
|
fn unknown_index(error: &redis::RedisError) -> bool {
|
|
let message = error.to_string().to_lowercase();
|
|
message.contains("unknown") && message.contains("index")
|
|
}
|
|
|
|
struct Attribute {
|
|
name: Option<String>,
|
|
field_type: Option<String>,
|
|
dim: Option<f64>,
|
|
data_type: Option<String>,
|
|
distance_metric: Option<String>,
|
|
}
|
|
|
|
fn attribute(value: &redis::Value) -> Option<Attribute> {
|
|
let redis::Value::Array(pairs) = value else {
|
|
return None;
|
|
};
|
|
let mut attribute = Attribute {
|
|
name: None,
|
|
field_type: None,
|
|
dim: None,
|
|
data_type: None,
|
|
distance_metric: None,
|
|
};
|
|
for pair in pairs.as_chunks::<2>().0 {
|
|
match string_value(&pair[0]).as_deref() {
|
|
Some("identifier") => attribute.name = string_value(&pair[1]),
|
|
Some("type") => attribute.field_type = string_value(&pair[1]),
|
|
Some("dim") => attribute.dim = number_value(&pair[1]),
|
|
Some("data_type") => attribute.data_type = string_value(&pair[1]),
|
|
Some("distance_metric") => attribute.distance_metric = string_value(&pair[1]),
|
|
_ => {}
|
|
}
|
|
}
|
|
Some(attribute)
|
|
}
|
|
|
|
fn schema_compatible(info: &redis::Value, dims: usize) -> bool {
|
|
let redis::Value::Array(entries) = info else {
|
|
return false;
|
|
};
|
|
let attributes = entries
|
|
.as_chunks::<2>()
|
|
.0
|
|
.iter()
|
|
.find(|pair| string_value(&pair[0]).as_deref() == Some("attributes"))
|
|
.map(|pair| &pair[1]);
|
|
let Some(redis::Value::Array(attributes)) = attributes else {
|
|
return false;
|
|
};
|
|
let fields = attributes.iter().filter_map(attribute).collect::<Vec<_>>();
|
|
let has_field = |name: &str, field_type: &str| {
|
|
fields.iter().any(|field| {
|
|
field.name.as_deref() == Some(name) && field.field_type.as_deref() == Some(field_type)
|
|
})
|
|
};
|
|
has_field("prompt", "TEXT")
|
|
&& has_field("response", "TEXT")
|
|
&& has_field("inserted_at", "NUMERIC")
|
|
&& has_field("updated_at", "NUMERIC")
|
|
&& has_field(CACHE_KEY_FIELD, "TAG")
|
|
&& fields.iter().any(|field| {
|
|
field.name.as_deref() == Some(VECTOR_FIELD)
|
|
&& field.field_type.as_deref() == Some("VECTOR")
|
|
&& field.dim == Some(dims as f64)
|
|
&& field
|
|
.data_type
|
|
.as_deref()
|
|
.is_some_and(|data| data.eq_ignore_ascii_case("float32"))
|
|
&& field
|
|
.distance_metric
|
|
.as_deref()
|
|
.is_some_and(|metric| metric.eq_ignore_ascii_case("cosine"))
|
|
})
|
|
}
|