In this article, we’ll walk through how to create a Spring Boot application that integrates with the DeepSeek API using an API key. DeepSeek is a powerful AI service that provides capabilities such as predictions, content generation, or other machine learning tasks. By the end of this guide, you’ll have a fully functional Spring Boot application that communicates with the DeepSeek API.
Prerequisites
Before we begin, ensure you have the following:
- Java Development Kit (JDK) 17 or later installed.
- Maven or Gradle for dependency management.
- An API key for DeepSeek. For this tutorial, we’ll use the key:
sk-e48a0caf02fe4820846b70c6423fc9d6. - Basic knowledge of Spring Boot and REST APIs.
Step 1: Set Up the Spring Boot Project
- Create a Spring Boot Project:
- Use Spring Initializr to generate a new project.
- Add the following dependencies:
- Spring Web (for building RESTful APIs)
- Spring Boot DevTools (optional, for development)
- Lombok (optional, for reducing boilerplate code)
- Project Structure:
-
Your project structure should look like this:
src/main/java/com/example/deepseek ├── controller ├── service ├── config └── DeepseekApplication.java
-
Step 2: Configure the API Key
-
Add the API Key to
application.properties:-
Open
src/main/resources/application.propertiesand add your DeepSeek API key:deepseek.api.key=sk-e48a0caf02fe4820846b70c6423fc9d0
-
-
Create a Configuration Class:
-
Create a
DeepSeekConfigclass to load the API key from the properties file.package com.example.deepseek.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; @Configuration public class DeepSeekConfig { @Value("${deepseek.api.key}") private String apiKey; public String getApiKey() { return apiKey; } }
-
Step 3: Create a Service to Interact with the DeepSeek API
- Create a
DeepSeekServiceClass:-
This class will handle HTTP requests to the DeepSeek API.
package com.example.deepseek.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.*; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import com.example.deepseek.config.DeepSeekConfig; @Service public class DeepSeekService { @Autowired private DeepSeekConfig deepSeekConfig; private final RestTemplate restTemplate = new RestTemplate(); public String callDeepSeekApi(String input) { String apiUrl = "<https://api.deepseek.com/v1/predict>"; // Replace with the actual API endpoint HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.set("Authorization", "Bearer " + deepSeekConfig.getApiKey()); // Create the request body (modify according to the API requirements) String requestBody = "{\\\\"input\\\\": \\\\"" + input + "\\\\"}"; HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers); ResponseEntity<String> responseEntity = restTemplate.exchange( apiUrl, HttpMethod.POST, requestEntity, String.class); return responseEntity.getBody(); } }
-
Step 4: Create a Controller to Expose the API
- Create a
DeepSeekControllerClass:-
This class will expose an endpoint to interact with the DeepSeek API.
package com.example.deepseek.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import com.example.deepseek.service.DeepSeekService; @RestController @RequestMapping("/api/deepseek") public class DeepSeekController { @Autowired private DeepSeekService deepSeekService; @PostMapping("/predict") public String predict(@RequestBody String input) { return deepSeekService.callDeepSeekApi(input); } }
-
Step 5: Run and Test the Application
-
Run the Spring Boot Application:
-
Navigate to the root of your project and run:
mvn spring-boot:run
-
-
Test the API:
-
Use a tool like Postman or curl to test the
/api/deepseek/predictendpoint. -
Example request:
curl -X POST -H "Content-Type: application/json" -d '{"input": "Your input here"}' <http://localhost:8080/api/deepseek/predict>
-
Step 6: (Optional) Add Error Handling
- Add Global Exception Handling:
-
Create a
GlobalExceptionHandlerclass to handle exceptions globally.package com.example.deepseek.exception; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ResponseEntity<String> handleException(Exception ex) { return new ResponseEntity<>("An error occurred: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } }
-
Conclusion
In this article, we’ve built a Spring Boot application that integrates with the DeepSeek API using an API key. This application can send requests to the DeepSeek API and retrieve predictions or other responses. You can expand this project by adding more features, such as logging, caching, or additional endpoints, depending on your needs.
By following this guide, you’ve learned how to:
- Set up a Spring Boot project.
- Configure and use an API key.
- Create a service to interact with an external API.
- Expose a RESTful endpoint for clients to use.
Feel free to customize the code and explore more advanced features of Spring Boot and the DeepSeek API. Happy coding!
Next Steps
- Explore DeepSeek API Documentation:
- Check the official DeepSeek API documentation to understand all available endpoints and features.
- Add Unit Tests:
- Write unit tests for your service and controller layers using JUnit and Mockito.
- Deploy the Application:
- Deploy your Spring Boot application to a cloud platform like AWS, Heroku, or Google Cloud.