Skip to content

Commit 76752cf

Browse files
committed
Create MongoDB chat memory implementation
Signed-off-by: Łukasz Jernaś <[email protected]>
1 parent fbec267 commit 76752cf

File tree

15 files changed

+864
-0
lines changed

15 files changed

+864
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.springframework.ai</groupId>
8+
<artifactId>spring-ai-parent</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
<relativePath>../../../../../pom.xml</relativePath>
11+
</parent>
12+
<artifactId>spring-ai-autoconfigure-model-chat-memory-mongodb</artifactId>
13+
<packaging>jar</packaging>
14+
<name>Spring AI MongoDB Chat Memory Auto Configuration</name>
15+
<description>Spring AI MongoDB Chat Memory Auto Configuration</description>
16+
<url>https://github.com/spring-projects/spring-ai</url>
17+
18+
<scm>
19+
<url>https://github.com/spring-projects/spring-ai</url>
20+
<connection>git://github.com/spring-projects/spring-ai.git</connection>
21+
<developerConnection>[email protected]:spring-projects/spring-ai.git</developerConnection>
22+
</scm>
23+
24+
25+
<dependencies>
26+
27+
<dependency>
28+
<groupId>org.springframework.ai</groupId>
29+
<artifactId>spring-ai-model-chat-memory-mongodb</artifactId>
30+
<version>${project.parent.version}</version>
31+
</dependency>
32+
33+
<!-- Boot dependencies -->
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter</artifactId>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-data-mongodb</artifactId>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-configuration-processor</artifactId>
47+
<optional>true</optional>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-autoconfigure-processor</artifactId>
53+
<optional>true</optional>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>org.mongodb</groupId>
58+
<artifactId>mongodb-driver-sync</artifactId>
59+
</dependency>
60+
61+
<!-- Test dependencies -->
62+
<dependency>
63+
<groupId>org.springframework.ai</groupId>
64+
<artifactId>spring-ai-test</artifactId>
65+
<version>${project.parent.version}</version>
66+
<scope>test</scope>
67+
</dependency>
68+
69+
<dependency>
70+
<groupId>org.springframework.ai</groupId>
71+
<artifactId>spring-ai-autoconfigure-model-chat-client</artifactId>
72+
<version>${project.parent.version}</version>
73+
<scope>test</scope>
74+
</dependency>
75+
76+
<dependency>
77+
<groupId>org.springframework.ai</groupId>
78+
<artifactId>spring-ai-openai</artifactId>
79+
<version>${project.parent.version}</version>
80+
<scope>test</scope>
81+
</dependency>
82+
83+
<dependency>
84+
<groupId>org.springframework.boot</groupId>
85+
<artifactId>spring-boot-starter-test</artifactId>
86+
<scope>test</scope>
87+
</dependency>
88+
89+
<dependency>
90+
<groupId>org.springframework.boot</groupId>
91+
<artifactId>spring-boot-testcontainers</artifactId>
92+
<scope>test</scope>
93+
</dependency>
94+
95+
<dependency>
96+
<groupId>org.testcontainers</groupId>
97+
<artifactId>junit-jupiter</artifactId>
98+
<scope>test</scope>
99+
</dependency>
100+
101+
<dependency>
102+
<groupId>org.testcontainers</groupId>
103+
<artifactId>mongodb</artifactId>
104+
<scope>test</scope>
105+
</dependency>
106+
107+
<dependency>
108+
<groupId>org.mockito</groupId>
109+
<artifactId>mockito-core</artifactId>
110+
<scope>test</scope>
111+
</dependency>
112+
</dependencies>
113+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2025-2025 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.model.chat.memory.mongodb.autoconfigure;
18+
19+
import org.springframework.ai.chat.memory.mongodb.MongoDbChatMemory;
20+
import org.springframework.ai.chat.memory.mongodb.MongoDbChatMemoryConfig;
21+
import org.springframework.boot.autoconfigure.AutoConfiguration;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
23+
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
24+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.data.mongodb.core.MongoTemplate;
27+
28+
/**
29+
* Spring Boot auto-configuration for {@link MongoDbChatMemory}.
30+
*
31+
* @author Łukasz Jernaś
32+
* @since 1.0.0
33+
*/
34+
@AutoConfiguration(after = MongoAutoConfiguration.class)
35+
@EnableConfigurationProperties(MongoDbChatMemoryProperties.class)
36+
public class MongoDbChatMemoryAutoConfiguration {
37+
38+
@Bean
39+
@ConditionalOnMissingBean
40+
public MongoDbChatMemory chatMemory(MongoTemplate mongoTemplate) {
41+
var config = MongoDbChatMemoryConfig.builder().withTemplate(mongoTemplate).build();
42+
return new MongoDbChatMemory(config);
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2025-2025 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.model.chat.memory.mongodb.autoconfigure;
18+
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
import org.springframework.ai.chat.memory.mongodb.Conversation;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
23+
import org.springframework.context.event.ContextRefreshedEvent;
24+
import org.springframework.context.event.EventListener;
25+
import org.springframework.data.domain.Sort;
26+
import org.springframework.data.mongodb.core.MongoTemplate;
27+
import org.springframework.data.mongodb.core.index.Index;
28+
import org.springframework.stereotype.Component;
29+
30+
/**
31+
* Class responsible for creating MongoDB proper indices for the ChatMemory. Creates a
32+
* main index on the conversationId and timestamp fields, and a TTL index on the timestamp
33+
* field if the TTL is set in properties.
34+
*
35+
* @see MongoDbChatMemoryProperties
36+
* @author Łukasz Jernaś
37+
* @since 1.0.0
38+
*/
39+
@Component
40+
@ConditionalOnProperty(value = "spring.ai.chat.memory.mongodb.create-indexes", havingValue = "true")
41+
public class MongoDbChatMemoryIndexCreator {
42+
43+
private static final Logger logger = LoggerFactory.getLogger(MongoDbChatMemoryIndexCreator.class);
44+
45+
private final MongoTemplate mongoTemplate;
46+
47+
private final MongoDbChatMemoryProperties mongoDbChatMemoryProperties;
48+
49+
public MongoDbChatMemoryIndexCreator(MongoTemplate mongoTemplate,
50+
MongoDbChatMemoryProperties mongoDbChatMemoryProperties) {
51+
this.mongoTemplate = mongoTemplate;
52+
this.mongoDbChatMemoryProperties = mongoDbChatMemoryProperties;
53+
}
54+
55+
@EventListener(ContextRefreshedEvent.class)
56+
public void initIndicesAfterStartup() {
57+
logger.info("Creating MongoDB indices for ChatMemory");
58+
// Create a main index
59+
mongoTemplate.indexOps(Conversation.class)
60+
.ensureIndex(new Index().on("conversationId", Sort.Direction.ASC).on("timestamp", Sort.Direction.DESC));
61+
62+
createOrUpdateTtlIndex();
63+
64+
}
65+
66+
private void createOrUpdateTtlIndex() {
67+
if (!this.mongoDbChatMemoryProperties.getTtl().isZero()) {
68+
// Check for existing TTL index
69+
mongoTemplate.indexOps(Conversation.class).getIndexInfo().forEach(idx -> {
70+
logger.error("Index info: {}", idx);
71+
if (idx.getExpireAfter().isPresent()
72+
&& !idx.getExpireAfter().get().equals(this.mongoDbChatMemoryProperties.getTtl())) {
73+
logger.warn("Dropping existing TTL index, because TTL is different");
74+
mongoTemplate.indexOps(Conversation.class).dropIndex(idx.getName());
75+
}
76+
});
77+
mongoTemplate.indexOps(Conversation.class)
78+
.ensureIndex(new Index().on("timestamp", Sort.Direction.ASC)
79+
.expire(this.mongoDbChatMemoryProperties.getTtl()));
80+
}
81+
}
82+
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2025-2025 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.model.chat.memory.mongodb.autoconfigure;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
import java.time.Duration;
22+
23+
/**
24+
* @author Łukasz Jernaś
25+
* @since 1.0.0
26+
*/
27+
@ConfigurationProperties(MongoDbChatMemoryProperties.CONFIG_PREFIX)
28+
public class MongoDbChatMemoryProperties {
29+
30+
public static final String CONFIG_PREFIX = "spring.ai.chat.memory.mongodb";
31+
32+
/**
33+
* If the indexes should be automatically created on app startup. Note: Changing the
34+
* TTL value will drop the TTL index and recreate it.
35+
*/
36+
private boolean createIndexes = false;
37+
38+
/**
39+
* The time to live (TTL) for the conversation documents in the database. The default
40+
* value is 0, which means that the documents will not expire.
41+
*/
42+
private Duration ttl = Duration.ZERO;
43+
44+
public Duration getTtl() {
45+
return ttl;
46+
}
47+
48+
public void setTtl(Duration ttl) {
49+
this.ttl = ttl;
50+
}
51+
52+
public boolean isCreateIndexes() {
53+
return createIndexes;
54+
}
55+
56+
public void setCreateIndexes(boolean createIndexes) {
57+
this.createIndexes = createIndexes;
58+
}
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# Copyright 2024-2025 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+
org.springframework.ai.model.chat.memory.mongodb.autoconfigure.MongoDbChatMemoryAutoConfiguration

0 commit comments

Comments
 (0)