Skip to content
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

add config field in MLToolSpec for static parameters #2977

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 @@ -11,11 +11,13 @@
import java.io.IOException;
import java.util.Map;

import org.opensearch.Version;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
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 lombok.Builder;
import lombok.EqualsAndHashCode;
Expand All @@ -24,20 +26,31 @@
@EqualsAndHashCode
@Getter
public class MLToolSpec implements ToXContentObject {
public static final Version MINIMAL_SUPPORTED_VERSION_FOR_TOOL_CONFIG = CommonValue.VERSION_2_17_0;
Copy link
Member

Choose a reason for hiding this comment

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

Isn't 2.17.0 already released so this would be a 2.18.0 feature?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We want to catch up with 2.17 patch.

Copy link
Member

Choose a reason for hiding this comment

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

2.17.0 open source is already released. If you want to change the version on the back port to that branch that gets synced elsewhere that’s a reasonable option but for anyone consuming the maven central artifact BWC would break with that version check.

Copy link
Member

Choose a reason for hiding this comment

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

Actually looks like a 2.17.1 release is happening so it should be changed to that.

Copy link
Member

Choose a reason for hiding this comment

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

Except they don't want any new features, just critical bugfixes. So nevermind :|

Copy link
Member

Choose a reason for hiding this comment

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

This PR is not a security fix. We can leave it to 2.18.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Based on the discussion with Yuye, I will make the version 2.18. Thanks @yuye-aws


public static final String TOOL_TYPE_FIELD = "type";
public static final String TOOL_NAME_FIELD = "name";
public static final String DESCRIPTION_FIELD = "description";
public static final String PARAMETERS_FIELD = "parameters";
public static final String INCLUDE_OUTPUT_IN_AGENT_RESPONSE = "include_output_in_agent_response";
public static final String CONFIG_FIELD = "config";

private String type;
private String name;
private String description;
private Map<String, String> parameters;
private boolean includeOutputInAgentResponse;
private Map<String, String> configMap;

@Builder(toBuilder = true)
public MLToolSpec(String type, String name, String description, Map<String, String> parameters, boolean includeOutputInAgentResponse) {
public MLToolSpec(
String type,
String name,
String description,
Map<String, String> parameters,
boolean includeOutputInAgentResponse,
Map<String, String> configMap
) {
if (type == null) {
throw new IllegalArgumentException("tool type is null");
}
Expand All @@ -46,6 +59,7 @@ public MLToolSpec(String type, String name, String description, Map<String, Stri
this.description = description;
this.parameters = parameters;
this.includeOutputInAgentResponse = includeOutputInAgentResponse;
this.configMap = configMap;
}

public MLToolSpec(StreamInput input) throws IOException {
Expand All @@ -56,6 +70,9 @@ public MLToolSpec(StreamInput input) throws IOException {
parameters = input.readMap(StreamInput::readString, StreamInput::readOptionalString);
}
includeOutputInAgentResponse = input.readBoolean();
if (input.getVersion().onOrAfter(MINIMAL_SUPPORTED_VERSION_FOR_TOOL_CONFIG) && input.readBoolean()) {
configMap = input.readMap(StreamInput::readString, StreamInput::readOptionalString);
}
}

public void writeTo(StreamOutput out) throws IOException {
Expand All @@ -69,6 +86,14 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeBoolean(false);
}
out.writeBoolean(includeOutputInAgentResponse);
if (out.getVersion().onOrAfter(MINIMAL_SUPPORTED_VERSION_FOR_TOOL_CONFIG)) {
if (configMap != null) {
out.writeBoolean(true);
out.writeMap(configMap, StreamOutput::writeString, StreamOutput::writeOptionalString);
} else {
out.writeBoolean(false);
}
}
}

@Override
Expand All @@ -87,6 +112,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.field(PARAMETERS_FIELD, parameters);
}
builder.field(INCLUDE_OUTPUT_IN_AGENT_RESPONSE, includeOutputInAgentResponse);
if (configMap != null && !configMap.isEmpty()) {
builder.field(CONFIG_FIELD, configMap);
}
builder.endObject();
return builder;
}
Expand All @@ -97,6 +125,7 @@ public static MLToolSpec parse(XContentParser parser) throws IOException {
String description = null;
Map<String, String> parameters = null;
boolean includeOutputInAgentResponse = false;
Map<String, String> configMap = null;

ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
Expand All @@ -119,6 +148,9 @@ public static MLToolSpec parse(XContentParser parser) throws IOException {
case INCLUDE_OUTPUT_IN_AGENT_RESPONSE:
includeOutputInAgentResponse = parser.booleanValue();
break;
case CONFIG_FIELD:
configMap = getParameterMap(parser.map());
Copy link
Member

Choose a reason for hiding this comment

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

may want to rename getParameterMap to be more general getStringToStringMap or similar.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's an existing function, I prefer to keep it unchanged at this moment.

break;
default:
parser.skipChildren();
break;
Expand All @@ -131,6 +163,7 @@ public static MLToolSpec parse(XContentParser parser) throws IOException {
.description(description)
.parameters(parameters)
.includeOutputInAgentResponse(includeOutputInAgentResponse)
.configMap(configMap)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void constructor_NullName() {
MLAgentType.CONVERSATIONAL.name(),
"test",
new LLMSpec("test_model", Map.of("test_key", "test_value")),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false)),
List.of(new MLToolSpec("test", "test", "test", Collections.emptyMap(), false, Collections.emptyMap())),
null,
null,
Instant.EPOCH,
Expand All @@ -66,7 +66,7 @@ public void constructor_NullType() {
null,
"test",
new LLMSpec("test_model", Map.of("test_key", "test_value")),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false)),
List.of(new MLToolSpec("test", "test", "test", Collections.emptyMap(), false, Collections.emptyMap())),
null,
null,
Instant.EPOCH,
Expand All @@ -86,7 +86,7 @@ public void constructor_NullLLMSpec() {
MLAgentType.CONVERSATIONAL.name(),
"test",
null,
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false)),
List.of(new MLToolSpec("test", "test", "test", Collections.emptyMap(), false, Collections.emptyMap())),
null,
null,
Instant.EPOCH,
Expand All @@ -100,7 +100,14 @@ public void constructor_NullLLMSpec() {
public void constructor_DuplicateTool() {
exceptionRule.expect(IllegalArgumentException.class);
exceptionRule.expectMessage("Duplicate tool defined: test_tool_name");
MLToolSpec mlToolSpec = new MLToolSpec("test_tool_type", "test_tool_name", "test", Collections.EMPTY_MAP, false);
MLToolSpec mlToolSpec = new MLToolSpec(
"test_tool_type",
"test_tool_name",
"test",
Collections.emptyMap(),
false,
Collections.emptyMap()
);
MLAgent agent = new MLAgent(
"test_name",
MLAgentType.CONVERSATIONAL.name(),
Expand All @@ -123,7 +130,7 @@ public void writeTo() throws IOException {
"CONVERSATIONAL",
"test",
new LLMSpec("test_model", Map.of("test_key", "test_value")),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false)),
List.of(new MLToolSpec("test", "test", "test", Collections.emptyMap(), false, Collections.emptyMap())),
Map.of("test", "test"),
new MLMemorySpec("test", "123", 0),
Instant.EPOCH,
Expand All @@ -150,7 +157,7 @@ public void writeTo_NullLLM() throws IOException {
"FLOW",
"test",
null,
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false)),
List.of(new MLToolSpec("test", "test", "test", Collections.emptyMap(), false, Collections.emptyMap())),
Map.of("test", "test"),
new MLMemorySpec("test", "123", 0),
Instant.EPOCH,
Expand Down Expand Up @@ -194,7 +201,7 @@ public void writeTo_NullParameters() throws IOException {
MLAgentType.CONVERSATIONAL.name(),
"test",
new LLMSpec("test_model", Map.of("test_key", "test_value")),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false)),
List.of(new MLToolSpec("test", "test", "test", Collections.emptyMap(), false, Collections.emptyMap())),
null,
new MLMemorySpec("test", "123", 0),
Instant.EPOCH,
Expand All @@ -216,7 +223,7 @@ public void writeTo_NullMemory() throws IOException {
"CONVERSATIONAL",
"test",
new LLMSpec("test_model", Map.of("test_key", "test_value")),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false)),
List.of(new MLToolSpec("test", "test", "test", Collections.emptyMap(), false, Collections.emptyMap())),
Map.of("test", "test"),
null,
Instant.EPOCH,
Expand All @@ -238,7 +245,7 @@ public void toXContent() throws IOException {
"CONVERSATIONAL",
"test",
new LLMSpec("test_model", Map.of("test_key", "test_value")),
List.of(new MLToolSpec("test", "test", "test", Map.of("test", "test"), false)),
List.of(new MLToolSpec("test", "test", "test", Map.of("test", "test"), false, Collections.emptyMap())),
Map.of("test", "test"),
new MLMemorySpec("test", "123", 0),
Instant.EPOCH,
Expand Down Expand Up @@ -294,7 +301,7 @@ public void fromStream() throws IOException {
MLAgentType.CONVERSATIONAL.name(),
"test",
new LLMSpec("test_model", Map.of("test_key", "test_value")),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false)),
List.of(new MLToolSpec("test", "test", "test", Collections.emptyMap(), false, Collections.emptyMap())),
Map.of("test", "test"),
new MLMemorySpec("test", "123", 0),
Instant.EPOCH,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class MLToolSpecTest {

@Test
public void writeTo() throws IOException {
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false);
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false, Map.of("configKey", "configValue"));
Copy link
Member

