跳到主要内容

DeepSeek and Spring AI

DeepSeek provides an OpenAI compatible API at the endpoint https://api.deepseek.com. In Spring AI projects, we can use OpenAI module to work with DeepSeek.

When using Spring Boot, we only need to add the spring-ai-openai-spring-boot-starter dependency.

OpenAI Spring Boot starter
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>

To use DeepSeek, we need to set spring.ai.openai.baseUrl to https://api.deepseek.com. For the model, we can use either deepseek-reasoner for DeepSeek V3.1 (Reasoning), or deepseek-chat for DeepSeek V3.1 (Non-reasoning). DeepSeek V3.1 (Reasoning) takes a longer time to respond, and doesn't support function calling or JSON output. DeepSeek V3.1 (Non-reasoning) supports function calling and JSON output, but has some issues.

The API key of DeepSeek is set to environment variable DEEPSEEK_API_KEY.

DeepSeek config
spring:
ai:
openai:
baseUrl: https://api.deepseek.com
api-key: ${DEEPSEEK_API_KEY:}
chat:
options:
model: deepseek-chat
temperature: 0.0