From fc385ee1a792a2814b70a0f6047c2fe1b4b9bb7f Mon Sep 17 00:00:00 2001 From: Cyanty Date: Fri, 13 Jun 2025 15:35:51 +0800 Subject: [PATCH 1/4] fix(memory): Replace the outdated manual way of creating the ChatMemoryRepository - Update the document information and replace the unavailable creation method of ChatMemoryRepository. - Increase the create function to create Neo4jChatMemoryRepository, this way to manually create the way. Signed-off-by: Caiyu Liu Signed-off-by: Cyanty --- .../Neo4jChatMemoryRepositoryAutoConfiguration.java | 2 +- .../memory/repository/neo4j/Neo4jChatMemoryRepository.java | 4 ++++ .../repository/neo4j/Neo4jChatMemoryRepositoryIT.java | 2 +- .../main/antora/modules/ROOT/pages/api/chat-memory.adoc | 7 +++---- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-neo4j/src/main/java/org/springframework/ai/model/chat/memory/repository/neo4j/autoconfigure/Neo4jChatMemoryRepositoryAutoConfiguration.java b/auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-neo4j/src/main/java/org/springframework/ai/model/chat/memory/repository/neo4j/autoconfigure/Neo4jChatMemoryRepositoryAutoConfiguration.java index 970cb6be91c..e406b0850c1 100644 --- a/auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-neo4j/src/main/java/org/springframework/ai/model/chat/memory/repository/neo4j/autoconfigure/Neo4jChatMemoryRepositoryAutoConfiguration.java +++ b/auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-neo4j/src/main/java/org/springframework/ai/model/chat/memory/repository/neo4j/autoconfigure/Neo4jChatMemoryRepositoryAutoConfiguration.java @@ -53,7 +53,7 @@ public Neo4jChatMemoryRepository neo4jChatMemoryRepository(Neo4jChatMemoryReposi .withToolResponseLabel(properties.getToolResponseLabel()) .withDriver(driver); - return new Neo4jChatMemoryRepository(builder.build()); + return Neo4jChatMemoryRepository.create(builder.build()); } } diff --git a/memory/repository/spring-ai-model-chat-memory-repository-neo4j/src/main/java/org/springframework/ai/chat/memory/repository/neo4j/Neo4jChatMemoryRepository.java b/memory/repository/spring-ai-model-chat-memory-repository-neo4j/src/main/java/org/springframework/ai/chat/memory/repository/neo4j/Neo4jChatMemoryRepository.java index 21cdd80a54e..784f17d67dc 100644 --- a/memory/repository/spring-ai-model-chat-memory-repository-neo4j/src/main/java/org/springframework/ai/chat/memory/repository/neo4j/Neo4jChatMemoryRepository.java +++ b/memory/repository/spring-ai-model-chat-memory-repository-neo4j/src/main/java/org/springframework/ai/chat/memory/repository/neo4j/Neo4jChatMemoryRepository.java @@ -56,6 +56,10 @@ public Neo4jChatMemoryRepository(Neo4jChatMemoryRepositoryConfig config) { this.config = config; } + public static Neo4jChatMemoryRepository create(Neo4jChatMemoryRepositoryConfig conf) { + return new Neo4jChatMemoryRepository(conf); + } + @Override public List findConversationIds() { return this.config.getDriver() diff --git a/memory/repository/spring-ai-model-chat-memory-repository-neo4j/src/test/java/org/springframework/ai/chat/memory/repository/neo4j/Neo4jChatMemoryRepositoryIT.java b/memory/repository/spring-ai-model-chat-memory-repository-neo4j/src/test/java/org/springframework/ai/chat/memory/repository/neo4j/Neo4jChatMemoryRepositoryIT.java index 83ff42a71ae..d5bdd5c28ce 100644 --- a/memory/repository/spring-ai-model-chat-memory-repository-neo4j/src/test/java/org/springframework/ai/chat/memory/repository/neo4j/Neo4jChatMemoryRepositoryIT.java +++ b/memory/repository/spring-ai-model-chat-memory-repository-neo4j/src/test/java/org/springframework/ai/chat/memory/repository/neo4j/Neo4jChatMemoryRepositoryIT.java @@ -77,7 +77,7 @@ class Neo4jChatMemoryRepositoryIT { void setUp() { this.driver = Neo4jDriverFactory.create(neo4jContainer.getBoltUrl()); this.config = Neo4jChatMemoryRepositoryConfig.builder().withDriver(this.driver).build(); - this.chatMemoryRepository = new Neo4jChatMemoryRepository(this.config); + this.chatMemoryRepository = Neo4jChatMemoryRepository.create(this.config); } @AfterEach diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat-memory.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat-memory.adoc index 435751c8b56..7ce82f986e5 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat-memory.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat-memory.adoc @@ -222,7 +222,7 @@ If you'd rather create the `CassandraChatMemoryRepository` manually, you can do [source,java] ---- ChatMemoryRepository chatMemoryRepository = CassandraChatMemoryRepository - .create(CassandraChatMemoryConfig.builder().withCqlSession(cqlSession)); + .create(CassandraChatMemoryRepositoryConfig.builder().withCqlSession(cqlSession).build()); ChatMemory chatMemory = MessageWindowChatMemory.builder() .chatMemoryRepository(chatMemoryRepository) @@ -296,9 +296,8 @@ If you'd rather create the `Neo4jChatMemoryRepository` manually, you can do so b [source,java] ---- -ChatMemoryRepository chatMemoryRepository = Neo4jChatMemoryRepository.builder() - .driver(driver) - .build(); +ChatMemoryRepository chatMemoryRepository = Neo4jChatMemoryRepository + .create(Neo4jChatMemoryRepositoryConfig.builder().withDriver(driver).build()); ChatMemory chatMemory = MessageWindowChatMemory.builder() .chatMemoryRepository(chatMemoryRepository) From 0a42d56ba10e9cdcc78edd45a884daf63388caf1 Mon Sep 17 00:00:00 2001 From: Caiyu Liu Date: Fri, 13 Jun 2025 16:24:36 +0800 Subject: [PATCH 2/4] fix(memory): Replace the outdated manual way of creating the ChatMemoryRepository - Update the document information and replace the unavailable creation method of ChatMemoryRepository. - Increase the create function to create Neo4jChatMemoryRepository, this way to manually create the way. Signed-off-by: Caiyu Liu Signed-off-by: Cyanty From f635a963cd0eb3ffd8c5a624c635d9cbfc288bfb Mon Sep 17 00:00:00 2001 From: Cyanty Date: Fri, 13 Jun 2025 16:27:20 +0800 Subject: [PATCH 3/4] fix(memory): Replace the outdated manual way of creating the ChatMemoryRepository - Update the document information and replace the unavailable creation method of ChatMemoryRepository. - Increase the create function to create Neo4jChatMemoryRepository, this way to manually create the way. Signed-off-by: Cyanty From 0f06cb5a6aaefda25362edacd7963498da67d76a Mon Sep 17 00:00:00 2001 From: Cyanty Date: Sat, 14 Jun 2025 15:16:02 +0800 Subject: [PATCH 4/4] fix(memory): Replace the outdated manual way of creating the ChatMemoryRepository - Update the document information and replace the unavailable creation method of ChatMemoryRepository. - Increase the builder function to create Neo4jChatMemoryRepository, this way to manually create the way. Signed-off-by: Cyanty --- ...ChatMemoryRepositoryAutoConfiguration.java | 21 +++---- .../neo4j/Neo4jChatMemoryRepository.java | 60 +++++++++++++++++-- .../neo4j/Neo4jChatMemoryRepositoryIT.java | 6 +- .../modules/ROOT/pages/api/chat-memory.adoc | 5 +- 4 files changed, 70 insertions(+), 22 deletions(-) diff --git a/auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-neo4j/src/main/java/org/springframework/ai/model/chat/memory/repository/neo4j/autoconfigure/Neo4jChatMemoryRepositoryAutoConfiguration.java b/auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-neo4j/src/main/java/org/springframework/ai/model/chat/memory/repository/neo4j/autoconfigure/Neo4jChatMemoryRepositoryAutoConfiguration.java index e406b0850c1..5694456cbf0 100644 --- a/auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-neo4j/src/main/java/org/springframework/ai/model/chat/memory/repository/neo4j/autoconfigure/Neo4jChatMemoryRepositoryAutoConfiguration.java +++ b/auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-neo4j/src/main/java/org/springframework/ai/model/chat/memory/repository/neo4j/autoconfigure/Neo4jChatMemoryRepositoryAutoConfiguration.java @@ -19,7 +19,6 @@ import org.neo4j.driver.Driver; import org.springframework.ai.chat.memory.repository.neo4j.Neo4jChatMemoryRepository; -import org.springframework.ai.chat.memory.repository.neo4j.Neo4jChatMemoryRepositoryConfig; import org.springframework.ai.model.chat.memory.autoconfigure.ChatMemoryAutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -43,17 +42,15 @@ public class Neo4jChatMemoryRepositoryAutoConfiguration { @ConditionalOnMissingBean public Neo4jChatMemoryRepository neo4jChatMemoryRepository(Neo4jChatMemoryRepositoryProperties properties, Driver driver) { - - var builder = Neo4jChatMemoryRepositoryConfig.builder() - .withMediaLabel(properties.getMediaLabel()) - .withMessageLabel(properties.getMessageLabel()) - .withMetadataLabel(properties.getMetadataLabel()) - .withSessionLabel(properties.getSessionLabel()) - .withToolCallLabel(properties.getToolCallLabel()) - .withToolResponseLabel(properties.getToolResponseLabel()) - .withDriver(driver); - - return Neo4jChatMemoryRepository.create(builder.build()); + return Neo4jChatMemoryRepository.builder() + .driver(driver) + .mediaLabel(properties.getMediaLabel()) + .messageLabel(properties.getMessageLabel()) + .metadataLabel(properties.getMetadataLabel()) + .sessionLabel(properties.getSessionLabel()) + .toolCallLabel(properties.getToolCallLabel()) + .toolResponseLabel(properties.getToolResponseLabel()) + .build(); } } diff --git a/memory/repository/spring-ai-model-chat-memory-repository-neo4j/src/main/java/org/springframework/ai/chat/memory/repository/neo4j/Neo4jChatMemoryRepository.java b/memory/repository/spring-ai-model-chat-memory-repository-neo4j/src/main/java/org/springframework/ai/chat/memory/repository/neo4j/Neo4jChatMemoryRepository.java index 784f17d67dc..a26ec1e9f58 100644 --- a/memory/repository/spring-ai-model-chat-memory-repository-neo4j/src/main/java/org/springframework/ai/chat/memory/repository/neo4j/Neo4jChatMemoryRepository.java +++ b/memory/repository/spring-ai-model-chat-memory-repository-neo4j/src/main/java/org/springframework/ai/chat/memory/repository/neo4j/Neo4jChatMemoryRepository.java @@ -25,6 +25,7 @@ import java.util.UUID; import java.util.stream.Collectors; +import org.neo4j.driver.Driver; import org.neo4j.driver.Session; import org.neo4j.driver.Transaction; import org.neo4j.driver.TransactionContext; @@ -38,6 +39,7 @@ import org.springframework.ai.chat.messages.UserMessage; import org.springframework.ai.content.Media; import org.springframework.ai.content.MediaContent; +import org.springframework.util.Assert; import org.springframework.util.MimeType; /** @@ -52,14 +54,10 @@ public final class Neo4jChatMemoryRepository implements ChatMemoryRepository { private final Neo4jChatMemoryRepositoryConfig config; - public Neo4jChatMemoryRepository(Neo4jChatMemoryRepositoryConfig config) { + private Neo4jChatMemoryRepository(Neo4jChatMemoryRepositoryConfig config) { this.config = config; } - public static Neo4jChatMemoryRepository create(Neo4jChatMemoryRepositoryConfig conf) { - return new Neo4jChatMemoryRepository(conf); - } - @Override public List findConversationIds() { return this.config.getDriver() @@ -330,4 +328,56 @@ private List> convertMediaToMap(List media) { return mediaMaps; } + public static Builder builder() { + return new Builder(); + } + + public static final class Builder { + + private final Neo4jChatMemoryRepositoryConfig.Builder builder = Neo4jChatMemoryRepositoryConfig.builder(); + + private Builder() { + } + + public Builder driver(Driver driver) { + this.builder.withDriver(driver); + return this; + } + + public Builder sessionLabel(String sessionLabel) { + this.builder.withSessionLabel(sessionLabel); + return this; + } + + public Builder toolCallLabel(String toolCallLabel) { + this.builder.withToolCallLabel(toolCallLabel); + return this; + } + + public Builder metadataLabel(String metadataLabel) { + this.builder.withMetadataLabel(metadataLabel); + return this; + } + + public Builder messageLabel(String messageLabel) { + this.builder.withMessageLabel(messageLabel); + return this; + } + + public Builder toolResponseLabel(String toolResponseLabel) { + this.builder.withToolResponseLabel(toolResponseLabel); + return this; + } + + public Builder mediaLabel(String mediaLabel) { + this.builder.withMediaLabel(mediaLabel); + return this; + } + + public Neo4jChatMemoryRepository build() { + Assert.notNull(this.builder.getDriver(), "Driver cannot be null"); + return new Neo4jChatMemoryRepository(this.builder.build()); + } + } + } diff --git a/memory/repository/spring-ai-model-chat-memory-repository-neo4j/src/test/java/org/springframework/ai/chat/memory/repository/neo4j/Neo4jChatMemoryRepositoryIT.java b/memory/repository/spring-ai-model-chat-memory-repository-neo4j/src/test/java/org/springframework/ai/chat/memory/repository/neo4j/Neo4jChatMemoryRepositoryIT.java index d5bdd5c28ce..cdbd8ffdb17 100644 --- a/memory/repository/spring-ai-model-chat-memory-repository-neo4j/src/test/java/org/springframework/ai/chat/memory/repository/neo4j/Neo4jChatMemoryRepositoryIT.java +++ b/memory/repository/spring-ai-model-chat-memory-repository-neo4j/src/test/java/org/springframework/ai/chat/memory/repository/neo4j/Neo4jChatMemoryRepositoryIT.java @@ -67,7 +67,7 @@ class Neo4jChatMemoryRepositoryIT { .withoutAuthentication() .withExposedPorts(7474, 7687); - private ChatMemoryRepository chatMemoryRepository; + private Neo4jChatMemoryRepository chatMemoryRepository; private Driver driver; @@ -76,8 +76,8 @@ class Neo4jChatMemoryRepositoryIT { @BeforeEach void setUp() { this.driver = Neo4jDriverFactory.create(neo4jContainer.getBoltUrl()); - this.config = Neo4jChatMemoryRepositoryConfig.builder().withDriver(this.driver).build(); - this.chatMemoryRepository = Neo4jChatMemoryRepository.create(this.config); + this.chatMemoryRepository = Neo4jChatMemoryRepository.builder().driver(driver).build(); + this.config = chatMemoryRepository.getConfig(); } @AfterEach diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat-memory.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat-memory.adoc index 7ce82f986e5..e8b9fe8948a 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat-memory.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat-memory.adoc @@ -296,8 +296,9 @@ If you'd rather create the `Neo4jChatMemoryRepository` manually, you can do so b [source,java] ---- -ChatMemoryRepository chatMemoryRepository = Neo4jChatMemoryRepository - .create(Neo4jChatMemoryRepositoryConfig.builder().withDriver(driver).build()); +ChatMemoryRepository chatMemoryRepository = Neo4jChatMemoryRepository.builder() + .driver(driver) + .build(); ChatMemory chatMemory = MessageWindowChatMemory.builder() .chatMemoryRepository(chatMemoryRepository)