令人兴奋的消息,spring ai 的最新快照版本已经内置了 deepseek,这使得在项目中集成 deepseek 变得更加便捷。然而,由于快照版本可能存在许多 bug,我们今天将探讨如何使用 spring ai 的稳定版本来全面集成 deepseek。
Spring AI 和 DeepSeek 介绍
Spring AI 是 Spring 生态系统中的一个关键项目,旨在将人工智能无缝集成到 Spring 应用程序中。它为 Java 开发者提供了一种便捷的方式来构建、管理和部署 AI 模型。
Spring AI 的核心是解决了 Spring 生态和 AI 的快速集成:将您的企业数据和 API 与 AI 模型连接起来。
Spring AI 几乎支持所有主流的 AI 模型提供商,例如 Anthropic、OpenAI、Microsoft、Amazon、Google 和 Ollama。支持的功能包括:
Spring AI 的最新预览版也将集成 DeepSeek 大模型。
DeepSeek 介绍
DeepSeek 是国内顶尖 AI 团队「深度求索」开发的多模态大模型,具备数学推理、代码生成等深度能力,被誉为"AI界的六边形战士"。DeepSeek 的最新版本 R1 采用了“思维链”技术,能够展示完整的推理过程,使其在复杂推理任务上表现出色,甚至在某些方面可以与 OpenAI 的 O1 模型相媲美。
DeepSeek 的标签很多,其中最具代表性的标签有以下两个:
Spring AI 集成 DeepSeek 的步骤如下:
环境准备
在开始集成之前,确保您的开发环境满足以下要求:
创建 Spring Boot 项目
使用 Spring Initializr 或其他工具创建一个新的 Spring Boot 项目,确保版本为 3.2.x 或更高。
添加依赖
在项目的 pom.xml 文件中添加 Spring AI 和 DeepSeek 的相关依赖。
以下是基于 Maven 的依赖配置示例:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-openai-spring-boot-starter</artifactId> </dependency> </dependencies>
配置文件
在 application.properties 或 application.yml 文件中添加 DeepSeek 的配置信息:
# 必填项 spring.ai.openai.api-key=your-apikey spring.ai.openai.base-url=https://api.deepseek.com # 模型选择(示例使用对话模型) spring.ai.openai.chat.options.model=deepseek-chat
其中,api-key 是你在 DeepSeek 官网注册后获取的密钥,base-url 是 DeepSeek API 的服务地址,model 指定使用的模型版本。
DeepSeek 模型介绍
DeepSeek 目前支持以下两种模型:
编写代码
创建一个控制器类,用于处理与 DeepSeek 的交互,以下是一个简单的示例:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Flux; <p>@RestController @RequestMapping("/api/chat") public class ChatController { @Autowired private DeepSeekClient deepSeekClient;</p><pre class="brush:php;toolbar:false">@PostMapping public String chat(@RequestBody String message) { return deepSeekClient.chatCompletion(message).getOutput().getContent(); } @GetMapping(value = "/stream", produces = "text/event-stream") public Flux<String> chatStream(@RequestParam String message) { return deepSeekClient.chatFluxCompletion(message) .map(response -> response.getOutput().getContent()); }
}
在上述代码中,chat 方法用于处理普通的非流式请求,而 chatStream 方法则支持流式响应,能够实时返回 AI 的推理结果。
课后思考:关于流式输出大模型的响应速度是很慢的,为了避免用户用户能够耐心等待输出的结果,我们通常会使用流式输出一点点将结果输出给用户,那么问题来了,想要实现流式结果输出,后端和前端要如何配合?后端要使用什么技术实现流式输出呢?
欢迎在评论区给出你的解决答案,文章点赞超过 100 我们将公布完整的解决思路和具体实现源码哦。
以上就是炸裂:SpringAI内置DeepSeek啦!的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号