Method 1: Based on HttpClient
Step 1: Preparation
Get the DeepSeek API Key: Access DeepSeek's developer platform, register and obtain the API key.
Step 2: Introduce dependencies
<dependency> <groupId></groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
Step 3: Configure the DeepSeek API
Configuration parameters:In the file, write the DeepSeek API endpoint and key:
existor
Configure the API key and endpoint of DeepSeek:
=sk-xxxxxxxxxx =/v1/chat/completions
Step 4: Customize DeepSeek Client Service
(1) Custom request header
public record DeepSeekRequest( String model, List<Message> messages, double temperature, int max_tokens ) { public record Message(String role, String content) {} }
(2) Custom response body
public record DeepSeekResponse( String id, String object, long created, String model, List<Choice> choices ) { public record Choice( int index, Message message, String finish_reason ) { public record Message(String role, String content) {} } }
(3)service
import ; import ; import .*; import ; import ; import ; @Service public class DeepSeekService { @Value("${}") private String apiUrl; @Value("${}") private String apiKey; private final RestTemplate restTemplate; public DeepSeekService(RestTemplateBuilder restTemplateBuilder) { = (); } public String getChatResponse(String userMessage) { // Construct the request body DeepSeekRequest request = new DeepSeekRequest( "deepseek-chat", (new ("user", userMessage)), 0.7, 1000 ); // Set request header HttpHeaders headers = new HttpHeaders(); (MediaType.APPLICATION_JSON); (apiKey); HttpEntity<DeepSeekRequest> entity = new HttpEntity<>(request, headers); // Send a request ResponseEntity<DeepSeekResponse> response = ( apiUrl, , entity, ); // Process the response if (().is2xxSuccessful() && () != null) { return ().choices().get(0).message().content(); } throw new RuntimeException("API request failed: " + ()); } }
You can specify the request body: adjust the model, temperature, maximum token number, etc.
What is temperature?
Temperature is a parameter that controls the "creativeness" or "randomness" of the model's generated text. You can think of it as adjusting the "brain size" of the model.
Low temperature (such as 0.1):
The model will become very conservative and tend to choose the most certain and safest answers.
Suitable for tasks that require accuracy and consistency, such as answering factual questions.
Example: When asking "What is 1+1 equal?", the model will answer "2" and will not make up.
High temperature (such as 1.0 or higher):
The model will become more creative and may give some unexpected answers.
Suitable for tasks that require creativity or diversity, such as writing stories and generating poetry.
Example: Ask “Write a poem about autumn” and the model may generate some imaginative sentences.
Moderate temperature (such as 0.7):
The model will find a balance between conservatism and creativity.
Suitable for most tasks, it can not only ensure certain accuracy but also make some changes.
Summarize:
The temperature is low → The model is like "academic master", with rigorous answers but may be boring.
High temperature → The model is like "artist", and the answer is interesting but may not be reliable.
Moderate temperature → The model is like "smart people", and the answers are both reliable and interesting.
Max tokens
token is the basic unit of the model's processing of text and can be understood as a word or part of a word.
for example:
The English word "hello" is a token.
The Chinese "Hello" may be two tokens (one token for each word).
Long words or complex characters may be broken into multiple tokens.
Maximum number of tokensIt is to limit the length of text generated by the model. Think of it as giving the model a "word limit".
Set the smaller maximum number of tokens (such as 50):
The text generated by the model will be very short and may only answer the core part of the question.
Example: Ask "Introduce the solar system", the model may only answer "the solar system includes the sun and the eight planets."
Set a larger maximum number of tokens (such as 500):
The text generated by the model will be longer and may contain more details.
Example: Ask “Introduce the solar system” and the model may describe the characteristics of each planet in detail.
Not setting the maximum number of tokens:
The model may continue to generate text until it reaches its internal limit (usually several thousand tokens).
This can cause the generated text to be too long or even off topic.
Summarize:
The maximum token number is small → The model is like "the person who talks less", and the answer is short.
The maximum token number is large → The model is like "people who talk a lot", with detailed answers.
If you don't set the maximum number of tokens → The model is like a "talks" and you may keep talking.
Step 5: Create a Controller
@RestController @RequestMapping("/deepseek/chat") public class DeepSeekController { private final DeepSeekService deepSeekService; public DeepSeekController(DeepSeekService deepSeekService) { = deepSeekService; } @PostMapping public ResponseEntity<?> chat(@RequestBody Map<String, String> request) { try { String response = (("message")); return (("response", response)); } catch (Exception e) { return (HttpStatus.INTERNAL_SERVER_ERROR) .body(("error", ())); } } }
Method 2: Based on spring-ai-openai
The steps are basically the same, here are a few implementation codes
rely:
<properties> <>17</> <>1.0.0-M5</> </properties> <dependency> <groupId></groupId> <artifactId>spring-ai-openai-spring-boot-starter</artifactId> </dependency> <dependencyManagement> <dependencies> <dependency> <groupId></groupId> <artifactId>spring-ai-bom</artifactId> <version>${}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
DeepSeekConfig:
import ; import ; import ; import ; @Configuration public class DeepSeekConfig { @Bean public ChatClient chatClient(OpenAiChatModel openAiChatModel) { return (openAiChatModel).build(); } }
DeepSeekController:
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; @RestController @RequestMapping("/api") public class ChatController { @Resource private OpenAiChatModel chatModel; private final List<Message> chatHistoryList = new ArrayList<>(); @PostConstruct public void init() { (new SystemMessage("You are a helpful assistant.")); } @PostMapping("/chat") public ChatResponse test(@RequestBody String message) { (new UserMessage(message)); Prompt prompt = new Prompt(chatHistoryList); ChatResponse chatResponse = (prompt); if (() != null && ().getOutput() != null) { (().getOutput()); } return chatResponse; } }
This is the end of this article about the implementation of SpringBoot access to DeepSeek. For more related content on SpringBoot access to DeepSeek, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!