DeepSeek Chat
Spring AI 支持来自 DeepSeek 的各种 AI 语言模型。您可以与 DeepSeek 语言模型交互,并基于 DeepSeek 模型创建多语言对话助手。
Prerequisites
您需要使用 DeepSeek 创建 API key 才能访问 DeepSeek 语言模型。
在 DeepSeek registration page 创建账户,并在 API Keys page 生成 token。
Spring AI 项目定义了一个名为 spring.ai.deepseek.api-key 的配置属性,您应将其设置为从 API Keys 页面获得的 API Key 值。
您可以在 application.properties 文件中设置此配置属性:
spring.ai.deepseek.api-key=<your-deepseek-api-key>
为了在处理敏感信息(如 API keys)时增强安全性,您可以使用 Spring Expression Language (SpEL) 来引用自定义环境变量:
# In application.yml
spring:
ai:
deepseek:
api-key: ${DEEPSEEK_API_KEY}
# In your environment or .env file
export DEEPSEEK_API_KEY=<your-deepseek-api-key>
您也可以在应用程序代码中以编程方式设置此配置:
// Retrieve API key from a secure source or environment variable
String apiKey = System.getenv("DEEPSEEK_API_KEY");
Add Repositories and BOM
Spring AI artifacts 发布在 Spring Milestone 和 Snapshot 仓库中。 请参阅 Artifact Repositories 部分,将这些仓库添加到您的构建系统中。
为了帮助进行依赖管理,Spring AI 提供了一个 BOM (bill of materials) ,以确保在整个项目中使用一致版本的 Spring AI。请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建系统中。
Auto-configuration
Spring AI 为 DeepSeek Chat Model 提供 Spring Boot auto-configuration。
要启用它,请将以下依赖项添加到项目的 Maven pom.xml 文件中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-deepseek</artifactId>
</dependency>
或添加到您的 Gradle build.gradle 文件中。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-deepseek'
}
提示: 请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。