Skip to content

Commit 758b963

Browse files
Cherry-pick BWC fix for system prompt and user instructions (#3437) (#3441)
* BWC (rag processor): add version control for newly added request params (#3125) (#3364) * gradle spotless Signed-off-by: Pavan Yekbote <mail2pavanyekbote@gmail.com> --------- Signed-off-by: Pavan Yekbote <mail2pavanyekbote@gmail.com> Co-authored-by: opensearch-trigger-bot[bot] <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> (cherry picked from commit f4d4481) Co-authored-by: Pavan Yekbote <mail2pavanyekbote@gmail.com>
1 parent a852152 commit 758b963

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

search-processors/src/main/java/org/opensearch/searchpipelines/questionanswering/generative/ext/GenerativeQAParameters.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ public class GenerativeQAParameters implements Writeable, ToXContentObject {
9090

9191
static final Version MINIMAL_SUPPORTED_VERSION_FOR_BEDROCK_CONVERSE_LLM_MESSAGES = CommonValue.VERSION_2_18_0;
9292

93+
public static final Version MINIMAL_SUPPORTED_VERSION_FOR_PROMPT_AND_INSTRUCTIONS = CommonValue.VERSION_2_13_0;
94+
9395
@Setter
9496
@Getter
9597
private String conversationId;
@@ -200,16 +202,23 @@ public GenerativeQAParameters(StreamInput input) throws IOException {
200202
this.llmQuestion = input.readString();
201203
}
202204

203-
this.systemPrompt = input.readOptionalString();
204-
this.userInstructions = input.readOptionalString();
205+
if (version.onOrAfter(MINIMAL_SUPPORTED_VERSION_FOR_PROMPT_AND_INSTRUCTIONS)) {
206+
this.systemPrompt = input.readOptionalString();
207+
this.userInstructions = input.readOptionalString();
208+
}
209+
205210
this.contextSize = input.readInt();
206211
this.interactionSize = input.readInt();
207212
this.timeout = input.readInt();
208-
this.llmResponseField = input.readOptionalString();
213+
214+
if (version.onOrAfter(MINIMAL_SUPPORTED_VERSION_FOR_PROMPT_AND_INSTRUCTIONS)) {
215+
this.llmResponseField = input.readOptionalString();
216+
}
209217

210218
if (version.onOrAfter(MINIMAL_SUPPORTED_VERSION_FOR_BEDROCK_CONVERSE_LLM_MESSAGES)) {
211219
this.llmMessages.addAll(input.readList(MessageBlock::new));
212220
}
221+
213222
}
214223

215224
@Override
@@ -272,12 +281,18 @@ public void writeTo(StreamOutput out) throws IOException {
272281
out.writeString(llmQuestion);
273282
}
274283

275-
out.writeOptionalString(systemPrompt);
276-
out.writeOptionalString(userInstructions);
284+
if (version.onOrAfter(MINIMAL_SUPPORTED_VERSION_FOR_PROMPT_AND_INSTRUCTIONS)) {
285+
out.writeOptionalString(systemPrompt);
286+
out.writeOptionalString(userInstructions);
287+
}
288+
277289
out.writeInt(contextSize);
278290
out.writeInt(interactionSize);
279291
out.writeInt(timeout);
280-
out.writeOptionalString(llmResponseField);
292+
293+
if (version.onOrAfter(MINIMAL_SUPPORTED_VERSION_FOR_PROMPT_AND_INSTRUCTIONS)) {
294+
out.writeOptionalString(llmResponseField);
295+
}
281296

282297
if (version.onOrAfter(MINIMAL_SUPPORTED_VERSION_FOR_BEDROCK_CONVERSE_LLM_MESSAGES)) {
283298
out.writeList(llmMessages);

search-processors/src/test/java/org/opensearch/searchpipelines/questionanswering/generative/ext/GenerativeQAParamExtBuilderTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public void testMiscMethods() throws IOException {
121121
assertNotEquals(builder1, builder2);
122122
assertNotEquals(builder1.hashCode(), builder2.hashCode());
123123

124+
// BWC test for bedrock converse params
124125
StreamOutput so1 = mock(StreamOutput.class);
125126
when(so1.getVersion()).thenReturn(GenerativeQAParameters.MINIMAL_SUPPORTED_VERSION_FOR_BEDROCK_CONVERSE_LLM_MESSAGES);
126127
builder1.writeTo(so1);
@@ -130,6 +131,19 @@ public void testMiscMethods() throws IOException {
130131
when(so2.getVersion()).thenReturn(Version.V_2_17_0);
131132
builder1.writeTo(so2);
132133
verify(so2, times(5)).writeOptionalString(any());
134+
135+
// BWC test for system prompt and instructions
136+
StreamOutput so3 = mock(StreamOutput.class);
137+
when(so3.getVersion()).thenReturn(GenerativeQAParameters.MINIMAL_SUPPORTED_VERSION_FOR_PROMPT_AND_INSTRUCTIONS);
138+
builder1.writeTo(so3);
139+
verify(so3, times(5)).writeOptionalString(any());
140+
verify(so3, times(1)).writeString(any());
141+
142+
StreamOutput so4 = mock(StreamOutput.class);
143+
when(so4.getVersion()).thenReturn(Version.V_2_12_0);
144+
builder1.writeTo(so4);
145+
verify(so4, times(2)).writeOptionalString(any());
146+
verify(so4, times(1)).writeString(any());
133147
}
134148

135149
public void testParse() throws IOException {

0 commit comments

Comments
 (0)