From 24c165420827305e813f4b6d501f93d18f6d46a4 Mon Sep 17 00:00:00 2001 From: Tamas Molnar Date: Thu, 13 Jul 2023 15:39:04 +0200 Subject: [PATCH] Fix SQLAlchemy LLM cache clear (#7653) Fixes #7652 Description: This is a fix for clearing the cache for SQL Alchemy based LLM caches. The langchain.llm_cache.clear() did not take effect for SQLite cache. Reason: it didn't commit the deletion database change. See SQLAlchemy documentation for proper usage: https://docs.sqlalchemy.org/en/20/orm/session_basics.html#opening-and-closing-a-session https://docs.sqlalchemy.org/en/20/orm/session_basics.html#deleting @hwchase17 @baskaryan --------- Co-authored-by: Tamas Molnar --- langchain/cache.py | 1 + tests/unit_tests/test_cache.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/langchain/cache.py b/langchain/cache.py index 14720c119f..5ba675e49d 100644 --- a/langchain/cache.py +++ b/langchain/cache.py @@ -180,6 +180,7 @@ class SQLAlchemyCache(BaseCache): """Clear cache.""" with Session(self.engine) as session: session.query(self.cache_schema).delete() + session.commit() class SQLiteCache(SQLAlchemyCache): diff --git a/tests/unit_tests/test_cache.py b/tests/unit_tests/test_cache.py index ffc0d801b3..39339c236f 100644 --- a/tests/unit_tests/test_cache.py +++ b/tests/unit_tests/test_cache.py @@ -139,6 +139,26 @@ def test_chat_model_caching_params() -> None: ) +def test_llm_cache_clear() -> None: + prompt = "How are you?" + response = "Test response" + cached_response = "Cached test response" + llm = FakeListLLM(responses=[response]) + if langchain.llm_cache: + langchain.llm_cache.update( + prompt=prompt, + llm_string=create_llm_string(llm), + return_val=[Generation(text=cached_response)], + ) + langchain.llm_cache.clear() + assert llm(prompt) == response + else: + raise ValueError( + "The cache not set. This should never happen, as the pytest fixture " + "`set_cache_and_teardown` always sets the cache." + ) + + def create_llm_string(llm: Union[BaseLLM, BaseChatModel]) -> str: _dict: Dict = llm.dict() _dict["stop"] = None