Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,13 @@ private static void runTool(
);
nextStepListener
.onResponse(
String.format(Locale.ROOT, "Failed to run the tool %s with the error message %s.", finalAction, e.getMessage())
String
.format(
Locale.ROOT,
"Failed to run the tool %s with the error message %s.",
finalAction,
e.getMessage().replaceAll("\\n", "\n")
)
);
});
if (tools.get(action) instanceof MLModelTool) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,11 @@
if (response.contains("```")) {
response = response.substring(0, response.lastIndexOf("```"));
}
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. This method doesn't have any null check for response
  2. Is there any chance that multiple code block can exist?
  3. what about json array?
  4. what happens if there's any string literals like {"data": "{nested}"}
  5. Possible IndexOutOfBoundsException if markers aren't found
  6. why do we need second trim?
  7. Using substring multiple times creates multiple intermediate string objects
    Each operation like contains(), indexOf(), and lastIndexOf() scans through the string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This method doesn't have any null check for response

response passed to this method could not be null, otherwise it will throw NPE before call it. But it's a good practice to add null check here

Is there any chance that multiple code block can exist?
what about json array?
what happens if there's any string literals like {"data": "{nested}"}

Yes, all of them are possible. the logic here is try our best to extract the expected json from LLM response.

Possible IndexOutOfBoundsException if markers aren't found

if markers are not found, it will not perform substring

why do we need second trim?

the content extracted from json block may have spaces, so trim is needed here before check it's a valid json string.

Using substring multiple times creates multiple intermediate string objects
Each operation like contains(), indexOf(), and lastIndexOf() scans through the string

that's not a big concern. the response is not large, and gc will help to handle intermediate string objects

// extract content from {} block
if (response.contains("{") && response.contains("}")) {
response = response.substring(response.indexOf("{"), response.lastIndexOf("}") + 1);

Check warning on line 526 in ml-algorithms/src/main/java/org/opensearch/ml/engine/algorithms/agent/MLPlanExecuteAndReflectAgentRunner.java

View check run for this annotation

Codecov / codecov/patch

ml-algorithms/src/main/java/org/opensearch/ml/engine/algorithms/agent/MLPlanExecuteAndReflectAgentRunner.java#L526

Added line #L526 was not covered by tests
}
}

response = response.trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@
try {
List<String> indexList = new ArrayList<>();
if (StringUtils.isNotBlank(parameters.get("index"))) {
indexList = gson.fromJson(parameters.get("index"), List.class);
try {
indexList = gson.fromJson(parameters.get("index"), List.class);
} catch (Exception e) {
indexList.add(parameters.get("index"));

Check warning on line 101 in ml-algorithms/src/main/java/org/opensearch/ml/engine/tools/IndexMappingTool.java

View check run for this annotation

Codecov / codecov/patch

ml-algorithms/src/main/java/org/opensearch/ml/engine/tools/IndexMappingTool.java#L100-L101

Added lines #L100 - L101 were not covered by tests
}
}

if (indexList.isEmpty()) {
Expand Down
Loading