Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bsbodden
Copy link

  • 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]

…ntations

Add comprehensive Redis-backed features to enhance Spring AI:

* 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.

Signed-off-by: Brian Sam-Bodden <[email protected]>
@bsbodden bsbodden force-pushed the redis/semantic-caching-advisor-and-chat-memory branch from 19bcf33 to e31e620 Compare February 23, 2025 04:30
@jruaux
Copy link
Contributor

jruaux commented Mar 3, 2025

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

@markpollack markpollack self-assigned this Mar 25, 2025
@markpollack markpollack added this to the 1.0.0-M7 milestone Mar 25, 2025
@ilayaperumalg ilayaperumalg modified the milestones: 1.0.0-M7, 1.0.0-RC1 Apr 10, 2025
@ThomasVitale
Copy link
Contributor

@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

@bsbodden
Copy link
Author

bsbodden commented May 2, 2025

@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!

@markpollack
Copy link
Member

In the meantime I rebased the PR and am a bit stuck - see #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.

bsbodden added a commit to bsbodden/spring-ai that referenced this pull request May 4, 2025
…nectivity

  Refactor Redis-based chat memory implementation to:
  - Implement ChatMemoryRepository interface as requested in PR spring-projects#2295
  - Fix Redis connection issues in integration tests reported in PR spring-projects#2982
  - Optimize conversation ID lookup with server-side deduplication
  - Add configurable result limits to avoid Redis cursor size limitations
  - Implement robust fallback mechanism for query failures
  - Enhance support for metadata, toolcalls, and media in messages
  - Add comprehensive test coverage with reliable Redis connections
bsbodden added a commit to bsbodden/spring-ai that referenced this pull request May 4, 2025
…nectivity

  Refactor Redis-based chat memory implementation to:
  - Implement ChatMemoryRepository interface as requested in PR spring-projects#2295
  - Fix Redis connection issues in integration tests reported in PR spring-projects#2982
  - Optimize conversation ID lookup with server-side deduplication
  - Add configurable result limits to avoid Redis cursor size limitations
  - Implement robust fallback mechanism for query failures
  - Enhance support for metadata, toolcalls, and media in messages
  - Add comprehensive test coverage with reliable Redis connections

  Signed-off-by: Brian Sam-Bodden <[email protected]>
bsbodden added a commit to bsbodden/spring-ai that referenced this pull request May 4, 2025
…nectivity

  Refactor Redis-based chat memory implementation to:
  - Implement ChatMemoryRepository interface as requested in PR spring-projects#2295
  - Fix Redis connection issues in integration tests reported in PR spring-projects#2982
  - Optimize conversation ID lookup with server-side deduplication
  - Add configurable result limits to avoid Redis cursor size limitations
  - Implement robust fallback mechanism for query failures
  - Enhance support for metadata, toolcalls, and media in messages
  - Add comprehensive test coverage with reliable Redis connections

Signed-off-by: Brian Sam-Bodden <[email protected]>
bsbodden added a commit to bsbodden/spring-ai that referenced this pull request May 4, 2025
…nectivity

  Refactor Redis-based chat memory implementation to:
  - Implement ChatMemoryRepository interface as requested in PR spring-projects#2295
  - Fix Redis connection issues in integration tests reported in PR spring-projects#2982
  - Optimize conversation ID lookup with server-side deduplication
  - Add configurable result limits to avoid Redis cursor size limitations
  - Implement robust fallback mechanism for query failures
  - Enhance support for metadata, toolcalls, and media in messages
  - Add comprehensive test coverage with reliable Redis connections

Signed-off-by: Brian Sam-Bodden <[email protected]>
@markpollack markpollack modified the milestones: 1.0.0-RC1-triage, 1.0.x May 9, 2025
bsbodden added a commit to bsbodden/spring-ai that referenced this pull request Jun 12, 2025
…nectivity

  Refactor Redis-based chat memory implementation to:
  - Implement ChatMemoryRepository interface as requested in PR spring-projects#2295
  - Fix Redis connection issues in integration tests reported in PR spring-projects#2982
  - Optimize conversation ID lookup with server-side deduplication
  - Add configurable result limits to avoid Redis cursor size limitations
  - Implement robust fallback mechanism for query failures
  - Enhance support for metadata, toolcalls, and media in messages
  - Add comprehensive test coverage with reliable Redis connections

Signed-off-by: Brian Sam-Bodden <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants