var_dump调试wordpress,泰安网络优化,阜阳企业网站建设,平台公司运作模式就在昨晚#xff0c;Spring AI发了个比较重要的更新。由于最近OpenAI推出了结构化输出的功能#xff0c;可确保 AI 生成的响应严格遵守预定义的 JSON 模式。此功能显着提高了人工智能生成内容在现实应用中的可靠性和可用性。Spring AI 紧随其后#xff0c;现在也可以对OpenA…就在昨晚Spring AI发了个比较重要的更新。由于最近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-keyYOUR_API_KEY
spring.ai.openai.chat.options.modelgpt-4o-minispring.ai.openai.chat.options.response-format.typeJSON_SCHEMA
spring.ai.openai.chat.options.response-format.nameMySchemaName
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.stricttrue
今天的分享就到这里感谢阅读码字不易点赞、关注、收藏支持一下随便转载标注下出处链接即可。
如果您学习过程中如遇困难可以加入我们超高质量的Spring技术交流群参与交流与讨论更好的学习与进步更多Spring Boot教程可以点击直达欢迎收藏与转发支持 欢迎关注我的公众号程序猿DD。前沿技术早知道弯道超车有希望积累超车资本从关注DD开始