Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Merge origin/main into litellm_valkey_semantic_native_cache

Yujong Lee90f93f76f4e9
diff --cc litellm-rust/crates/python-bridge/Cargo.toml
index 3c0eafaffe2,f5d08850b39..25b0a677a2d
--- a/litellm-rust/crates/python-bridge/Cargo.toml
+++ b/litellm-rust/crates/python-bridge/Cargo.toml
@@@ -24,8 -24,8 +24,9 @@@ litellm-cache.workspace = tru
litellm-cache-azure-blob.workspace = true
litellm-cache-memory.workspace = true
litellm-cache-redis.workspace = true
+ litellm-cache-disk.workspace = true
litellm-cache-response.workspace = true
+litellm-cache-valkey-semantic = { path = "../cache-valkey-semantic" }
serde.workspace = true
litellm-auth.workspace = true
litellm-callbacks-legacy-python.workspace = true
diff --cc litellm-rust/crates/python-bridge/src/cache/config.rs
index 1d931b7b339,927017e2e60..b91fbee603c
--- a/litellm-rust/crates/python-bridge/src/cache/config.rs
+++ b/litellm-rust/crates/python-bridge/src/cache/config.rs
@@@ -102,7 -98,7 +106,8 @@@ pub(super) struct AzureBlobCacheConfig
pub(super) enum CacheBackendConfig {
Memory(MemoryCacheConfig),
Redis(Box<RedisCacheConfig>),
+ ValkeySemantic(Box<ValkeySemanticCacheConfig>),
+ Disk(DiskCacheConfig),
AzureBlob(AzureBlobCacheConfig),
}
@@@ -170,13 -168,13 +177,20 @@@ impl NativeCacheConfig
}))),
Err(reason) => Ok(CacheConfigProjection::Unsupported(reason)),
},
+ Some(CacheType::ValkeySemantic) => match project_valkey_semantic(&backend)? {
+ Ok(backend) => Ok(CacheConfigProjection::Native(Box::new(Self {
+ policy,
+ backend: CacheBackendConfig::ValkeySemantic(Box::new(backend)),
+ }))),
+ Err(reason) => Ok(CacheConfigProjection::Unsupported(reason)),
+ },
+ Some(CacheType::Disk) => match project_disk(&backend)? {
+ Ok(backend) => Ok(CacheConfigProjection::Native(Box::new(Self {
+ policy,
+ backend: CacheBackendConfig::Disk(backend),
+ }))),
+ Err(reason) => Ok(CacheConfigProjection::Unsupported(reason)),
+ },
Some(CacheType::AzureBlob) => project_azure_blob(&backend).map(|backend| {
CacheConfigProjection::Native(Box::new(Self {
policy,
@@@ -185,8 -183,8 +199,7 @@@
}),
Some(
CacheType::RedisSemantic
- | CacheType::ValkeySemantic
| CacheType::S3
- | CacheType::Disk
| CacheType::QdrantSemantic
| CacheType::Gcs,
)
@@@ -200,12 -198,10 +213,13 @@@
let default_ttl = match &self.backend {
CacheBackendConfig::Memory(config) => Some(config.default_ttl),
CacheBackendConfig::Redis(config) => Some(config.default_ttl),
+ CacheBackendConfig::ValkeySemantic(_) => Some(Duration::ZERO),
+ CacheBackendConfig::Disk(_) => None,
CacheBackendConfig::AzureBlob(_) => None,
};
- if service.default_ttl() != default_ttl {
+ if !matches!(self.backend, CacheBackendConfig::ValkeySemantic(_))
+ && service.default_ttl() != default_ttl
+ {
return Some("facade and native backend default TTLs must match");
}
match &self.backend {
@@@ -230,16 -226,17 +244,27 @@@
CacheBackendConfig::Redis(config) => (service.namespace()
!= config.namespace.as_deref())
.then_some("facade and native backend namespaces must match"),
+ CacheBackendConfig::ValkeySemantic(config) => {
+ if service.kind() != "valkey-semantic" {
+ return Some("facade and native backend types must match");
+ }
+ let Some((threshold, index_name)) = service.semantic_config() else {
+ return Some("facade and native backend types must match");
+ };
+ (threshold != config.similarity_threshold || index_name != config.index_name)
+ .then_some("facade and native semantic settings must match")
+ }
+ CacheBackendConfig::Disk(_) if service.kind() != "disk" => {
+ Some("facade and native backend types must match")
+ }
+ CacheBackendConfig::Disk(config) => {
+ let Some(directory) = service.directory() else {
+ return Some("facade and native backend types must match");
+ };
+ let native = std::fs::canonicalize(directory).ok();
+ let facade = std::fs::canonicalize(&config.directory).ok();
+ (native != facade).then_some("facade and native backend directories must match")
+ }
CacheBackendConfig::AzureBlob(config) => match service.azure_blob_identity() {
None => Some("facade and native backend types must match"),
Some((account_url, container))
diff --cc litellm-rust/crates/python-bridge/src/cache/facade.rs
index a5e83be0830,ee6423daf6e..085db26c0d7
--- a/litellm-rust/crates/python-bridge/src/cache/facade.rs
+++ b/litellm-rust/crates/python-bridge/src/cache/facade.rs
@@@ -258,21 -274,8 +284,22 @@@ impl AzureBlobClientGuard
impl ConnectionGuard {
fn capture(kind: &str, cluster: bool, backend: &Bound<'_, PyAny>) -> PyResult<Self> {
Ok(match (kind, cluster) {
- ("redis", false) => Self::RedisPool(RedisPoolGuard::capture(backend, STANDALONE_POOL)?),
- ("redis", true) => Self::RedisPool(RedisPoolGuard::capture(backend, CLUSTER_POOL)?),
+ ("redis", false) => Self::RedisPool(RedisPoolGuard::capture(
+ backend,
+ "redis_client",
+ STANDALONE_POOL,
+ )?),
+ ("redis", true) => Self::RedisPool(RedisPoolGuard::capture(
+ backend,
+ "redis_client",
+ CLUSTER_POOL,
+ )?),
+ ("valkey-semantic", _) => Self::RedisPool(RedisPoolGuard::capture(
+ backend,
+ "sync_client",
+ VALKEY_POOL,
+ )?),
++ ("disk", _) => Self::None,
("azure-blob", _) => Self::AzureBlob(AzureBlobClientGuard::capture(backend)?),
_ => Self::None,
})
@@@ -317,11 -320,7 +344,12 @@@ impl FacadeGuard
"RedisClusterCache",
"redis",
),
+ ("valkey-semantic", false) => (
+ "litellm.caching.valkey_semantic_cache",
+ "ValkeySemanticCache",
+ "valkey-semantic",
+ ),
+ ("disk", _) => ("litellm.caching.disk_cache", "DiskCache", "disk"),
("azure-blob", _) => (
"litellm.caching.azure_blob_cache",
"AzureBlobCache",
@@@ -370,13 -369,11 +398,16 @@@
"max_size_per_item",
"redis_kwargs",
"redis_flush_size",
+ "similarity_threshold",
+ "embedding_model",
+ "index_name",
+ "embedding_max_input_tokens",
+ "embedding_timeout",
],
)?,
+ disk_store: (kind == "disk")
+ .then(|| DiskStoreGuard::capture(&backend))
+ .transpose()?,
connection: ConnectionGuard::capture(kind, cluster, &backend)?,
})
}
diff --cc litellm-rust/crates/python-bridge/src/cache/handle.rs
index 5df3a5f25a2,6e12e12040b..273252d36dc
--- a/litellm-rust/crates/python-bridge/src/cache/handle.rs
+++ b/litellm-rust/crates/python-bridge/src/cache/handle.rs
@@@ -67,29 -64,18 +67,41 @@@ impl CacheTestHandle
})
}
+ #[staticmethod]
+ #[pyo3(signature = (directory))]
+ fn disk(py: Python<'_>, directory: String) -> PyResult<Self> {
+ let service =
+ release_gil(py, move || NativeResponseCache::disk(&directory)).map_err(cache_error)?;
+ Ok(Self {
+ service,
+ guard: None,
+ pid: std::process::id(),
+ })
+ }
+
+ #[staticmethod]
+ #[pyo3(signature = (url, similarity_threshold, index_name, embedder))]
+ fn valkey_semantic(
+ url: String,
+ similarity_threshold: f64,
+ index_name: String,
+ embedder: &Bound<'_, PyAny>,
+ ) -> PyResult<Self> {
+ let python_embedder = PythonEmbedder::from_backend(embedder)?;
+ let service = NativeResponseCache::valkey_semantic(
+ &url,
+ similarity_threshold,
+ index_name,
+ python_embedder,
+ )
+ .map_err(cache_error)?;
+ Ok(Self {
+ service,
+ guard: None,
+ pid: std::process::id(),
+ })
+ }
+
#[staticmethod]
#[pyo3(signature = (account_url, container))]
fn azure_blob(py: Python<'_>, account_url: String, container: String) -> PyResult<Self> {
diff --cc litellm-rust/crates/python-bridge/src/cache/native.rs
index d8f7a693108,80789cc279a..5f2ddf484a6
--- a/litellm-rust/crates/python-bridge/src/cache/native.rs
+++ b/litellm-rust/crates/python-bridge/src/cache/native.rs
@@@ -1,9 -1,8 +1,10 @@@
- use std::{sync::Arc, time::Duration};
+ use std::{path::Path, sync::Arc, time::Duration};
-use litellm_cache::{CacheCodec, CacheConnectionResult, Error};
+use litellm_cache::{
+ CacheCodec, CacheConnectionResult, Error, ExactCacheContext, SemanticCacheContext,
+};
use litellm_cache_azure_blob::AzureBlobCache;
+ use litellm_cache_disk::DiskCache;
use litellm_cache_memory::InMemoryCache;
use litellm_cache_redis::{RedisCache, RedisTopology};
use litellm_cache_response::{
@@@ -76,11 -17,7 +77,12 @@@ pub(super) enum NativeResponseCache
cache: Arc<ResponseCache<RedisCache<ResponseCacheCodec>>>,
buffer: Option<Arc<WriteBuffer>>,
},
+ ValkeySemantic {
+ cache: Arc<ResponseCache<ValkeySemanticCache<PythonEmbedder, ResponseCacheCodec>>>,
+ embedder: PythonEmbedder,
+ scope: String,
+ },
+ Disk(Arc<ResponseCache<DiskCache<ResponseCacheCodec>>>),
AzureBlob(Arc<ResponseCache<AzureBlobCache<ResponseCacheCodec>>>),
}
@@@ -112,29 -49,11 +114,34 @@@ impl NativeResponseCache
buffer: None,
})
}
+
+ pub fn valkey_semantic(
+ url: &str,
+ similarity_threshold: f64,
+ index_name: String,
+ embedder: PythonEmbedder,
+ ) -> Result<Self, Error> {
+ let backend = ValkeySemanticCache::new(
+ url,
+ embedder.clone(),
+ ResponseCacheCodec,
+ ValkeySemanticConfig {
+ similarity_threshold,
+ index_name,
+ },
+ )?;
+ Ok(Self::ValkeySemantic {
+ cache: Arc::new(ResponseCache::new(Arc::new(backend))),
+ embedder,
+ scope: String::from("key"),
+ })
+ }
+
+ pub fn disk(directory: &str) -> Result<Self, Error> {
+ let cache = DiskCache::open(directory, ResponseCacheCodec)?;
+ Ok(Self::Disk(Arc::new(ResponseCache::new(Arc::new(cache)))))
+ }
+
pub async fn azure_blob(account_url: &str, container: &str) -> Result<Self, Error> {
let backend = AzureBlobCache::connect(
account_url,
@@@ -154,65 -73,17 +161,68 @@@
cache.backend().account_url(),
cache.backend().container_name(),
)),
- Self::Memory(_) | Self::Redis { .. } | Self::ValkeySemantic { .. } => None,
- Self::Memory(_) | Self::Redis { .. } | Self::Disk(_) => None,
++ Self::Memory(_) | Self::Redis { .. } | Self::ValkeySemantic { .. } | Self::Disk(_) => {
++ None
++ }
+ }
+ }
+
+ fn exact(request: &NativeRequest) -> ResponseCacheRequest<ExactCacheContext> {
+ ResponseCacheRequest {
+ key: request.key.clone(),
+ controls: request.controls,
+ context: ExactCacheContext { ttl: request.ttl },
+ max_age: request.max_age,
+ }
+ }
+
+ fn semantic(
+ request: &NativeRequest,
+ scope: &str,
+ ) -> ResponseCacheRequest<SemanticCacheContext> {
+ ResponseCacheRequest {
+ key: semantic_key(request, scope),
+ controls: request.controls,
+ context: SemanticCacheContext {
+ input: request.input.clone(),
+ messages: request.messages.clone(),
+ metadata: request.metadata.clone(),
+ scope: Some(scope.to_owned()),
+ ttl: request.ttl,
+ },
+ max_age: request.max_age,
+ }
+ }
+
+ pub fn with_redis_flush_size(self, flush_size: Option<usize>) -> Self {
+ match self {
+ Self::Redis { cache, .. } => Self::Redis {
+ cache,
+ buffer: flush_size.map(|size| Arc::new(WriteBuffer::new(size))),
+ },
+ value => value,
+ }
+ }
+
+ pub fn with_scope(self, scope: String) -> Self {
+ match self {
+ Self::ValkeySemantic {
+ cache, embedder, ..
+ } => Self::ValkeySemantic {
+ cache,
+ embedder,
+ scope,
+ },
+ value => value,
}
}
-}
-impl NativeResponseCache {
pub fn kind(&self) -> &'static str {
match self {
Self::Memory(_) => "memory",
Self::Redis { .. } => "redis",
+ Self::ValkeySemantic { .. } => "valkey-semantic",
+ Self::Disk(_) => "disk",
Self::AzureBlob(_) => "azure-blob",
}
}
@@@ -221,21 -92,21 +231,26 @@@
match self {
Self::Memory(cache) => cache.default_ttl(),
Self::Redis { cache, .. } => cache.default_ttl(),
+ Self::ValkeySemantic { cache, .. } => cache.default_ttl(),
+ Self::Disk(cache) => cache.default_ttl(),
Self::AzureBlob(cache) => cache.default_ttl(),
}
}
pub fn namespace(&self) -> Option<&str> {
match self {
- Self::Memory(_) | Self::ValkeySemantic { .. } | Self::AzureBlob(_) => None,
- Self::Memory(_) | Self::Disk(_) | Self::AzureBlob(_) => None,
++ Self::Memory(_) | Self::ValkeySemantic { .. } | Self::Disk(_) | Self::AzureBlob(_) => {
++ None
++ }
Self::Redis { cache, .. } => cache.backend().namespace(),
}
}
pub fn topology(&self) -> Option<&RedisTopology> {
match self {
- Self::Memory(_) | Self::ValkeySemantic { .. } | Self::AzureBlob(_) => None,
- Self::Memory(_) | Self::Disk(_) | Self::AzureBlob(_) => None,
++ Self::Memory(_) | Self::ValkeySemantic { .. } | Self::Disk(_) | Self::AzureBlob(_) => {
++ None
++ }
Self::Redis { cache, .. } => Some(cache.backend().topology()),
}
}
@@@ -243,14 -114,24 +258,30 @@@
pub fn capacity(&self) -> Option<usize> {
match self {
Self::Memory(cache) => Some(cache.backend().max_size_in_memory()),
- Self::Redis { .. } | Self::ValkeySemantic { .. } | Self::AzureBlob(_) => None,
- Self::Redis { .. } | Self::Disk(_) | Self::AzureBlob(_) => None,
++ Self::Redis { .. }
++ | Self::ValkeySemantic { .. }
++ | Self::Disk(_)
++ | Self::AzureBlob(_) => None,
}
}
pub fn max_entry_bytes(&self) -> Option<usize> {
match self {
Self::Memory(cache) => cache.backend().max_entry_bytes(),
- Self::Redis { .. } | Self::ValkeySemantic { .. } | Self::AzureBlob(_) => None,
- Self::Redis { .. } | Self::Disk(_) | Self::AzureBlob(_) => None,
++ Self::Redis { .. }
++ | Self::ValkeySemantic { .. }
++ | Self::Disk(_)
++ | Self::AzureBlob(_) => None,
+ }
+ }
+
- pub fn with_redis_flush_size(self, flush_size: Option<usize>) -> Self {
++ pub fn directory(&self) -> Option<&Path> {
+ match self {
- Self::Redis { cache, .. } => Self::Redis {
- cache,
- buffer: flush_size.map(|flush_size| Arc::new(WriteBuffer::new(flush_size))),
- },
- other => other,
++ Self::Disk(cache) => Some(cache.backend().directory()),
++ Self::Memory(_)
++ | Self::Redis { .. }
++ | Self::ValkeySemantic { .. }
++ | Self::AzureBlob(_) => None,
}
}
@@@ -264,14 -142,16 +295,15 @@@
}
}
- pub fn lookup(
- &self,
- request: &ResponseCacheRequest,
- now: Duration,
- ) -> Result<Option<Value>, Error> {
+ pub fn lookup(&self, request: &NativeRequest, now: Duration) -> Result<Option<Value>, Error> {
match self {
- Self::Memory(cache) => cache.lookup(request, now),
- Self::Redis { cache, .. } => cache.lookup(request, now),
- Self::Disk(cache) => cache.lookup(request, now),
- Self::AzureBlob(cache) => cache.lookup(request, now),
+ Self::Memory(cache) => cache.lookup(&Self::exact(request), now),
+ Self::Redis { cache, .. } => cache.lookup(&Self::exact(request), now),
+ Self::ValkeySemantic { cache, scope, .. } => {
+ cache.lookup(&Self::semantic(request, scope), now)
+ }
++ Self::Disk(cache) => cache.lookup(&Self::exact(request), now),
+ Self::AzureBlob(cache) => cache.lookup(&Self::exact(request), now),
}
}
@@@ -282,12 -162,10 +314,13 @@@
now: Duration,
) -> Result<(), Error> {
match self {
- Self::Memory(cache) => cache.store(request, response, now),
- Self::Redis { cache, .. } => cache.store(request, response, now),
- Self::Disk(cache) => cache.store(request, response, now),
- Self::AzureBlob(cache) => cache.store(request, response, now),
+ Self::Memory(cache) => cache.store(&Self::exact(request), response, now),
+ Self::Redis { cache, .. } => cache.store(&Self::exact(request), response, now),
+ Self::ValkeySemantic { cache, scope, .. } => {
+ cache.store(&Self::semantic(request, scope), response, now)
+ }
++ Self::Disk(cache) => cache.store(&Self::exact(request), response, now),
+ Self::AzureBlob(cache) => cache.store(&Self::exact(request), response, now),
}
}
@@@ -297,18 -175,10 +330,21 @@@
now: Duration,
) -> Result<PartialHits, Error> {
match self {
- Self::Memory(cache) => cache.lookup_batch(requests, now),
- Self::Redis { cache, .. } => cache.lookup_batch(requests, now),
- Self::Disk(cache) => cache.lookup_batch(requests, now),
- Self::AzureBlob(cache) => cache.lookup_batch(requests, now),
+ Self::Memory(cache) => {
+ let requests = requests.iter().map(Self::exact).collect::<Vec<_>>();
+ cache.lookup_batch(&requests, now)
+ }
+ Self::Redis { cache, .. } => {
+ let requests = requests.iter().map(Self::exact).collect::<Vec<_>>();
+ cache.lookup_batch(&requests, now)
+ }
+ Self::ValkeySemantic { .. } => Err(Error::UnsupportedOperation),
++ Self::Disk(cache) => {
++ cache.lookup_batch(&requests.iter().map(Self::exact).collect::<Vec<_>>(), now)
++ }
+ Self::AzureBlob(cache) => {
+ cache.lookup_batch(&requests.iter().map(Self::exact).collect::<Vec<_>>(), now)
+ }
}
}
@@@ -318,43 -188,10 +354,44 @@@
now: Duration,
) -> Result<Option<Value>, Error> {
match self {
- Self::Memory(cache) => cache.async_lookup(request, now).await,
- Self::Redis { cache, .. } => cache.async_lookup(request, now).await,
- Self::Disk(cache) => cache.async_lookup(request, now).await,
- Self::AzureBlob(cache) => cache.async_lookup(request, now).await,
+ Self::Memory(cache) => cache.async_lookup(&Self::exact(request), now).await,
+ Self::Redis { cache, .. } => cache.async_lookup(&Self::exact(request), now).await,
+ Self::ValkeySemantic { cache, scope, .. } => {
+ cache
+ .async_lookup(&Self::semantic(request, scope), now)
+ .await
+ }
++ Self::Disk(cache) => cache.async_lookup(&Self::exact(request), now).await,
+ Self::AzureBlob(cache) => cache.async_lookup(&Self::exact(request), now).await,
+ }
+ }
+
+ pub(super) fn async_lookup_py<'py>(
+ &self,
+ py: Python<'py>,
+ request: NativeRequest,
+ ) -> PyResult<Bound<'py, PyAny>> {
+ match self {
- Self::Memory(_) | Self::Redis { .. } | Self::AzureBlob(_) => {
++ Self::Memory(_) | Self::Redis { .. } | Self::Disk(_) | Self::AzureBlob(_) => {
+ let service = self.clone();
+ litellm_host_python::run_async(
+ py,
+ async move { service.async_lookup(&request, super::request::now()).await },
+ super::cache_error,
+ )
+ }
+ Self::ValkeySemantic {
+ cache,
+ embedder,
+ scope,
+ } => drive_semantic(
+ py,
+ SemanticEmbedExecution::lookup(
+ Arc::clone(cache.backend_arc()),
+ embedder.clone(),
+ Self::semantic(&request, scope),
+ ),
+ ),
}
}
@@@ -381,56 -210,9 +418,61 @@@
Self::Redis {
cache,
buffer: Some(buffer),
- } => buffer.async_store(cache, request, response, now).await,
- Self::Disk(cache) => cache.async_store(request, response, now).await,
- Self::AzureBlob(cache) => cache.async_store(request, response, now).await,
+ } => {
+ buffer
+ .async_store(cache, &Self::exact(request), response, now)
+ .await
+ }
+ Self::ValkeySemantic { cache, scope, .. } => {
+ cache
+ .async_store(&Self::semantic(request, scope), response, now)
+ .await
+ }
++ Self::Disk(cache) => {
++ cache
++ .async_store(&Self::exact(request), response, now)
++ .await
++ }
+ Self::AzureBlob(cache) => {
+ cache
+ .async_store(&Self::exact(request), response, now)
+ .await
+ }
+ }
+ }
+
+ pub(super) fn async_store_py<'py>(
+ &self,
+ py: Python<'py>,
+ request: NativeRequest,
+ response: Value,
+ ) -> PyResult<Bound<'py, PyAny>> {
+ match self {
- Self::Memory(_) | Self::Redis { .. } | Self::AzureBlob(_) => {
++ Self::Memory(_) | Self::Redis { .. } | Self::Disk(_) | Self::AzureBlob(_) => {
+ let service = self.clone();
+ litellm_host_python::run_async(
+ py,
+ async move {
+ service
+ .async_store(&request, response, super::request::now())
+ .await
+ },
+ super::cache_error,
+ )
+ }
+ Self::ValkeySemantic {
+ cache,
+ embedder,
+ scope,
+ } => drive_semantic(
+ py,
+ SemanticEmbedExecution::store(
+ Arc::clone(cache.backend_arc()),
+ embedder.clone(),
+ Self::semantic(&request, scope),
+ response,
+ ),
+ ),
}
}
@@@ -440,20 -222,10 +482,25 @@@
now: Duration,
) -> Result<PartialHits, Error> {
match self {
- Self::Memory(cache) => cache.async_lookup_batch(requests, now).await,
- Self::Redis { cache, .. } => cache.async_lookup_batch(requests, now).await,
- Self::Disk(cache) => cache.async_lookup_batch(requests, now).await,
- Self::AzureBlob(cache) => cache.async_lookup_batch(requests, now).await,
+ Self::Memory(cache) => {
+ let requests = requests.iter().map(Self::exact).collect::<Vec<_>>();
+ cache.async_lookup_batch(&requests, now).await
+ }
+ Self::Redis { cache, .. } => {
+ let requests = requests.iter().map(Self::exact).collect::<Vec<_>>();
+ cache.async_lookup_batch(&requests, now).await
+ }
+ Self::ValkeySemantic { .. } => Err(Error::UnsupportedOperation),
++ Self::Disk(cache) => {
++ cache
++ .async_lookup_batch(&requests.iter().map(Self::exact).collect::<Vec<_>>(), now)
++ .await
++ }
+ Self::AzureBlob(cache) => {
+ cache
+ .async_lookup_batch(&requests.iter().map(Self::exact).collect::<Vec<_>>(), now)
+ .await
+ }
}
}
@@@ -463,74 -235,10 +510,81 @@@
now: Duration,
) -> Result<(), Error> {
match self {
- Self::Memory(cache) => cache.async_store_batch(entries, now).await,
- Self::Redis { cache, .. } => cache.async_store_batch(entries, now).await,
- Self::Disk(cache) => cache.async_store_batch(entries, now).await,
- Self::AzureBlob(cache) => cache.async_store_batch(entries, now).await,
+ Self::Memory(cache) => {
+ let entries = entries
+ .into_iter()
+ .map(|(request, value)| (Self::exact(&request), value))
+ .collect();
+ cache.async_store_batch(entries, now).await
+ }
+ Self::Redis { cache, .. } => {
+ let entries = entries
+ .into_iter()
+ .map(|(request, value)| (Self::exact(&request), value))
+ .collect();
+ cache.async_store_batch(entries, now).await
+ }
+ Self::ValkeySemantic { cache, scope, .. } => {
+ let entries = entries
+ .into_iter()
+ .map(|(request, value)| (Self::semantic(&request, scope), value))
+ .collect();
+ cache.async_store_batch(entries, now).await
+ }
++ Self::Disk(cache) => {
++ let entries = entries
++ .into_iter()
++ .map(|(request, value)| (Self::exact(&request), value))
++ .collect();
++ cache.async_store_batch(entries, now).await
++ }
+ Self::AzureBlob(cache) => {
+ let entries = entries
+ .into_iter()
+ .map(|(request, value)| (Self::exact(&request), value))
+ .collect();
+ cache.async_store_batch(entries, now).await
+ }
+ }
+ }
+
+ pub(super) fn async_store_batch_py<'py>(
+ &self,
+ py: Python<'py>,
+ entries: Vec<(NativeRequest, Value)>,
+ ) -> PyResult<Bound<'py, PyAny>> {
+ match self {
- Self::Memory(_) | Self::Redis { .. } | Self::AzureBlob(_) => {
++ Self::Memory(_) | Self::Redis { .. } | Self::Disk(_) | Self::AzureBlob(_) => {
+ let service = self.clone();
+ litellm_host_python::run_async(
+ py,
+ async move {
+ service
+ .async_store_batch(entries, super::request::now())
+ .await
+ },
+ super::cache_error,
+ )
+ }
+ Self::ValkeySemantic {
+ cache,
+ embedder,
+ scope,
+ } => {
+ let (requests, responses): (Vec<_>, Vec<_>) = entries
+ .into_iter()
+ .map(|(request, response)| (Self::semantic(&request, scope), response))
+ .unzip();
+ drive_semantic(
+ py,
+ SemanticEmbedExecution::store_batch(
+ Arc::clone(cache.backend_arc()),
+ embedder.clone(),
+ requests,
+ responses,
+ ),
+ )
+ }
}
}
@@@ -543,7 -251,7 +597,8 @@@
}
cache.async_flush().await
}
+ Self::ValkeySemantic { .. } => Err(Error::UnsupportedOperation),
+ Self::Disk(cache) => cache.async_flush().await,
Self::AzureBlob(cache) => cache.async_flush().await,
}
}
@@@ -552,7 -260,7 +607,8 @@@
match self {
Self::Memory(cache) => cache.test_connection().await,
Self::Redis { cache, .. } => cache.test_connection().await,
+ Self::ValkeySemantic { cache, .. } => cache.test_connection().await,
+ Self::Disk(cache) => cache.test_connection().await,
Self::AzureBlob(cache) => cache.test_connection().await,
}
}