fix bean parser and tests

This commit is contained in:
Christian Tzolov
2024-01-26 16:45:03 +01:00
parent 944c4ebbaa
commit 836c4fd88f
2 changed files with 21 additions and 6 deletions

View File

@@ -97,12 +97,8 @@ public class BeanOutputParser<T> implements OutputParser<T> {
public T parse(String text) {
try {
// If the response is a JSON Schema, extract the properties and use them as
// the
// response.
Map<String, Object> map = this.objectMapper.readValue(text, Map.class);
if (map.containsKey("$schema")) {
text = this.objectMapper.writeValueAsString(map.get("properties"));
}
// the response.
text = this.jsonSchemaToInstance(text);
return (T) this.objectMapper.readValue(text, this.clazz);
}
catch (JsonProcessingException e) {
@@ -110,6 +106,24 @@ public class BeanOutputParser<T> implements OutputParser<T> {
}
}
/**
* If the response is a JSON Schema, extract the properties and use them as the
* response.
* @param text
* @return
*/
private String jsonSchemaToInstance(String text) {
try {
Map<String, Object> map = this.objectMapper.readValue(text, Map.class);
if (map.containsKey("$schema")) {
return this.objectMapper.writeValueAsString(map.get("properties"));
}
}
catch (Exception e) {
}
return text;
}
/**
* Configures and returns an object mapper for JSON operations.
* @return Configured object mapper.

View File

@@ -69,6 +69,7 @@ class BeanOutputParserTest {
"""
Your response should be in JSON format.
Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.
Do not include markdown code blocks in your response.
Here is the JSON Schema instance your output must adhere to:
```{
"$schema" : "https://json-schema.org/draft/2020-12/schema",