|
| 1 | +package org.springframework.ai.integration.tests.client.advisor; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; |
| 5 | +import org.springframework.ai.chat.client.ChatClient; |
| 6 | +import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor; |
| 7 | +import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor; |
| 8 | +import org.springframework.ai.chat.memory.InMemoryChatMemory; |
| 9 | +import org.springframework.ai.chat.model.ChatResponse; |
| 10 | +import org.springframework.ai.openai.OpenAiChatModel; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.boot.test.context.SpringBootTest; |
| 13 | + |
| 14 | +import static org.assertj.core.api.Assertions.assertThat; |
| 15 | + |
| 16 | +/** |
| 17 | + * Integration tests for {@link MessageChatMemoryAdvisor}. |
| 18 | + * |
| 19 | + * @author Alexandros Pappas |
| 20 | + */ |
| 21 | +@SpringBootTest |
| 22 | +@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".*") |
| 23 | +class MessageChatMemoryAdvisorIT { |
| 24 | + |
| 25 | + @Autowired |
| 26 | + OpenAiChatModel openAiChatModel; |
| 27 | + |
| 28 | + @Test |
| 29 | + void chatMemoryStoresAndRecallsConversation() { |
| 30 | + var chatMemory = new InMemoryChatMemory(); |
| 31 | + var conversationId = "test-conversation"; |
| 32 | + |
| 33 | + var memoryAdvisor = MessageChatMemoryAdvisor.builder(chatMemory).conversationId(conversationId).build(); |
| 34 | + |
| 35 | + var chatClient = ChatClient.builder(openAiChatModel).defaultAdvisors(memoryAdvisor).build(); |
| 36 | + |
| 37 | + // First interaction |
| 38 | + ChatResponse response1 = chatClient.prompt().user("Hello, my name is John.").call().chatResponse(); |
| 39 | + |
| 40 | + assertThat(response1).isNotNull(); |
| 41 | + String assistantReply1 = response1.getResult().getOutput().getText(); |
| 42 | + System.out.println("Assistant reply 1: " + assistantReply1); |
| 43 | + |
| 44 | + // Second interaction - Verify memory recall |
| 45 | + ChatResponse response2 = chatClient.prompt().user("What is my name?").call().chatResponse(); |
| 46 | + |
| 47 | + assertThat(response2).isNotNull(); |
| 48 | + String assistantReply2 = response2.getResult().getOutput().getText(); |
| 49 | + System.out.println("Assistant reply 2: " + assistantReply2); |
| 50 | + |
| 51 | + assertThat(assistantReply2.toLowerCase()).contains("john"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void separateConversationsDoNotMixMemory() { |
| 56 | + var chatMemory = new InMemoryChatMemory(); |
| 57 | + |
| 58 | + var memoryAdvisor = MessageChatMemoryAdvisor.builder(chatMemory).build(); |
| 59 | + |
| 60 | + var chatClient = ChatClient.builder(openAiChatModel).defaultAdvisors(memoryAdvisor).build(); |
| 61 | + |
| 62 | + // First conversation |
| 63 | + chatClient.prompt() |
| 64 | + .user("Remember my secret code is blue.") |
| 65 | + .advisors(advisors -> advisors.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, "conv-1")) |
| 66 | + .call(); |
| 67 | + |
| 68 | + // Second conversation |
| 69 | + ChatResponse response = chatClient.prompt() |
| 70 | + .user("Do you remember my secret code?") |
| 71 | + .advisors(advisors -> advisors.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, "conv-2")) |
| 72 | + .call() |
| 73 | + .chatResponse(); |
| 74 | + |
| 75 | + assertThat(response).isNotNull(); |
| 76 | + String assistantReply = response.getResult().getOutput().getText(); |
| 77 | + System.out.println("Assistant reply: " + assistantReply); |
| 78 | + |
| 79 | + assertThat(assistantReply.toLowerCase()).doesNotContain("blue"); |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments