-
Notifications
You must be signed in to change notification settings - Fork 186
Add tool parameter type validation #4361
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
base: main
Are you sure you want to change the base?
Changes from all commits
256937f
8723b3c
9daf064
26cd805
8fc76cd
f342866
f6c3fcc
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 | ||
|---|---|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |||
| import org.opensearch.common.settings.Settings; | ||||
| import org.opensearch.common.unit.TimeValue; | ||||
| import org.opensearch.core.action.ActionListener; | ||||
| import org.opensearch.ingest.ConfigurationUtils; | ||||
| import org.opensearch.ml.common.spi.tools.Parser; | ||||
| import org.opensearch.ml.common.spi.tools.Tool; | ||||
| import org.opensearch.ml.common.spi.tools.ToolAnnotation; | ||||
|
|
@@ -55,6 +56,9 @@ public class IndexMappingTool implements Tool { | |||
| + "\"required\":[\"index\"]," | ||||
| + "\"additionalProperties\":false}"; | ||||
| public static final Map<String, Object> DEFAULT_ATTRIBUTES = Map.of(TOOL_INPUT_SCHEMA_FIELD, DEFAULT_INPUT_SCHEMA, STRICT_FIELD, true); | ||||
| public static final int DEFAULT_MAX_QUESTION_LENGTH = 10000; | ||||
| public static final String MAX_QUESTION_LENGTH_FIELD = "max_question_length"; | ||||
| private int maxQuestionLength = DEFAULT_MAX_QUESTION_LENGTH; | ||||
|
|
||||
| @Setter | ||||
| @Getter | ||||
|
|
@@ -175,7 +179,17 @@ public String getType() { | |||
|
|
||||
| @Override | ||||
| public boolean validate(Map<String, String> parameters) { | ||||
| return parameters != null && !parameters.isEmpty() && parameters.containsKey("index"); | ||||
| if (parameters == null || parameters.isEmpty() || !parameters.containsKey("index")) { | ||||
| return false; | ||||
| } | ||||
|
|
||||
| // Validate question length | ||||
| String question = parameters.get("question"); | ||||
| if (question != null && question.length() > maxQuestionLength) { | ||||
| throw new IllegalArgumentException("question length cannot exceed " + maxQuestionLength + " characters"); | ||||
| } | ||||
|
Comment on lines
+187
to
+190
Contributor
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. Why would the index mapping tool, need a "question" attribute?
Contributor
Author
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. We use it during tool execution. Also, in the doc, it listed I tested and we can run this tool without the question parameter. I'll update the doc to make it optional. ml-commons/ml-algorithms/src/main/java/org/opensearch/ml/engine/tools/IndexMappingTool.java Line 229 in 8932e94
Collaborator
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. Although the question is shown in the parameter map passed to tools, but |
||||
|
|
||||
| return true; | ||||
| } | ||||
|
|
||||
| /** | ||||
|
|
@@ -212,8 +226,14 @@ public void init(Client client) { | |||
|
|
||||
| @Override | ||||
| public IndexMappingTool create(Map<String, Object> params) { | ||||
| ConfigurationUtils.readOptionalStringProperty(TYPE, null, params, "question"); | ||||
| ConfigurationUtils.readOptionalList(TYPE, null, params, "index"); | ||||
| ConfigurationUtils.readBooleanProperty(TYPE, null, params, "local", false); | ||||
|
|
||||
| IndexMappingTool indexMappingTool = new IndexMappingTool(client); | ||||
| indexMappingTool.setOutputParser(ToolParser.createFromToolParams(params)); | ||||
| indexMappingTool.maxQuestionLength = ConfigurationUtils | ||||
| .readIntProperty(TYPE, null, params, MAX_QUESTION_LENGTH_FIELD, DEFAULT_MAX_QUESTION_LENGTH); | ||||
| return indexMappingTool; | ||||
| } | ||||
|
|
||||
|
|
||||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -54,6 +54,7 @@ | |||
| import org.opensearch.core.action.ActionListener; | ||||
| import org.opensearch.core.action.ActionResponse; | ||||
| import org.opensearch.index.IndexSettings; | ||||
| import org.opensearch.ingest.ConfigurationUtils; | ||||
| import org.opensearch.ml.common.spi.tools.Parser; | ||||
| import org.opensearch.ml.common.spi.tools.Tool; | ||||
| import org.opensearch.ml.common.spi.tools.ToolAnnotation; | ||||
|
|
@@ -86,6 +87,9 @@ public class ListIndexTool implements Tool { | |||
| + "for example: [\\\"index1\\\", \\\"index2\\\"], use empty array [] to list all indices in the cluster\"}}," | ||||
| + "\"additionalProperties\":false}"; | ||||
| public static final Map<String, Object> DEFAULT_ATTRIBUTES = Map.of(TOOL_INPUT_SCHEMA_FIELD, DEFAULT_INPUT_SCHEMA, STRICT_FIELD, false); | ||||
| public static final int DEFAULT_MAX_QUESTION_LENGTH = 10000; | ||||
| public static final String MAX_QUESTION_LENGTH_FIELD = "max_question_length"; | ||||
| private int maxQuestionLength = DEFAULT_MAX_QUESTION_LENGTH; | ||||
|
Comment on lines
+90
to
+92
Contributor
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. Same as above, we don't need the question parameter. The tool just ignores the question param
Contributor
Author
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. Same thing here, we use it during tool execution. Doc marked it as a required field. But looking at the code, we can actually run this tool without question parameter. I'll update the doc to make this parameter optional. We can keep the question length validation here since users can still pass the question parameter, but I'll modify this to optional ml-commons/ml-algorithms/src/main/java/org/opensearch/ml/engine/tools/ListIndexTool.java Line 471 in 8932e94
|
||||
|
|
||||
| @Setter | ||||
| @Getter | ||||
|
|
@@ -415,7 +419,16 @@ public void onFailure(final Exception e) { | |||
|
|
||||
| @Override | ||||
| public boolean validate(Map<String, String> parameters) { | ||||
| return parameters != null && !parameters.isEmpty(); | ||||
| if (parameters == null || parameters.isEmpty()) { | ||||
| return false; | ||||
| } | ||||
|
|
||||
| // Validate question length | ||||
| String question = parameters.get("question"); | ||||
| if (question != null && question.length() > maxQuestionLength) { | ||||
| throw new IllegalArgumentException("question length cannot exceed " + maxQuestionLength + " characters"); | ||||
| } | ||||
| return true; | ||||
| } | ||||
|
|
||||
| /** | ||||
|
|
@@ -455,8 +468,15 @@ public void init(Client client, ClusterService clusterService) { | |||
|
|
||||
| @Override | ||||
| public ListIndexTool create(Map<String, Object> params) { | ||||
| ConfigurationUtils.readOptionalStringProperty(TYPE, null, params, "question"); | ||||
| ConfigurationUtils.readOptionalList(TYPE, null, params, "indices"); | ||||
| ConfigurationUtils.readBooleanProperty(TYPE, null, params, "local", false); | ||||
| ConfigurationUtils.readIntProperty(TYPE, null, params, "page_size", 100); | ||||
|
|
||||
| ListIndexTool tool = new ListIndexTool(client, clusterService); | ||||
| tool.setOutputParser(ToolParser.createFromToolParams(params)); | ||||
| tool.maxQuestionLength = ConfigurationUtils | ||||
| .readIntProperty(TYPE, null, params, MAX_QUESTION_LENGTH_FIELD, DEFAULT_MAX_QUESTION_LENGTH); | ||||
| return tool; | ||||
| } | ||||
|
|
||||
|
|
||||
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 see the benefit of parsing these values, if a tool requires different types like map or double/float, then they're still being passed to tool in String format.