结构化输出
LLM生成结构化输出的能力对于下游应用非常重要
- 基于 StructuredOutputConverter
快速将AI模型的结果转换为数据类型,例如JSON、XML或Java类,以便可以传递给其他应用程序函数和方法

示例
StructuredOutputConverter outputConverter = ...
String userInputTemplate = """
... user text input ....
{format}
"""; // user input with a "format" placeholder.
Prompt prompt = new Prompt(
PromptTemplate.builder()
.template(this.userInputTemplate)
.variables(Map.of(..., "format", this.outputConverter.getFormat())) // replace the "format" placeholder with the converter's format.
.build().createMessage()
);
源码示例
https://gitee.com/kcnf_open/spring-ai-sample/tree/master/spring-ai/spring-ai-sample03
返回BeanOutputConverter
@GetMapping("/ai/output")
public ActorsFilms generate(@RequestParam(value = "actor", defaultValue = "小猪佩奇") String actor) {
var outputConverter = new BeanOutputConverter<>(ActorsFilms.class);
String userMessage = """
Generate the filmography for the actor {actor}.
Provide the output in JSON format that matches the following structure:
Actor name and list of movies.
""";
return chatClient.prompt()
.user(user -> user.text(userMessage)
.param("actor", actor))
.call()
.entity(outputConverter);
}
- 测试结果

返回MapOutputConverter
@GetMapping("/ai/map")
public Map<String, Object> map(@RequestParam(value = "actor", defaultValue = "小猪佩奇") String actor) {
MapOutputConverter mapOutputConverter = new MapOutputConverter();
String template = """
Provide me a List of films for the actor {actor}.
Return the data as JSON with two fields: 'actor' (the actor name) and 'name movies' (list of movie titles).
""";
return chatClient.prompt()
.user(user -> user.text(template)
.param("actor", actor))
.call()
.entity(mapOutputConverter);
}
- 测试结果

