Skip to content

Commit

Permalink
BWC (rag processor): add version control for newly added request para…
Browse files Browse the repository at this point in the history
…ms (#3125) (#3361)

* fix: handle bwc for new fields added in 2.13

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

* fix: spotless changes

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

* fix: test cases with bwc

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

* fix: spotless apply

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

---------

Signed-off-by: Pavan Yekbote <[email protected]>
(cherry picked from commit 40c4e68)

Co-authored-by: Pavan Yekbote <[email protected]>
  • Loading branch information
opensearch-trigger-bot[bot] and pyek-bot authored Jan 9, 2025
1 parent 5c4bd3a commit 589e24b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
import java.io.IOException;
import java.util.Objects;

import org.opensearch.Version;
import org.opensearch.core.common.Strings;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.ml.common.CommonValue;

import com.google.common.base.Preconditions;

Expand Down Expand Up @@ -81,6 +83,8 @@ public class GenerativeQAParameters implements Writeable, ToXContentObject {

public static final int SIZE_NULL_VALUE = -1;

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 @@ -145,15 +149,23 @@ public GenerativeQAParameters(
}

public GenerativeQAParameters(StreamInput input) throws IOException {
Version version = input.getVersion();
this.conversationId = input.readOptionalString();
this.llmModel = input.readOptionalString();
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();
}
}

@Override
Expand Down Expand Up @@ -201,17 +213,25 @@ public XContentBuilder toXContent(XContentBuilder xContentBuilder, Params params

@Override
public void writeTo(StreamOutput out) throws IOException {
Version version = out.getVersion();
out.writeOptionalString(conversationId);
out.writeOptionalString(llmModel);

Preconditions.checkNotNull(llmQuestion, "llm_question must not be null.");
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);
}
}

public static GenerativeQAParameters parse(XContentParser parser) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.opensearch.core.xcontent.ToXContent.EMPTY_PARAMS;

import java.io.EOFException;
import java.io.IOException;
import java.util.Collections;

import org.junit.Assert;
import org.opensearch.Version;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentType;
Expand Down Expand Up @@ -105,10 +107,17 @@ public void testMiscMethods() throws IOException {
assertNotEquals(builder1, builder2);
assertNotEquals(builder1.hashCode(), builder2.hashCode());

StreamOutput so = mock(StreamOutput.class);
builder1.writeTo(so);
verify(so, times(5)).writeOptionalString(any());
verify(so, times(1)).writeString(any());
StreamOutput so1 = mock(StreamOutput.class);
when(so1.getVersion()).thenReturn(GenerativeQAParameters.MINIMAL_SUPPORTED_VERSION_FOR_PROMPT_AND_INSTRUCTIONS);
builder1.writeTo(so1);
verify(so1, times(5)).writeOptionalString(any());
verify(so1, times(1)).writeString(any());

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

public void testParse() throws IOException {
Expand Down

0 comments on commit 589e24b

Please sign in to comment.