SpringAI更新:支持OpenAI的结构化输出,增强JSON响应可靠性

哥们看看码农 2024-08-10 19:40:35

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

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

本文首发:https://www.didispace.com/article/news/spring-ai-openai-json.html

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

0 阅读:0

哥们看看码农

简介:感谢大家的关注