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 添加到您的构建文件中。
Chat Properties
Retry Properties
前缀 spring.ai.retry 是用于配置 DeepSeek Chat model 的 retry 机制的属性前缀。
| Property | Description | Default |
|---|---|---|
| spring.ai.retry.max-attempts | 最大重试次数。 | 10 |
| spring.ai.retry.backoff.initial-interval | 指数退避策略的初始睡眠持续时间。 | 2 sec. |
| spring.ai.retry.backoff.multiplier | 退避间隔乘数。 | 5 |
| spring.ai.retry.backoff.max-interval | 最大退避持续时间。 | 3 min. |
| spring.ai.retry.on-client-errors | 如果为 false,抛出 NonTransientAiException,并且不对 4xx 客户端错误代码尝试重试 | false |
| spring.ai.retry.exclude-on-http-codes | 不应触发重试的 HTTP 状态代码列表(例如,抛出 NonTransientAiException)。 | empty |
| spring.ai.retry.on-http-codes | 应触发重试的 HTTP 状态代码列表(例如,抛出 TransientAiException)。 | empty |
Connection Properties
前缀 spring.ai.deepseek 是用于连接到 DeepSeek 的属性前缀。
| Property | Description | Default |
|---|---|---|
| spring.ai.deepseek.base-url | 要连接到的 URL | +https://api.deepseek.com+ |
| spring.ai.deepseek.api-key | API Key | - |
Configuration Properties
前缀 spring.ai.deepseek.chat 是用于配置 DeepSeek 的 chat model 实现的属性前缀。
| Property | Description | Default |
|---|---|---|
| spring.ai.deepseek.chat.enabled | 启用 DeepSeek chat model。 | true |
| spring.ai.deepseek.chat.base-url | 可选覆盖 spring.ai.deepseek.base-url 以提供 chat 特定的 URL | +https://api.deepseek.com/+ |
| spring.ai.deepseek.chat.api-key | 可选覆盖 spring.ai.deepseek.api-key 以提供 chat 特定的 API key | - |
| spring.ai.deepseek.chat.completions-path | chat completions endpoint 的路径 | /chat/completions |
| spring.ai.deepseek.chat.beta-prefix-path | beta feature endpoint 的前缀路径 | /beta |
| spring.ai.deepseek.chat.options.model | 要使用的模型 ID。您可以使用 deepseek-reasoner 或 deepseek-chat。 | deepseek-chat |
| spring.ai.deepseek.chat.options.frequencyPenalty | 介于 -2.0 和 2.0 之间的数字。正值会根据到目前为止文本中现有频率对新 tokens 进行惩罚,降低模型逐字重复同一行的可能性。 | 0.0f |
| spring.ai.deepseek.chat.options.maxTokens | 在 chat completion 中生成的最大 tokens 数。输入 tokens 和生成 tokens 的总长度受模型上下文长度的限制。 | - |
| spring.ai.deepseek.chat.options.presencePenalty | 介于 -2.0 和 2.0 之间的数字。正值会根据到目前为止是否出现新 tokens 进行惩罚,增加模型讨论新主题的可能性。 | 0.0f |
| spring.ai.deepseek.chat.options.stop | 最多 4 个序列,API 将停止生成更多 tokens。 | - |
| spring.ai.deepseek.chat.options.temperature | 要使用的采样 temperature,介于 0 和 2 之间。较高的值(如 0.8)将使输出更加随机,而较低的值(如 0.2)将使输出更加聚焦和确定性。我们通常建议修改此值或 top_p,但不要同时修改两者。 | 1.0F |
| spring.ai.deepseek.chat.options.topP | 除了 temperature 采样之外,还有一种称为 nucleus sampling 的替代方法,其中模型考虑具有 top_p 概率质量的 tokens 结果。因此,0.1 意味着只考虑包含 top 10% 概率质量的 tokens。我们通常建议修改此值或 temperature,但不要同时修改两者。 | 1.0F |
| spring.ai.deepseek.chat.options.logprobs | 是否返回输出 tokens 的 log probabilities。如果为 true,返回消息内容中返回的每个输出 token 的 log probabilities。 | - |
| spring.ai.deepseek.chat.options.topLogprobs | 介于 0 和 20 之间的整数,指定在每个 token 位置返回的最可能的 tokens 数量,每个都有相关的 log probability。如果使用此参数,必须将 logprobs 设置为 true。 | - |
| spring.ai.deepseek.chat.options.tool-names | 按名称标识的工具列表,用于在单个 prompt 请求中启用 function calling。具有这些名称的工具必须存在于 ToolCallback 注册表中。 | - |
| spring.ai.deepseek.chat.options.tool-callbacks | 要注册到 ChatModel 的 Tool Callbacks。 | - |
| spring.ai.deepseek.chat.options.internal-tool-execution-enabled | 如果为 false,Spring AI 不会在内部处理 tool calls,而是将它们代理到客户端。然后客户端负责处理 tool calls,将它们分派到适当的 function,并返回结果。如果为 true(默认值),Spring AI 将在内部处理 function calls。仅适用于支持 function calling 的 chat models | true |