springboot集成deepseek初体验

依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>groupId</groupId>
    <artifactId>SpringWebProAi</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>17</java.version>
        <spring-ai.version>1.0.0-M5</spring-ai.version>
    </properties>

    
### Spring Boot 集成 DeepSeek 并基于 SSE 实现 #### 使用场景描述 为了实现在 Spring Boot集成 DeepSeek,并通过服务器发送事件 (SSE) 提供实时更新的功能,可以按照如下方式构建应用程序。这不仅能够增强用户体验,还能提高数据处理效率。 #### 添加依赖项 首先,在 `pom.xml` 文件中加入必要的 Maven 依赖来支持 WebFlux 和 Thymeleaf 模板引擎: ```xml <dependencies> <!-- Other dependencies --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency> <!-- Add dependency for integrating with DeepSeek API here --> </dependencies> ``` 对于 DeepSeek集成,则需根据官方文档添加相应的 SDK 或者 RESTful 接口调用所需的库[^1]。 #### 创建控制器类 定义一个新的 RestController 来管理来自客户端的请求以及向其推送消息: ```java @RestController public class SseController { @GetMapping("/stream") public Flux<String> stream() { return Flux.interval(Duration.ofSeconds(1)) .map(sequence -> "Event #" + sequence); } } ``` 此代码片段创建了一个简单的流服务端点 `/stream` ,它每隔一秒就会发出一条新信息给订阅它的前端页面。 #### 前端 HTML 页面配置 在 resources/templates 下新建一个名为 index.html 的文件用于展示接收到的数据并设置自动刷新机制以便于持续接收新的通知: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>SSE Example Page</title> <script type="text/javascript"> document.addEventListener('DOMContentLoaded', function () { var source = new EventSource('/stream'); source.onmessage = function(event){ console.log(`New message from server: ${event.data}`); document.body.innerHTML += `<div>${event.data}</div>`; }; }); </script> </head> <body> <h1>Welcome to the Server Sent Events Demo!</h1> <p id="content"></p> </body> </html> ``` 这段 JavaScript 脚本会在网页加载完成后建立与后端之间的连接,并监听任何传入的消息;每当有新消息到达时就将其显示出来。 #### 启动应用测试效果 完成上述步骤之后启动 Spring Boot 应用程序即可看到每秒钟都会有一条记录被追加至浏览器窗口内,证明已经成功实现了基本的 SSE 功能。 关于如何具体对接 DeepSeek 这部分取决于该平台所提供的开发指南,请参照相关说明进行适配工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值