-
Notifications
You must be signed in to change notification settings - Fork 129
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
||
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"); | ||
} | ||
|
@@ -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 { | ||
|
@@ -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 { | ||
|
@@ -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 | ||
|
@@ -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; | ||
} | ||
|
@@ -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) { | ||
|
@@ -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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. may want to rename There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -131,6 +163,7 @@ public static MLToolSpec parse(XContentParser parser) throws IOException { | |
.description(description) | ||
.parameters(parameters) | ||
.includeOutputInAgentResponse(includeOutputInAgentResponse) | ||
.configMap(configMap) | ||
.build(); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid confusion from too many |
||
BytesStreamOutput output = new BytesStreamOutput(); | ||
spec.writeTo(output); | ||
MLToolSpec spec1 = new MLToolSpec(output.bytes().streamInput()); | ||
|
@@ -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\"}}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have any suggestions? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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()); | ||
|
@@ -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()); | ||
} | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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