LarFii commited on
Commit
7b8ac1f
·
1 Parent(s): 54c6ae9

Add clear_cache

Browse files
Files changed (2) hide show
  1. README.md +34 -0
  2. lightrag/lightrag.py +49 -0
README.md CHANGED
@@ -751,6 +751,40 @@ rag.delete_by_entity("Project Gutenberg")
751
  rag.delete_by_doc_id("doc_id")
752
  ```
753
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
754
  ## LightRAG init parameters
755
 
756
  <details>
 
751
  rag.delete_by_doc_id("doc_id")
752
  ```
753
 
754
+ ## Cache
755
+
756
+ <details>
757
+ <summary> <b>Clear Cache</b> </summary>
758
+
759
+ You can clear the LLM response cache with different modes:
760
+
761
+ ```python
762
+ # Clear all cache
763
+ await rag.aclear_cache()
764
+
765
+ # Clear local mode cache
766
+ await rag.aclear_cache(modes=["local"])
767
+
768
+ # Clear extraction cache
769
+ await rag.aclear_cache(modes=["default"])
770
+
771
+ # Clear multiple modes
772
+ await rag.aclear_cache(modes=["local", "global", "hybrid"])
773
+
774
+ # Synchronous version
775
+ rag.clear_cache(modes=["local"])
776
+ ```
777
+
778
+ Valid modes are:
779
+ - `"default"`: Extraction cache
780
+ - `"naive"`: Naive search cache
781
+ - `"local"`: Local search cache
782
+ - `"global"`: Global search cache
783
+ - `"hybrid"`: Hybrid search cache
784
+ - `"mix"`: Mix search cache
785
+
786
+ </details>
787
+
788
  ## LightRAG init parameters
789
 
790
  <details>
lightrag/lightrag.py CHANGED
@@ -1588,3 +1588,52 @@ class LightRAG:
1588
  f"Storage implementation '{storage_name}' requires the following "
1589
  f"environment variables: {', '.join(missing_vars)}"
1590
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1588
  f"Storage implementation '{storage_name}' requires the following "
1589
  f"environment variables: {', '.join(missing_vars)}"
1590
  )
1591
+
1592
+ async def aclear_cache(self, modes: list[str] | None = None) -> None:
1593
+ """Clear cache data from the LLM response cache storage.
1594
+
1595
+ Args:
1596
+ modes (list[str] | None): Modes of cache to clear. Options: ["default", "naive", "local", "global", "hybrid", "mix"].
1597
+ "default" represents extraction cache.
1598
+ If None, clears all cache.
1599
+
1600
+ Example:
1601
+ # Clear all cache
1602
+ await rag.aclear_cache()
1603
+
1604
+ # Clear local mode cache
1605
+ await rag.aclear_cache(modes=["local"])
1606
+
1607
+ # Clear extraction cache
1608
+ await rag.aclear_cache(modes=["default"])
1609
+ """
1610
+ if not self.llm_response_cache:
1611
+ logger.warning("No cache storage configured")
1612
+ return
1613
+
1614
+ valid_modes = ["default", "naive", "local", "global", "hybrid", "mix"]
1615
+
1616
+ # Validate input
1617
+ if modes and not all(mode in valid_modes for mode in modes):
1618
+ raise ValueError(f"Invalid mode. Valid modes are: {valid_modes}")
1619
+
1620
+ try:
1621
+ # Reset the cache storage for specified mode
1622
+ if modes:
1623
+ await self.llm_response_cache.delete(modes)
1624
+ logger.info(f"Cleared cache for modes: {modes}")
1625
+ else:
1626
+ # Clear all modes
1627
+ await self.llm_response_cache.delete(valid_modes)
1628
+ logger.info("Cleared all cache")
1629
+
1630
+ await self.llm_response_cache.index_done_callback()
1631
+
1632
+ except Exception as e:
1633
+ logger.error(f"Error while clearing cache: {e}")
1634
+
1635
+ def clear_cache(self, modes: list[str] | None = None) -> None:
1636
+ """Synchronous version of aclear_cache."""
1637
+ return always_get_an_event_loop().run_until_complete(
1638
+ self.aclear_cache(modes)
1639
+ )