-
Notifications
You must be signed in to change notification settings - Fork 136
2415 chat-memory-vectorstore - Add actions #4469
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
.../com/bytechef/component/ai/chat/memory/action/VectorStoreChatMemoryAddMessagesAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
| } | ||
81 changes: 81 additions & 0 deletions
81
.../java/com/bytechef/component/ai/chat/memory/action/VectorStoreChatMemoryDeleteAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
133 changes: 133 additions & 0 deletions
133
.../com/bytechef/component/ai/chat/memory/action/VectorStoreChatMemoryGetMessagesAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
...n/java/com/bytechef/component/ai/chat/memory/constant/VectorStoreChatMemoryConstants.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() { | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.