成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Spring AI + MCP + DeepSeek-R1-7B + SSE 全流程實(shí)戰(zhàn)指南

人工智能
通過(guò)以上步驟,我們成功實(shí)現(xiàn)了:Ollama 部署并運(yùn)行 DeepSeek-R1-7B 本地大模型;Spring Boot 封裝 OpenAI 接口 ??stream: true??;實(shí)現(xiàn)后端 SSE 推流 + 前端實(shí)時(shí) Token 渲染;支持國(guó)產(chǎn)開(kāi)源模型的類 ChatGPT 對(duì)話功能?。

本教程將帶你從 0 到 1 實(shí)現(xiàn)一個(gè)完整的 AI 流式問(wèn)答應(yīng)用,整合以下組件:

  • Spring Boot + Spring AI 構(gòu)建服務(wù)框架
  • MCP(Model Connector Plugin) 用于統(tǒng)一管理本地/云端大模型
  • DeepSeek-R1-7B 國(guó)產(chǎn)高性能大模型(OpenAI API 兼容)
  • SSE(Server-Sent Events) 實(shí)現(xiàn)前后端實(shí)時(shí)流式響應(yīng)
  • Ollama(可選) 更便捷地部署 DeepSeek-R1-7B 模型并提供 OpenAI 接口支持

模型部署方式推薦:Ollama 運(yùn)行 DeepSeek-R1-7B

安裝 Ollama

訪問(wèn):https://ollama.com

# macOS / Linux 安裝
curl-fsSL https://ollama.com/install.sh |sh

# Windows:安裝官方 MSI 安裝包

拉取模型(以 DeepSeek 為例)

ollama pull deepseek:chat

也可以加載其它模型,如 llama3qwen:chatyi:34bphi3mistral 等。

啟動(dòng) Ollama

ollama run deepseek:chat

Ollama 會(huì)自動(dòng)監(jiān)聽(tīng) OpenAI 風(fēng)格接口(http://localhost:11434/v1/chat/completions),兼容 stream: true

Spring Boot 接入 SSE 流式輸出服務(wù)

添加依賴(pom.xml)

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
</dependencies>

WebClient 配置類

@Configuration
public class WebClientConfig {
    @Bean
    public WebClient webClient() {
        return WebClient.builder()
                .baseUrl("http://localhost:11434/v1") // Ollama API 地址
                .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .build();
    }
}

請(qǐng)求體結(jié)構(gòu)封裝

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ChatCompletionRequest {
    private String model;
    private List<Map<String, String>> messages;
    private boolean stream = true;
    private double temperature = 0.7;
}

DeepSeek-R1-7B 接口封裝(支持 stream: true

@Service
public class DeepSeekStreamingService {


    @Autowired
    private WebClient webClient;


    public void streamChat(String userPrompt, SseEmitter emitter) {
        ChatCompletionRequest request = new ChatCompletionRequest();
        request.setModel("deepseek:chat");
        request.setStream(true);
        request.setMessages(List.of(
                Map.of("role", "user", "content", userPrompt)
        ));


        webClient.post()
                .uri("/chat/completions")
                .body(BodyInserters.fromValue(request))
                .accept(MediaType.TEXT_EVENT_STREAM)
                .retrieve()
                .bodyToFlux(String.class)
                .doOnNext(chunk -> {
                    try {
                        if (chunk.contains("[DONE]")) {
                            emitter.send(SseEmitter.event().data("[DONE]"));
                            emitter.complete();
                        } else if (chunk.startsWith("data:")) {
                            String json = chunk.replaceFirst("data: *", "");
                            String token = parseTokenFromJson(json);
                            emitter.send(SseEmitter.event().data(token));
                        }
                    } catch (Exception e) {
                        emitter.completeWithError(e);
                    }
                })
                .doOnError(emitter::completeWithError)
                .subscribe();
    }


    private String parseTokenFromJson(String json) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            JsonNode node = mapper.readTree(json);
            return node.path("choices").get(0).path("delta").path("content").asText("");
        } catch (Exception e) {
            return "";
        }
    }
}

