spring boot 实现接入 deepseek gpt接口 流式输出

controller

复制代码
package com.xmkjsoft.ssegpt;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@CrossOrigin
@RestController
@RequestMapping("/chat")
public class ChatController {

    @Value("${deepseek.api.key}")
    private String apiKey;

    @Value("${deepseek.api.url}")
    private String apiUrl;

    // 简单保存 prompt 用,实际可优化
    private final ConcurrentHashMap<String, String> promptMap = new ConcurrentHashMap<>();

    @PostMapping("/start")
    public StartResponse startChat(@RequestBody ChatRequest userRequest) {
        String id = UUID.randomUUID().toString();
        promptMap.put(id, userRequest.getPrompt());
        return new StartResponse(id);
    }

    @GetMapping("/stream")
    public SseEmitter stream(@RequestParam String id) {
        SseEmitter emitter = new SseEmitter(0L);
        String prompt = promptMap.get(id);
        if (prompt == null) {
            emitter.completeWithError(new IllegalArgumentException("无效的id"));
            return emitter;
        }

        new Thread(() -> {
            try {
                String json = """
                    {
                      "model": "deepseek-chat",
                      "stream": true,
                      "messages": [
                        { "role": "system", "content": "You are a helpful assistant." },
                        { "role": "user", "content": "%s" }
                      ]
                    }
                    """.formatted(prompt);

                URL url = new URL(apiUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "application/json");
                conn.setRequestProperty("Authorization", "Bearer " + apiKey);
                conn.setDoOutput(true);
                conn.getOutputStream().write(json.getBytes());

                try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
                    String line;
                    while ((line = reader.readLine()) != null) {
                        if (line.startsWith("data: ")) {
                            String data = line.substring("data: ".length());
                            if ("[DONE]".equals(data)) break;
                            emitter.send(data);
                        }
                    }
                }
                emitter.complete();
                promptMap.remove(id);
            } catch (Exception e) {
                emitter.completeWithError(e);
            }
        }).start();

        return emitter;
    }

    @Data
    public static class ChatRequest {
        private String prompt;
    }

    @Data
    public static class StartResponse {
        private final String id;
    }
}

application.properties

复制代码
spring.application.name=ssegpt
#deepseek.model=deepseek-reasoner
deepseek.model=deepseek-chat
deepseek.api.key=自己去获取
deepseek.api.url=https://api.deepseek.com/v1/chat/completions

index.html

复制代码
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8" />
    <title>SSE Chat Demo</title>
</head>
<body>
<textarea id="input" rows="3" cols="40" placeholder="输入你的问题"></textarea><br/>
<button id="sendBtn">发送</button>

<div id="output" style="white-space: pre-wrap; border: 1px solid #ccc; padding: 10px; margin-top: 10px;"></div>

<script>
    document.getElementById('sendBtn').onclick = () => {
        const prompt = document.getElementById('input').value.trim();
        if (!prompt) return alert("请输入内容");

        fetch('/chat/start', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ prompt })
        }).then(res => res.json())
            .then(data => {
                if (!data.id) throw new Error("启动失败");
                listenSSE(data.id);
            }).catch(err => alert("启动错误:" + err));
    };

    function listenSSE(id) {
        const evtSource = new EventSource('/chat/stream?id=' + encodeURIComponent(id));
        const output = document.getElementById('output');
        output.textContent = '';  // 清空之前内容

        evtSource.onmessage = (e) => {
            if (e.data === '[DONE]') {
                evtSource.close();
                return;
            }
            try {
                const json = JSON.parse(e.data);
                const content = json.choices && json.choices[0] && json.choices[0].delta && json.choices[0].delta.content;
                if (content !== undefined) {
                    output.textContent += content;
                }
            } catch (err) {
                output.textContent += e.data;
            }
        };

        evtSource.onerror = (e) => {
            console.error("连接错误", e);
            evtSource.close();
        };
    }
</script>

</body>
</html>

maven 依赖

复制代码
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

 <dependency>
    <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
相关推荐
程序猿小D几秒前
[附源码+数据库+毕业论]基于Spring Boot+mysql+vue结合内容推荐算法的学生咨询系统
数据库·vue.js·spring boot·mysql·毕业设计·推荐算法·学生咨询系统
我爱加班、、15 分钟前
element-plus表单校验失败问题
前端·javascript·vue.js·elementui·ecmascript
香香甜甜的辣椒炒肉20 分钟前
vue快速上手
前端·javascript·vue.js
大菠萝学姐22 分钟前
基于Spring Boot和Vue的高校图书馆座位预约系统的设计与实现
java·vue.js·spring boot·后端·python·mysql·vue
用户2519162427111 小时前
Canvas之概述,画布与画笔
前端·javascript·canvas
mrsk1 小时前
JavaScript之变量的解构赋值全面解析(●'◡'●)
前端·javascript·面试
归于尽1 小时前
回调函数在Node.js中是怎么执行的?
前端·javascript·node.js
浏览器API调用工程师_Taylor1 小时前
Look my eyes 都2025年了,你还不会将重复的事情自动化?
前端·javascript·爬虫
zhaocarbon1 小时前
vue2 echarts中国地图、在地图上标注经纬度及标注点
前端·javascript·echarts
咔咔咔索菲斯1 小时前
Vue 中mounted 生命周期钩子的执行时机和 v-for 的渲染顺序
前端·javascript·vue.js