关注

java(webflux)流式接入deepseek推理大模型

  • deepseek请求体
@Data
public class DeepSeekRequestBody {

    private String model;

    private List<DeepSeekMessage> messages;

    private Boolean stream;


}


@Data
@NoArgsConstructor
@AllArgsConstructor
public class DeepSeekMessage {

    private String role;

    private String content;

}
  • deepseek返回结果对象
@Data
public class DeepSeekResult {

    private Integer index;

    private JSONObject delta;

}
  • main方法
public static void main(String[] args) throws InterruptedException {
        //创建webclient客户端
        WebClient webClient = WebClient.create("https://api.deepseek.com");
        //构建deepseek请求体
        DeepSeekRequestBody deepSeekRequestBody = new DeepSeekRequestBody();
        deepSeekRequestBody.setStream(true);
        deepSeekRequestBody.setModel("deepseek-reasoner");
        deepSeekRequestBody.setMessages(List.of(new DeepSeekMessage("user", "你想要问的问题")));
        Semaphore semaphore = new Semaphore(0);
        List<String> resultList = new ArrayList<>();
        webClient.post()
                .uri("/chat/completions")
                .contentType(MediaType.APPLICATION_JSON)
                .header("Authorization", "Bearer API-keys")
                .bodyValue(JSON.toJSON(deepSeekRequestBody))
                .accept(MediaType.TEXT_EVENT_STREAM)
                .retrieve()
                .bodyToFlux(String.class)
                .subscribeOn(Schedulers.boundedElastic())
                .doOnError(e-> log.error("异常:",e))
                .subscribe(re->{
                    if("[DONE]".equals(re)){
                        semaphore.release();
                        return;
                    }
                    JSONObject jsonObject = JSON.parseObject(re,JSONObject.class);
                    List<DeepSeekResult> deepSeekResultList = JSON.parseArray(jsonObject.getString("choices"),DeepSeekResult.class);
                    String content = deepSeekResultList.stream().map(deepSeekResult -> {
                        JSONObject delta = deepSeekResult.getDelta();
                        return delta.getString("content");
                    }).filter(StringUtils::isNotBlank).collect(Collectors.joining());
                    resultList.add(content);
                });
        semaphore.acquire();
        //最终结果
        String finalResult = String.join("", resultList);
        log.info("结果:{}",finalResult);
        semaphore.release();
    }

转载自CSDN-专业IT技术社区

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/AL_guli/article/details/145496614

评论

赞0

评论列表

微信小程序
QQ小程序

关于作者

点赞数:0
关注数:0
粉丝:0
文章:0
关注标签:0
加入于:--