在人工智能技术飞速发展的今天,将大语言模型集成到应用程序中已成为许多开发者关注的焦点。Spring AI作为Spring生态系统的一部分,为开发者提供了一套简洁、统一的API来集成各种AI模型。本文将介绍如何使用Spring AI接入DeepSeek,构建您的第一个AI应用。
什么是Spring AI?
Spring AI是Spring框架的一个扩展模块,它为开发者提供了一种标准化的方式来与各种人工智能模型和服务进行交互。通过Spring AI,开发者可以:
使用统一的API访问不同的AI模型
简化AI模型的集成过程
利用Spring Boot的自动配置功能快速启动项目
在生产环境中轻松切换不同的AI服务提供商
Spring AI支持多种AI模型和服务,包括OpenAI、Anthropic等,同时也支持本地模型如Ollama。本文将重点介绍如何接入DeepSeek,一个新兴的优秀大语言模型。
官网:https://spring.io/projects/spring-ai
项目结构
首先,让我们看一下项目的结构:
chat-model-demo/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── cn/river/spring/ai/
│ │ │ └── SpringbootSpringAiApplication.java
│ │ └── resources/
│ │ └── application.properties
│ └── test/
│ └── java/cn/river/spring/ai/
│ └── DeepseekTest.java
└── pom.xml
配置DeepSeek
在[application.properties]文件中,我们需要配置DeepSeek的相关参数:
# deepseek
spring.ai.deepseek.api-key=xxxx
spring.ai.deepseek.chat.options.model=deepseek-chat
spring.ai.deepseek.chat.options.temperature=0.8
其中:
spring.ai.deepseek.api-key:您在DeepSeek平台获取的API密钥spring.ai.deepseek.chat.options.model:使用的模型名称,这里使用的是deepseek-chatspring.ai.deepseek.chat.options.temperature:控制生成文本的随机性,值越高结果越随机
环境要求
JDK17
SpringBoot3
Maven依赖配置
在[pom.xml]文件中,需要添加Spring AI对DeepSeek的支持:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.6</version>
<relativePath/>
</parent>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-deepseek</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
编写测试代码
在测试文件[DeepseekTest.java]中,我们可以使用Spring AI与DeepSeek进行交互:
基础调用示例
@SpringBootTest
public class DeepseekTest {
@Test
void testDeepseek(@Autowired DeepSeekChatModel deepSeekChatModel) throws Exception {
String result = deepSeekChatModel.call("你好你是谁");
System.out.println(result);
}
}
运行单元测试方法,结果如下:

流式调用示例
@Test
void testDeepseekStream(@Autowired DeepSeekChatModel deepSeekChatModel) throws Exception {
Flux<String> stream = deepSeekChatModel.stream("你好你是谁");
stream.toIterable().forEach(System.out::println);
}
运行结果:

高级配置示例
@Test
void testChatOptions(@Autowired DeepSeekChatModel deepSeekChatModel) throws Exception {
DeepSeekChatOptions options = DeepSeekChatOptions.builder()
.model("deepseek-chat")
.temperature(0.1)
.build();
ChatResponse response = deepSeekChatModel.call(
new Prompt("请写一首诗描述中秋国庆双节,请只基于已知事实回答,不要主管臆想或添加额外内容", options));
System.out.println(response.getResult().getOutput().getText());
}
temperature参数用于控制生成文本的多样性。 具体来说值越高,生成的文本越多样化,但也可能包含更多的随机性和不可预测的内容; 值越低,生成的文本越接近于确定性的结果,即生成的文本会更加一致和可预测。
temperature参数的取值范围是0到2,默认值为1。
运行结果:

总结
通过本文,我们了解了如何使用Spring AI集成DeepSeek大语言模型。Spring AI提供了一套简洁统一的API,让开发者可以轻松地在不同的AI模型之间切换,而无需大幅修改代码。这为构建AI驱动的应用程序提供了极大的便利。