xiangpei
2025-04-18 0aa739db8268b442ab74634289ffed00124a976a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.monkeylessey.service.impl;
 
import cn.hutool.http.HttpUtil;
import com.monkeylessey.config.AIConfig;
import com.monkeylessey.domain.form.ChatForm;
import com.monkeylessey.service.ChatService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
 
/**
 * @author:xp
 * @date:2025/4/18 14:22
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ChatServiceImpl implements ChatService {
 
    private final AIConfig aiConfig;
 
    @Override
    public SseEmitter sendMsg(ChatForm form) {
 
        SseEmitter emitter = new SseEmitter(Long.MAX_VALUE); // 设置无限超时
 
        // 1. 构建请求体
        Map<String, Object> body = new HashMap<>();
        body.put("query", form.getQuery());
        body.put("mode", form.getMode());
        body.put("kb_name", form.getKbName());
        body.put("top_k", form.getTopK());
        body.put("score_threshold", form.getScoreThreshold());
        body.put("history", form.getHistory());
        body.put("stream", form.getStream());
        body.put("model", form.getModel());
        body.put("temperature", form.getTemperature());
        body.put("max_tokens", form.getMaxTokens());
        body.put("prompt_name", form.getPromptName());
        body.put("return_direct", form.getReturnDirect());
 
        // 2. 异步处理SSE转发
        CompletableFuture.runAsync(() -> {
            try {
                WebClient client = WebClient.create(aiConfig.getFullDomain());
                client.post()
                        .uri("/chat/kb_chat")
                        .contentType(MediaType.APPLICATION_JSON)
                        .accept(MediaType.TEXT_EVENT_STREAM) // 声明接受SSE响应
                        .bodyValue(body)
                        .retrieve()
                        .bodyToFlux(String.class) // 使用Flux接收流式响应
                        .subscribe(
                                data -> {
                                    try {
                                        emitter.send(SseEmitter.event().data(data));
                                    } catch (IOException e) {
                                        log.error("发送失败", e.getMessage());
                                    }
                                },
                                error -> emitter.completeWithError(error),
                                () -> emitter.complete()
                        );
            } catch (Exception e) {
                emitter.completeWithError(e);
            }
        });
 
        // 3. 连接生命周期回调
        emitter.onCompletion(() -> log.info("SSE connection completed"));
        emitter.onTimeout(() -> log.warn("SSE connection timed out"));
        emitter.onError(ex -> log.error("SSE error: {}", ex.getMessage()));
        return emitter;
    }
}