Skip to content

Commit f396e17

Browse files
committed
Add a method to delete all chat memories in ChatMemory
Signed-off-by: fanxt0218 <[email protected]>
1 parent 2666138 commit f396e17

File tree

12 files changed

+72
-0
lines changed

12 files changed

+72
-0
lines changed

memory/repository/spring-ai-model-chat-memory-repository-cassandra/src/main/java/org/springframework/ai/chat/memory/repository/cassandra/CassandraChatMemoryRepository.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
* An implementation of {@link ChatMemoryRepository} for Apache Cassandra.
4646
*
4747
* @author Mick Semb Wever
48+
* @author Xiaotong Fan
4849
* @since 1.0.0
4950
*/
5051
public final class CassandraChatMemoryRepository implements ChatMemoryRepository {
@@ -165,6 +166,14 @@ public void deleteByConversationId(String conversationId) {
165166
saveAll(conversationId, List.of());
166167
}
167168

169+
@Override
170+
public void deleteConversationIds() {
171+
List<String> conversationIds = findConversationIds();
172+
for (String conversationId : conversationIds) {
173+
deleteByConversationId(conversationId);
174+
}
175+
}
176+
168177
private PreparedStatement prepareAddStmt() {
169178
RegularInsert stmt = null;
170179
InsertInto stmtStart = QueryBuilder.insertInto(this.conf.schema.keyspace(), this.conf.schema.table());

memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/HsqldbChatMemoryRepositoryDialect.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public String getDeleteMessagesSql() {
3636
return "DELETE FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ?";
3737
}
3838

39+
@Override
40+
public String getDeleteAllMessageSql() {
41+
return "DELETE FROM SPRING_AI_CHAT_MEMORY";
42+
}
43+
3944
@Override
4045
public String getSelectConversationIdsSql() {
4146
return "SELECT DISTINCT conversation_id FROM SPRING_AI_CHAT_MEMORY";

memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/JdbcChatMemoryRepository.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
* @author Linar Abzaltdinov
5454
* @author Mark Pollack
5555
* @author Yanming Zhou
56+
* @author Xiaotong Fan
5657
* @since 1.0.0
5758
*/
5859
public final class JdbcChatMemoryRepository implements ChatMemoryRepository {
@@ -106,6 +107,12 @@ public void deleteByConversationId(String conversationId) {
106107
this.jdbcTemplate.update(this.dialect.getDeleteMessagesSql(), conversationId);
107108
}
108109

110+
@Override
111+
public void deleteConversationIds() {
112+
this.jdbcTemplate.update(this.dialect.getDeleteAllMessageSql());
113+
}
114+
115+
109116
public static Builder builder() {
110117
return new Builder();
111118
}

memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/JdbcChatMemoryRepositoryDialect.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public interface JdbcChatMemoryRepositoryDialect {
4545
*/
4646
String getDeleteMessagesSql();
4747

48+
/**
49+
* Returns the SQL to delete all conversation IDs.
50+
*/
51+
String getDeleteAllMessageSql();
52+
4853
/**
4954
* Optionally, dialect can provide more advanced SQL as needed.
5055
*/

memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/MysqlChatMemoryRepositoryDialect.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,9 @@ public String getSelectConversationIdsSql() {
4343
public String getDeleteMessagesSql() {
4444
return "DELETE FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ?";
4545
}
46+
@Override
47+
public String getDeleteAllMessageSql() {
48+
return "DELETE FROM SPRING_AI_CHAT_MEMORY";
49+
}
4650

4751
}

memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/PostgresChatMemoryRepositoryDialect.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@ public String getDeleteMessagesSql() {
4444
return "DELETE FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ?";
4545
}
4646

47+
@Override
48+
public String getDeleteAllMessageSql() {
49+
return "DELETE FROM SPRING_AI_CHAT_MEMORY";
50+
}
51+
4752
}

memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/SqlServerChatMemoryRepositoryDialect.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@ public String getDeleteMessagesSql() {
4444
return "DELETE FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ?";
4545
}
4646

47+
@Override
48+
public String getDeleteAllMessageSql() {
49+
return "DELETE FROM SPRING_AI_CHAT_MEMORY";
50+
}
51+
4752
}

memory/repository/spring-ai-model-chat-memory-repository-neo4j/src/main/java/org/springframework/ai/chat/memory/repository/neo4j/Neo4jChatMemoryRepository.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
*
4646
* @author Enrico Rampazzo
4747
* @author Michael J. Simons
48+
* @author Xiaotong Fan
4849
* @since 1.0.0
4950
*/
5051

@@ -166,6 +167,14 @@ OPTIONAL MATCH (m)-[:HAS_TOOL_CALL]->(tc:%s)
166167
}
167168
}
168169

170+
@Override
171+
public void deleteConversationIds() {
172+
List<String> conversationIds = findConversationIds();
173+
for (String conversationId : conversationIds) {
174+
deleteByConversationId(conversationId);
175+
}
176+
}
177+
169178
public Neo4jChatMemoryRepositoryConfig getConfig() {
170179
return this.config;
171180
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*
2727
* @author Christian Tzolov
2828
* @author Thomas Vitale
29+
* @author Xiaotong Fan
2930
* @since 1.0.0
3031
*/
3132
public interface ChatMemory {
@@ -61,4 +62,9 @@ default void add(String conversationId, Message message) {
6162
*/
6263
void clear(String conversationId);
6364

65+
/**
66+
Clear all the chat memory.
67+
*/
68+
void clear();
69+
6470
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* A repository for storing and retrieving chat messages.
2525
*
2626
* @author Thomas Vitale
27+
* @author Xiaotong Fan
2728
* @since 1.0.0
2829
*/
2930
public interface ChatMemoryRepository {
@@ -40,4 +41,8 @@ public interface ChatMemoryRepository {
4041

4142
void deleteByConversationId(String conversationId);
4243

44+
/**
45+
* Deletes all the conversation IDs.
46+
*/
47+
void deleteConversationIds();
4348
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* An in-memory implementation of {@link ChatMemoryRepository}.
2929
*
3030
* @author Thomas Vitale
31+
* @author Xiaotong Fan
3132
* @since 1.0.0
3233
*/
3334
public final class InMemoryChatMemoryRepository implements ChatMemoryRepository {
@@ -60,4 +61,9 @@ public void deleteByConversationId(String conversationId) {
6061
this.chatMemoryStore.remove(conversationId);
6162
}
6263

64+
@Override
65+
public void deleteConversationIds() {
66+
this.chatMemoryStore.clear();
67+
}
68+
6369
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
*
3838
* @author Thomas Vitale
3939
* @author Ilayaperumal Gopinathan
40+
* @author Xiaotong Fan
4041
* @since 1.0.0
4142
*/
4243
public final class MessageWindowChatMemory implements ChatMemory {
@@ -77,6 +78,11 @@ public void clear(String conversationId) {
7778
this.chatMemoryRepository.deleteByConversationId(conversationId);
7879
}
7980

81+
@Override
82+
public void clear() {
83+
this.chatMemoryRepository.deleteConversationIds();
84+
}
85+
8086
private List<Message> process(List<Message> memoryMessages, List<Message> newMessages) {
8187
List<Message> processedMessages = new ArrayList<>();
8288

0 commit comments

Comments
 (0)