-
Notifications
You must be signed in to change notification settings - Fork 184
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?
Add tool parameter type validation #4361
Conversation
ml-algorithms/src/main/java/org/opensearch/ml/engine/tools/AgentTool.java
Outdated
Show resolved
Hide resolved
ml-algorithms/src/main/java/org/opensearch/ml/engine/tools/ConnectorTool.java
Outdated
Show resolved
Hide resolved
d0d23bb to
34e1cb4
Compare
31503fa to
8932e94
Compare
|
Working on adding tests |
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.
Thanks for this PR Nathalie, these validations have been long time pending. Added 2 small comments, other than that LGTM!
| String question = parameters.get("question"); | ||
| if (question != null && question.length() > maxQuestionLength) { | ||
| throw new IllegalArgumentException("question length cannot exceed " + maxQuestionLength + " characters"); | ||
| } |
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.
Why would the index mapping tool, need a "question" attribute?
Looks like we are not using it anywhere
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 use it during tool execution. Also, in the doc, it listed question as a required parameter.
POST /_plugins/_ml/agents/9X7xWI0Bpc3sThaJdY9i/_execute
{
"parameters": {
"index": [ "sample-ecommerce" ],
"question": "What fields are in the sample-ecommerce index?"
}
}
I tested and we can run this tool without the question parameter. I'll update the doc to make it 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/IndexMappingTool.java
Line 229 in 8932e94
| ConfigurationUtils.readStringProperty(TYPE, null, params, "question"); |
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.
Although the question is shown in the parameter map passed to tools, but question is an agent level parameter, let's say an agent runs 10 tools, we only need to validate the question once when agent runs, instead of validating it 10 times when each tool runs.
| 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; |
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.
Same as above, we don't need the question parameter. The tool just ignores the question param
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.
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
| ConfigurationUtils.readStringProperty(TYPE, null, params, "question"); |
|
Some outdated tool information I found during testing, will open doc PR to update these
|
Signed-off-by: Nathalie Jonathan <[email protected]>
Signed-off-by: Nathalie Jonathan <[email protected]>
Signed-off-by: Nathalie Jonathan <[email protected]>
Signed-off-by: Nathalie Jonathan <[email protected]>
Signed-off-by: Nathalie Jonathan <[email protected]>
Signed-off-by: Nathalie Jonathan <[email protected]>
Signed-off-by: Nathalie Jonathan <[email protected]>
78c29fd to
f6c3fcc
Compare
| String question = parameters.get("question"); | ||
| if (question != null && question.length() > maxQuestionLength) { | ||
| throw new IllegalArgumentException("question length cannot exceed " + maxQuestionLength + " characters"); | ||
| } |
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.
Although the question is shown in the parameter map passed to tools, but question is an agent level parameter, let's say an agent runs 10 tools, we only need to validate the question once when agent runs, instead of validating it 10 times when each tool runs.
| return tool; | ||
| } | ||
|
|
||
| private static Object parseValue(String value) { |
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.
|
High level comment:
|
Description
Add tool parameter type validation.
max_question_length/max_input_lengthto control max size forquestion/inputfield. Default is 10000. Users can change the value by passing this parameter when register an agent or executing toolExamples:
More examples in this doc:
Tool Validation Test.pdf
Related Issues
Resolves #[Issue number to be closed when this PR is merged]
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.