領先一步
VMware 提供培訓和認證,助您加速進步。
瞭解更多OpenAI 最近 引入了一項強大的功能,稱為結構化輸出,該功能可確保 AI 生成的響應嚴格遵循預定義的 JSON 架構。此功能顯著提高了 AI 生成內容在實際應用中的可靠性和可用性。今天,我們很高興地宣佈 Spring AI (1.0.0-SNAPSHOT) 已 完全支援 OpenAI 的結構化輸出,以一種無縫的、Spring 原生的方式將此功能帶給 Java 開發人員。
下圖顯示了新的結構化輸出功能如何擴充套件 OpenAI Chat API。

注意: Spring AI 已經提供了強大的、與模型無關的結構化輸出工具,可用於包括 OpenAI 在內的各種 AI 模型。OpenAI 結構化輸出功能提供了一種額外的、一致的、但特定於模型的解決方案,目前僅適用於
gpt-4o、gpt-4o-mini及後續模型。
OpenAI 的 Structured Outputs 功能可確保 AI 模型生成符合提供的 JSON Schema 的響應。這解決了 AI 驅動應用程式中的幾個常見挑戰:型別安全:不再擔心缺少必需的鍵或無效的列舉值;明確的拒絕:基於安全的模型拒絕可以以程式設計方式檢測;簡化的提示:在不依賴於過於具體的提示的情況下獲得一致的格式。
Spring AI 允許開發人員以最少的配置利用此功能。讓我們探討如何在您的 Spring 應用程式中使用它。
您可以如下所示使用 OpenAiChatOptions builder 以程式設計方式設定響應格式
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);
注意:您必須遵守 OpenAI JSON Schema 語言格式的子集。
Spring AI 提供了一個方便的 BeanOutputConverter 工具,它可以從您的域物件自動生成 JSON Schema,並將結構化響應轉換為 Java 例項
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);
注意:請確保使用 @JsonProperty(required = true,…) 註解。這對於生成將欄位準確標記為必需的 Schema 至關重要。OpenAI 強制要求才能使結構化響應正常工作。
或者,當使用 OpenAI 自動配置時,您可以透過以下 聊天應用程式屬性配置所需的響應格式
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
當使用結構化輸出時,OpenAI 模型有時會出於安全原因拒絕滿足請求。由於拒絕不一定遵循您在 response_format 中提供的 Schema,因此 API 響應包含一個名為 refusal 的新欄位,以指示模型拒絕滿足請求。
Spring AI 將此拒絕欄位對映到 AssistantMessage 的元資料。按 refusal 鍵進行搜尋。
我們正在探索將新的 OpenAI 特定結構化輸出功能整合到 Spring AI 的模型無關結構化輸出工具集中。
有關更多資訊,請檢視 Spring AI 和 OpenAI 的參考文件。
Spring AI - 結構化輸出 (部落格)
Spring AI 部落格
Spring AI 對 OpenAI 結構化輸出功能的支援使 AI 驅動的應用程式更可靠,開發更簡單。透過確保型別安全和一致的結構化格式,開發人員可以專注於構建創新功能,而不是與不可預測的 AI 輸出進行鬥爭。
我們可以強調以下對 Spring 開發人員的好處
請探索這一新功能並分享您的經驗。一如既往,我們歡迎反饋和貢獻,以幫助改進 Spring AI,使其更加強大和使用者友好。
請繼續關注更多更新,我們將繼續增強 Spring AI 與尖端 AI 技術的整合!