spring-ai icon indicating copy to clipboard operation
spring-ai copied to clipboard

feat(redis): Add Redis-based semantic caching and chat memory implementations

Open bsbodden opened this issue 1 year ago • 7 comments

  • Add semantic caching for chat responses:

    • SemanticCache interface and Redis implementation using vector similarity
    • SemanticCacheAdvisor for intercepting and caching chat responses
    • Uses vector search to cache and retrieve responses based on query similarity
    • Support for TTL-based cache expiration
    • Improves response times and reduces API costs for similar questions
  • Add Redis-based chat memory implementation:

    • RedisChatMemory using RedisJSON + RediSearch for conversation storage
    • Configurable RedisChatMemoryConfig with builder pattern support
    • Message TTL, ordering, multi-conversation and batch operations
    • Efficient conversation history retrieval using RediSearch indexes
  • Add integration tests:

    • Comprehensive test coverage using TestContainers
    • Tests for semantic caching features and chat memory operations
    • Integration test for RedisVectorStore with VectorStoreChatMemoryAdvisor
    • Verify chat completion augmentation with vector store content

The Redis implementations enable efficient storage and retrieval of chat responses and conversation history, with semantic search capabilities and configurable persistence options.

/cc @jruaux

Signed-off-by: Brian Sam-Bodden [email protected]

bsbodden avatar Feb 23 '25 04:02 bsbodden

Looks good! Thanks @bsbodden. @tzolov Can we get this merged?

jruaux avatar Mar 03 '25 17:03 jruaux

@bsbodden thank you so much for this! We have just delivered some changes to the ChatMemory API which include the introduction of a ChatMemoryRepository API to separate the two different concerns: memory management strategy and storage mechanism. For example, the JdbcChatMemory has now been superseded by JdbcChatMemoryRepository. It would be great if we could use the new approach directly for Redis.

Upgrade Notes: https://docs.spring.io/spring-ai/reference/upgrade-notes.html#_chat_memory New Chat Memory Docs: https://docs.spring.io/spring-ai/reference/api/chat-memory.html PR with the change: https://github.com/spring-projects/spring-ai/pull/2890

ThomasVitale avatar Apr 26 '25 11:04 ThomasVitale

@bsbodden thank you so much for this! We have just delivered some changes to the ChatMemory API which include the introduction of a ChatMemoryRepository API to separate the two different concerns: memory management strategy and storage mechanism. For example, the JdbcChatMemory has now been superseded by JdbcChatMemoryRepository. It would be great if we could use the new approach directly for Redis.

Upgrade Notes: https://docs.spring.io/spring-ai/reference/upgrade-notes.html#_chat_memory New Chat Memory Docs: https://docs.spring.io/spring-ai/reference/api/chat-memory.html PR with the change: #2890

@ThomasVitale I will make the needed changes and re-submit the branch! Thanks for looking into this. Cheers!

bsbodden avatar May 02 '25 16:05 bsbodden

In the meantime I rebased the PR and am a bit stuck - see https://github.com/spring-projects/spring-ai/pull/2982

As thomas mentioned, we will definitely need to separate this out into multiple modules, but in the meantime, if @bsbodden you could look at the reworked PR to see what is broken, that would be a big help.

Thanks for the PR, I've been meaning to get to it for a while.

markpollack avatar May 03 '25 22:05 markpollack

Thanks, it's been a while. Would like to get this in for 1.1 M1

markpollack avatar Jul 17 '25 16:07 markpollack

Thanks, it's been a while. Would like to get this in for 1.1 M1

BTW, the latest is here https://github.com/spring-projects/spring-ai/pull/2991

bsbodden avatar Jul 20 '25 16:07 bsbodden

@markpollack I thought this was making it into 1.1 - could you help us understand what is missing to get this merged? Thank you!

raphaeldelio avatar Nov 13 '25 08:11 raphaeldelio

Hi, What's missing is between keyboard and monitor. There has just been so much of a flurry of activity good things like this get lost. We are going to 'sneak' it into 1.1.1 despite the patch release nature. I'll be working on this today and will ping if I have questions. My sincere apologies!

markpollack avatar Nov 17 '25 17:11 markpollack

closing in favor of #2991

markpollack avatar Nov 17 '25 17:11 markpollack