-
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
Fix MLModelTool returns null if the response of LLM is a pure json object #2655
Conversation
…ject Signed-off-by: Heng Qian <[email protected]>
Signed-off-by: Heng Qian <[email protected]>
return mlModelOutputs.get(0).getMlModelTensors().get(0).getDataAsMap().get(responseField); | ||
Map<String, ?> dataAsMap = mlModelOutputs.get(0).getMlModelTensors().get(0).getDataAsMap(); | ||
// Return the response field if it exists, otherwise return the whole response a json string. | ||
if (dataAsMap.containsKey(responseField)) { |
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.
Original code doesn't have check on dataAsMap which might cause NPE in edge case, it would be good to add a check here.
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 see many places that access dataAsMap directly without any index checking, so I follow that rule. But I can add array index checking.
Signed-off-by: Heng Qian <[email protected]>
Signed-off-by: Heng Qian <[email protected]>
@@ -54,6 +55,7 @@ public class MLModelTool implements Tool { | |||
private Parser inputParser; | |||
@Setter | |||
@Getter | |||
@VisibleForTesting |
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 do we need this?
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.
Make UT be able to access the private field outputParser
and test whether its results are as expected.
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.
But in the test you already declared: Parser outputParser = new MLModelTool(client, "modelId", "response").getOutputParser();
How are you using outputParser
from the MLModelTool class then?
…ject (#2655) * Fix MLModelTool returns null if the response of LLM is a pure json object Signed-off-by: Heng Qian <[email protected]> * Fix UT failure Signed-off-by: Heng Qian <[email protected]> * Avoid NPE Signed-off-by: Heng Qian <[email protected]> * spotlessApply Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]> (cherry picked from commit 007b914)
@qianheng-aws This PR will break 2.x compile if backport, you need to fix and raise a backport PR manually, please check |
…ject (opensearch-project#2655) * Fix MLModelTool returns null if the response of LLM is a pure json object Signed-off-by: Heng Qian <[email protected]> * Fix UT failure Signed-off-by: Heng Qian <[email protected]> * Avoid NPE Signed-off-by: Heng Qian <[email protected]> * spotlessApply Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]> (cherry picked from commit 007b914)
Thanks for reminding. It's because I use a java21 API in this change while branch 2.x is built under java17. I've raised a new PR to backport it, #2675 |
…ject (#2655) * Fix MLModelTool returns null if the response of LLM is a pure json object Signed-off-by: Heng Qian <[email protected]> * Fix UT failure Signed-off-by: Heng Qian <[email protected]> * Avoid NPE Signed-off-by: Heng Qian <[email protected]> * spotlessApply Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]> (cherry picked from commit 007b914)
…ject (#2655) (#2677) * Fix MLModelTool returns null if the response of LLM is a pure json object Signed-off-by: Heng Qian <[email protected]> * Fix UT failure Signed-off-by: Heng Qian <[email protected]> * Avoid NPE Signed-off-by: Heng Qian <[email protected]> * spotlessApply Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]> (cherry picked from commit 007b914) Co-authored-by: qianheng <[email protected]>
…ject (#2655) * Fix MLModelTool returns null if the response of LLM is a pure json object Signed-off-by: Heng Qian <[email protected]> * Fix UT failure Signed-off-by: Heng Qian <[email protected]> * Avoid NPE Signed-off-by: Heng Qian <[email protected]> * spotlessApply Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]> (cherry picked from commit 007b914)
…ject (#2655) * Fix MLModelTool returns null if the response of LLM is a pure json object Signed-off-by: Heng Qian <[email protected]> * Fix UT failure Signed-off-by: Heng Qian <[email protected]> * Avoid NPE Signed-off-by: Heng Qian <[email protected]> * spotlessApply Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]> (cherry picked from commit 007b914)
https://github.com/opensearch-project/ml-commons/actions/runs/9997362810/job/27633752977?pr=2679 this workflow is failing. May be we should use method which is compatible with java 11 too. Could you please refactor this change? Thanks |
Raised this PR #2682 to avoid diverge on main branch. It will be compatible with java11 as well. |
… a pure json object (#2675) * Fix MLModelTool returns null if the response of LLM is a pure json object (#2655) * Fix MLModelTool returns null if the response of LLM is a pure json object Signed-off-by: Heng Qian <[email protected]> * Fix UT failure Signed-off-by: Heng Qian <[email protected]> * Avoid NPE Signed-off-by: Heng Qian <[email protected]> * spotlessApply Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]> (cherry picked from commit 007b914) * remove java21 API for backporting to 2.x Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]>
… a pure json object (#2675) * Fix MLModelTool returns null if the response of LLM is a pure json object (#2655) * Fix MLModelTool returns null if the response of LLM is a pure json object Signed-off-by: Heng Qian <[email protected]> * Fix UT failure Signed-off-by: Heng Qian <[email protected]> * Avoid NPE Signed-off-by: Heng Qian <[email protected]> * spotlessApply Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]> (cherry picked from commit 007b914) * remove java21 API for backporting to 2.x Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]> (cherry picked from commit 0a6a2b0)
… a pure json object (#2675) * Fix MLModelTool returns null if the response of LLM is a pure json object (#2655) * Fix MLModelTool returns null if the response of LLM is a pure json object Signed-off-by: Heng Qian <[email protected]> * Fix UT failure Signed-off-by: Heng Qian <[email protected]> * Avoid NPE Signed-off-by: Heng Qian <[email protected]> * spotlessApply Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]> (cherry picked from commit 007b914) * remove java21 API for backporting to 2.x Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]> (cherry picked from commit 0a6a2b0)
… a pure json object (#2675) * Fix MLModelTool returns null if the response of LLM is a pure json object (#2655) * Fix MLModelTool returns null if the response of LLM is a pure json object Signed-off-by: Heng Qian <[email protected]> * Fix UT failure Signed-off-by: Heng Qian <[email protected]> * Avoid NPE Signed-off-by: Heng Qian <[email protected]> * spotlessApply Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]> (cherry picked from commit 007b914) * remove java21 API for backporting to 2.x Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]> (cherry picked from commit 0a6a2b0)
… a pure json object (#2675) (#2685) * Fix MLModelTool returns null if the response of LLM is a pure json object (#2655) * Fix MLModelTool returns null if the response of LLM is a pure json object Signed-off-by: Heng Qian <[email protected]> * Fix UT failure Signed-off-by: Heng Qian <[email protected]> * Avoid NPE Signed-off-by: Heng Qian <[email protected]> * spotlessApply Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]> (cherry picked from commit 007b914) * remove java21 API for backporting to 2.x Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]> (cherry picked from commit 0a6a2b0) Co-authored-by: qianheng <[email protected]>
… a pure json object (#2675) (#2683) * Fix MLModelTool returns null if the response of LLM is a pure json object (#2655) * Fix MLModelTool returns null if the response of LLM is a pure json object Signed-off-by: Heng Qian <[email protected]> * Fix UT failure Signed-off-by: Heng Qian <[email protected]> * Avoid NPE Signed-off-by: Heng Qian <[email protected]> * spotlessApply Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]> (cherry picked from commit 007b914) * remove java21 API for backporting to 2.x Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]> (cherry picked from commit 0a6a2b0) Co-authored-by: qianheng <[email protected]>
… a pure json object (#2675) (#2684) * Fix MLModelTool returns null if the response of LLM is a pure json object (#2655) * Fix MLModelTool returns null if the response of LLM is a pure json object Signed-off-by: Heng Qian <[email protected]> * Fix UT failure Signed-off-by: Heng Qian <[email protected]> * Avoid NPE Signed-off-by: Heng Qian <[email protected]> * spotlessApply Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]> (cherry picked from commit 007b914) * remove java21 API for backporting to 2.x Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]> (cherry picked from commit 0a6a2b0) Co-authored-by: qianheng <[email protected]>
Description
Fix MLModelTool returns null if the response of LLM is a pure json object. It will detect wether the LLM response is a pure json object or a response string, and parse in different way.
There is similar logic in AgentUtils.java for MLChatAgentRunner
ml-commons/ml-algorithms/src/main/java/org/opensearch/ml/engine/algorithms/agent/AgentUtils.java
Line 193 in b6618b2
Issues Resolved
#2654
Check List
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.