The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix redis cache chat model (#8041)
Bagatur7717c24fc4ca
diff --git a/langchain/cache.py b/langchain/cache.pyindex 5ba675e49d..b589a29891 100644--- a/langchain/cache.py+++ b/langchain/cache.py@@ -5,6 +5,7 @@ import hashlibimport inspectimport jsonimport logging+import warningsfrom abc import ABC, abstractmethodfrom datetime import timedeltafrom typing import (@@ -34,7 +35,7 @@ except ImportError:from langchain.embeddings.base import Embeddingsfrom langchain.load.dump import dumpsfrom langchain.load.load import loads-from langchain.schema import Generation+from langchain.schema import ChatGeneration, Generationfrom langchain.vectorstores.redis import Redis as RedisVectorstorelogger = logging.getLogger(__file__)@@ -232,6 +233,12 @@ class RedisCache(BaseCache):"RedisCache only supports caching of normal LLM generations, "f"got {type(gen)}")+ if isinstance(gen, ChatGeneration):+ warnings.warn(+ "NOTE: Generation has not been cached. RedisCache does not"+ " support caching ChatModel outputs."+ )+ return# Write to a Redis HASHkey = self._key(prompt, llm_string)self.redis.hset(@@ -345,6 +352,12 @@ class RedisSemanticCache(BaseCache):"RedisSemanticCache only supports caching of "f"normal LLM generations, got {type(gen)}")+ if isinstance(gen, ChatGeneration):+ warnings.warn(+ "NOTE: Generation has not been cached. RedisSentimentCache does not"+ " support caching ChatModel outputs."+ )+ returnllm_cache = self._get_llm_cache(llm_string)# Write to vectorstoremetadata = {diff --git a/tests/integration_tests/cache/test_redis_cache.py b/tests/integration_tests/cache/test_redis_cache.pyindex 7ce18bf65e..b39cc0fac9 100644--- a/tests/integration_tests/cache/test_redis_cache.py+++ b/tests/integration_tests/cache/test_redis_cache.py@@ -1,10 +1,12 @@"""Test Redis cache functionality."""+import pytestimport redisimport langchainfrom langchain.cache import RedisCache, RedisSemanticCachefrom langchain.schema import Generation, LLMResultfrom tests.integration_tests.vectorstores.fake_embeddings import FakeEmbeddings+from tests.unit_tests.llms.fake_chat_model import FakeChatModelfrom tests.unit_tests.llms.fake_llm import FakeLLMREDIS_TEST_URL = "redis://localhost:6379"@@ -28,6 +30,17 @@ def test_redis_cache() -> None:langchain.llm_cache.redis.flushall()+def test_redis_cache_chat() -> None:+ langchain.llm_cache = RedisCache(redis_=redis.Redis.from_url(REDIS_TEST_URL))+ llm = FakeChatModel()+ params = llm.dict()+ params["stop"] = None+ with pytest.warns():+ llm.predict("foo")+ llm.predict("foo")+ langchain.llm_cache.redis.flushall()++def test_redis_semantic_cache() -> None:langchain.llm_cache = RedisSemanticCache(embedding=FakeEmbeddings(), redis_url=REDIS_TEST_URL, score_threshold=0.1@@ -53,3 +66,14 @@ def test_redis_semantic_cache() -> None:# expect different output now without cached resultassert output != expected_outputlangchain.llm_cache.clear(llm_string=llm_string)+++def test_redis_semantic_cache_chat() -> None:+ langchain.llm_cache = RedisCache(redis_=redis.Redis.from_url(REDIS_TEST_URL))+ llm = FakeChatModel()+ params = llm.dict()+ params["stop"] = None+ with pytest.warns():+ llm.predict("foo")+ llm.predict("foo")+ langchain.llm_cache.redis.flushall()