Spring AI 集成 DeepSeek V3 模型开发指南
Spring AI 集成 DeepSeek V3 模型开发指南
前言
在人工智能飞速发展的当下,大语言模型不断推陈出新,DeepSeek AI 推出的开源 DeepSeek V3 模型凭借其卓越的推理和问题解决能力备受瞩目。与此同时,Spring AI 作为一个强大的框架,为开发者集成各类 AI 服务提供了便利。本文将深入探讨如何通过 Spring AI 集成 DeepSeek V3 模型,助力开发者快速搭建高效的 AI 应用。
Spring AI 与 DeepSeek 集成要点
1. 准备工作
- 获取 API 密钥:访问指定平台创建 DeepSeek API 密钥,并通过
spring.ai.openai.api - key
属性进行配置。 - 设置基 URL:将
spring.ai.openai.base - url
属性设置为api.deepseek.com
,以此确定与 DeepSeek 服务通信的基础地址。 - 选择模型:利用
spring.ai.openai.chat.options.model = <model name>
属性,从支持的模型列表中指定使用的 DeepSeek 模型。例如,若要使用deepseek - chat
模型,可进行相应配置。同时,可通过环境变量配置,如:
export SPRING_AI_OPENAI_API_KEY = <INSERT DEEPSEEK API KEY HERE>
export SPRING_AI_OPENAI_BASE_URL = https://api.deepseek.com
export SPRING_AI_OPENAI_CHAT_MODEL = deepseek - chat
2. 添加存储库和 BOM
- 存储库:Spring AI 工件发布于 Maven Central 和 Spring Snapshot 存储库。开发者需将这些存储库添加到构建系统中,具体操作可参考 Repositories 部分。
- BOM:为实现依赖项的有效管理,Spring AI 提供了 BOM(物料清单)。通过添加 Spring AI BOM 到构建系统,能够确保项目中 Spring AI 版本的一致性,详细步骤可查阅依赖项管理部分。
3. 自动配置
- Spring AI 为 OpenAI Chat 客户端提供了 Spring Boot 自动配置功能。要启用该功能,需在项目的 Maven
pom.xml
或 Gradlebuild.gradle
文件中添加以下依赖项:
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring - ai - starter - model - openai</artifactId>
</dependency>
同时,别忘了将 Spring AI BOM 添加到构建文件中。
4. 聊天属性配置
- 重试属性:以
spring.ai.retry
为前缀的属性,可用于配置 OpenAI 聊天模型的重试机制。例如,spring.ai.retry.max - attempts
用于设置最大重试尝试次数,默认值为 10;spring.ai.retry.backoff.initial - interval
表示指数回退策略的初始休眠持续时间,默认 2 秒等。通过合理配置这些属性,能够增强应用在面对网络波动等异常情况时的稳定性。 - 连接属性:
spring.ai.openai
前缀的属性用于连接到 DeepSeek。其中,spring.ai.openai.base - url
必须设置为api.deepseek.com
,spring.ai.openai.api - key
需配置为申请到的 DeepSeek API 密钥。 - 配置属性:如今,启用和禁用聊天自动配置通过
spring.ai.model.chat
前缀实现。默认spring.ai.model.chat = openai
为启用状态,若要禁用可设置为spring.ai.model.chat = none
或其他非openai
的值。此外,spring.ai.openai.chat
前缀下还有众多属性,如spring.ai.openai.chat.options.model
用于指定要使用的 DeepSeek LLM 模型;spring.ai.openai.chat.options.temperature
控制生成完成项的随机性,取值范围影响输出的创造性程度等。
5. 运行时选项
- 启动时,可通过
OpenAiChatModel(api, options)
构造函数或spring.ai.openai.chat.options.*
属性进行模型配置。在运行时,可通过向Prompt
添加特定于请求的运行时选项。例如,若要覆盖特定请求的默认模型和温度,可按如下方式编写代码:
ChatResponse response = chatModel.call(new Prompt("Generate the names of 5 famous pirates.",OpenAiChatOptions.builder().model("deepseek - chat").temperature(0.4).build()));
6. 函数调用与限制
需要注意的是,deepseek - chat
模型的函数调用功能当前版本不稳定,可能出现循环调用或空响应的问题。并且,目前 DeepSeek API 暂不支持媒体内容。
7. Samples 控制器示例
创建一个新的 Spring Boot 项目,在 POM(或 Gradle)依赖项中添加 spring - ai - starter - model - openai
。然后在 src/main/resources
目录下的 application.properties
文件中进行如下配置:
spring.ai.openai.api - key = <DEEPSEEK_API_KEY>
spring.ai.openai.base - url = https://api.deepseek.com
spring.ai.openai.chat.options.model = deepseek - chat
spring.ai.openai.chat.options.temperature = 0.7
# The DeepSeek API doesn't support embeddings, so we need to disable it.
spring.ai.openai.embedding.enabled = false
将 <DEEPSEEK_API_KEY>
替换为实际申请到的密钥。通过上述配置,会创建一个 OpenAiChatModel
实现,可将其注入到类中。以下是一个简单的 @Controller
类示例,用于使用聊天模型生成文本:
@RestController
public class ChatController {private final OpenAiChatModel chatModel;@Autowiredpublic ChatController(OpenAiChatModel chatModel) {this.chatModel = chatModel;}@GetMapping("/ai/generate")public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {return Map.of("generation", this.chatModel.call(message));}@GetMapping("/ai/generateStream")public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {Prompt prompt = new Prompt(new UserMessage(message));return this.chatModel.stream(prompt);}
}
若要查询如何使用本地部署的DeepSeek模型,可以查看文章: 利用ollama.com本地部署大模型及Java验证全攻略
总结
通过本文介绍的步骤,开发者能够成功地将 Spring AI 与 DeepSeek V3 模型集成,充分利用 DeepSeek 模型强大的能力来构建智能应用。在集成过程中,要特别注意 API 密钥的安全配置、模型选项的合理调整以及对不稳定功能(如函数调用)的妥善处理。同时,由于 AI 技术和相关框架处于不断发展中,开发者需持续关注官方文档,以便及时获取最新信息,优化应用性能和功能。希望本文能为大家在基于 Spring AI 和 DeepSeek 进行开发的道路上提供有力的指导和帮助,助力大家开发出更具创新性和竞争力的 AI 应用。