Choose a reason for hiding this comment

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

To avoid confusion from too many test String, I would recommend declaring MLToolSpec spec as a private static final member in this class. The test should be like new MLToolSpec("test_type", "test_name", "test_description", Map.of("test_parameter_key", "test_parameter_value", false, Map.of("configKey", "configValue")).

BytesStreamOutput output = new BytesStreamOutput();
spec.writeTo(output);
MLToolSpec spec1 = new MLToolSpec(output.bytes().streamInput());
Expand All @@ -32,11 +32,70 @@ public void writeTo() throws IOException {
Assert.assertEquals(spec.getParameters(), spec1.getParameters());
Assert.assertEquals(spec.getDescription(), spec1.getDescription());
Assert.assertEquals(spec.isIncludeOutputInAgentResponse(), spec1.isIncludeOutputInAgentResponse());
Assert.assertEquals(spec.getConfigMap(), spec1.getConfigMap());
}

@Test
public void writeToEmptyConfigMap() throws IOException {
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false, Collections.emptyMap());
BytesStreamOutput output = new BytesStreamOutput();
spec.writeTo(output);
MLToolSpec spec1 = new MLToolSpec(output.bytes().streamInput());

Assert.assertEquals(spec.getType(), spec1.getType());
Assert.assertEquals(spec.getName(), spec1.getName());
Assert.assertEquals(spec.getParameters(), spec1.getParameters());
Assert.assertEquals(spec.getDescription(), spec1.getDescription());
Assert.assertEquals(spec.isIncludeOutputInAgentResponse(), spec1.isIncludeOutputInAgentResponse());
Assert.assertEquals(spec.getConfigMap(), spec1.getConfigMap());
}

