Skip to content

Commit 1a2df6c

Browse files
pyek-botgithub-actions[bot]
authored andcommitted
[BUG] Agent Framework: Handle model response when toolUse is not accompanied by text (#3755)
* fix: handle model response when toolUse is not accompanied by text Signed-off-by: Pavan Yekbote <[email protected]> * feat: add test case for parseLLMOutput Signed-off-by: Pavan Yekbote <[email protected]> --------- Signed-off-by: Pavan Yekbote <[email protected]> (cherry picked from commit 736bf10)
1 parent 6c5c580 commit 1a2df6c

File tree

2 files changed

+467
-2
lines changed

2 files changed

+467
-2
lines changed

ml-algorithms/src/main/java/org/opensearch/ml/engine/algorithms/agent/AgentUtils.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,15 @@ public static Map<String, String> parseLLMOutput(
320320
parseThoughtResponse(modelOutput, thoughtResponse);
321321
} else if (parameters.containsKey(TOOL_CALLS_PATH)) {
322322
modelOutput.put(THOUGHT_RESPONSE, StringUtils.toJson(dataAsMap));
323-
Object response = JsonPath.read(dataAsMap, parameters.get(LLM_RESPONSE_FILTER));
323+
Object response;
324+
boolean isToolUseResponse = false;
325+
try {
326+
response = JsonPath.read(dataAsMap, parameters.get(LLM_RESPONSE_FILTER));
327+
} catch (PathNotFoundException e) {
328+
// If the regular response path fails, try the tool calls path
329+
response = JsonPath.read(dataAsMap, parameters.get(TOOL_CALLS_PATH));
330+
isToolUseResponse = true;
331+
}
324332

325333
String llmFinishReasonPath = parameters.get(LLM_FINISH_REASON_PATH);
326334
String llmFinishReason = "";
@@ -330,7 +338,7 @@ public static Map<String, String> parseLLMOutput(
330338
} else {
331339
llmFinishReason = JsonPath.read(dataAsMap, llmFinishReasonPath);
332340
}
333-
if (parameters.get(LLM_FINISH_REASON_TOOL_USE).equalsIgnoreCase(llmFinishReason)) {
341+
if (parameters.get(LLM_FINISH_REASON_TOOL_USE).equalsIgnoreCase(llmFinishReason) || isToolUseResponse) {
334342
List toolCalls = null;
335343
try {
336344
String toolCallsPath = parameters.get(TOOL_CALLS_PATH);

0 commit comments

Comments
 (0)