ooai_llm.cache¶
LangChain cache bootstrap helpers.
- Purpose:
Provide small utilities for resolving a project-local LLM cache path and configuring the global LangChain cache.
- Design:
Keep imports lazy so the module can be imported without LangChain.
Default to SQLite because it is file-backed, simple, and local.
Support networked caches through optional Redis and Upstash Redis clients.
Reuse
ooai_llm.settings.AppSettingsfor path resolution.
Examples
>>> from pathlib import Path
>>> settings = AppSettings(app_root=Path.cwd())
>>> resolve_llm_cache_path(settings).name
'langchain_llm_cache.sqlite3'
Attributes¶
Classes¶
Wrap a LangChain cache and add namespace/profile material to cache keys. |
Functions¶
|
Resolve the effective LLM cache path. |
|
Build a SQLite-backed LangChain cache. |
|
Build an in-memory LangChain cache. |
|
Build a SQLAlchemy-backed LangChain cache. |
|
Build a Redis-backed LangChain cache. |
|
Build an Upstash Redis-backed LangChain cache. |
|
Build the configured LangChain cache backend. |
|
Build the configured cache backend wrapped with namespace key material. |
|
Configure the global LangChain LLM cache. |
|
Normalize the per-model |
Module Contents¶
- class ooai_llm.cache.NamespacedCache(cache: langchain_core.caches.BaseCache, *, namespace: str, profile_key: str)[source]¶
Bases:
langchain_core.caches.BaseCacheWrap a LangChain cache and add namespace/profile material to cache keys.
The wrapper rewrites only LangChain’s
llm_stringcomponent. It does not mutate prompt serialization, so chat prompt cache semantics remain delegated to the underlying cache implementation.- namespaced_llm_string(llm_string: str) str[source]¶
Return the LangChain cache key component with namespace material.
- lookup(prompt: str, llm_string: str) Any[source]¶
Look up a cached value using the namespaced key component.
- update(prompt: str, llm_string: str, return_val: Any) None[source]¶
Update a cached value using the namespaced key component.
- async alookup(prompt: str, llm_string: str) Any[source]¶
Async cache lookup when the wrapped cache supports it.
- ooai_llm.cache.resolve_llm_cache_path(settings: ooai_llm.settings.AppSettings, *, path: str | pathlib.Path | None = None) pathlib.Path[source]¶
Resolve the effective LLM cache path.
- Parameters:
settings – Application settings.
path – Optional explicit cache path override.
- Returns:
Resolved cache path.
- ooai_llm.cache.build_sqlite_cache(settings: ooai_llm.settings.AppSettings, *, path: str | pathlib.Path | None = None) Any[source]¶
Build a SQLite-backed LangChain cache.
- Parameters:
settings – Application settings.
path – Optional explicit cache path override.
- Returns:
SQLite cache instance.
- Raises:
ImportError – If
langchain_communityis not installed.
- ooai_llm.cache.build_memory_cache() Any[source]¶
Build an in-memory LangChain cache.
- Returns:
In-memory cache instance.
- Raises:
ImportError – If
langchain_communityis not installed.
- ooai_llm.cache.build_sqlalchemy_cache(settings: ooai_llm.settings.AppSettings, *, path: str | pathlib.Path | None = None) Any[source]¶
Build a SQLAlchemy-backed LangChain cache.
- Parameters:
settings – Application settings.
path – Optional SQLite path used when
sqlalchemy_urlis unset.
- Returns:
SQLAlchemy cache instance.
- Raises:
ImportError – If SQLAlchemy or LangChain community caches are missing.
- ooai_llm.cache.build_redis_cache(settings: ooai_llm.settings.AppSettings) Any[source]¶
Build a Redis-backed LangChain cache.
- Parameters:
settings – Application settings.
- Returns:
Redis cache instance.
- Raises:
ImportError – If
redisor LangChain community caches are missing.
- ooai_llm.cache.build_upstash_redis_cache(settings: ooai_llm.settings.AppSettings) Any[source]¶
Build an Upstash Redis-backed LangChain cache.
- Parameters:
settings – Application settings.
- Returns:
Upstash Redis cache instance.
- Raises:
ValueError – If the Upstash URL or token is missing.
ImportError – If
upstash_redisor LangChain community caches are missing.
- ooai_llm.cache.build_llm_cache(settings: ooai_llm.settings.AppSettings, *, path: str | pathlib.Path | None = None) Any[source]¶
Build the configured LangChain cache backend.
- Parameters:
settings – Application settings.
path – Optional SQLite path override for file-backed backends.
- Returns:
Cache object.
- Raises:
ValueError – If the configured backend is unsupported.
ImportError – If backend-specific dependencies are missing.
- ooai_llm.cache.build_namespaced_cache(settings: ooai_llm.settings.AppSettings, *, namespace: str, profile_key: str, path: str | pathlib.Path | None = None) NamespacedCache[source]¶
Build the configured cache backend wrapped with namespace key material.
- ooai_llm.cache.configure_global_llm_cache(settings: ooai_llm.settings.AppSettings, *, path: str | pathlib.Path | None = None) Any[source]¶
Configure the global LangChain LLM cache.
- Parameters:
settings – Application settings.
path – Optional explicit cache path override.
- Returns:
Cache object when enabled, otherwise
None.- Raises:
ValueError – If the configured backend is unsupported.
ImportError – If required LangChain cache packages are not installed.
- ooai_llm.cache.normalize_cache_argument(cache: BaseCache | bool | None) BaseCache | bool | None[source]¶
Normalize the per-model
cacheargument.- Parameters:
cache –
Trueto force the global cache,Falseto disable,Noneto inherit the global setting, or a concrete cache object.- Returns:
Unchanged normalized cache value.