Skip to content
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

feature: Add the ability to pass custom metadata for MessageChatMemoryAdvisor and PromptChatMemoryAdvisor #2439

Open
wants to merge 1 commit 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
Expand Up @@ -16,9 +16,9 @@

package org.springframework.ai.chat.client.advisor;

import java.util.ArrayList;
import java.util.List;
import java.util.*;

import org.springframework.ai.chat.messages.AssistantMessage;
import reactor.core.publisher.Flux;

import org.springframework.ai.chat.client.advisor.api.AdvisedRequest;
Expand Down Expand Up @@ -48,7 +48,7 @@ public MessageChatMemoryAdvisor(ChatMemory chatMemory, String defaultConversatio
}

public MessageChatMemoryAdvisor(ChatMemory chatMemory, String defaultConversationId, int chatHistoryWindowSize,
int order) {
int order) {
super(chatMemory, defaultConversationId, chatHistoryWindowSize, true, order);
}

Expand Down Expand Up @@ -92,9 +92,9 @@ private AdvisedRequest before(AdvisedRequest request) {

// 3. Create a new request with the advised messages.
AdvisedRequest advisedRequest = AdvisedRequest.from(request).messages(advisedMessages).build();

// 4. Add the new user input to the conversation memory.
UserMessage userMessage = new UserMessage(request.userText(), request.media());
Map<String, Object> metadata = new HashMap<>(request.adviseContext());
UserMessage userMessage = new UserMessage(request.userText(), request.media(), metadata);
this.getChatMemoryStore().add(this.doGetConversationId(request.adviseContext()), userMessage);

return advisedRequest;
Expand All @@ -103,10 +103,17 @@ private AdvisedRequest before(AdvisedRequest request) {
private void observeAfter(AdvisedResponse advisedResponse) {

List<Message> assistantMessages = advisedResponse.response()
.getResults()
.stream()
.map(g -> (Message) g.getOutput())
.toList();
.getResults()
.stream()
.map(g -> {
AssistantMessage output = g.getOutput();
Map<String, Object> metadata = Optional.ofNullable(output.getMetadata()).orElse(new HashMap<>());
metadata.putAll(advisedResponse.adviseContext());
AssistantMessage assistantMessage = new AssistantMessage(output.getText(),
metadata, output.getToolCalls(), output.getMedia());
return (Message) assistantMessage;
})
.toList();

this.getChatMemoryStore().add(this.doGetConversationId(advisedResponse.adviseContext()), assistantMessages);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.ai.chat.messages.AssistantMessage;
import reactor.core.publisher.Flux;

import org.springframework.ai.chat.client.advisor.api.AdvisedRequest;
Expand Down Expand Up @@ -66,13 +68,13 @@ public PromptChatMemoryAdvisor(ChatMemory chatMemory, String systemTextAdvise) {
}

public PromptChatMemoryAdvisor(ChatMemory chatMemory, String defaultConversationId, int chatHistoryWindowSize,
String systemTextAdvise) {
String systemTextAdvise) {
this(chatMemory, defaultConversationId, chatHistoryWindowSize, systemTextAdvise,
Advisor.DEFAULT_CHAT_MEMORY_PRECEDENCE_ORDER);
}

public PromptChatMemoryAdvisor(ChatMemory chatMemory, String defaultConversationId, int chatHistoryWindowSize,
String systemTextAdvise, int order) {
String systemTextAdvise, int order) {
super(chatMemory, defaultConversationId, chatHistoryWindowSize, true, order);
this.systemTextAdvise = systemTextAdvise;
}
Expand Down Expand Up @@ -106,13 +108,13 @@ private AdvisedRequest before(AdvisedRequest request) {

// 1. Advise system parameters.
List<Message> memoryMessages = this.getChatMemoryStore()
.get(this.doGetConversationId(request.adviseContext()),
this.doGetChatMemoryRetrieveSize(request.adviseContext()));
.get(this.doGetConversationId(request.adviseContext()),
this.doGetChatMemoryRetrieveSize(request.adviseContext()));

String memory = (memoryMessages != null) ? memoryMessages.stream()
.filter(m -> m.getMessageType() == MessageType.USER || m.getMessageType() == MessageType.ASSISTANT)
.map(m -> m.getMessageType() + ":" + ((Content) m).getText())
.collect(Collectors.joining(System.lineSeparator())) : "";
.filter(m -> m.getMessageType() == MessageType.USER || m.getMessageType() == MessageType.ASSISTANT)
.map(m -> m.getMessageType() + ":" + ((Content) m).getText())
.collect(Collectors.joining(System.lineSeparator())) : "";

Map<String, Object> advisedSystemParams = new HashMap<>(request.systemParams());
advisedSystemParams.put("memory", memory);
Expand All @@ -122,12 +124,13 @@ private AdvisedRequest before(AdvisedRequest request) {

// 3. Create a new request with the advised system text and parameters.
AdvisedRequest advisedRequest = AdvisedRequest.from(request)
.systemText(advisedSystemText)
.systemParams(advisedSystemParams)
.build();
.systemText(advisedSystemText)
.systemParams(advisedSystemParams)
.build();

// 4. Add the new user input to the conversation memory.
UserMessage userMessage = new UserMessage(request.userText(), request.media());
Map<String, Object> metadata = new HashMap<>(request.adviseContext());
UserMessage userMessage = new UserMessage(request.userText(), request.media(), metadata);
this.getChatMemoryStore().add(this.doGetConversationId(request.adviseContext()), userMessage);

return advisedRequest;
Expand All @@ -136,10 +139,17 @@ private AdvisedRequest before(AdvisedRequest request) {
private void observeAfter(AdvisedResponse advisedResponse) {

List<Message> assistantMessages = advisedResponse.response()
.getResults()
.stream()
.map(g -> (Message) g.getOutput())
.toList();
.getResults()
.stream()
.map(g -> {
AssistantMessage output = g.getOutput();
Map<String, Object> metadata = Optional.ofNullable(output.getMetadata()).orElse(new HashMap<>());
metadata.putAll(advisedResponse.adviseContext());
AssistantMessage assistantMessage = new AssistantMessage(output.getText(),
metadata, output.getToolCalls(), output.getMedia());
return (Message) assistantMessage;
})
.toList();

this.getChatMemoryStore().add(this.doGetConversationId(advisedResponse.adviseContext()), assistantMessages);
}
Expand Down