|
| 1 | +/* |
| 2 | + * Copyright 2023-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ai.autoconfigure.chat.memory.neo4j; |
| 18 | + |
| 19 | +import com.datastax.driver.core.utils.UUIDs; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.springframework.ai.chat.memory.neo4j.Neo4jChatMemory; |
| 22 | +import org.springframework.ai.chat.memory.neo4j.Neo4jChatMemoryConfig; |
| 23 | +import org.springframework.ai.chat.messages.*; |
| 24 | +import org.springframework.ai.chat.messages.ToolResponseMessage.ToolResponse; |
| 25 | +import org.springframework.ai.model.Media; |
| 26 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 27 | +import org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration; |
| 28 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 29 | +import org.springframework.util.MimeType; |
| 30 | +import org.testcontainers.containers.Neo4jContainer; |
| 31 | +import org.testcontainers.junit.jupiter.Container; |
| 32 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 33 | +import org.testcontainers.utility.DockerImageName; |
| 34 | + |
| 35 | +import java.net.URI; |
| 36 | +import java.nio.charset.StandardCharsets; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +import static org.assertj.core.api.Assertions.assertThat; |
| 41 | + |
| 42 | +/** |
| 43 | + * @author Mick Semb Wever |
| 44 | + * @author Jihoon Kim |
| 45 | + * @since 1.0.0 |
| 46 | + */ |
| 47 | +@Testcontainers |
| 48 | +class Neo4jChatMemoryAutoConfigurationIT { |
| 49 | + |
| 50 | + static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("neo4j"); |
| 51 | + |
| 52 | + @SuppressWarnings({"rawtypes", "resource"}) |
| 53 | + @Container |
| 54 | + static Neo4jContainer neo4jContainer = (Neo4jContainer) new Neo4jContainer(DEFAULT_IMAGE_NAME.withTag("5")).withoutAuthentication().withExposedPorts(7474,7687); |
| 55 | + |
| 56 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 57 | + .withConfiguration( |
| 58 | + AutoConfigurations.of(Neo4jChatMemoryAutoConfiguration.class, Neo4jAutoConfiguration.class)); |
| 59 | + |
| 60 | + |
| 61 | + @Test |
| 62 | + void addAndGet() { |
| 63 | + this.contextRunner.withPropertyValues("spring.neo4j.uri=" + neo4jContainer.getBoltUrl()) |
| 64 | + .run(context -> { |
| 65 | + Neo4jChatMemory memory = context.getBean(Neo4jChatMemory.class); |
| 66 | + |
| 67 | + String sessionId = UUIDs.timeBased().toString(); |
| 68 | + assertThat(memory.get(sessionId, Integer.MAX_VALUE)).isEmpty(); |
| 69 | + |
| 70 | + UserMessage userMessage = new UserMessage("test question"); |
| 71 | + |
| 72 | + |
| 73 | + memory.add(sessionId, userMessage); |
| 74 | + List<Message> messages = memory.get(sessionId, Integer.MAX_VALUE); |
| 75 | + assertThat(messages).hasSize(1); |
| 76 | + assertThat(messages.get(0)).usingRecursiveAssertion().isEqualTo(userMessage); |
| 77 | + |
| 78 | + memory.clear(sessionId); |
| 79 | + assertThat(memory.get(sessionId, Integer.MAX_VALUE)).isEmpty(); |
| 80 | + |
| 81 | + AssistantMessage assistantMessage = new AssistantMessage("test answer", Map.of(), |
| 82 | + List.of(new AssistantMessage.ToolCall( |
| 83 | + "id", "type", "name", "arguments"))); |
| 84 | + |
| 85 | + memory.add(sessionId, List.of(userMessage, assistantMessage)); |
| 86 | + messages = memory.get(sessionId, Integer.MAX_VALUE); |
| 87 | + assertThat(messages).hasSize(2); |
| 88 | + assertThat(messages.get(1)).isEqualTo(userMessage); |
| 89 | + |
| 90 | + assertThat(messages.get(0)).isEqualTo(assistantMessage); |
| 91 | + memory.clear(sessionId); |
| 92 | + MimeType textPlain = MimeType.valueOf("text/plain"); |
| 93 | + List<Media> media = List.of(Media.builder().name("some media").id(UUIDs.random().toString()) |
| 94 | + .mimeType(textPlain).data("hello".getBytes(StandardCharsets.UTF_8)).build(), |
| 95 | + Media.builder().data(URI.create("http://www.google.com").toURL()).mimeType(textPlain).build()); |
| 96 | + UserMessage userMessageWithMedia = new UserMessage("Message with media", media); |
| 97 | + memory.add(sessionId, userMessageWithMedia); |
| 98 | + |
| 99 | + messages = memory.get(sessionId, Integer.MAX_VALUE); |
| 100 | + assertThat(messages.size()).isEqualTo(1); |
| 101 | + assertThat(messages.get(0)).isEqualTo(userMessageWithMedia); |
| 102 | + assertThat(((UserMessage)messages.get(0)).getMedia()).hasSize(2); |
| 103 | + assertThat(((UserMessage) messages.get(0)).getMedia()).usingRecursiveFieldByFieldElementComparator().isEqualTo(media); |
| 104 | + memory.clear(sessionId); |
| 105 | + ToolResponseMessage toolResponseMessage = new ToolResponseMessage(List.of( |
| 106 | + new ToolResponse("id", "name", "responseData"), |
| 107 | + new ToolResponse("id2", "name2", "responseData2")), |
| 108 | + Map.of("id", "id", "metadataKey", "metadata")); |
| 109 | + memory.add(sessionId, toolResponseMessage); |
| 110 | + messages = memory.get(sessionId, Integer.MAX_VALUE); |
| 111 | + assertThat(messages.size()).isEqualTo(1); |
| 112 | + assertThat(messages.get(0)).isEqualTo(toolResponseMessage); |
| 113 | + |
| 114 | + memory.clear(sessionId); |
| 115 | + SystemMessage sm = new SystemMessage("this is a System message"); |
| 116 | + memory.add(sessionId, sm); |
| 117 | + messages = memory.get(sessionId, Integer.MAX_VALUE); |
| 118 | + assertThat(messages).hasSize(1); |
| 119 | + assertThat(messages.get(0)).usingRecursiveAssertion().isEqualTo(sm); |
| 120 | + }); |
| 121 | + } |
| 122 | + @Test |
| 123 | + void setCustomConfiguration(){ |
| 124 | + final String sessionLabel = "LabelSession"; |
| 125 | + final String toolCallLabel = "LabelToolCall"; |
| 126 | + final String metadataLabel = "LabelMetadata"; |
| 127 | + final String messageLabel = "LabelMessage"; |
| 128 | + final String toolResponseLabel = "LabelToolResponse"; |
| 129 | + final String mediaLabel = "LabelMedia"; |
| 130 | + |
| 131 | + final String propertyBase = "spring.ai.chat.memory.neo4j.%s=%s"; |
| 132 | + this.contextRunner.withPropertyValues("spring.neo4j.uri=" + neo4jContainer.getBoltUrl(), |
| 133 | + propertyBase.formatted("sessionlabel", sessionLabel), |
| 134 | + propertyBase.formatted("toolcallLabel", toolCallLabel), |
| 135 | + propertyBase.formatted("metadatalabel", metadataLabel), |
| 136 | + propertyBase.formatted("messagelabel", messageLabel), |
| 137 | + propertyBase.formatted("toolresponselabel", toolResponseLabel), |
| 138 | + propertyBase.formatted("medialabel", mediaLabel)) |
| 139 | + .run(context -> { |
| 140 | + Neo4jChatMemory chatMemory = context.getBean(Neo4jChatMemory.class); |
| 141 | + Neo4jChatMemoryConfig config = chatMemory.getConfig(); |
| 142 | + assertThat(config.getMessageLabel()).isEqualTo(messageLabel); |
| 143 | + assertThat(config.getMediaLabel()).isEqualTo(mediaLabel); |
| 144 | + assertThat(config.getMetadataLabel()).isEqualTo(metadataLabel); |
| 145 | + assertThat(config.getSessionLabel()).isEqualTo(sessionLabel); |
| 146 | + assertThat(config.getToolResponseLabel()).isEqualTo(toolResponseLabel); |
| 147 | + assertThat(config.getToolCallLabel()).isEqualTo(toolCallLabel); |
| 148 | + }); |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | +} |
0 commit comments