Skip to content
Merged
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
Expand Up @@ -20,6 +20,9 @@
import static com.bytechef.component.definition.ComponentDsl.component;

import com.bytechef.component.ComponentHandler;
import com.bytechef.component.ai.chat.memory.action.VectorStoreChatMemoryAddMessagesAction;
import com.bytechef.component.ai.chat.memory.action.VectorStoreChatMemoryDeleteAction;
import com.bytechef.component.ai.chat.memory.action.VectorStoreChatMemoryGetMessagesAction;
import com.bytechef.component.ai.chat.memory.cluster.VectorStoreChatMemory;
import com.bytechef.component.definition.ComponentCategory;
import com.bytechef.component.definition.ComponentDefinition;
Expand All @@ -45,6 +48,10 @@ public VectorStoreChatMemoryComponentHandler(ClusterElementDefinitionService clu
.description("Vector Store Chat Memory.")
.icon("path:assets/vector-store-chat-memory.svg")
.categories(ComponentCategory.ARTIFICIAL_INTELLIGENCE)
.actions(
VectorStoreChatMemoryAddMessagesAction.getActionDefinition(clusterElementDefinitionService),
VectorStoreChatMemoryGetMessagesAction.getActionDefinition(clusterElementDefinitionService),
VectorStoreChatMemoryDeleteAction.getActionDefinition(clusterElementDefinitionService))
.clusterElements(
new VectorStoreChatMemory(clusterElementDefinitionService).clusterElementDefinition));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright 2025 ByteChef
*
* 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 com.bytechef.component.ai.chat.memory.action;

import static com.bytechef.component.ai.chat.memory.constant.VectorStoreChatMemoryConstants.CONVERSATION_ID;
import static com.bytechef.component.ai.chat.memory.constant.VectorStoreChatMemoryConstants.MESSAGES;
import static com.bytechef.component.ai.chat.memory.constant.VectorStoreChatMemoryConstants.MESSAGE_CONTENT;
import static com.bytechef.component.ai.chat.memory.constant.VectorStoreChatMemoryConstants.MESSAGE_ROLE;
import static com.bytechef.component.ai.chat.memory.constant.VectorStoreChatMemoryConstants.METADATA_CONVERSATION_ID;
import static com.bytechef.component.ai.chat.memory.constant.VectorStoreChatMemoryConstants.METADATA_MESSAGE_TYPE;
import static com.bytechef.component.ai.chat.memory.util.VectorStoreChatMemoryUtils.getVectorStore;
import static com.bytechef.component.definition.ComponentDsl.action;
import static com.bytechef.component.definition.ComponentDsl.array;
import static com.bytechef.component.definition.ComponentDsl.integer;
import static com.bytechef.component.definition.ComponentDsl.object;
import static com.bytechef.component.definition.ComponentDsl.option;
import static com.bytechef.component.definition.ComponentDsl.outputSchema;
import static com.bytechef.component.definition.ComponentDsl.string;

import com.bytechef.component.definition.ActionDefinition;
import com.bytechef.component.definition.Parameters;
import com.bytechef.platform.component.ComponentConnection;
import com.bytechef.platform.component.definition.MultipleConnectionsPerformFunction;
import com.bytechef.platform.component.service.ClusterElementDefinitionService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.VectorStore;

