langchain-ai / langchain-redis

MIT License
12 stars 8 forks source link

Unexpected behavior with RedisSemanticCache in LangChain - Cache entries not updating as expected #22

Closed spacepirate0001 closed 1 month ago

spacepirate0001 commented 1 month ago

I've run into an interesting issue while testing RedisSemanticCache, and I'm hoping someone can shed some light on whether this is expected behavior or potentially a bug.

Setup:

Observed Behavior:

  1. Cache entries in Redis remain unchanged across different queries
  2. The same response content is repeated for all queries, regardless of the input
  3. Switching models adds a new key, but the behavior persists

Pip List:

langchain_openai==0.2.1
langchain-community==0.3.1
langchain==0.3.1
langchain-core==0.3.6
langchain-redis==0.1.0

Code Snippet:

r_s = RedisSemanticCache(
    embeddings=openai_embeddings,
    distance_threshold=0.9,
    redis_url="redis://localhost:6379"
)

set_llm_cache(r_s) 

# Test queries 
test_queries = [ 
    "What is 1+1?", 
    "Tell me one joke", 
    "What is 21+1?", 
    "What is 1+1?" # Repeated query to test caching 
] 

for query in test_queries: 
    result = model_1.invoke(query, config=runnable_conf) 
    print(f"Result: {result}") 
    print_cache_contents()

Output Highlights:

  1. All queries return the same result: "1 + 1 equals 2."
  2. Cache contents show only one entry that doesn't change: Key: llmcache:cfbba30096aef098692dfd596bfe8caa01e49dc95fa8285f529fd762090b0050, Type: hash, Value: <hash type>
  3. Switching to model_2 adds a new key but exhibits the same behavior

Questions:

  1. Is this the expected behavior for RedisSemanticCache?
  2. Should the cache not update with new entries for different queries?
  3. Are there any configuration settings I might be missing?

I'd greatly appreciate any insights or suggestions on how to properly implement and utilize RedisSemanticCache. Thanks in advance for your help!

spacepirate0001 commented 1 month ago

here is the full log in case its useful!

Invoking model_2 with 'What is 1+1?'
Result: content='1+1 equals 2.' additional_kwargs={'refusal': None} response_metadata={'token_usage': {'completion_tokens': 7, 'prompt_tokens': 14, 'total_tokens': 21, 'completion_tokens_details': None}, 'model_name': 'gpt-35-turbo-16k', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None, 'content_filter_results': {}} id='run-5f88e392-131e-45d4-a55b-f009ae0887ac-0' usage_metadata={'input_tokens': 14, 'output_tokens': 7, 'total_tokens': 21}
Time taken: 0.94 seconds
Current cache contents:
Key: llmcache:cfbba30096aef098692dfd596bfe8caa01e49dc95fa8285f529fd762090b0050, Type: hash, Value: <hash type>

==================================================
Invoking model_2 with 'Tell me one joke'
Result: content='1+1 equals 2.' additional_kwargs={'refusal': None} response_metadata={'token_usage': {'completion_tokens': 7, 'prompt_tokens': 14, 'total_tokens': 21, 'completion_tokens_details': None}, 'model_name': 'gpt-35-turbo-16k', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None, 'content_filter_results': {}} id='run-5f88e392-131e-45d4-a55b-f009ae0887ac-0' usage_metadata={'input_tokens': 14, 'output_tokens': 7, 'total_tokens': 21}
Time taken: 0.56 seconds
Current cache contents:
Key: llmcache:cfbba30096aef098692dfd596bfe8caa01e49dc95fa8285f529fd762090b0050, Type: hash, Value: <hash type>

==================================================
Invoking model_2 with 'What is 21+1?'
Result: content='1+1 equals 2.' additional_kwargs={'refusal': None} response_metadata={'token_usage': {'completion_tokens': 7, 'prompt_tokens': 14, 'total_tokens': 21, 'completion_tokens_details': None}, 'model_name': 'gpt-35-turbo-16k', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None, 'content_filter_results': {}} id='run-5f88e392-131e-45d4-a55b-f009ae0887ac-0' usage_metadata={'input_tokens': 14, 'output_tokens': 7, 'total_tokens': 21}
Time taken: 0.17 seconds
Current cache contents:
Key: llmcache:cfbba30096aef098692dfd596bfe8caa01e49dc95fa8285f529fd762090b0050, Type: hash, Value: <hash type>

==================================================
Invoking model_2 with 'What is 1+1?'
Result: content='1+1 equals 2.' additional_kwargs={'refusal': None} response_metadata={'token_usage': {'completion_tokens': 7, 'prompt_tokens': 14, 'total_tokens': 21, 'completion_tokens_details': None}, 'model_name': 'gpt-35-turbo-16k', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None, 'content_filter_results': {}} id='run-5f88e392-131e-45d4-a55b-f009ae0887ac-0' usage_metadata={'input_tokens': 14, 'output_tokens': 7, 'total_tokens': 21}Time taken: 0.17 seconds
Current cache contents:
Key: llmcache:cfbba30096aef098692dfd596bfe8caa01e49dc95fa8285f529fd762090b0050, Type: hash, Value: <hash type>

==================================================
spacepirate0001 commented 1 month ago

After adjusting distance_threshold all works as expected!

bsbodden commented 1 month ago

@spacepirate0001 glad you got it working!