diff --git a/spring-ai-client-chat/src/main/java/org/springframework/ai/chat/client/advisor/AbstractChatMemoryAdvisor.java b/spring-ai-client-chat/src/main/java/org/springframework/ai/chat/client/advisor/AbstractChatMemoryAdvisor.java index 214ec9671..c39a654b3 100644 --- a/spring-ai-client-chat/src/main/java/org/springframework/ai/chat/client/advisor/AbstractChatMemoryAdvisor.java +++ b/spring-ai-client-chat/src/main/java/org/springframework/ai/chat/client/advisor/AbstractChatMemoryAdvisor.java @@ -53,13 +53,6 @@ public abstract class AbstractChatMemoryAdvisor implements CallAdvisor, Strea */ public static final String CHAT_MEMORY_RETRIEVE_SIZE_KEY = "chat_memory_response_size"; - /** - * The default conversation id to use when no conversation id is provided. - * @deprecated in favor of {@link ChatMemory#DEFAULT_CONVERSATION_ID}. - */ - @Deprecated - public static final String DEFAULT_CHAT_MEMORY_CONVERSATION_ID = "default"; - /** * The default chat memory retrieve size to use when no retrieve size is provided. */ diff --git a/spring-ai-client-chat/src/main/java/org/springframework/ai/chat/memory/InMemoryChatMemory.java b/spring-ai-client-chat/src/main/java/org/springframework/ai/chat/memory/InMemoryChatMemory.java deleted file mode 100644 index 3056bb5de..000000000 --- a/spring-ai-client-chat/src/main/java/org/springframework/ai/chat/memory/InMemoryChatMemory.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2023-2024 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.ai.chat.memory; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.springframework.ai.chat.messages.Message; - -/** - * The InMemoryChatMemory class is an implementation of the ChatMemory interface that - * represents an in-memory storage for chat conversation history. - * - * This class stores the conversation history in a ConcurrentHashMap, where the keys are - * the conversation IDs and the values are lists of messages representing the conversation - * history. - * - * @see ChatMemory - * @author Christian Tzolov - * @since 1.0.0 M1 - * @deprecated in favor of {@link MessageWindowChatMemory}, which internally uses - * {@link InMemoryChatMemoryRepository}. - */ -@Deprecated -public class InMemoryChatMemory implements ChatMemory { - - Map> conversationHistory = new ConcurrentHashMap<>(); - - @Override - public void add(String conversationId, List messages) { - this.conversationHistory.putIfAbsent(conversationId, new ArrayList<>()); - this.conversationHistory.get(conversationId).addAll(messages); - } - - @Override - public List get(String conversationId, int lastN) { - List all = this.conversationHistory.get(conversationId); - return all != null ? all.stream().skip(Math.max(0, all.size() - lastN)).toList() : List.of(); - } - - @Override - public void clear(String conversationId) { - this.conversationHistory.remove(conversationId); - } - -} diff --git a/spring-ai-client-chat/src/test/java/org/springframework/ai/chat/client/ChatClientAdvisorTests.java b/spring-ai-client-chat/src/test/java/org/springframework/ai/chat/client/ChatClientAdvisorTests.java index 43d82c763..e833f0cd9 100644 --- a/spring-ai-client-chat/src/test/java/org/springframework/ai/chat/client/ChatClientAdvisorTests.java +++ b/spring-ai-client-chat/src/test/java/org/springframework/ai/chat/client/ChatClientAdvisorTests.java @@ -29,7 +29,8 @@ import reactor.core.publisher.Flux; import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor; import org.springframework.ai.chat.memory.ChatMemory; -import org.springframework.ai.chat.memory.InMemoryChatMemory; +import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository; +import org.springframework.ai.chat.memory.MessageWindowChatMemory; import org.springframework.ai.chat.messages.AssistantMessage; import org.springframework.ai.chat.messages.Message; import org.springframework.ai.chat.messages.MessageType; @@ -76,8 +77,10 @@ public class ChatClientAdvisorTests { .willReturn(new ChatResponse(List.of(new Generation(new AssistantMessage("Your name is John"))), chatResponseMetadata)); - // Initialize an in-memory chat memory to store conversation history - ChatMemory chatMemory = new InMemoryChatMemory(); + // Initialize a message window chat memory to store conversation history + ChatMemory chatMemory = MessageWindowChatMemory.builder() + .chatMemoryRepository(new InMemoryChatMemoryRepository()) + .build(); // Build a ChatClient with default system text and a memory advisor var chatClient = ChatClient.builder(this.chatModel) @@ -153,8 +156,10 @@ public class ChatClientAdvisorTests { return state; })); - // Initialize an in-memory chat memory to store conversation history - ChatMemory chatMemory = new InMemoryChatMemory(); + // Initialize a message window chat memory to store conversation history + ChatMemory chatMemory = MessageWindowChatMemory.builder() + .chatMemoryRepository(new InMemoryChatMemoryRepository()) + .build(); // Build a ChatClient with default system text and a memory advisor var chatClient = ChatClient.builder(this.chatModel) diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chatclient.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chatclient.adoc index 3af0b275a..ab5629bba 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chatclient.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chatclient.adoc @@ -534,7 +534,7 @@ IMPORTANT: Refer to the new xref:api/chat-memory.adoc[Chat Memory] documentation The interface `ChatMemory` represents a storage for chat conversation history. It provides methods to add messages to a conversation, retrieve messages from a conversation, and clear the conversation history. -There are currently four implementations: `InMemoryChatMemory`, `CassandraChatMemory`, `Neo4jChatMemory`, and `JdbcChatMemory`, which provide storage for chat conversation history in-memory, persisted with `time-to-live` in Cassandra, and persisted without `time-to-live` in Neo4j and Jdbc, respectively. +There are currently four implementations: `CassandraChatMemory`, `Neo4jChatMemory`, and `JdbcChatMemory`, which provide storage for chat conversation history in-memory, persisted with `time-to-live` in Cassandra, and persisted without `time-to-live` in Neo4j and Jdbc, respectively. === CassandraChatMemory diff --git a/spring-ai-integration-tests/src/test/java/org/springframework/ai/integration/tests/client/advisor/RetrievalAugmentationAdvisorIT.java b/spring-ai-integration-tests/src/test/java/org/springframework/ai/integration/tests/client/advisor/RetrievalAugmentationAdvisorIT.java index b72c77eff..da17ed0ed 100644 --- a/spring-ai-integration-tests/src/test/java/org/springframework/ai/integration/tests/client/advisor/RetrievalAugmentationAdvisorIT.java +++ b/spring-ai-integration-tests/src/test/java/org/springframework/ai/integration/tests/client/advisor/RetrievalAugmentationAdvisorIT.java @@ -26,13 +26,13 @@ import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor; import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor; -import org.springframework.ai.chat.memory.InMemoryChatMemory; +import org.springframework.ai.chat.evaluation.RelevancyEvaluator; +import org.springframework.ai.chat.memory.MessageWindowChatMemory; import org.springframework.ai.chat.model.ChatResponse; import org.springframework.ai.document.Document; import org.springframework.ai.document.DocumentReader; import org.springframework.ai.evaluation.EvaluationRequest; import org.springframework.ai.evaluation.EvaluationResponse; -import org.springframework.ai.chat.evaluation.RelevancyEvaluator; import org.springframework.ai.integration.tests.TestApplication; import org.springframework.ai.openai.OpenAiChatModel; import org.springframework.ai.rag.advisor.RetrievalAugmentationAdvisor; @@ -133,7 +133,9 @@ class RetrievalAugmentationAdvisorIT { @Test void ragWithCompression() { - MessageChatMemoryAdvisor memoryAdvisor = MessageChatMemoryAdvisor.builder(new InMemoryChatMemory()).build(); + MessageChatMemoryAdvisor memoryAdvisor = MessageChatMemoryAdvisor + .builder(MessageWindowChatMemory.builder().build()) + .build(); RetrievalAugmentationAdvisor ragAdvisor = RetrievalAugmentationAdvisor.builder() .queryTransformers(CompressionQueryTransformer.builder() diff --git a/spring-ai-model/src/main/java/org/springframework/ai/chat/prompt/TemplateFormat.java b/spring-ai-model/src/main/java/org/springframework/ai/chat/prompt/TemplateFormat.java deleted file mode 100644 index 159ab8c28..000000000 --- a/spring-ai-model/src/main/java/org/springframework/ai/chat/prompt/TemplateFormat.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2023-2025 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.ai.chat.prompt; - -import org.springframework.ai.template.TemplateRenderer; - -/** - * @deprecated in favor of {@link TemplateRenderer}. - */ -@Deprecated -public enum TemplateFormat { - - ST("ST"); - - private final String value; - - TemplateFormat(String value) { - this.value = value; - } - - public static TemplateFormat fromValue(String value) { - for (TemplateFormat templateFormat : TemplateFormat.values()) { - if (templateFormat.getValue().equals(value)) { - return templateFormat; - } - } - throw new IllegalArgumentException("Invalid TemplateFormat value: " + value); - } - - public String getValue() { - return this.value; - } - -}