Skip to content

Commit

Permalink
Cherry-pick BWC fix for system prompt and user instructions (#3437)
Browse files Browse the repository at this point in the history
* BWC (rag processor): add version control for newly added request params (#3125) (#3364)

* gradle spotless

Signed-off-by: Pavan Yekbote <[email protected]>

---------

Signed-off-by: Pavan Yekbote <[email protected]>
Co-authored-by: opensearch-trigger-bot[bot] <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com>
(cherry picked from commit f4d4481)
  • Loading branch information
pyek-bot authored and github-actions[bot] committed Jan 27, 2025
1 parent 307ab56 commit 79ca857
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public class GenerativeQAParameters implements Writeable, ToXContentObject {

static final Version MINIMAL_SUPPORTED_VERSION_FOR_BEDROCK_CONVERSE_LLM_MESSAGES = CommonValue.VERSION_2_18_0;

public static final Version MINIMAL_SUPPORTED_VERSION_FOR_PROMPT_AND_INSTRUCTIONS = CommonValue.VERSION_2_13_0;

@Setter
@Getter
private String conversationId;
Expand Down Expand Up @@ -200,16 +202,23 @@ public GenerativeQAParameters(StreamInput input) throws IOException {
this.llmQuestion = input.readString();
}

this.systemPrompt = input.readOptionalString();
this.userInstructions = input.readOptionalString();
if (version.onOrAfter(MINIMAL_SUPPORTED_VERSION_FOR_PROMPT_AND_INSTRUCTIONS)) {
this.systemPrompt = input.readOptionalString();
this.userInstructions = input.readOptionalString();
}

this.contextSize = input.readInt();
this.interactionSize = input.readInt();
this.timeout = input.readInt();
this.llmResponseField = input.readOptionalString();

if (version.onOrAfter(MINIMAL_SUPPORTED_VERSION_FOR_PROMPT_AND_INSTRUCTIONS)) {
this.llmResponseField = input.readOptionalString();
}

if (version.onOrAfter(MINIMAL_SUPPORTED_VERSION_FOR_BEDROCK_CONVERSE_LLM_MESSAGES)) {
this.llmMessages.addAll(input.readList(MessageBlock::new));
}

}

@Override
Expand Down Expand Up @@ -272,12 +281,18 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeString(llmQuestion);
}

out.writeOptionalString(systemPrompt);
out.writeOptionalString(userInstructions);
if (version.onOrAfter(MINIMAL_SUPPORTED_VERSION_FOR_PROMPT_AND_INSTRUCTIONS)) {
out.writeOptionalString(systemPrompt);
out.writeOptionalString(userInstructions);
}

out.writeInt(contextSize);
out.writeInt(interactionSize);
out.writeInt(timeout);
out.writeOptionalString(llmResponseField);

if (version.onOrAfter(MINIMAL_SUPPORTED_VERSION_FOR_PROMPT_AND_INSTRUCTIONS)) {
out.writeOptionalString(llmResponseField);
}

if (version.onOrAfter(MINIMAL_SUPPORTED_VERSION_FOR_BEDROCK_CONVERSE_LLM_MESSAGES)) {
out.writeList(llmMessages);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public void testMiscMethods() throws IOException {
assertNotEquals(builder1, builder2);
assertNotEquals(builder1.hashCode(), builder2.hashCode());

// BWC test for bedrock converse params
StreamOutput so1 = mock(StreamOutput.class);
when(so1.getVersion()).thenReturn(GenerativeQAParameters.MINIMAL_SUPPORTED_VERSION_FOR_BEDROCK_CONVERSE_LLM_MESSAGES);
builder1.writeTo(so1);
Expand All @@ -130,6 +131,19 @@ public void testMiscMethods() throws IOException {
when(so2.getVersion()).thenReturn(Version.V_2_17_0);
builder1.writeTo(so2);
verify(so2, times(5)).writeOptionalString(any());

// BWC test for system prompt and instructions
StreamOutput so3 = mock(StreamOutput.class);
when(so3.getVersion()).thenReturn(GenerativeQAParameters.MINIMAL_SUPPORTED_VERSION_FOR_PROMPT_AND_INSTRUCTIONS);
builder1.writeTo(so3);
verify(so3, times(5)).writeOptionalString(any());
verify(so3, times(1)).writeString(any());

StreamOutput so4 = mock(StreamOutput.class);
when(so4.getVersion()).thenReturn(Version.V_2_12_0);
builder1.writeTo(so4);
verify(so4, times(2)).writeOptionalString(any());
verify(so4, times(1)).writeString(any());
}

public void testParse() throws IOException {
Expand Down

0 comments on commit 79ca857

Please sign in to comment.