-
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?
Conversation
Signed-off-by: Jing Zhang <[email protected]>
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.
Can you open an issue on Flow Framework repo amplifying what changed here? We will need to update the ToolStep
to account for a change in API. Better yet, open a PR with the changes!
Also please make sure you update the API specification which Flow Framework will be using to validate these API parameters. (It's a checklist item at the top of this PR.)
common/src/main/java/org/opensearch/ml/common/agent/MLToolSpec.java
Outdated
Show resolved
Hide resolved
The flow framework issue, opensearch-project/flow-framework#878. |
Signed-off-by: Jing Zhang <[email protected]>
@@ -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; |
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
@@ -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::readOptionalString, StreamInput::readOptionalString); |
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 seems odd here, it looks like we're allowing null keys and null values in the map which I doubt is the case (at least the keys shouldn't be optional, and I'm not sure of the benefit of null values vs. just omitting the key). Same comment in the writeTo()
below.
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.
For the key, changed from readOptionalString/writeOptionalString
to readString/writeString
.
@@ -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.size() > 0) { |
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.
if (configMap != null && configMap.size() > 0) { | |
if (configMap != null && !configMap.isEmpty()) { |
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.
Done.
@@ -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 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.
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.
It's an existing function, I prefer to keep it unchanged at this moment.
@@ -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.EMPTY_MAP, false, Collections.EMPTY_MAP)), |
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.
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false, Collections.EMPTY_MAP)), | |
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false, Collections.emptyMap())), |
Use emptyMap()
rather than EMPTY_MAP
to get built in type validation. This will probably create unchecked cast warnings. (Same comment multiple times below.)
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.
Makes sense, changed.
|
||
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 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.
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.
Do you have any suggestions?
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.
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 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.
Assert.assertEquals(spec.getType(), "test"); | ||
Assert.assertEquals(spec.getName(), "test"); | ||
Assert.assertEquals(spec.getDescription(), "test"); |
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.
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.
Signed-off-by: Jing Zhang <[email protected]>
@reuschling Can you help review this PR? |
@jngz-es Can you please provide an example on how to use tool |
if (toolSpecMap.get(action).getConfigMap() != null) { | ||
llmToolTmpParameters.putAll(toolSpecMap.get(action).getConfigMap()); | ||
} | ||
llmToolTmpParameters.put(MLAgentExecutor.QUESTION, actionInput); | ||
tools.get(action).run(llmToolTmpParameters, toolListener); // run tool | ||
} else { | ||
Map<String, String> parameters = new HashMap<>(); | ||
parameters.putAll(tmpParameters); | ||
parameters.putAll(toolParams); | ||
if (toolSpecMap.get(action).getConfigMap() != null) { | ||
parameters.putAll(toolSpecMap.get(action).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.
I would suggest extract parameters.putAll(toolSpecMap.get(action).getConfigMap());
outside of the if-else statement.
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.
Don't get it. I need to add the config parameters override immediately before tool run.
|
||
// Override all parameters in tool config to tool execution parameters as the config contains the static parameters. | ||
if (toolSpec.getConfigMap() != null && !toolSpec.getConfigMap().isEmpty()) { | ||
executeParams.putAll(toolSpec.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.
The function is the same as MLFlowAgentRunner. Please extract the function to classes like AgentUtils
.
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.
As far as I see, this PR writes all parameters from the new config map into the parameter map that is relevant for execution, which means that config parameters will overwrite existing ones with same name, i.e. have prioritization. Thus, specifying an own static input parameter as suggested in #2836 and #2918 is possible, but crucial is also the possibility to specify placeholders inside the new config. I can not see some substitutions here but maybe this takes place somewhere else, for all parameters?
For #2836, the placeholder "${parameters.MLModelTool.output}"
was asked for.
For #2918, SearchIndexTool inside conversational agent, there is the need of a placeholder for the last action input of the agent, that must inserted into the OpenSearch query defined statically inside the new config. If conversational agent just sets the input parameter for the next tool, I doubt this information is lost if I specify a static input inside the tool config.
Last but not least, the documentation should be adjusted accordingly, e.g. SearchIndexTool and tutorial example. Also some list or hint to the common available placeholders would be nice, in order to know e.g. how the placeholder for the former conversational agent step action input looks like.
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.
Good callout, I am thinking of supporting placeholders inside the config. Thanks @reuschling
@@ -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 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")).
Map<String, String> params = Map | ||
.of("toolType.param2", "value2", "toolName.param3", "value3", "param4", "value4", "toolName.tool_key", "dynamic value"); | ||
|
||
Map<String, String> result = mlFlowAgentRunner.getToolExecuteParams(toolSpec, params); | ||
|
||
assertEquals("value1", result.get("param1")); | ||
assertEquals("value3", result.get("param3")); | ||
assertEquals("value4", result.get("param4")); | ||
assertFalse(result.containsKey("toolType.param2")); | ||
assertEquals("tool_config_value", result.get("tool_key")); |
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.
Please also verify that non-config parameters can override
Provided an example in the description section. |
Description
Add config field in MLToolSpec to support static parameters in tool execution.
An example from the issue 2918.
Related Issues
#2836
#2918
Check List
--signoff
.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.