Skip to content

Commit cb8d68b

Browse files
committed
move location of CHAT_MEMORY_CONVERSATION_ID_KEY
1 parent 1d335ed commit cb8d68b

File tree

9 files changed

+42
-47
lines changed

9 files changed

+42
-47
lines changed

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/chat/client/advisor/AbstractChatMemoryAdvisorIT.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737

3838
import static org.assertj.core.api.Assertions.assertThat;
3939

40-
import reactor.core.publisher.Flux;
41-
4240
/**
4341
* Abstract base class for chat memory advisor integration tests. Contains common test
4442
* logic to avoid duplication between different advisor implementations.
@@ -102,7 +100,7 @@ protected void testMultipleUserMessagesInPrompt() {
102100
Prompt prompt = new Prompt(messages);
103101

104102
String answer = chatClient.prompt(prompt)
105-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
103+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
106104
.call()
107105
.content();
108106

@@ -118,7 +116,7 @@ protected void testMultipleUserMessagesInPrompt() {
118116
// Send a follow-up question
119117
String followUpAnswer = chatClient.prompt()
120118
.user("What is my name?")
121-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
119+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
122120
.call()
123121
.content();
124122

@@ -154,7 +152,7 @@ protected void testMultipleUserMessagesInSamePrompt() {
154152

155153
// Send the prompt to the chat client
156154
String answer = chatClient.prompt(prompt)
157-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
155+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
158156
.call()
159157
.content();
160158

@@ -173,7 +171,7 @@ protected void testMultipleUserMessagesInSamePrompt() {
173171
// Act - Send a follow-up question
174172
String followUpAnswer = chatClient.prompt()
175173
.user("What is my name?")
176-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
174+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
177175
.call()
178176
.content();
179177

@@ -209,7 +207,7 @@ protected void testUseCustomConversationId() {
209207

210208
String answer = chatClient.prompt()
211209
.user(question)
212-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, customConversationId))
210+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, customConversationId))
213211
.call()
214212
.content();
215213

@@ -246,7 +244,7 @@ protected void testMaintainSeparateConversations() {
246244
// Act - First conversation
247245
String answer1 = chatClient.prompt()
248246
.user("My name is Alice.")
249-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId1))
247+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId1))
250248
.call()
251249
.content();
252250

@@ -255,7 +253,7 @@ protected void testMaintainSeparateConversations() {
255253
// Act - Second conversation
256254
String answer2 = chatClient.prompt()
257255
.user("My name is Bob.")
258-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId2))
256+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId2))
259257
.call()
260258
.content();
261259

@@ -273,7 +271,7 @@ protected void testMaintainSeparateConversations() {
273271
// Act - Follow-up in first conversation
274272
String followUpAnswer1 = chatClient.prompt()
275273
.user("What is my name?")
276-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId1))
274+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId1))
277275
.call()
278276
.content();
279277

@@ -282,7 +280,7 @@ protected void testMaintainSeparateConversations() {
282280
// Act - Follow-up in second conversation
283281
String followUpAnswer2 = chatClient.prompt()
284282
.user("What is my name?")
285-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId2))
283+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId2))
286284
.call()
287285
.content();
288286

@@ -333,7 +331,7 @@ protected void testHandleNonExistentConversation() {
333331

334332
String answer = chatClient.prompt()
335333
.user(question)
336-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, nonExistentId))
334+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, nonExistentId))
337335
.call()
338336
.content();
339337

@@ -390,7 +388,7 @@ protected void testHandleMultipleMessagesInReactiveMode() {
390388
List<String> responseList = new ArrayList<>();
391389
for (String message : List.of("My name is Charlie.", "I am 30 years old.", "I live in London.")) {
392390
String response = chatClient.prompt()
393-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
391+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
394392
.user(message)
395393
.call()
396394
.content();
@@ -408,7 +406,7 @@ protected void testHandleMultipleMessagesInReactiveMode() {
408406
assertThat(memoryMessages.get(4).getText()).isEqualTo("I live in London.");
409407

410408
String followUpAnswer = chatClient.prompt()
411-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
409+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
412410
.user("What is my name and where do I live?")
413411
.call()
414412
.content();

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/chat/client/advisor/MessageChatMemoryAdvisorIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void shouldHandleMultipleUserMessagesInPrompt() {
105105

106106
// Send the prompt to the chat client
107107
String answer = chatClient.prompt(prompt)
108-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
108+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
109109
.call()
110110
.content();
111111

@@ -124,7 +124,7 @@ void shouldHandleMultipleUserMessagesInPrompt() {
124124
// Send a follow-up question
125125
String followUpAnswer = chatClient.prompt()
126126
.user("What is my name?")
127-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
127+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
128128
.call()
129129
.content();
130130

spring-ai-client-chat/src/main/java/org/springframework/ai/chat/client/advisor/AbstractChatMemoryAdvisor.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@
4747
*/
4848
public abstract class AbstractChatMemoryAdvisor<T> implements BaseAdvisor {
4949

50-
/**
51-
* The key to retrieve the chat memory conversation id from the context.
52-
*/
53-
public static final String CHAT_MEMORY_CONVERSATION_ID_KEY = "chat_memory_conversation_id";
54-
5550
/**
5651
* The chat memory store.
5752
*/
@@ -129,12 +124,12 @@ protected T getChatMemoryStore() {
129124
* @return the conversation id
130125
*/
131126
protected String doGetConversationId(Map<String, Object> context) {
132-
if (context == null || !context.containsKey(CHAT_MEMORY_CONVERSATION_ID_KEY)) {
127+
if (context == null || !context.containsKey(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY)) {
133128
logger.warn("No conversation ID found in context; using defaultConversationId '{}'.",
134129
this.defaultConversationId);
135130
}
136-
return context != null && context.containsKey(CHAT_MEMORY_CONVERSATION_ID_KEY)
137-
? context.get(CHAT_MEMORY_CONVERSATION_ID_KEY).toString() : this.defaultConversationId;
131+
return context != null && context.containsKey(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY)
132+
? context.get(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY).toString() : this.defaultConversationId;
138133
}
139134

140135
@Override

spring-ai-client-chat/src/main/java/org/springframework/ai/chat/client/observation/DefaultChatClientObservationConvention.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818

1919
import io.micrometer.common.KeyValue;
2020
import io.micrometer.common.KeyValues;
21-
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
21+
2222
import org.springframework.ai.chat.client.advisor.api.Advisor;
2323
import org.springframework.ai.chat.client.observation.ChatClientObservationDocumentation.LowCardinalityKeyNames;
24+
import org.springframework.ai.chat.memory.ChatMemory;
2425
import org.springframework.ai.chat.observation.ChatModelObservationDocumentation;
2526
import org.springframework.ai.model.tool.ToolCallingChatOptions;
2627
import org.springframework.ai.observation.ObservabilityHelper;
@@ -110,9 +111,7 @@ protected KeyValues conversationId(KeyValues keyValues, ChatClientObservationCon
110111
return keyValues;
111112
}
112113

113-
var conversationIdValue = context.getRequest()
114-
.context()
115-
.get(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY);
114+
var conversationIdValue = context.getRequest().context().get(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY);
116115

117116
if (!(conversationIdValue instanceof String conversationId) || !StringUtils.hasText(conversationId)) {
118117
return keyValues;

spring-ai-client-chat/src/test/java/org/springframework/ai/chat/client/observation/DefaultChatClientObservationConventionTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828

2929
import org.springframework.ai.chat.client.ChatClientRequest;
3030
import org.springframework.ai.chat.client.ChatClientResponse;
31-
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
3231
import org.springframework.ai.chat.client.advisor.api.CallAdvisor;
3332
import org.springframework.ai.chat.client.advisor.api.CallAdvisorChain;
3433
import org.springframework.ai.chat.client.observation.ChatClientObservationDocumentation.HighCardinalityKeyNames;
3534
import org.springframework.ai.chat.client.observation.ChatClientObservationDocumentation.LowCardinalityKeyNames;
35+
import org.springframework.ai.chat.memory.ChatMemory;
3636
import org.springframework.ai.chat.model.ChatModel;
3737
import org.springframework.ai.chat.prompt.Prompt;
3838
import org.springframework.ai.model.tool.ToolCallingChatOptions;
@@ -150,7 +150,7 @@ void shouldHaveOptionalKeyValues() {
150150
.toolNames("tool1", "tool2")
151151
.toolCallbacks(dummyFunction("toolCallback1"), dummyFunction("toolCallback2"))
152152
.build()))
153-
.context(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, "007")
153+
.context(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, "007")
154154
.build();
155155

156156
ChatClientObservationContext observationContext = ChatClientObservationContext.builder()

spring-ai-integration-tests/src/test/java/org/springframework/ai/integration/tests/client/advisor/RetrievalAugmentationAdvisorIT.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
2525

2626
import org.springframework.ai.chat.client.ChatClient;
27-
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
2827
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
2928
import org.springframework.ai.chat.evaluation.RelevancyEvaluator;
29+
import org.springframework.ai.chat.memory.ChatMemory;
3030
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
3131
import org.springframework.ai.chat.model.ChatResponse;
3232
import org.springframework.ai.document.Document;
@@ -152,8 +152,7 @@ void ragWithCompression() {
152152

153153
ChatResponse chatResponse1 = chatClient.prompt()
154154
.user("Where does the adventure of Anacletus and Birba take place?")
155-
.advisors(advisors -> advisors.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY,
156-
conversationId))
155+
.advisors(advisors -> advisors.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
157156
.call()
158157
.chatResponse();
159158

@@ -163,8 +162,7 @@ void ragWithCompression() {
163162

164163
ChatResponse chatResponse2 = chatClient.prompt()
165164
.user("Did they meet any cow?")
166-
.advisors(advisors -> advisors.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY,
167-
conversationId))
165+
.advisors(advisors -> advisors.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
168166
.call()
169167
.chatResponse();
170168

spring-ai-model/src/main/java/org/springframework/ai/chat/memory/ChatMemory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public interface ChatMemory {
3232

3333
String DEFAULT_CONVERSATION_ID = "default";
3434

35+
/**
36+
* The key to retrieve the chat memory conversation id from the context.
37+
*/
38+
String CHAT_MEMORY_CONVERSATION_ID_KEY = "chat_memory_conversation_id";
39+
3540
/**
3641
* Save the specified message in the chat memory for the specified conversation.
3742
*/

vector-stores/spring-ai-pgvector-store/src/test/java/org/springframework/ai/vectorstore/pgvector/PgVectorStoreVectorStoreChatMemoryAdvisorIT.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
import org.testcontainers.junit.jupiter.Testcontainers;
2626

2727
import org.springframework.ai.chat.client.ChatClient;
28-
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
2928
import org.springframework.ai.chat.client.advisor.vectorstore.VectorStoreChatMemoryAdvisor;
29+
import org.springframework.ai.chat.memory.ChatMemory;
3030
import org.springframework.ai.document.Document;
3131
import org.springframework.ai.embedding.EmbeddingModel;
3232
import org.springframework.ai.model.ApiKey;
@@ -86,7 +86,7 @@ void testUseCustomConversationId() throws Exception {
8686
// Send a prompt
8787
String answer = chatClient.prompt()
8888
.user("Say hello")
89-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
89+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
9090
.call()
9191
.content();
9292

@@ -124,7 +124,7 @@ void testSemanticSearchRetrievesRelevantMemory() throws Exception {
124124
// Send a semantically related query
125125
String answer = chatClient.prompt()
126126
.user("Where is the Eiffel Tower located?")
127-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
127+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
128128
.call()
129129
.content();
130130

@@ -159,7 +159,7 @@ void testSemanticSynonymRetrieval() throws Exception {
159159

160160
String answer = chatClient.prompt()
161161
.user("Tell me about cars.")
162-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
162+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
163163
.call()
164164
.content();
165165
assertThat(answer).satisfiesAnyOf(a -> assertThat(a).containsIgnoringCase("automobile"),
@@ -191,7 +191,7 @@ void testIrrelevantMessageExclusion() throws Exception {
191191

192192
String answer = chatClient.prompt()
193193
.user("What is the capital of Italy?")
194-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
194+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
195195
.call()
196196
.content();
197197
assertThat(answer).containsIgnoringCase("rome");
@@ -225,7 +225,7 @@ void testTopKSemanticRelevance() throws Exception {
225225

226226
String answer = chatClient.prompt()
227227
.user("What can you tell me about cats?")
228-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
228+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
229229
.call()
230230
.content();
231231
assertThat(answer).containsIgnoringCase("cat");
@@ -256,7 +256,7 @@ void testSemanticRetrievalWithParaphrasing() throws Exception {
256256

257257
String answer = chatClient.prompt()
258258
.user("Tell me about a fast animal leaping over another.")
259-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
259+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
260260
.call()
261261
.content();
262262
assertThat(answer).satisfiesAnyOf(a -> assertThat(a).containsIgnoringCase("fox"),
@@ -288,7 +288,7 @@ void testMultipleRelevantMemoriesTopK() throws Exception {
288288

289289
String answer = chatClient.prompt()
290290
.user("What fruits are red?")
291-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
291+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
292292
.call()
293293
.content();
294294
assertThat(answer).containsIgnoringCase("apple");
@@ -320,7 +320,7 @@ void testNoRelevantMemory() throws Exception {
320320

321321
String answer = chatClient.prompt()
322322
.user("What is the capital of Spain?")
323-
.advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
323+
.advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
324324
.call()
325325
.content();
326326
assertThat(answer).doesNotContain("sun");

vector-stores/spring-ai-pgvector-store/src/test/java/org/springframework/ai/vectorstore/pgvector/PgVectorStoreWithChatMemoryAdvisorIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
import org.mockito.ArgumentMatchers;
2828
import org.mockito.Mockito;
2929
import org.postgresql.ds.PGSimpleDataSource;
30-
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
3130
import org.testcontainers.containers.PostgreSQLContainer;
3231
import org.testcontainers.junit.jupiter.Container;
3332
import org.testcontainers.junit.jupiter.Testcontainers;
3433

3534
import org.springframework.ai.chat.client.ChatClient;
3635
import org.springframework.ai.chat.client.advisor.vectorstore.VectorStoreChatMemoryAdvisor;
36+
import org.springframework.ai.chat.memory.ChatMemory;
3737
import org.springframework.ai.chat.messages.AssistantMessage;
3838
import org.springframework.ai.chat.messages.SystemMessage;
3939
import org.springframework.ai.chat.model.ChatModel;
@@ -138,7 +138,7 @@ void advisedChatShouldHaveSimilarMessagesFromVectorStore() throws Exception {
138138
.prompt()
139139
.user("joke")
140140
.advisors(a -> a.advisors(VectorStoreChatMemoryAdvisor.builder(store).build())
141-
.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
141+
.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
142142
.call()
143143
.chatResponse();
144144

@@ -162,7 +162,7 @@ void advisedChatShouldHaveSimilarMessagesFromVectorStoreWhenSystemMessageProvide
162162
.system("You are a helpful assistant.")
163163
.user("joke")
164164
.advisors(a -> a.advisors(VectorStoreChatMemoryAdvisor.builder(store).build())
165-
.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
165+
.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
166166
.call()
167167
.chatResponse();
168168

0 commit comments

Comments
 (0)