java端SSE流式接口实现AI问答打字式数据回显

引入依赖(我是java8环境)

<dependency>
   <groupId>com.squareup.okhttp3</groupId>
   <artifactId>okhttp</artifactId>
   <version>3.14.9</version> <!-- 支持Java 8的最后一个版本 -->
</dependency>
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>5.3.33</version>
</dependency>

 相关代码

API_KEY在如下界面新建即可

appId在对应的问题地址中

public class MyController extends BaseController {

    @ApiOperation("FastGPT测试(SSE流式接口)")
    @PostMapping("/fastGPT")
    public SseEmitter test(String question){
        String API_URL = "http://yourip:yourport/api/v1/chat/completions";
        String API_KEY = "yourapikey";

        OkHttpClient client=new OkHttpClient.Builder()
                .connectTimeout(300, TimeUnit.SECONDS)   // 连接超时
                .readTimeout(300, TimeUnit.SECONDS)    // 读取超时(关键参数)
                .writeTimeout(300, TimeUnit.SECONDS)
                .build();

        // 1. 构建请求体
        JSONObject message =new JSONObject();
        message.put("role", "user");
        message.put("content", question);
        JSONArray messages = new JSONArray();
        messages.add(message);
        JSONObject requestBody=new JSONObject();
        requestBody.put("appId", "yourappid");//使用指定的工作台
        requestBody.put("stream", true);//是否流式返回结果
        requestBody.put("detail", false);
        requestBody.put("messages", messages);//传入问题
        MediaType JSON = MediaType.parse("application/json");
        okhttp3.RequestBody bodyTemp = okhttp3.RequestBody.create(JSON, requestBody.toJSONString());

        // 2. 创建HTTP请求
        Request request = new Request.Builder()
                .url(API_URL)
                .post(bodyTemp)
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", "Bearer " + API_KEY)
                .addHeader("Accept", "text/event-stream") // 关键头信息
                .build();

        // 3. 异步执行FastGPT请求
        SseEmitter emitter = new SseEmitter(60_000L);
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                emitter.completeWithError(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (!response.isSuccessful()) {
                    emitter.completeWithError(new RuntimeException("HTTP " + response.code()));
                    return;
                }

                // 流式读取并转发给前端
                try (BufferedSource source = response.body().source()) {
                    while (!source.exhausted()) {
                        String line = source.readUtf8Line();
                        if (line == null) continue;

                        if (line.startsWith("data: ")) {
                            String data = line.substring(6).trim();
                            if ("[DONE]".equals(data)) {
                                sendEndSignal(emitter);
                                break;
                            }

                            // 解析并发送内容(示例解析逻辑)
                            String content = parseContent(data);
                            if (!content.isEmpty()) {
                                sendDataChunk(emitter, content);
                            }
                        }
                    }
                } catch (Exception e) {
                    emitter.completeWithError(e);
                } finally {
                    response.close();
                }
            }
        });
        return emitter;
    }

    // 发送数据块(封装为StreamResponse)
    private void sendDataChunk(SseEmitter emitter, String content) {
        try {
            AjaxResult response = new AjaxResult(200, content);
            emitter.send(SseEmitter.event()
                    .data(response)
                    .id(UUID.randomUUID().toString()));
        } catch (IOException e) {
            emitter.completeWithError(e);
        }
    }

    // 发送结束标记
    private void sendEndSignal(SseEmitter emitter) {
        try {
            emitter.send(SseEmitter.event()
                    .data(new AjaxResult(200, "[END]")));
            emitter.complete();
        } catch (IOException e) {
            emitter.completeWithError(e);
        }
    }

    private String parseContent(String jsonData) {
        // 实现JSON解析逻辑(示例)
        try {
            // 假设返回格式:{"choices":[{"delta":{"content":"..."}}]}
            JSONObject json = JSONObject.parseObject(jsonData);
            return json.getJSONArray("choices")
                    .getJSONObject(0)
                    .getJSONObject("delta")
                    .get("content")
                    .toString();
        } catch (Exception e) {
            return "";
        }
    }
}

public class AjaxResult{
        private int code;
        private String msg;

        // 构造方法
        public AjaxResult(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        // Getters(必须存在,否则JSON序列化失败)
        public int getCode() { return code; }
        public String getMsg() { return msg; }
}

接口调用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值