Skip to content

fix(MessageChatMemoryAdvisor): Fix incorrect prompt position for system messages #3520

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
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 @@ -19,6 +19,9 @@
import java.util.ArrayList;
import java.util.List;

import org.springframework.ai.chat.messages.MessageType;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;
Expand Down Expand Up @@ -83,9 +86,18 @@ public ChatClientRequest before(ChatClientRequest chatClientRequest, AdvisorChai
List<Message> memoryMessages = this.chatMemory.get(conversationId);

// 2. Advise the request messages list.
List<Message> processedMessages = new ArrayList<>(memoryMessages);
processedMessages.addAll(chatClientRequest.prompt().getInstructions());

List<Message> processedMessages = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why put system in the first place? In my opinion, system indicates LLMS, and all subsequent conversations are based on system.

And from https://openai.com/index/gpt-4-api-general-availability/, system and user message do not have the same function.

SystemMessage systemMessage = chatClientRequest.prompt().getSystemMessage();
if (StringUtils.hasText(systemMessage.getText())) {
processedMessages.add(systemMessage);
}
processedMessages.addAll(memoryMessages);
// Add non-system messages from instructions
for (Message message : chatClientRequest.prompt().getInstructions()) {
if (message.getMessageType() != MessageType.SYSTEM) {
processedMessages.add(message);
}
}
// 3. Create a new request with the advised messages.
ChatClientRequest processedChatClientRequest = chatClientRequest.mutate()
.prompt(chatClientRequest.prompt().mutate().messages(processedMessages).build())
Expand Down
Loading