/**
* @author Ivica Cardic
*/
public class VectorStoreChatMemoryAddMessagesAction {

public static ActionDefinition getActionDefinition(
ClusterElementDefinitionService clusterElementDefinitionService) {

return action("addMessages")
.title("Add Messages")
.description("Adds messages to the vector store chat memory for a conversation.")
.properties(
string(CONVERSATION_ID)
.label("Conversation ID")
.description("The unique identifier for the conversation.")
.required(true),
array(MESSAGES)
.label("Messages")
.description("The messages to add to the conversation.")
.required(true)
.items(
object()
.properties(
string(MESSAGE_ROLE)
.label("Role")
.description("The role of the message sender.")
.required(true)
.options(
option("User", "user"),
option("Assistant", "assistant")),
string(MESSAGE_CONTENT)
.label("Content")
.description("The content of the message.")
.required(true))))
.output(
outputSchema(
object()
.properties(
string(CONVERSATION_ID),
integer("messageCount"))))
.perform(
(MultipleConnectionsPerformFunction) (
inputParameters, componentConnections, extensions, context) -> perform(inputParameters,
componentConnections, extensions, clusterElementDefinitionService));
}

private VectorStoreChatMemoryAddMessagesAction() {
}

protected static Object perform(
Parameters inputParameters, Map<String, ComponentConnection> componentConnections,
Parameters extensions, ClusterElementDefinitionService clusterElementDefinitionService) throws Exception {

String conversationId = inputParameters.getRequiredString(CONVERSATION_ID);
Object[] messagesArray = inputParameters.getRequiredArray(MESSAGES);

VectorStore vectorStore = getVectorStore(extensions, componentConnections, clusterElementDefinitionService);

List<Document> documents = new ArrayList<>();

for (Object messageObj : messagesArray) {
if (messageObj instanceof Map<?, ?> messageMap) {
String role = (String) messageMap.get(MESSAGE_ROLE);
String content = (String) messageMap.get(MESSAGE_CONTENT);

Document document = new Document(
content,
Map.of(
METADATA_CONVERSATION_ID, conversationId,
METADATA_MESSAGE_TYPE, role));

documents.add(document);
}
}

vectorStore.add(documents);

return Map.of(
CONVERSATION_ID, conversationId,
"messageCount", documents.size());
}
Comment thread
ivicac marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2025 ByteChef
*
* 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 com.bytechef.component.ai.chat.memory.action;

import static com.bytechef.component.ai.chat.memory.constant.VectorStoreChatMemoryConstants.CONVERSATION_ID;
import static com.bytechef.component.ai.chat.memory.constant.VectorStoreChatMemoryConstants.METADATA_CONVERSATION_ID;
import static com.bytechef.component.ai.chat.memory.util.VectorStoreChatMemoryUtils.getVectorStore;
import static com.bytechef.component.definition.ComponentDsl.action;
import static com.bytechef.component.definition.ComponentDsl.bool;
import static com.bytechef.component.definition.ComponentDsl.object;
import static com.bytechef.component.definition.ComponentDsl.outputSchema;
import static com.bytechef.component.definition.ComponentDsl.string;

import com.bytechef.component.definition.ActionDefinition;
import com.bytechef.component.definition.Parameters;
import com.bytechef.platform.component.ComponentConnection;
import com.bytechef.platform.component.definition.MultipleConnectionsPerformFunction;
import com.bytechef.platform.component.service.ClusterElementDefinitionService;
import java.util.Map;
import org.springframework.ai.vectorstore.VectorStore;

/**
* @author Ivica Cardic
*/
public class VectorStoreChatMemoryDeleteAction {

public static ActionDefinition getActionDefinition(
ClusterElementDefinitionService clusterElementDefinitionService) {

return action("deleteConversation")
.title("Delete Conversation")
.description("Deletes all messages for a conversation from the vector store.")
.properties(
string(CONVERSATION_ID)
.label("Conversation ID")
.description("The unique identifier for the conversation to delete.")
.required(true))
.output(
outputSchema(
object()
.properties(
string(CONVERSATION_ID),
bool("deleted"))))
.perform(
(MultipleConnectionsPerformFunction) (
inputParameters, componentConnections, extensions, context) -> perform(inputParameters,
componentConnections, extensions, clusterElementDefinitionService));
}

private VectorStoreChatMemoryDeleteAction() {
}

protected static Object perform(
Parameters inputParameters, Map<String, ComponentConnection> componentConnections,
Parameters extensions, ClusterElementDefinitionService clusterElementDefinitionService) throws Exception {

String conversationId = inputParameters.getRequiredString(CONVERSATION_ID);

VectorStore vectorStore = getVectorStore(extensions, componentConnections, clusterElementDefinitionService);

vectorStore.delete(METADATA_CONVERSATION_ID + " == '" + conversationId + "'");

return Map.of(
CONVERSATION_ID, conversationId,
"deleted", true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright 2025 ByteChef
*
* 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 com.bytechef.component.ai.chat.memory.action;

import static com.bytechef.component.ai.chat.memory.constant.VectorStoreChatMemoryConstants.CONVERSATION_ID;
import static com.bytechef.component.ai.chat.memory.constant.VectorStoreChatMemoryConstants.METADATA_CONVERSATION_ID;
import static com.bytechef.component.ai.chat.memory.constant.VectorStoreChatMemoryConstants.METADATA_MESSAGE_TYPE;
import static com.bytechef.component.ai.chat.memory.constant.VectorStoreChatMemoryConstants.TOP_K;
import static com.bytechef.component.ai.chat.memory.util.VectorStoreChatMemoryUtils.getVectorStore;
import static com.bytechef.component.definition.ComponentDsl.action;
import static com.bytechef.component.definition.ComponentDsl.array;
import static com.bytechef.component.definition.ComponentDsl.integer;
import static com.bytechef.component.definition.ComponentDsl.object;
import static com.bytechef.component.definition.ComponentDsl.outputSchema;
import static com.bytechef.component.definition.ComponentDsl.string;

import com.bytechef.component.definition.ActionDefinition;
import com.bytechef.component.definition.Parameters;
import com.bytechef.platform.component.ComponentConnection;
import com.bytechef.platform.component.definition.MultipleConnectionsPerformFunction;
import com.bytechef.platform.component.service.ClusterElementDefinitionService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.filter.Filter;
import org.springframework.ai.vectorstore.filter.FilterExpressionBuilder;

/**
* @author Ivica Cardic
*/
public class VectorStoreChatMemoryGetMessagesAction {

public static ActionDefinition getActionDefinition(
ClusterElementDefinitionService clusterElementDefinitionService) {

return action("getMessages")
.title("Get Messages")
.description("Retrieves messages from the vector store chat memory for a conversation.")
.properties(
string(CONVERSATION_ID)
.label("Conversation ID")
.description("The unique identifier for the conversation.")
.required(true),
integer(TOP_K)
.label("Top K")
.description("The maximum number of messages to retrieve.")
.defaultValue(100)
.required(false))
.output(
outputSchema(
object()
.properties(
string(CONVERSATION_ID),
array("messages")
.items(
object()
.properties(
string("role"),
string("content"))))))
.perform(
(MultipleConnectionsPerformFunction) (
inputParameters, componentConnections, extensions, context) -> perform(inputParameters,
componentConnections, extensions, clusterElementDefinitionService));
}

private VectorStoreChatMemoryGetMessagesAction() {
}

protected static Object perform(
Parameters inputParameters, Map<String, ComponentConnection> componentConnections,
Parameters extensions, ClusterElementDefinitionService clusterElementDefinitionService) throws Exception {

String conversationId = inputParameters.getRequiredString(CONVERSATION_ID);
int topK = inputParameters.getInteger(TOP_K, 100);

VectorStore vectorStore = getVectorStore(extensions, componentConnections, clusterElementDefinitionService);

FilterExpressionBuilder filterExpressionBuilder = new FilterExpressionBuilder();

Filter.Expression filterExpression = filterExpressionBuilder
.eq(METADATA_CONVERSATION_ID, conversationId)
.build();

SearchRequest searchRequest = SearchRequest.builder()
.query(conversationId)
.topK(topK)
.filterExpression(filterExpression)
.build();

List<Document> documents = vectorStore.similaritySearch(searchRequest);

List<Map<String, String>> messageList = documents.stream()
.map(VectorStoreChatMemoryGetMessagesAction::toMessageMap)
.toList();

return Map.of(
CONVERSATION_ID, conversationId,
"messages", messageList);
}

private static Map<String, String> toMessageMap(Document document) {
Map<String, String> map = new HashMap<>();

Map<String, Object> metadata = document.getMetadata();

Object messageType = metadata.get(METADATA_MESSAGE_TYPE);

if (messageType != null) {
map.put("role", messageType.toString());
}

map.put("content", document.getText());

return map;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2025 ByteChef
*
* 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 com.bytechef.component.ai.chat.memory.constant;

/**
* @author Ivica Cardic
*/
public class VectorStoreChatMemoryConstants {

public static final String CONVERSATION_ID = "conversationId";
public static final String MESSAGES = "messages";
public static final String MESSAGE_CONTENT = "content";
public static final String MESSAGE_ROLE = "role";
public static final String TOP_K = "topK";

public static final String METADATA_CONVERSATION_ID = "conversationId";
public static final String METADATA_MESSAGE_TYPE = "messageType";

private VectorStoreChatMemoryConstants() {
}
}
Loading