由于最近OpenAI推出了結構化輸出的功能,可確保 AI 生成的響應嚴格遵守預定義的 JSON 模式。此功能顯著提高了人工智能生成內容在現實應用中的可靠性和可用性。Spring AI 緊隨其后,現在也可以對OpenAI的結構化輸出完美支持了。
下圖展示了本次擴展的實現結構,如果對于當前實現還不夠滿意,需要擴展的可以根據此圖來著手理解分析進行下一步擴展工作。
#使用樣例
通過Spring AI,開發者可以很方便的來構建針對 OpenAI 結構化輸出的請求和解析:
String jsonSchema = """
{
"type": "object",
"properties": {
"steps": {
"type": "array",
"items": {
"type": "object",
"properties": {
"explanation": { "type": "string" },
"output": { "type": "string" }
},
"required": ["explanation", "output"],
"additionalProperties": false
}
},
"final_answer": { "type": "string" }
},
"required": ["steps", "final_answer"],
"additionalProperties": false
}
""";
Prompt prompt = new Prompt("how can I solve 8x + 7 = -23",
OpenAiChatOptions.builder()
.withModel(ChatModel.GPT_4_O_MINI)
.withResponseFormat(new ResponseFormat(ResponseFormat.Type.JSON_SCHEMA, jsonSchema))
.build());
ChatResponse response = this.openAiChatModel.call(prompt);
通過 OpenAiChatOptions
中指定ResponseFormat
來讓OpenAI返回JSON格式。
Spring AI還提供了BeanOutputConverter
來實現將JSON出轉換成Java Bean,比如下面這樣:
record MathReasoning(
@JsonProperty(required = true, value = "steps") Steps steps,
@JsonProperty(required = true, value = "final_answer") String finalAnswer) {
record Steps(
@JsonProperty(required = true, value = "items") Items[] items) {
record Items(
@JsonProperty(required = true, value = "explanation") String explanation,
@JsonProperty(required = true, value = "output") String output) {}
}
}
var outputConverter = new BeanOutputConverter<>(MathReasoning.class);
var jsonSchema = outputConverter.getJsonSchema();
Prompt prompt = new Prompt("how can I solve 8x + 7 = -23",
OpenAiChatOptions.builder()
.withModel(ChatModel.GPT_4_O_MINI)
.withResponseFormat(new ResponseFormat(ResponseFormat.Type.JSON_SCHEMA, jsonSchema))
.build());
ChatResponse response = this.openAiChatModel.call(prompt);
String content = response.getResult().getOutput().getContent();
MathReasoning mathReasoning = outputConverter.convert(content);
如果你整合了Spring AI針對OpenAI的Spring Boot Starter模塊,那么也可以通過下面的方式來自動配置默認的JSON返回格式:
spring.ai.openai.api-key=YOUR_API_KEY
spring.ai.openai.chat.options.model=gpt-4o-mini
spring.ai.openai.chat.options.response-format.type=JSON_SCHEMA
spring.ai.openai.chat.options.response-format.name=MySchemaName
spring.ai.openai.chat.options.response-format.schema={"type":"object","properties":{"steps":{"type":"array","items":{"type":"object","properties":{"explanation":{"type":"string"},"output":{"type":"string"}},"required":["explanation","output"],"additionalProperties":false}},"final_answer":{"type":"string"}},"required":["steps","final_answer"],"additionalProperties":false}
spring.ai.openai.chat.options.response-format.strict=true
轉載: https://spring.didispace.com/article/spring-ai-openai-json.html#%E4%BD%BF%E7%94%A8%E6%A0%B7%E4%BE%8B