@@ -124,7 +124,7 @@ public void testMLModelsWithDefaultOutputParserAndMalformedResponseField() throw
124
124
tool .run (null , listener );
125
125
126
126
future .join ();
127
- assertEquals (null , future .get ());
127
+ assertEquals ("{ \" response \" : \" response 1 \" , \" action \" : \" action1 \" }" , future .get ());
128
128
}
129
129
130
130
@ Test
@@ -170,6 +170,26 @@ public void testOutputParserLambda() {
170
170
assertEquals ("testResponse" , result );
171
171
}
172
172
173
+ @ Test
174
+ public void testOutputParserWithJsonResponse () {
175
+ Parser outputParser = new MLModelTool (client , "modelId" , "response" ).getOutputParser ();
176
+ String expectedJson = "{\" key1\" :\" value1\" ,\" key2\" :\" value2\" }" ;
177
+
178
+ // Create a mock ModelTensors with json object
179
+ ModelTensor modelTensor = ModelTensor .builder ().dataAsMap (ImmutableMap .of ("key1" , "value1" , "key2" , "value2" )).build ();
180
+ ModelTensors modelTensors = ModelTensors .builder ().mlModelTensors (Arrays .asList (modelTensor )).build ();
181
+ ModelTensorOutput mlModelTensorOutput = ModelTensorOutput .builder ().mlModelOutputs (Arrays .asList (modelTensors )).build ();
182
+ Object result = outputParser .parse (mlModelTensorOutput .getMlModelOutputs ());
183
+ assertEquals (expectedJson , result );
184
+
185
+ // Create a mock ModelTensors with response string
186
+ modelTensor = ModelTensor .builder ().dataAsMap (ImmutableMap .of ("response" , "{\" key1\" :\" value1\" ,\" key2\" :\" value2\" }" )).build ();
187
+ modelTensors = ModelTensors .builder ().mlModelTensors (Arrays .asList (modelTensor )).build ();
188
+ mlModelTensorOutput = ModelTensorOutput .builder ().mlModelOutputs (Arrays .asList (modelTensors )).build ();
189
+ result = outputParser .parse (mlModelTensorOutput .getMlModelOutputs ());
190
+ assertEquals (expectedJson , result );
191
+ }
192
+
173
193
@ Test
174
194
public void testRunWithError () {
175
195
// Mocking the client.execute to simulate an error
0 commit comments