控制器對(duì)外暴露 SSE 接口

@RestController
@RequestMapping("/api/ai")
public class ChatSseController {


    @Autowired
    private DeepSeekStreamingService streamingService;


    @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter stream(@RequestParam("prompt") String prompt) {
        SseEmitter emitter = new SseEmitter(0L); // 永不超時(shí)
        streamingService.streamChat(prompt, emitter);
        return emitter;
    }
}

前端 JS 接入 SSE 實(shí)現(xiàn)流式展示

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>AI 流式問(wèn)答</title>
</head>
<body>
  <input id="prompt" placeholder="請(qǐng)輸入你的問(wèn)題...">
  <button onclick="startStream()">提問(wèn)</button>
  <div id="result"></div>


  <script>
    function startStream() {
      const prompt = document.getElementById('prompt').value;
      const eventSource = new EventSource(`/api/ai/stream?prompt=${encodeURIComponent(prompt)}`);
      document.getElementById('result').innerHTML = '';
      eventSource.onmessage = function (event) {
        if (event.data === '[DONE]') {
          eventSource.close();
        } else {
          document.getElementById('result').innerHTML += event.data;
        }
      };
    }
  </script>
</body>
</html>

總結(jié)

通過(guò)以上步驟,我們成功實(shí)現(xiàn)了:

  • Ollama 部署并運(yùn)行 DeepSeek-R1-7B 本地大模型
  • Spring Boot 封裝 OpenAI 接口 stream: true
  • 實(shí)現(xiàn)后端 SSE 推流 + 前端實(shí)時(shí) Token 渲染
  • 支持國(guó)產(chǎn)開(kāi)源模型的類 ChatGPT 對(duì)話功能


責(zé)任編輯:武曉燕 來(lái)源: 路條編程
相關(guān)推薦

2025-04-21 04:22:00

Spring AIMCPDeepSeek

2025-04-03 15:57:48

2025-05-08 02:10:00

SpringAIAPI

2025-03-27 09:34:42

2025-03-07 08:50:03

2025-02-12 12:12:59

2025-04-02 09:00:00

DeepSeek人工智能AI

2025-04-27 08:54:00

英偉達(dá)開(kāi)源模型

2025-06-12 09:48:27

2025-03-27 10:28:32

2025-02-03 06:00:00

2025-01-27 12:30:07

2025-02-19 08:00:00

2025-05-06 09:09:37

2025-03-05 03:00:00

DeepSeek大模型調(diào)優(yōu)

2025-02-24 00:00:00

DeepSeek機(jī)器人模型

2025-03-17 12:13:26

AI模型生成

2025-02-20 15:32:28

2024-12-27 12:37:18

2025-04-14 09:15:00

英偉達(dá)模型數(shù)據(jù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: a毛片| 国产视频一区二区三区四区五区 | 久久99精品久久久久子伦 | 亚洲xxxxx| 91久久精品日日躁夜夜躁欧美 | 岛国二区 | 在线播放精品视频 | 欧美高清性xxxxhd | 亚洲一区二区三区四区五区中文 | 亚洲一区二区在线视频 | 91社区在线高清 | 福利视频一区 | 日韩精品在线观看网站 | 成人在线免费看 | 国内自拍真实伦在线观看 | 午夜影院在线观看 | 欧洲一区二区三区 | 久久综合一区 | 欧美在线国产精品 | 亚洲国产成人精品一区二区 | 一级毛片成人免费看a | 综合久久色 | 欧美一级片免费看 | 91视频在线观看 | 一区精品在线观看 | 久久精品一区二区 | 韩日av在线 | 日韩av看片 | 欧美日韩在线观看一区二区三区 | 成人午夜网站 | 国产精品久久久久久久7电影 | 高清18麻豆 | 一区二区三区在线观看视频 | www.午夜 | 久久精品福利 | 欧美日韩中文字幕在线 | av在线免费观看不卡 | 亚洲精品黑人 | 午夜精品久久久久久久星辰影院 | 成人免费一级视频 | 精品在线免费观看视频 |