Spring AI DeepSeek COT Example
In recent years, artificial intelligence (AI) has become one of the most powerful tools for automating tasks and enhancing user experiences. One of the most popular applications of AI is chatbots, which are used to simulate human-like conversations and assist with various tasks. Whether it’s customer support, information retrieval, or automated interaction, chatbots can make a significant impact. Let us delve into understanding how Spring AI integrates with DeepSeek COT to build powerful AI-driven applications.
1. Overview of Spring AI and Deepseek?
1.1 What is Spring AI?
Spring AI is part of the broader Spring ecosystem, which has been a go-to platform for enterprise Java applications for years. With the rise of machine learning (ML) and AI, Spring AI was developed to make it easier to integrate machine learning models into Spring Boot applications. Spring AI is designed to handle the complexities of AI/ML workflows while keeping things simple for developers familiar with the Spring framework.
Unlike other frameworks that require extensive configurations or complex pipelines, Spring AI provides a straightforward approach to deploying models into production environments. It supports integration with popular machine-learning tools and services, allowing you to work with models directly from your Spring Boot application.
Spring AI also offers seamless support for deploying, serving, and interacting with AI models. The Spring Boot framework itself is known for its ease of use and speed, and with Spring AI, you can integrate AI capabilities quickly and efficiently.
1.1.1 Key features of Spring AI
- Seamless Integration with Spring Boot: Spring AI allows for smooth integration with existing Spring Boot applications, reducing the time and effort required to add machine learning functionalities.
- Model Deployment and Serving: You can deploy pre-trained models, serve them through APIs, and integrate them into your application with minimal code.
- Support for Popular ML Frameworks: Spring AI supports integration with popular machine learning frameworks such as TensorFlow, PyTorch, and more.
- Real-time Inference: Spring AI makes it easy to perform real-time inference for use cases like chatbots, personalized recommendations, and more.
1.2 What is DeepSeek?
DeepSeek is an AI-powered platform designed for natural language processing (NLP) and conversational AI. It provides pre-trained models capable of handling complex tasks like text generation, sentiment analysis, question answering, and chatbot interactions. DeepSeek has made it easier for developers to integrate sophisticated AI models into applications without the need for deep expertise in machine learning.
DeepSeek’s models are trained on vast datasets, enabling them to generate accurate responses to user input. This makes DeepSeek an excellent choice for building chatbots and virtual assistants. The platform offers APIs that allow developers to interact with these models programmatically, making integration into web and mobile applications seamless.
Before we proceed with integrating DeepSeek into a Spring Boot application, you’ll need to generate an API key from DeepSeek. Here’s how you can do that:
1.1.1 How to Generate a DeepSeek API Key?
- Visit DeepSeek’s Website: Navigate to DeepSeek.com and sign up for an account if you don’t already have one. If you do, simply log in.
- Navigate to the API Section: After logging in, go to the ‘API’ section of your account dashboard. This is where you’ll be able to generate an API key.
- Generate the API Key: Click on the button that says ‘Generate API Key.’ A new key will be created that you can use to authenticate your requests to DeepSeek’s API.
- Copy and Store Your API Key: Once generated, copy the API key. It is crucial to store it securely, as it will be used in your application to access the models.
Once you have the API key, you’ll be able to use it to interact with DeepSeek’s models, sending them text input and receiving model-generated responses.
2. Code Example
Now that we have the necessary background information on Spring AI and DeepSeek, it’s time to dive into the implementation. Below is a step-by-step guide to building the chatbot application using Spring Boot and the DeepSeek API.
2.1 Setting Up Spring Boot
The first step in building your Spring Boot application is to set up the project. We’ll start by adding the necessary dependencies to the pom.xml file. If you haven’t created a Spring Boot project yet, you can do so via Spring Initializr or manually in your IDE.
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> </dependencies>
2.2 Setting Up application.properties
To keep our code clean, secure, and flexible, it’s a good practice to move configuration parameters, like the DeepSeek API Key and API endpoint URL, into the application.properties file. This file is located in the src/main/resources/ directory of your Spring Boot project.
# DeepSeek API configuration deepseek.api.url=https://api.deepseek.com/chatbot deepseek.api.key=your-deepseek-api-key-here # Server configuration (optional) server.port=8080
2.3 Create the Service Layer
In this step, we will create a service class that will handle communication with the DeepSeek API. The service class will contain the logic to make HTTP requests to the DeepSeek API, send user input, and process the response.
@Service
public class ChatbotService {
@Value("${deepseek.api.url}")
private String apiUrl;
@Value("${deepseek.api.key}")
private String apiKey;
@Autowired
private RestTemplate restTemplate;
public String getResponseFromDeepSeek(String userMessage) {
// Create request body
Map<String, String> requestBody = new HashMap<>();
requestBody.put("model", "deepseek-chat");
requestBody.put("message", userMessage);
requestBody.put("api_key", apiKey);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(requestBody, headers);
// Send POST request to DeepSeek API
ResponseEntity<String> response = restTemplate.exchange(
apiUrl, HttpMethod.POST, requestEntity, String.class);
return response.getBody();
}
}
2.3.1 Code Explanation
The ChatbotService class is a Spring service component that communicates with the DeepSeek AI API. It uses the @Value annotation to inject the DeepSeek API URL and API Key from the application.properties file, promoting clean and configurable code. The class autowires a RestTemplate to perform HTTP operations. Inside the getResponseFromDeepSeek method, it constructs a request body as a map containing the user’s message and the API key, sets the appropriate HTTP headers with Content-Type: application/json, and then creates an HttpEntity that wraps both the body and headers. It sends a POST request to the DeepSeek API using the RestTemplate.exchange() method, passing the API URL, HTTP method, request entity, and expected response type. Finally, it returns the body of the API response, which typically contains the chatbot’s generated reply. This design cleanly separates configuration from logic, handles HTTP communication robustly, and follows Spring Boot best practices.
2.4 Controller to Handle User Input
Next, we’ll create a REST controller to handle user input. This controller will accept user messages, pass them to the service layer, and return the chatbot’s response.
@RestController
@RequestMapping("/chat")
public class ChatController {
@Autowired
private ChatbotService chatbotService;
@PostMapping("/sendMessage")
public String chat(@RequestBody String userMessage) {
return chatbotService.getResponseFromDeepSeek(userMessage);
}
}
2.4.1 Code Explanation
The ChatController class is a Spring REST controller that handles incoming HTTP requests for the chatbot application. It is annotated with @RestController to indicate that it will handle RESTful web services and with @RequestMapping("/chat") to prefix all its endpoints with “/chat”. The controller injects an instance of ChatbotService using @Autowired for accessing the chatbot logic. The chat() method is mapped to a POST endpoint “/sendMessage” using @PostMapping. It accepts the user’s message as the request body, passes this message to the getResponseFromDeepSeek() method of the service layer, and returns the chatbot’s response directly as a plain String. This structure cleanly separates the controller from the service logic, making the code modular, readable, and maintainable.
2.5 Running the Application
Once you have completed the above steps, you can run your Spring Boot application. You can test it by sending POST requests to the /chat/sendMessage endpoint. Below is an example of how to interact with the API using Postman or any other HTTP client:
POST http://localhost:8080/chat/sendMessage
Content-Type: application/json
Body:
{
"message": "Hello, how are you?"
}
Upon sending this request, you will receive a response generated by the DeepSeek AI model, such as:
{
"response": "Hello! I'm an AI chatbot. How can I assist you today?"
}
3. Conclusion
Building an AI chatbot using DeepSeek models integrated with Spring AI provides a powerful and flexible solution for automating interactions and improving user engagement. By leveraging the simplicity of Spring Boot and the advanced capabilities of DeepSeek, we can create a conversational agent that understands and responds to user input in a human-like manner.
AI chatbots are a critical component of modern digital platforms, and with tools like Spring AI and DeepSeek, building and deploying them has never been easier.




