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

打印 上一主题 下一主题

主题 889|帖子 889|积分 2667

就在昨晚,Spring AI发了个比较重要的更新。由于最近OpenAI推出了结构化输出的功能,可确保 AI 生成的相应严格遵守预定义的 JSON 模式。此功能显着提高了人工智能生成内容在现实应用中的可靠性和可用性。Spring AI 紧随厥后,现在也可以对OpenAI的结构化输出完美支持了。
下图展示了本次扩展的实现结构,如果对于当前实现还不够满意,需要扩展的可以根据此图来着手理解分析进行下一步扩展工作。

使用样例

通过Spring AI,开发者可以很方便的来构建针对 OpenAI 结构化输出的请求息争析:
  1. String jsonSchema = """
  2.   {
  3.       "type": "object",
  4.       "properties": {
  5.           "steps": {
  6.               "type": "array",
  7.               "items": {
  8.                   "type": "object",
  9.                   "properties": {
  10.                       "explanation": { "type": "string" },
  11.                       "output": { "type": "string" }
  12.                   },
  13.                   "required": ["explanation", "output"],
  14.                   "additionalProperties": false
  15.               }
  16.           },
  17.           "final_answer": { "type": "string" }
  18.       },
  19.       "required": ["steps", "final_answer"],
  20.       "additionalProperties": false
  21.   }
  22.   """;
  23. Prompt prompt = new Prompt("how can I solve 8x + 7 = -23",
  24. OpenAiChatOptions.builder()
  25.     .withModel(ChatModel.GPT_4_O_MINI)
  26.     .withResponseFormat(new ResponseFormat(ResponseFormat.Type.JSON_SCHEMA, jsonSchema))
  27.     .build());
  28. ChatResponse response = this.openAiChatModel.call(prompt);
复制代码
通过 OpenAiChatOptions中指定ResponseFormat来让OpenAI返回JSON格式。
Spring AI还提供了BeanOutputConverter来实现将JSON出转换成Java Bean,比如下面这样:
  1. record MathReasoning(
  2.   @JsonProperty(required = true, value = "steps") Steps steps,
  3.   @JsonProperty(required = true, value = "final_answer") String finalAnswer) {
  4.   record Steps(
  5.     @JsonProperty(required = true, value = "items") Items[] items) {
  6.     record Items(
  7.       @JsonProperty(required = true, value = "explanation") String explanation,
  8.       @JsonProperty(required = true, value = "output") String output) {}
  9.   }
  10. }
  11. var outputConverter = new BeanOutputConverter<>(MathReasoning.class);
  12. var jsonSchema = outputConverter.getJsonSchema();
  13. Prompt prompt = new Prompt("how can I solve 8x + 7 = -23",
  14. OpenAiChatOptions.builder()
  15.     .withModel(ChatModel.GPT_4_O_MINI)
  16.     .withResponseFormat(new ResponseFormat(ResponseFormat.Type.JSON_SCHEMA, jsonSchema))
  17.     .build());
  18. ChatResponse response = this.openAiChatModel.call(prompt);
  19. String content = response.getResult().getOutput().getContent();
  20. MathReasoning mathReasoning = outputConverter.convert(content);
复制代码
如果你整合了Spring AI针对OpenAI的Spring Boot Starter模块,那么也可以通过下面的方式来自动配置默认的JSON返回格式:
  1. spring.ai.openai.api-key=YOUR_API_KEY
  2. spring.ai.openai.chat.options.model=gpt-4o-mini
  3. spring.ai.openai.chat.options.response-format.type=JSON_SCHEMA
  4. spring.ai.openai.chat.options.response-format.name=MySchemaName
  5. 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}
  6. spring.ai.openai.chat.options.response-format.strict=true
复制代码
本日的分享就到这里,感谢阅读!码字不易,点赞、关注、收藏支持一下!随便转载,标注下出处链接即可。
如果您学习过程中如遇困难?可以加入我们超高质量的Spring技术交换群,参与交换与讨论,更好的学习与进步!更多Spring Boot教程可以点击直达!,欢迎收藏与转发支持!
欢迎关注我的公众号:程序猿DD。第一时间了解前沿行业消息、分享深度技术干货、获取优质学习资源

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

老婆出轨

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表