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

Fix MLModelTool returns null if the response of LLM is a pure json object #2655

Merged
merged 4 commits into from
Jul 18, 2024

Conversation

qianheng-aws
Copy link
Contributor

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

if (dataAsMap.size() == 1 && dataAsMap.containsKey("response")) {

Issues Resolved

#2654

Check List

  • New functionality includes testing.
    • All tests pass
  • New functionality has been documented.
    • New functionality has javadoc added
  • Commits are signed per the DCO using --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.

xinyual
xinyual previously approved these changes Jul 16, 2024
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)) {
Copy link
Collaborator

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.

Copy link
Contributor Author

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
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

Copy link
Collaborator

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?

@xinyual xinyual merged commit 007b914 into opensearch-project:main Jul 18, 2024
7 checks passed
opensearch-trigger-bot bot pushed a commit that referenced this pull request Jul 18, 2024
…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)
@b4sjoo
Copy link
Collaborator

b4sjoo commented Jul 18, 2024

@qianheng-aws This PR will break 2.x compile if backport, you need to fix and raise a backport PR manually, please check

qianheng-aws added a commit to qianheng-aws/ml-commons that referenced this pull request Jul 18, 2024
…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)
@qianheng-aws
Copy link
Contributor Author

@qianheng-aws This PR will break 2.x compile if backport, you need to fix and raise a backport PR manually, please check

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

opensearch-trigger-bot bot pushed a commit that referenced this pull request Jul 18, 2024
…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)
dhrubo-os pushed a commit that referenced this pull request Jul 18, 2024
…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]>
opensearch-trigger-bot bot pushed a commit that referenced this pull request Jul 18, 2024
…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)
opensearch-trigger-bot bot pushed a commit that referenced this pull request Jul 18, 2024
…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)
@dhrubo-os
Copy link
Collaborator

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

@qianheng-aws
Copy link
Contributor Author

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.

dhrubo-os pushed a commit that referenced this pull request Jul 19, 2024
… 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]>
opensearch-trigger-bot bot pushed a commit that referenced this pull request Jul 19, 2024
… 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)
opensearch-trigger-bot bot pushed a commit that referenced this pull request Jul 19, 2024
… 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)
opensearch-trigger-bot bot pushed a commit that referenced this pull request Jul 19, 2024
… 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)
b4sjoo pushed a commit that referenced this pull request Jul 19, 2024
… 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]>
b4sjoo pushed a commit that referenced this pull request Jul 20, 2024
… 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]>
b4sjoo pushed a commit that referenced this pull request Jul 20, 2024
… 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants