Posted in

Spring AI 集成 OpenAI 结构化输出:提升 JSON 响应可靠性_AI阅读总结 — 包阅AI

包阅导读总结

1.

关键词:Spring AI、OpenAI、Structured Outputs、JSON 响应、Java 开发

2.

总结:Spring AI 已完全集成 OpenAI 的 Structured Outputs 功能,提升了 AI 生成内容的可靠性和可用性,为 Java 开发者提供多种配置方式,还探讨了未来工作,强调其带来的诸多好处。

3.

主要内容:

– Spring AI 集成 OpenAI 的 Structured Outputs 功能

– 此功能确保 AI 生成响应遵循预定义的 JSON 模式

– 显著提升了在实际应用中的可靠性和可用性

– 配置方式

– 编程式配置

– 使用 BeanOutputConverter

– 通过应用属性配置

– 拒绝响应

– OpenAI 模型可能因安全原因拒绝请求,有新字段指示

– 未来工作

– 探索将新功能集成到 Spring AI 的通用结构化输出工具集

– 结论

– 此集成使应用更可靠、易开发

– 具有无缝集成、类型安全、配置灵活和领域驱动设计等优点

思维导图:

文章地址:https://spring.io/blog/2024/08/09/spring-ai-embraces-openais-structured-outputs-enhancing-json-response

文章来源:spring.io

作者:Christian Tzolov

发布时间:2024/8/9 0:00

语言:英文

总字数:795字

预计阅读时间:4分钟

评分:93分

标签:Spring AI,OpenAI,JSON 架构,Java 开发,类型安全


以下为原文内容

本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com

OpenAI recently introduced a powerful feature called Structured Outputs, which ensures that AI-generated responses adhere strictly to a predefined JSON schema. This feature significantly improves the reliability and usability of AI-generated content in real-world applications. Today, we’re excited to announce that Spring AI (1.0.0-SNAPSHOT) has fully integrated support for OpenAI’s Structured Outputs, bringing this capability to Java developers in a seamless, Spring-native way.

Following diagram shows how the new Structured Outputs feature extends the OpenAI Chat API:

Restored Spring AI (2)

Note: Spring AI already provides a powerful, Model-agnostic Structured Output utilities that can be use with various AI models including the OpenAI.The OpenAI Structured Outputs feature offers an additional, consistent, but a model specific solution, currently available only to the gpt-4o, gpt-4o-mini and later models.

The OpenAI Structured Outputs feature guarantees the AI model will generate responses that conform to a supplied JSON Schema. This addresses several common challenges in AI-powered applications: Type Safety: No more worrying about missing required keys or invalid enum values; Explicit Refusals: Safety-based model refusals become programmatically detectable; Simplified Prompting: Achieve consistent formatting without resorting to overly specific prompts.

Spring AI allows developers to leverage this feature with minimal configuration. Let’s explore how you can use it in your Spring applications.

Programmatic Configuration

You can set the response format programmatically with the OpenAiChatOptions builder as shown below:

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

NOTE: You must adhere to the OpenAI subset of the JSON Schema language format.

Using the BeanOutputConverter

Spring AI provides a convenient BeanOutputConverter utility that can automatically generate JSON schemas from your domain objects and convert structured responses into Java instances:

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

NOTE: Ensure you use the @JsonProperty(required = true,…​) annotation.This is crucial for generating a schema that accurately marks fields as required. OpenAI mandates it for the structured response to function correctly.

Configuring via Application Properties

Alternatively, when using the OpenAI auto-configuration, you can configure the desired response format through the following chat application properties:

spring.ai.openai.api-key=YOUR_API_KEYspring.ai.openai.chat.options.model=gpt-4o-minispring.ai.openai.chat.options.response-format.type=JSON_SCHEMAspring.ai.openai.chat.options.response-format.name=MySchemaNamespring.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

Refusal Response

When using Structured Outputs, OpenAI models may occasionally refuse to fulfill the request for safety reasons. Since a refusal does not necessarily follow the schema you have supplied in response_format, the API response includes a new field called refusal to indicate that the model refused to fulfill the request.

Spring AI maps this refusal field to the AssistantMessage’s metadata. Search by refusal key.

Future work

We are exploring the possibilities to integrate the new OpenAI specific Structured Outputs features into Spring AI’s Model-agnostic Structured Output utility set.

References

For further information check the Spring AI and OpenAI reference documentations.

Conclusion

Spring AI’s support for OpenAI’s Structured Outputs feature makes the AI-powered applications more reliable and easier to develop.By ensuring type safety and consistent, structured formatting, developers can focus on building innovative features rather than wrestling with unpredictable AI outputs.

We can highlight the following benefits for Spring Developers:

  • Seamless Integration: Leverage Structured Outputs without leaving the Spring ecosystem.
  • Type Safety: Work with strongly-typed Java objects, reducing runtime errors.
  • Flexibility: Choose between programmatic and property-based configuration.
  • Domain-Driven Design: Use your domain objects to define the expected AI output structure.

Please explore this new capability and share your experiences.As always, we welcome feedback and contributions to help improve Spring AI and make it even more powerful and user-friendly.

Stay tuned for more updates as we continue to enhance Spring AI’s integration with cutting-edge AI technologies!