Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-parent</artifactId>
<version>1.1.0-SNAPSHOT</version>
<relativePath>../../../../../../pom.xml</relativePath>
</parent>

<artifactId>spring-ai-autoconfigure-model-chat-memory-repository-elasticsearch</artifactId>
<name>Spring AI Auto Configuration - Chat Memory Repository - Elasticsearch</name>
<description>Spring AI Auto Configuration for Elasticsearch Chat Memory Repository</description>

<url>https://github.com/spring-projects/spring-ai</url>

<scm>
<url>https://github.com/spring-projects/spring-ai</url>
<connection>git://github.com/spring-projects/spring-ai.git</connection>
<developerConnection>[email protected]:spring-projects/spring-ai.git</developerConnection>
</scm>

<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-model-chat-memory-repository-elasticsearch</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-autoconfigure-model-chat-memory</artifactId>
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<optional>true</optional>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-test</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>elasticsearch</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.model.chat.memory.repository.elasticsearch.autoconfigure;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.client.RestClient;

import org.springframework.ai.chat.memory.repository.elasticsearch.ElasticSearchChatMemoryRepository;
import org.springframework.ai.chat.memory.repository.elasticsearch.ElasticSearchChatMemoryRepositoryConfig;
import org.springframework.ai.model.chat.memory.autoconfigure.ChatMemoryAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
* {@link AutoConfiguration Auto-configuration} for
* {@link ElasticSearchChatMemoryRepository}.
*
* @author Fu Jian
* @since 1.1.0
*/
@AutoConfiguration(after = ElasticsearchRestClientAutoConfiguration.class, before = ChatMemoryAutoConfiguration.class)
@ConditionalOnClass({ ElasticSearchChatMemoryRepository.class, RestClient.class })
@EnableConfigurationProperties(ElasticSearchChatMemoryRepositoryProperties.class)
public class ElasticSearchChatMemoryRepositoryAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public ElasticSearchChatMemoryRepositoryConfig elasticSearchChatMemoryRepositoryConfig(
ElasticSearchChatMemoryRepositoryProperties properties, RestClient restClient) {
ElasticsearchClient elasticsearchClient = new ElasticsearchClient(new RestClientTransport(restClient,
new JacksonJsonpMapper(
new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false))))
.withTransportOptions(t -> t.addHeader("user-agent", "spring-ai-chat-memory elastic-java"));
return ElasticSearchChatMemoryRepositoryConfig.builder()
.withClient(elasticsearchClient)
.withIndexName(properties.getIndexName())
.build();
}

