Skip to content

Commit b317dbd

Browse files
Fix final answer with extra meaningless symbol (#2676) (#2749)
* Fix final answer with extra meaningless symbol Signed-off-by: Heng Qian <[email protected]> * Add a new unit test to verify the case of final answer with quotes Signed-off-by: Heng Qian <[email protected]> --------- Signed-off-by: Heng Qian <[email protected]> (cherry picked from commit 5ad26df) Co-authored-by: qianheng <[email protected]>
1 parent f18a5bc commit b317dbd

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public static void parseThoughtResponse(Map<String, String> modelOutput, String
295295
public static String extractFinalAnswer(String text) {
296296
String result = null;
297297
if (text.contains("\"final_answer\"")) {
298-
String pattern = "\"final_answer\"\\s*:\\s*\"(.*?)$";
298+
String pattern = "\"final_answer\"\\s*:\\s*\"(.*)\"";
299299
Pattern jsonBlockPattern = Pattern.compile(pattern, Pattern.DOTALL);
300300
Matcher jsonBlockMatcher = jsonBlockPattern.matcher(text);
301301
if (jsonBlockMatcher.find()) {

ml-algorithms/src/test/java/org/opensearch/ml/engine/algorithms/agent/AgentUtilsTest.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ public class AgentUtilsTest {
7575
+ "\"thought\": \"Now I know the final answer\",\n "
7676
+ "\"final_answer\": \"PPLTool generates such query ```json source=iris_data | fields petal_length_in_cm,petal_width_in_cm | kmeans centroids=3 ```.\"\n}\n```";
7777

78+
private String responseForFinalAnswerWithMultilines = "---------------------```json\n{\n "
79+
+ "\"thought\": \"Now I know the final answer\",\n "
80+
+ "\"final_answer\": \"PPLTool generates such query \n```json source=iris_data | fields petal_length_in_cm,petal_width_in_cm | kmeans centroids=3 ```.\"\n}\n```";
81+
82+
private String responseForFinalAnswerWithQuotes = "---------------------```json\n{\n "
83+
+ "\"thought\": \"Now I know the final answer\",\n "
84+
+ "\"final_answer\": \"PPLTool generates such query \n```json source=iris_data | fields petal_length_in_cm,petal_width_in_cm | kmeans name=\"Jack\" ```.\"\n}\n```";
85+
7886
private String wrongResponseForAction = "---------------------```json\n{\n "
7987
+ "\"thought\": \"Let's try VectorDBTool\",\n "
8088
+ "\"action\": \"After checking online weather forecasts, it looks like tomorrow will be sunny with a high of 25 degrees Celsius.\"\n}\n```";
@@ -120,7 +128,7 @@ public void setup() {
120128
THOUGHT,
121129
"Unfortunately the tools did not provide the weather forecast directly. Let me check online sources:",
122130
FINAL_ANSWER,
123-
"After checking online weather forecasts, it looks like tomorrow will be sunny with a high of 25 degrees Celsius.\"\n}\n```"
131+
"After checking online weather forecasts, it looks like tomorrow will be sunny with a high of 25 degrees Celsius."
124132
);
125133
llmResponseExpectedParseResults.put(responseForFinalAnswerInvalidJson, responseForFinalAnswerExpectedResultExpectedResult);
126134
Map responseForFinalAnswerWithJsonExpectedResultExpectedResult = Map
@@ -144,6 +152,24 @@ public void setup() {
144152
);
145153
llmResponseExpectedParseResults.put(wrongResponseForAction, wrongResponseForActionExpectedResultExpectedResult);
146154

155+
Map responseForFinalAnswerWithMultilinesExpectedResult = Map
156+
.of(
157+
THOUGHT,
158+
"Now I know the final answer",
159+
FINAL_ANSWER,
160+
"PPLTool generates such query \n```json source=iris_data | fields petal_length_in_cm,petal_width_in_cm | kmeans centroids=3 ```."
161+
);
162+
llmResponseExpectedParseResults.put(responseForFinalAnswerWithMultilines, responseForFinalAnswerWithMultilinesExpectedResult);
163+
164+
Map responseForFinalAnswerWithQuotesExpectedResult = Map
165+
.of(
166+
THOUGHT,
167+
"Now I know the final answer",
168+
FINAL_ANSWER,
169+
"PPLTool generates such query \n```json source=iris_data | fields petal_length_in_cm,petal_width_in_cm | kmeans name=\"Jack\" ```."
170+
);
171+
llmResponseExpectedParseResults.put(responseForFinalAnswerWithQuotes, responseForFinalAnswerWithQuotesExpectedResult);
172+
147173
}
148174

149175
@Test
@@ -442,7 +468,7 @@ public void testExtractMethods_FinalAnswer() {
442468
Assert.assertNull(actionInput);
443469
Assert
444470
.assertEquals(
445-
"After checking online weather forecasts, it looks like tomorrow will be sunny with a high of 25 degrees Celsius.\"\n}\n```",
471+
"After checking online weather forecasts, it looks like tomorrow will be sunny with a high of 25 degrees Celsius.",
446472
finalAnswer
447473
);
448474
}

0 commit comments

Comments
 (0)