由于最近OpenAI推出了結(jié)構(gòu)化輸出的功能,可確保 AI 生成的響應(yīng)嚴(yán)格遵守預(yù)定義的 JSON 模式。此功能顯著提高了人工智能生成內(nèi)容在現(xiàn)實(shí)應(yīng)用中的可靠性和可用性。Spring AI 緊隨其后,現(xiàn)在也可以對(duì)OpenAI的結(jié)構(gòu)化輸出完美支持了。
下圖展示了本次擴(kuò)展的實(shí)現(xiàn)結(jié)構(gòu),如果對(duì)于當(dāng)前實(shí)現(xiàn)還不夠滿意,需要擴(kuò)展的可以根據(jù)此圖來著手理解分析進(jìn)行下一步擴(kuò)展工作。
#使用樣例
通過Spring AI,開發(fā)者可以很方便的來構(gòu)建針對(duì) OpenAI 結(jié)構(gòu)化輸出的請求和解析:
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
來實(shí)現(xiàn)將JSON出轉(zhuǎn)換成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針對(duì)OpenAI的Spring Boot Starter模塊,那么也可以通過下面的方式來自動(dòng)配置默認(rèn)的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
轉(zhuǎn)載: https://spring.didispace.com/article/spring-ai-openai-json.html#%E4%BD%BF%E7%94%A8%E6%A0%B7%E4%BE%8B