@Test
public void writeToNullConfigMap() throws IOException {
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false, null);
BytesStreamOutput output = new BytesStreamOutput();
spec.writeTo(output);
MLToolSpec spec1 = new MLToolSpec(output.bytes().streamInput());

Assert.assertEquals(spec.getType(), spec1.getType());
Assert.assertEquals(spec.getName(), spec1.getName());
Assert.assertEquals(spec.getParameters(), spec1.getParameters());
Assert.assertEquals(spec.getDescription(), spec1.getDescription());
Assert.assertEquals(spec.isIncludeOutputInAgentResponse(), spec1.isIncludeOutputInAgentResponse());
Assert.assertNull(spec1.getConfigMap());
}

@Test
public void toXContent() throws IOException {
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false);
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false, Map.of("configKey", "configValue"));
XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent());
spec.toXContent(builder, ToXContent.EMPTY_PARAMS);
String content = TestHelper.xContentBuilderToString(builder);

Assert
.assertEquals(
"{\"type\":\"test\",\"name\":\"test\",\"description\":\"test\",\"parameters\":{\"test\":\"test\"},\"include_output_in_agent_response\":false,\"config\":{\"configKey\":\"configValue\"}}",
Copy link
Member

Choose a reason for hiding this comment

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

I don't think the order of fields in JSON is guaranteed unless you specify a sorting order. Same comment in other assertions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Do you have any suggestions?

Copy link
Member

Choose a reason for hiding this comment

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

I usually do assertions that the string contains each key value pair. Or just extract to a map with a util function and check keys. I think there are some optional arguments with the Xcontent that let you specify an ordering.

Copy link
Member

Choose a reason for hiding this comment

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

Looked into this. Seems internal comments indicate the "XContent" preserves the same ordering, so since you're just parsing the same thing you built, this should be fine in this test case. The concern would be if you're using any sort of external parsing using a map, where the ordering isn't guaranteed.

content
);
}

@Test
public void toXContentEmptyConfigMap() throws IOException {
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false, Collections.emptyMap());
XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent());
spec.toXContent(builder, ToXContent.EMPTY_PARAMS);
String content = TestHelper.xContentBuilderToString(builder);

Assert
.assertEquals(
"{\"type\":\"test\",\"name\":\"test\",\"description\":\"test\",\"parameters\":{\"test\":\"test\"},\"include_output_in_agent_response\":false}",
content
);
}

