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

Conversation

jngz-es
Copy link
Collaborator

@jngz-es jngz-es commented Sep 23, 2024

Description

Add config field in MLToolSpec to support static parameters in tool execution.

An example from the issue 2918.

POST /_plugins/_ml/agents/_register
{
	"name": "Test Agent",
	"type": "conversational",
	"description": "test agent",
	"llm": {
		"model_id": "<model id>",
		"parameters": {
      "max_iteration": 5,
      "stop_when_no_tool_found": true,
      "response_filter": "$.completion"
    }
	},
	"memory": {
		"type": "conversation_index"
	},
	"app_type": "chat",
	"tools": [
		{
			"type": "SearchIndexTool",
			"description": "A tool to search opensearch index with natural language question.",
			"include_output_in_agent_response": true,
			"config": {
				"input": "{\"index\": \"sample-index\", \"query\": {\"query\": { \"match_all\": {}}} }"
			}
		}
	]
}

Related Issues

#2836
#2918

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff.
  • Public documentation issue/PR created.

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.

Copy link
Member

@dbwiddis dbwiddis left a 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.)

CC: @owaiskazi19 @amitgalitz @junweid62 @ylwu-amzn

@jngz-es
Copy link
Collaborator Author

jngz-es commented Sep 23, 2024

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;
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

@@ -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);
Copy link
Member

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.

Copy link
Collaborator Author

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) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (configMap != null && configMap.size() > 0) {
if (configMap != null && !configMap.isEmpty()) {

Copy link
Collaborator Author

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());
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.

@@ -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)),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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.)

Copy link
Collaborator Author

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\"}}",
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.

Comment on lines +124 to +126
Assert.assertEquals(spec.getType(), "test");
Assert.assertEquals(spec.getName(), "test");
Assert.assertEquals(spec.getDescription(), "test");
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.

Signed-off-by: Jing Zhang <[email protected]>
@yuye-aws
Copy link
Member

@reuschling Can you help review this PR?

@yuye-aws
Copy link
Member

@jngz-es Can you please provide an example on how to use tool config?

Comment on lines +473 to +484
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());
}
Copy link
Member

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.

Copy link
Collaborator Author

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.

Comment on lines +437 to +442

// 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());
}

Copy link
Member

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.

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.

Copy link
Collaborator Author

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"));
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")).

Comment on lines +311 to +320
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"));
Copy link
Member

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

@jngz-es
Copy link
Collaborator Author

jngz-es commented Sep 24, 2024

@jngz-es Can you please provide an example on how to use tool config?

Provided an example in the description section.

@jngz-es jngz-es added the v2.18.0 Issues targeting release v2.18.0 label Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v2.18.0 Issues targeting release v2.18.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants