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

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

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

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

使用样例

通过Spring AI,开发者可以很方便的来构建针对 OpenAI 结构化输出的请求和解析:

go 复制代码
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,比如下面这样:

go 复制代码
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返回格式:

go 复制代码
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

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

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


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

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

推荐阅读

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

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

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

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

相关推荐
郭不耐5 分钟前
DeepSeek智能时空数据分析(六):大模型NL2SQL绘制城市之间连线
人工智能·数据分析·时序数据库·数据可视化·deepseek
winfredzhang1 小时前
Deepseek 生成新玩法:从文本到可下载 Word 文档?思路与实践
人工智能·word·deepseek
raoxiaoya1 小时前
同时安装多个版本的golang
开发语言·后端·golang
KY_chenzhao1 小时前
ChatGPT与DeepSeek在科研论文撰写中的整体科研流程与案例解析
人工智能·机器学习·chatgpt·论文·科研·deepseek
不爱吃于先生2 小时前
生成对抗网络(Generative Adversarial Nets,GAN)
人工智能·神经网络·生成对抗网络
cxr8282 小时前
基于Playwright的浏览器自动化MCP服务
人工智能·自动化·大语言模型·mcp
PPIO派欧云2 小时前
PPIO X OWL:一键开启任务自动化的高效革命
运维·人工智能·自动化·github·api·教程·ppio派欧云
此木|西贝2 小时前
【设计模式】享元模式
java·设计模式·享元模式
奋斗者1号2 小时前
数值数据标准化:机器学习中的关键预处理技术
人工智能·机器学习
kyle~2 小时前
深度学习---框架流程
人工智能·深度学习