Add clear_cache
This commit is contained in:
34
README.md
34
README.md
@@ -751,6 +751,40 @@ rag.delete_by_entity("Project Gutenberg")
|
|||||||
rag.delete_by_doc_id("doc_id")
|
rag.delete_by_doc_id("doc_id")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Cache
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary> <b>Clear Cache</b> </summary>
|
||||||
|
|
||||||
|
You can clear the LLM response cache with different modes:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Clear all cache
|
||||||
|
await rag.aclear_cache()
|
||||||
|
|
||||||
|
# Clear local mode cache
|
||||||
|
await rag.aclear_cache(modes=["local"])
|
||||||
|
|
||||||
|
# Clear extraction cache
|
||||||
|
await rag.aclear_cache(modes=["default"])
|
||||||
|
|
||||||
|
# Clear multiple modes
|
||||||
|
await rag.aclear_cache(modes=["local", "global", "hybrid"])
|
||||||
|
|
||||||
|
# Synchronous version
|
||||||
|
rag.clear_cache(modes=["local"])
|
||||||
|
```
|
||||||
|
|
||||||
|
Valid modes are:
|
||||||
|
- `"default"`: Extraction cache
|
||||||
|
- `"naive"`: Naive search cache
|
||||||
|
- `"local"`: Local search cache
|
||||||
|
- `"global"`: Global search cache
|
||||||
|
- `"hybrid"`: Hybrid search cache
|
||||||
|
- `"mix"`: Mix search cache
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
## LightRAG init parameters
|
## LightRAG init parameters
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
|
@@ -1588,3 +1588,52 @@ class LightRAG:
|
|||||||
f"Storage implementation '{storage_name}' requires the following "
|
f"Storage implementation '{storage_name}' requires the following "
|
||||||
f"environment variables: {', '.join(missing_vars)}"
|
f"environment variables: {', '.join(missing_vars)}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def aclear_cache(self, modes: list[str] | None = None) -> None:
|
||||||
|
"""Clear cache data from the LLM response cache storage.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
modes (list[str] | None): Modes of cache to clear. Options: ["default", "naive", "local", "global", "hybrid", "mix"].
|
||||||
|
"default" represents extraction cache.
|
||||||
|
If None, clears all cache.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
# Clear all cache
|
||||||
|
await rag.aclear_cache()
|
||||||
|
|
||||||
|
# Clear local mode cache
|
||||||
|
await rag.aclear_cache(modes=["local"])
|
||||||
|
|
||||||
|
# Clear extraction cache
|
||||||
|
await rag.aclear_cache(modes=["default"])
|
||||||
|
"""
|
||||||
|
if not self.llm_response_cache:
|
||||||
|
logger.warning("No cache storage configured")
|
||||||
|
return
|
||||||
|
|
||||||
|
valid_modes = ["default", "naive", "local", "global", "hybrid", "mix"]
|
||||||
|
|
||||||
|
# Validate input
|
||||||
|
if modes and not all(mode in valid_modes for mode in modes):
|
||||||
|
raise ValueError(f"Invalid mode. Valid modes are: {valid_modes}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Reset the cache storage for specified mode
|
||||||
|
if modes:
|
||||||
|
await self.llm_response_cache.delete(modes)
|
||||||
|
logger.info(f"Cleared cache for modes: {modes}")
|
||||||
|
else:
|
||||||
|
# Clear all modes
|
||||||
|
await self.llm_response_cache.delete(valid_modes)
|
||||||
|
logger.info("Cleared all cache")
|
||||||
|
|
||||||
|
await self.llm_response_cache.index_done_callback()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error while clearing cache: {e}")
|
||||||
|
|
||||||
|
def clear_cache(self, modes: list[str] | None = None) -> None:
|
||||||
|
"""Synchronous version of aclear_cache."""
|
||||||
|
return always_get_an_event_loop().run_until_complete(
|
||||||
|
self.aclear_cache(modes)
|
||||||
|
)
|
||||||
|
Reference in New Issue
Block a user