<noframes id="bhrfl"><address id="bhrfl"></address>

    <address id="bhrfl"></address>

    <noframes id="bhrfl"><address id="bhrfl"><th id="bhrfl"></th></address>

    <form id="bhrfl"><th id="bhrfl"><progress id="bhrfl"></progress></th></form>

    <em id="bhrfl"><span id="bhrfl"></span></em>

    全部
    常見問題
    產品動態
    精選推薦

    Spring AI更新:支持OpenAI的結構化輸出,增強JSON響應可靠性

    管理 管理 編輯 刪除

    由于最近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
    

    轉載: https://spring.didispace.com/article/spring-ai-openai-json.html#%E4%BD%BF%E7%94%A8%E6%A0%B7%E4%BE%8B


    請登錄后查看

    CRMEB 最后編輯于2025-02-24 15:29:37

    快捷回復
    回復
    回復
    回復({{post_count}}) {{!is_user ? '我的回復' :'全部回復'}}
    排序 默認正序 回復倒序 點贊倒序

    {{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level }}

    作者 管理員 企業

    {{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推薦': '推薦'}}
    {{item.is_suggest == 1? '取消推薦': '推薦'}}
    沙發 板凳 地板 {{item.floor}}#
    {{item.user_info.title || '暫無簡介'}}
    附件

    {{itemf.name}}

    {{item.created_at}}  {{item.ip_address}}
    打賞
    已打賞¥{{item.reward_price}}
    {{item.like_count}}
    {{item.showReply ? '取消回復' : '回復'}}
    刪除
    回復
    回復

    {{itemc.user_info.nickname}}

    {{itemc.user_name}}

    回復 {{itemc.comment_user_info.nickname}}

    附件

    {{itemf.name}}

    {{itemc.created_at}}
    打賞
    已打賞¥{{itemc.reward_price}}
    {{itemc.like_count}}
    {{itemc.showReply ? '取消回復' : '回復'}}
    刪除
    回復
    回復
    查看更多
    打賞
    已打賞¥{{reward_price}}
    1013
    {{like_count}}
    {{collect_count}}
    添加回復 ({{post_count}})

    相關推薦

    快速安全登錄

    使用微信掃碼登錄
    {{item.label}} 加精
    {{item.label}} {{item.label}} 板塊推薦 常見問題 產品動態 精選推薦 首頁頭條 首頁動態 首頁推薦
    取 消 確 定
    回復
    回復
    問題:
    問題自動獲取的帖子內容,不準確時需要手動修改. [獲取答案]
    答案:
    提交
    bug 需求 取 消 確 定
    打賞金額
    當前余額:¥{{rewardUserInfo.reward_price}}
    {{item.price}}元
    請輸入 0.1-{{reward_max_price}} 范圍內的數值
    打賞成功
    ¥{{price}}
    完成 確認打賞

    微信登錄/注冊

    切換手機號登錄

    {{ bind_phone ? '綁定手機' : '手機登錄'}}

    {{codeText}}
    切換微信登錄/注冊
    暫不綁定
    亚洲欧美字幕
    CRMEB客服

    CRMEB咨詢熱線 咨詢熱線

    400-8888-794

    微信掃碼咨詢

    CRMEB開源商城下載 源碼下載 CRMEB幫助文檔 幫助文檔
    返回頂部 返回頂部
    CRMEB客服