Skip to content

Add a method to delete all chat memories in ChatMemory #3590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* 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.
Expand Down Expand Up @@ -60,6 +60,7 @@
* @author Eddú Meléndez
* @author Soby Chacko
* @author Thomas Vitale
* @author Jonghoon Park
*/
@Testcontainers
public class ChromaVectorStoreAutoConfigurationIT {
Expand Down Expand Up @@ -182,7 +183,7 @@ public void throwExceptionOnMissingCollectionAndDisabledInitializedSchema() {
.hasCauseInstanceOf(BeanCreationException.class)
.hasRootCauseExactlyInstanceOf(RuntimeException.class)
.hasRootCauseMessage(
"Collection TestCollection doesn't exist and won't be created as the initializeSchema is set to false."));
"Collection TestCollection with the tenant: SpringAiTenant and the database: SpringAiDatabase doesn't exist and won't be created as the initializeSchema is set to false."));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
* An implementation of {@link ChatMemoryRepository} for Apache Cassandra.
*
* @author Mick Semb Wever
* @author Xiaotong Fan
* @since 1.0.0
*/
public final class CassandraChatMemoryRepository implements ChatMemoryRepository {
Expand Down Expand Up @@ -165,6 +166,14 @@ public void deleteByConversationId(String conversationId) {
saveAll(conversationId, List.of());
}

@Override
public void deleteConversationIds() {
List<String> conversationIds = findConversationIds();
for (String conversationId : conversationIds) {
deleteByConversationId(conversationId);
}
}

private PreparedStatement prepareAddStmt() {
RegularInsert stmt = null;
InsertInto stmtStart = QueryBuilder.insertInto(this.conf.schema.keyspace(), this.conf.schema.table());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public String getDeleteMessagesSql() {
return "DELETE FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ?";
}

@Override
public String getDeleteAllMessageSql() {
return "DELETE FROM SPRING_AI_CHAT_MEMORY";
}

@Override
public String getSelectConversationIdsSql() {
return "SELECT DISTINCT conversation_id FROM SPRING_AI_CHAT_MEMORY";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
* @author Linar Abzaltdinov
* @author Mark Pollack
* @author Yanming Zhou
* @author Xiaotong Fan
* @since 1.0.0
*/
public final class JdbcChatMemoryRepository implements ChatMemoryRepository {
Expand Down Expand Up @@ -106,6 +107,12 @@ public void deleteByConversationId(String conversationId) {
this.jdbcTemplate.update(this.dialect.getDeleteMessagesSql(), conversationId);
}

@Override
public void deleteConversationIds() {
this.jdbcTemplate.update(this.dialect.getDeleteAllMessageSql());
}


public static Builder builder() {
return new Builder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public interface JdbcChatMemoryRepositoryDialect {
*/
String getDeleteMessagesSql();

/**
* Returns the SQL to delete all conversation IDs.
*/
String getDeleteAllMessageSql();

/**
* Optionally, dialect can provide more advanced SQL as needed.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,9 @@ public String getSelectConversationIdsSql() {
public String getDeleteMessagesSql() {
return "DELETE FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ?";
}
@Override
public String getDeleteAllMessageSql() {
return "DELETE FROM SPRING_AI_CHAT_MEMORY";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public String getDeleteMessagesSql() {
return "DELETE FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ?";
}

@Override
public String getDeleteAllMessageSql() {
return "DELETE FROM SPRING_AI_CHAT_MEMORY";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public String getDeleteMessagesSql() {
return "DELETE FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ?";
}

@Override
public String getDeleteAllMessageSql() {
return "DELETE FROM SPRING_AI_CHAT_MEMORY";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
*
* @author Enrico Rampazzo
* @author Michael J. Simons
* @author Xiaotong Fan
* @since 1.0.0
*/

Expand Down Expand Up @@ -166,6 +167,14 @@ OPTIONAL MATCH (m)-[:HAS_TOOL_CALL]->(tc:%s)
}
}

@Override
public void deleteConversationIds() {
List<String> conversationIds = findConversationIds();
for (String conversationId : conversationIds) {
deleteByConversationId(conversationId);
}
}

public Neo4jChatMemoryRepositoryConfig getConfig() {
return this.config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class OpenAiAudioTranscriptionResponseMetadata extends AudioTranscription

};

protected static final String AI_METADATA_STRING = "{ @type: %1$s, rateLimit: %4$s }";
protected static final String AI_METADATA_STRING = "{ @type: %1$s, rateLimit: %2$s }";

@Nullable
private RateLimit rateLimit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*
* @author Christian Tzolov
* @author Thomas Vitale
* @author Xiaotong Fan
* @since 1.0.0
*/
public interface ChatMemory {
Expand Down Expand Up @@ -61,4 +62,9 @@ default void add(String conversationId, Message message) {
*/
void clear(String conversationId);

/**
Clear all the chat memory.
*/
void clear();

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* A repository for storing and retrieving chat messages.
*
* @author Thomas Vitale
* @author Xiaotong Fan
* @since 1.0.0
*/
public interface ChatMemoryRepository {
Expand All @@ -40,4 +41,8 @@ public interface ChatMemoryRepository {

void deleteByConversationId(String conversationId);

/**
* Deletes all the conversation IDs.
*/
void deleteConversationIds();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* An in-memory implementation of {@link ChatMemoryRepository}.
*
* @author Thomas Vitale
* @author Xiaotong Fan
* @since 1.0.0
*/
public final class InMemoryChatMemoryRepository implements ChatMemoryRepository {
Expand Down Expand Up @@ -60,4 +61,9 @@ public void deleteByConversationId(String conversationId) {
this.chatMemoryStore.remove(conversationId);
}

@Override
public void deleteConversationIds() {
this.chatMemoryStore.clear();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*
* @author Thomas Vitale
* @author Ilayaperumal Gopinathan
* @author Xiaotong Fan
* @since 1.0.0
*/
public final class MessageWindowChatMemory implements ChatMemory {
Expand Down Expand Up @@ -77,6 +78,11 @@ public void clear(String conversationId) {
this.chatMemoryRepository.deleteByConversationId(conversationId);
}

@Override
public void clear() {
this.chatMemoryRepository.deleteConversationIds();
}

private List<Message> process(List<Message> memoryMessages, List<Message> newMessages) {
List<Message> processedMessages = new ArrayList<>();

Expand Down