Spring AI DeepSeek 集成
application.properties
文件中设置 API 密钥配置:
spring.ai.retry
用作属性前缀,允许你配置 DeepSeek 聊天模型的重试机制。
属性 | 描述 | 默认值 |
---|---|---|
spring.ai.retry.max-attempts | 最大重试次数。 | 10 |
spring.ai.retry.backoff.initial-interval | 指数退避策略的初始休眠持续时间。 | 2 秒 |
spring.ai.retry.backoff.multiplier | 退避间隔乘数。 | 5 |
spring.ai.retry.backoff.max-interval | 最大退避持续时间。 | 3 分钟 |
spring.ai.retry.on-client-errors | 如果为 false,则抛出 NonTransientAiException,并且不尝试对 4xx 客户端错误代码进行重试 | false |
spring.ai.retry.exclude-on-http-codes | 不应触发重试的 HTTP 状态代码列表(例如,抛出 NonTransientAiException)。 | 空 |
spring.ai.retry.on-http-codes | 应触发重试的 HTTP 状态代码列表(例如,抛出 TransientAiException)。 | 空 |
spring.ai.deepseek
用作属性前缀,允许你连接到 DeepSeek。
属性 | 描述 | 默认值 |
---|---|---|
spring.ai.deepseek.base-url | 要连接的 URL | https://api.deepseek.com |
spring.ai.deepseek.api-key | API 密钥 | - |
spring.ai.deepseek.chat
是属性前缀,允许你配置 DeepSeek 的聊天模型实现。
属性 | 描述 | 默认值 |
---|---|---|
spring.ai.deepseek.chat.enabled | 启用 DeepSeek 聊天模型。 | true |
spring.ai.deepseek.chat.base-url | 可选地覆盖 spring.ai.deepseek.base-url 以提供特定于聊天的 URL | https://api.deepseek.com/ |
spring.ai.deepseek.chat.api-key | 可选地覆盖 spring.ai.deepseek.api-key 以提供特定于聊天的 API 密钥 | - |
spring.ai.deepseek.chat.completions-path | 聊天补全端点的路径 | /chat/completions |
spring.ai.deepseek.chat.beta-prefix-path | Beta 功能端点的前缀路径 | /beta/chat/completions |
spring.ai.deepseek.chat.options.model | 要使用的模型的 ID。你可以使用 deepseek-coder 或 deepseek-chat。 | deepseek-chat |
spring.ai.deepseek.chat.options.frequencyPenalty | -2.0 到 2.0 之间的数字。正值会根据新标记在文本中已有的频率对其进行惩罚。 | 0.0f |
spring.ai.deepseek.chat.options.maxTokens | 在聊天补全中生成的最大标记数。 | - |
spring.ai.deepseek.chat.options.presencePenalty | -2.0 到 2.0 之间的数字。正值会根据新标记是否已出现在文本中对其进行惩罚。 | 0.0f |
spring.ai.deepseek.chat.options.stop | API 将停止生成更多标记的最多 4 个序列。 | - |
spring.ai.deepseek.chat.options.temperature | 采样温度在 0 到 2 之间。较高的值(如 0.8)会使输出更随机。 | 1.0F |
spring.ai.deepseek.chat.options.topP | 核采样的温度替代方案。值在 0 到 1 之间。 | 1.0F |
spring.ai.deepseek.chat.options.logprobs | 是否返回输出标记的对数概率。 | - |
spring.ai.deepseek.chat.options.topLogprobs | 与对数概率一起返回的最可能标记的数量 (0-20)。 | - |
ChatModel
实现覆盖通用的 spring.ai.deepseek.base-url
和 spring.ai.deepseek.api-key
。
如果设置了 spring.ai.deepseek.chat.base-url
和 spring.ai.deepseek.chat.api-key
属性,则它们优先于通用属性。
如果你想为不同的模型和不同的模型端点使用不同的 DeepSeek 帐户,这将非常有用。spring.ai.deepseek.chat.options
为前缀的属性都可以在运行时通过向 Prompt
调用添加特定于请求的运行时选项来覆盖。DeepSeekChatModel(api, options)
构造函数或 spring.ai.deepseek.chat.options.*
属性配置默认选项。
在运行时,你可以通过向 Prompt
调用添加新的、特定于请求的选项来覆盖默认选项。
例如,要为特定请求覆盖默认模型和温度:
spring-ai-starter-model-deepseek
添加到你的 pom (或 gradle) 依赖项中。
在 src/main/resources
目录下添加一个 application.properties
文件,以启用和配置 DeepSeek 聊天模型:
api-key
替换为你的 DeepSeek 凭据。DeepSeekChatModel
实现,你可以将其注入到你的类中。
以下是一个简单的 @Controller
类的示例,该类使用聊天模型进行文本生成:
python\n" 以强制模型输出 Python 代码,并将 stop 参数设置为 ['
’] 以防止模型进行额外解释:
deepseek-reasoner
是 DeepSeek 开发的推理模型。在给出最终答案之前,模型首先生成一个思维链 (CoT) 以提高其响应的准确性。
deepseek-reasoner
生成的 CoT 内容,从而能够查看、显示和提炼它。DeepSeekAssistantMessage
获取 deepseek-reasoner
生成的 CoT 内容: