JAVA使用DeepSeek4j集成DeepSeek调用,流式返回。

1.引入依赖

        <dependency>
            <groupId>io.github.pig-mesh.ai</groupId>
            <artifactId>deepseek-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>



<!-- 此处会解决okhttp的的版本问题,在全局加依赖管理。或者在自己当前项目pom加不使用<dependencyManagement>-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.squareup.okhttp3</groupId>
                <artifactId>okhttp</artifactId>
                <version>4.12.0</version>
            </dependency>
            <dependency>
                <groupId>com.squareup.okhttp3</groupId>
                <artifactId>okhttp-sse</artifactId>
                <version>4.12.0</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson</groupId>
                <artifactId>jackson-bom</artifactId>
                <version>2.12.4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

    </dependencyManagement>

2.配置application.yml

deepseek:
  api-key: xxxxxxxxxxxxxxxxxxxx
  base-url: https://api.deepseek.com
  connect-timeout: 10  # 连接超时(秒)
  read-timeout: 30     # 读取超时(秒)
  call-timeout: 60     # 完整调用超时(秒)
  log-level: INFO


3.调用

    /**
     * 流式对话接口,此处是流式调用返回
     * @param prompt 用户输入
     * @return SSE 流式响应
     */
    @Transactional
    @GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ChatCompletionResponse> streamChat(String prompt, Long conversationId) {

      
        //创建请求
        ChatCompletionRequest request = ChatCompletionRequest.builder()
                .model(ChatCompletionModel.DEEPSEEK_CHAT)//模型必须为DEEPSEEK_CHAT,使用DEEPSEEK_REASONER会报错
                .addUserMessage(prompt)
                .addAssistantMessage("上一轮对话结果")//此处应当填写上一轮对话的String类型
                .addSystemMessage("你是一名律师")//指定用户
                .temperature(0.7)
                .build();
        //对用户提问问题进行保存
        Conversation userConversation = GetConversationEnums.USER.getConversation(conversationId, prompt, null);
        conversationService.save(userConversation);
        // 线程安全的内容收集器
        StringBuilder contentCollector = new StringBuilder();
        StringBuilder jsonCollector = new StringBuilder();
        //开始流式调用
        return deepSeekClient.chatFluxCompletion(request)
                .doOnNext(response -> {
                    if (Objects.nonNull(response.usage())){
						//对最后一个流式返回responese进行保存
                        jsonCollector.append(response);
                    }
                    List<ChatCompletionChoice> choices = response.choices();
                    for (ChatCompletionChoice choice : choices) {
						//拼接所有返回的助手对话内容
                        contentCollector.append(choice.delta().content());
                    }
                })
                .doOnComplete(() -> {
                    System.out.println(contentCollector);
                    System.out.println(jsonCollector);
					//存储助手对话内容
                    Conversation conversation = Conversation.builder()
                            .conversation(contentCollector.toString())
                            .conversationId(conversationId)
                            .jsonContent(jsonCollector.toString())
                            .userId(userId)
                            .role(Role.ASSISTANT.name()).build();
                    conversationService.save(conversation);
                });


    }
4.结论

到此后端调用完成,如果有问题请留言或者私信我。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值