A simple Java HTTP client for interacting with the DeepSeek API using Java 21 and Jackson for JSON processing. Written by Jason Bell as a quick demo as I don't use Python for everything.
- Java 21 (LTS version)
- Maven 3.8+
- DeepSeek API Key - Get yours at https://platform.deepseek.com/
cd /path/to/dsclientExport your DeepSeek API key as an environment variable:
export DEEPSEEK_API_KEY=your-api-key-hereTo make it permanent, add it to your ~/.zshrc or ~/.bash_profile:
echo 'export DEEPSEEK_API_KEY=your-api-key-here' >> ~/.zshrc
source ~/.zshrcmvn clean compilemvn exec:java -Dexec.mainClass="com.jasonbelldata.deepseek.Main"Expected output:
Sending request to DeepSeek API...
=== Response ===
ID: 99e4fd20-d1bf-47bb-bbe4-997188a0c918
Model: deepseek-chat
Assistant's reply:
The capital of France is **Paris**.
=== Token Usage ===
Prompt tokens: 17
Completion tokens: 8
Total tokens: 25
import com.jasonbelldata.deepseek.DeepSeekClient;
import com.jasonbelldata.deepseek.RequestModel;
import com.jasonbelldata.deepseek.ResponseModel;
import java.util.List;
public class MyApp {
public static void main(String[] args) {
try {
// Get API key from environment
String apiKey = System.getenv("DEEPSEEK_API_KEY");
// Create client
DeepSeekClient client = new DeepSeekClient(apiKey);
// Create request
RequestModel request = new RequestModel(
"deepseek-chat",
List.of(
new RequestModel.Message("system", "You are a helpful assistant."),
new RequestModel.Message("user", "What is Java 21?")
),
false // stream = false
);
// Send request
ResponseModel response = client.sendRequest(request);
// Get the response content
String answer = response.getChoices().get(0).getMessage().getContent();
System.out.println(answer);
} catch (Exception e) {
e.printStackTrace();
}
}
}DeepSeekClient client = new DeepSeekClient(apiKey);
RequestModel request = new RequestModel(
"deepseek-chat",
List.of(new RequestModel.Message("user", "Explain quantum computing in one sentence")),
false
);
ResponseModel response = client.sendRequest(request);
String answer = response.getChoices().get(0).getMessage().getContent();
System.out.println(answer);DeepSeekClient client = new DeepSeekClient(apiKey);
RequestModel request = new RequestModel(
"deepseek-chat",
List.of(
new RequestModel.Message("system", "You are a helpful coding assistant."),
new RequestModel.Message("user", "How do I sort a list in Java?"),
new RequestModel.Message("assistant", "You can use Collections.sort() or the stream API."),
new RequestModel.Message("user", "Show me an example with streams")
),
false
);
ResponseModel response = client.sendRequest(request);
String answer = response.getChoices().get(0).getMessage().getContent();
System.out.println(answer);public class DeepSeekClient {
public DeepSeekClient(String apiKey)
public ResponseModel sendRequest(RequestModel request) throws Exception
}public class RequestModel {
public RequestModel(String model, List<Message> messages, boolean stream)
public static class Message {
public Message(String role, String content)
// role: "system", "user", or "assistant"
}
}public class ResponseModel {
public String getId()
public String getModel()
public List<Choice> getChoices()
public Usage getUsage()
public static class Choice {
public Message getMessage()
public String getFinishReason()
}
public static class Usage {
public int getPromptTokens()
public int getCompletionTokens()
public int getTotalTokens()
}
}To create an executable JAR:
mvn clean package- Jackson Databind 2.16.1 - JSON processing
- Java 21 HttpClient - Built-in HTTP/2 client
Authentication Fails, Your api key: ****xxxx is invalid
Solution: Verify your API key is correct and properly set in the environment variable.
echo $DEEPSEEK_API_KEY # Should print your keySolution: Already handled with @JsonIgnoreProperties(ignoreUnknown = true) on all model classes.
The client is configured with:
- 30 second connection timeout
- 2 minute request timeout
Adjust in DeepSeekClient.java if needed.
This is a sample project for educational purposes. Please feel to use and modify for your own purposes. If you wish to mention the project then please refer back to this githut repo.