Spring AI 更新:支持OpenAI的结构化输出,增强对JSON响应的支持
  lnZqfDbuKjjk 2024年08月10日 94 0

就在昨晚,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-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

今天的分享就到这里,感谢阅读!码字不易,点赞、关注、收藏支持一下!随便转载,标注下出处链接即可。

如果您学习过程中如遇困难?可以加入我们超高质量的Spring技术交流群,参与交流与讨论,更好的学习与进步!更多Spring Boot教程可以点击直达!,欢迎收藏与转发支持!

欢迎关注我的公众号:程序猿DD。第一时间了解前沿行业消息、分享深度技术干货、获取优质学习资源

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年08月10日 0

暂无评论

推荐阅读
  VGxawBTN4xmE   13小时前   10   0   0 Java
  FHUfYd9S4EP5   3天前   24   0   0 Java
  sSh3rBaiME5f   4天前   32   0   0 Java
  qCe06rFCa8NK   3天前   18   0   0 Java
  ZTo294hNoDcA   3天前   26   0   0 Java
  FHUfYd9S4EP5   3天前   20   0   0 Java
  QGiRIwDaZAe8   4天前   23   0   0 Java
lnZqfDbuKjjk