Location>code7788 >text

Spring AI Update: Structured Output Support for OpenAI, Enhanced Support for JSON Responses

Popularity:965 ℃/2024-08-10 09:56:05

Just last night, Spring AI posted a rather important update. Since OpenAI recently introduced the ability to structure output to ensure that AI-generated responses strictly adhere to predefined JSON patterns, Spring AI has followed suit. This feature significantly improves the reliability and usability of AI-generated content in real-world applications, and Spring AI has followed suit and now has perfect support for OpenAI's structured output.

The following figure shows the structure of the implementation of this extension, if the current implementation is not satisfied enough, the need to expand the next step can be based on this figure to begin to understand the analysis of the extension work.

usage sample

With Spring AI, developers can easily build requests and parses for OpenAI structured output:

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",
()
    .withModel(ChatModel.GPT_4_O_MINI)
    .withResponseFormat(new ResponseFormat(.JSON_SCHEMA, jsonSchema))
    .build());

ChatResponse response = (prompt);

pass (a bill or inspection etc)OpenAiChatOptionsspecified inResponseFormatto have OpenAI return JSON format.

Spring AI also providesBeanOutputConverterto realize the JSON out into a Java bean, such as the following:

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<>();

var jsonSchema = ();

Prompt prompt = new Prompt("how can I solve 8x + 7 = -23",
()
    .withModel(ChatModel.GPT_4_O_MINI)
    .withResponseFormat(new ResponseFormat(.JSON_SCHEMA, jsonSchema))
    .build());

ChatResponse response = (prompt);
String content = ().getOutput().getContent();

MathReasoning mathReasoning = (content);

If you have integrated Spring AI's Spring Boot Starter module for OpenAI, then you can also automatically configure the default JSON return format in the following way:

-key=YOUR_API_KEY
=gpt-4o-mini

=JSON_SCHEMA
=MySchemaName
={"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}
=true

That's all for today's sharing, thanks for reading! It's not easy to code, so like, follow, and favorite to support! Feel free to reprint, mark down the source link can be.

If you're having trouble learning? Join our super high qualitySpring Technology Exchange GroupThe company's mission is to provide the best possible service to the community, and to participate in exchanges and discussions for better learning and advancement! MoreSpring Boot tutorials can be clicked directly!, welcome favorites & reblogs for support!

Welcome to my public number: program ape DD. the first time to understand the cutting-edge industry news, share in-depth technical dry goods, access to high-quality learning resources