@Test
public void toXContentNullConfigMap() throws IOException {
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false, null);
XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent());
spec.toXContent(builder, ToXContent.EMPTY_PARAMS);
String content = TestHelper.xContentBuilderToString(builder);
Expand All @@ -50,6 +109,28 @@ public void toXContent() throws IOException {

@Test
public void parse() throws IOException {
String jsonStr =
"{\"type\":\"test\",\"name\":\"test\",\"description\":\"test\",\"parameters\":{\"test\":\"test\"},\"include_output_in_agent_response\":false,\"config\":{\"configKey\":\"configValue\"}}";
XContentParser parser = XContentType.JSON
.xContent()
.createParser(
new NamedXContentRegistry(new SearchModule(Settings.EMPTY, Collections.emptyList()).getNamedXContents()),
null,
jsonStr
);
parser.nextToken();
MLToolSpec spec = MLToolSpec.parse(parser);

Assert.assertEquals(spec.getType(), "test");
Assert.assertEquals(spec.getName(), "test");
Assert.assertEquals(spec.getDescription(), "test");
Comment on lines +124 to +126
Copy link
Member

Choose a reason for hiding this comment

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

Nit: using the same string everywhere can let copy-paste-and-forget-to-change errors slip through. Recommend using a different string for each field.

Assert.assertEquals(spec.getParameters(), Map.of("test", "test"));
Assert.assertEquals(spec.isIncludeOutputInAgentResponse(), false);
Assert.assertEquals(spec.getConfigMap(), Map.of("configKey", "configValue"));
}

@Test
public void parseEmptyConfigMap() throws IOException {
String jsonStr =
"{\"type\":\"test\",\"name\":\"test\",\"description\":\"test\",\"parameters\":{\"test\":\"test\"},\"include_output_in_agent_response\":false}";
XContentParser parser = XContentType.JSON
Expand All @@ -67,11 +148,42 @@ public void parse() throws IOException {
Assert.assertEquals(spec.getDescription(), "test");
Assert.assertEquals(spec.getParameters(), Map.of("test", "test"));
Assert.assertEquals(spec.isIncludeOutputInAgentResponse(), false);
Assert.assertEquals(spec.getConfigMap(), null);
}

@Test
public void fromStream() throws IOException {
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false);
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false, Map.of("configKey", "configValue"));
BytesStreamOutput output = new BytesStreamOutput();
spec.writeTo(output);
MLToolSpec spec1 = MLToolSpec.fromStream(output.bytes().streamInput());

Assert.assertEquals(spec.getType(), spec1.getType());
Assert.assertEquals(spec.getName(), spec1.getName());
Assert.assertEquals(spec.getParameters(), spec1.getParameters());
Assert.assertEquals(spec.getDescription(), spec1.getDescription());
Assert.assertEquals(spec.isIncludeOutputInAgentResponse(), spec1.isIncludeOutputInAgentResponse());
Assert.assertEquals(spec.getConfigMap(), spec1.getConfigMap());
}

@Test
public void fromStreamEmptyConfigMap() throws IOException {
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false, Collections.emptyMap());
BytesStreamOutput output = new BytesStreamOutput();
spec.writeTo(output);
MLToolSpec spec1 = MLToolSpec.fromStream(output.bytes().streamInput());

Assert.assertEquals(spec.getType(), spec1.getType());
Assert.assertEquals(spec.getName(), spec1.getName());
Assert.assertEquals(spec.getParameters(), spec1.getParameters());
Assert.assertEquals(spec.getDescription(), spec1.getDescription());
Assert.assertEquals(spec.isIncludeOutputInAgentResponse(), spec1.isIncludeOutputInAgentResponse());
Assert.assertEquals(spec.getConfigMap(), spec1.getConfigMap());
}

@Test
public void fromStreamNullConfigMap() throws IOException {
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false, null);
BytesStreamOutput output = new BytesStreamOutput();
spec.writeTo(output);
MLToolSpec spec1 = MLToolSpec.fromStream(output.bytes().streamInput());
Expand All @@ -81,5 +193,6 @@ public void fromStream() throws IOException {
Assert.assertEquals(spec.getParameters(), spec1.getParameters());
Assert.assertEquals(spec.getDescription(), spec1.getDescription());
Assert.assertEquals(spec.isIncludeOutputInAgentResponse(), spec1.isIncludeOutputInAgentResponse());
Assert.assertEquals(spec.getConfigMap(), spec1.getConfigMap());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void writeTo() throws IOException {
MLAgentType.CONVERSATIONAL.name(),
"test",
new LLMSpec("test_model", Map.of("test_key", "test_value")),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false)),
List.of(new MLToolSpec("test", "test", "test", Collections.emptyMap(), false, Collections.emptyMap())),
Map.of("test", "test"),
new MLMemorySpec("test", "123", 0),
Instant.EPOCH,
Expand Down
Loading
Loading