@Bean
@ConditionalOnMissingBean
public ElasticSearchChatMemoryRepository elasticSearchChatMemoryRepository(
ElasticSearchChatMemoryRepositoryConfig config) {
return ElasticSearchChatMemoryRepository.create(config);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.model.chat.memory.repository.elasticsearch.autoconfigure;

import org.springframework.ai.chat.memory.repository.elasticsearch.ElasticSearchChatMemoryRepositoryConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* Configuration properties for Elasticsearch chat memory.
*
* @author Fu Jian
* @since 1.1.0
*/
@ConfigurationProperties(ElasticSearchChatMemoryRepositoryProperties.CONFIG_PREFIX)
public class ElasticSearchChatMemoryRepositoryProperties {

public static final String CONFIG_PREFIX = "spring.ai.chat.memory.repository.elasticsearch";

private String indexName = ElasticSearchChatMemoryRepositoryConfig.DEFAULT_INDEX_NAME;

public String getIndexName() {
return this.indexName;
}

public void setIndexName(String indexName) {
this.indexName = indexName;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.springframework.ai.model.chat.memory.repository.elasticsearch.autoconfigure.ElasticSearchChatMemoryRepositoryAutoConfiguration

Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* 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.model.chat.memory.repository.elasticsearch.autoconfigure;

import java.util.List;
import java.util.UUID;

import org.junit.jupiter.api.Test;
import org.testcontainers.elasticsearch.ElasticsearchContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import org.springframework.ai.chat.memory.repository.elasticsearch.ElasticSearchChatMemoryRepository;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.MessageType;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

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

/**
* Integration tests for {@link ElasticSearchChatMemoryRepositoryAutoConfiguration}.
*
* @author Fu Jian
* @since 1.1.0
*/
@Testcontainers
class ElasticSearchChatMemoryRepositoryAutoConfigurationIT {

@Container
static ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer(
"docker.elastic.co/elasticsearch/elasticsearch:8.10.2")
.withEnv("xpack.security.enabled", "false")
.withEnv("xpack.security.http.ssl.enabled", "false");

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(ElasticsearchRestClientAutoConfiguration.class,
ElasticSearchChatMemoryRepositoryAutoConfiguration.class))
.withPropertyValues("spring.elasticsearch.uris=http://" + elasticsearchContainer.getHost() + ":"
+ elasticsearchContainer.getMappedPort(9200))
.withPropertyValues("spring.ai.chat.memory.repository.elasticsearch.index-name=autoconfig-test-chat-memory");

@Test
void addAndGet() {
this.contextRunner.run(context -> {
ElasticSearchChatMemoryRepository memory = context.getBean(ElasticSearchChatMemoryRepository.class);

String conversationId = UUID.randomUUID().toString();
assertThat(memory.findByConversationId(conversationId)).isEmpty();

memory.saveAll(conversationId, List.of(new UserMessage("test question")));

assertThat(memory.findByConversationId(conversationId)).hasSize(1);
assertThat(memory.findByConversationId(conversationId).get(0).getMessageType()).isEqualTo(MessageType.USER);
assertThat(memory.findByConversationId(conversationId).get(0).getText()).isEqualTo("test question");

memory.deleteByConversationId(conversationId);
assertThat(memory.findByConversationId(conversationId)).isEmpty();

memory.saveAll(conversationId,
List.of(new UserMessage("test question"), new AssistantMessage("test answer")));

assertThat(memory.findByConversationId(conversationId)).hasSize(2);
assertThat(memory.findByConversationId(conversationId).get(0).getMessageType()).isEqualTo(MessageType.USER);
assertThat(memory.findByConversationId(conversationId).get(0).getText()).isEqualTo("test question");
assertThat(memory.findByConversationId(conversationId).get(1).getMessageType())
.isEqualTo(MessageType.ASSISTANT);
assertThat(memory.findByConversationId(conversationId).get(1).getText()).isEqualTo("test answer");
});
}

@Test
void propertiesConfiguration() {
this.contextRunner
.withPropertyValues("spring.ai.chat.memory.repository.elasticsearch.index-name=custom-testindex")
.run(context -> {
ElasticSearchChatMemoryRepositoryProperties properties = context
.getBean(ElasticSearchChatMemoryRepositoryProperties.class);
assertThat(properties.getIndexName()).isEqualTo("custom-testindex");
});
}

@Test
void findConversationIds() {
this.contextRunner.run(context -> {
ElasticSearchChatMemoryRepository memory = context.getBean(ElasticSearchChatMemoryRepository.class);

String conversationId1 = UUID.randomUUID().toString();
String conversationId2 = UUID.randomUUID().toString();

memory.saveAll(conversationId1, List.of(new UserMessage("test question 1")));
memory.saveAll(conversationId2, List.of(new UserMessage("test question 2")));

List<String> conversationIds = memory.findConversationIds();
assertThat(conversationIds).contains(conversationId1, conversationId2);
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.model.chat.memory.repository.elasticsearch.autoconfigure;

import org.junit.jupiter.api.Test;

import org.springframework.ai.chat.memory.repository.elasticsearch.ElasticSearchChatMemoryRepositoryConfig;

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

/**
* Unit tests for {@link ElasticSearchChatMemoryRepositoryProperties}.
*
* @author Fu Jian
* @since 1.1.0
*/
class ElasticSearchChatMemoryRepositoryPropertiesTest {

@Test
void defaultValues() {
var props = new ElasticSearchChatMemoryRepositoryProperties();
assertThat(props.getIndexName()).isEqualTo(ElasticSearchChatMemoryRepositoryConfig.DEFAULT_INDEX_NAME);
}

@Test
void customValues() {
var props = new ElasticSearchChatMemoryRepositoryProperties();
props.setIndexName("custom_chat_memory");

assertThat(props.getIndexName()).isEqualTo("custom_chat_memory");
}

}
Loading