Skip to content

Commit 9286dfb

Browse files
pgerhardtzolov
authored andcommitted
Extend format to improve generate JSON
- Add an additional instruction to the format for the model not to include markdown syntax for JSON quotes. - Extend the conversion to strip markdown quotes in case the model included them in the response
1 parent ac91302 commit 9286dfb

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

spring-ai-core/src/main/java/org/springframework/ai/converter/MapOutputConverter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public MapOutputConverter() {
4040

4141
@Override
4242
public Map<String, Object> convert(@NonNull String text) {
43+
if (text.startsWith("```json") && text.endsWith("```")) {
44+
text = text.substring(7, text.length() - 3);
45+
}
46+
4347
Message<?> message = MessageBuilder.withPayload(text.getBytes(StandardCharsets.UTF_8)).build();
4448
return (Map) this.getMessageConverter().fromMessage(message, HashMap.class);
4549
}
@@ -50,7 +54,8 @@ public String getFormat() {
5054
Your response should be in JSON format.
5155
The data structure for the JSON should match this Java class: %s
5256
Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.
53-
""";
57+
Remove the ```json markdown from the output.
58+
""";
5459
return String.format(raw, HashMap.class.getName());
5560
}
5661

0 commit comments

Comments
 (0)