当前位置: 首页> 健康> 科研 > Spring AI 更新:支持OpenAI的结构化输出,增强对JSON响应的支持

Spring AI 更新:支持OpenAI的结构化输出,增强对JSON响应的支持

时间:2025/7/12 7:28:41来源:https://blog.csdn.net/j3T9Z7H/article/details/141098676 浏览次数:0次

就在昨晚,Spring AI发了个比较重要的更新。

由于最近OpenAI推出了结构化输出的功能,可确保 AI 生成的响应严格遵守预定义的 JSON 模式。此功能显着提高了人工智能生成内容在现实应用中的可靠性和可用性。Spring AI 紧随其后,现在也可以对OpenAI的结构化输出完美支持了。

下图展示了本次扩展的实现结构,如果对于当前实现还不够满意,需要扩展的可以根据此图来着手理解分析进行下一步扩展工作。

outside_default.png

使用样例

通过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-minispring.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

今天的分享就到这里,感谢阅读!码字不易,点赞、关注、收藏支持一下!

我们创建了一个高质量的技术交流群,与优秀的人在一起,自己也会优秀起来,赶紧点击加群,享受一起成长的快乐。


你还在购买国内的各种昂贵又低质的技术教程吗?这里给大家推荐下我们自研的Youtube视频语音转换插件(https://youtube-dubbing.com/),一键外语转中文,英语不好的小伙伴也可以轻松的学习油管上的优质教程了,下面是演示视频,可以直观的感受一下:

如果您觉得这款插件不错,也可以推荐给您身边的朋友,目前我们开通了分享赚钱功能,只要安装本插件登录注册之后,获取邀请链接,放到你的博客侧边栏、友情链接或者发到朋友圈、微博、X等社交平台,就能获得积分,积分现在是可以i直接提现的哦~

推荐阅读

IntelliJ IDEA 2024.2 发布:Spring Data JPA即时查询

Spring Boot 中使用 JSON Schema 来校验复杂JSON数据

手把手教你本地运行Meta最新大模型:Llama 3.1

万字长文解析谷歌日历的数据库是怎么设计的

关键字:Spring AI 更新:支持OpenAI的结构化输出,增强对JSON响应的支持

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: