Skip to content

Fixed a bug in the augmentSystemMessage method of the Prompt. #3268

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

Closed
Closed
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 @@ -198,21 +198,21 @@ else if (message instanceof ToolResponseMessage toolResponseMessage) {
* @return a new {@link Prompt} instance with the augmented system message.
*/
public Prompt augmentSystemMessage(Function<SystemMessage, SystemMessage> systemMessageAugmenter) {

var messagesCopy = new ArrayList<>(this.messages);
for (int i = 0; i <= this.messages.size() - 1; i++) {
boolean found = false;
for (int i = 0; i < messagesCopy.size(); i++) {
Message message = messagesCopy.get(i);
if (message instanceof SystemMessage systemMessage) {
messagesCopy.set(i, systemMessageAugmenter.apply(systemMessage));
found = true;
break;
}
if (i == 0) {
// If no system message is found, create a new one with the provided text
// and add it as the first item in the list.
messagesCopy.add(0, systemMessageAugmenter.apply(new SystemMessage("")));
}
}

if (!found) {
// If no system message is found, create a new one with the provided text
// and add it as the first item in the list.
messagesCopy.add(0, systemMessageAugmenter.apply(new SystemMessage("")));
}
return new Prompt(messagesCopy, null == this.chatOptions ? null : this.chatOptions.copy());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,26 @@ void augmentSystemMessageWhenNone() {
assertThat(prompt.getSystemMessage().getText()).isEqualTo("");
}

@Test
void augmentSystemMessageWhenNotFirst() {
Message[] messages = { new UserMessage("Hi"), new SystemMessage("Hello") };
Prompt prompt = Prompt.builder().messages(messages).build();

assertThat(prompt.getSystemMessage()).isNotNull();
assertThat(prompt.getUserMessage()).isNotNull();
assertThat(prompt.getUserMessage().getText()).isEqualTo("Hi");
assertThat(prompt.getSystemMessage().getText()).isEqualTo("Hello");

Prompt copy = prompt.augmentSystemMessage(message -> message.mutate().text("How are you?").build());

assertThat(copy.getSystemMessage()).isNotNull();
assertThat(copy.getInstructions().size()).isEqualTo(messages.length);
assertThat(copy.getSystemMessage().getText()).isEqualTo("How are you?");

assertThat(prompt.getSystemMessage()).isNotNull();
assertThat(prompt.getUserMessage()).isNotNull();
assertThat(prompt.getUserMessage().getText()).isEqualTo("Hi");
assertThat(prompt.getSystemMessage().getText()).isEqualTo("Hello");
}

}