Skip to content

jasebell/deepseek-java-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

DeepSeek Java Client

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.

Prerequisites

Setup

1. Clone or Download the Project

cd /path/to/dsclient

2. Set Your API Key

Export your DeepSeek API key as an environment variable:

export DEEPSEEK_API_KEY=your-api-key-here

To make it permanent, add it to your ~/.zshrc or ~/.bash_profile:

echo 'export DEEPSEEK_API_KEY=your-api-key-here' >> ~/.zshrc
source ~/.zshrc

3. Compile the Project

mvn clean compile

4. Run the Example

mvn 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

Using the Client in Your Own Code

Basic Example

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();
        }
    }
}

Simple Question-Answer Example

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);

Multi-Turn Conversation

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);

API Reference

DeepSeekClient

public class DeepSeekClient {
    public DeepSeekClient(String apiKey)
    public ResponseModel sendRequest(RequestModel request) throws Exception
}

RequestModel

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"
    }
}

ResponseModel

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()
    }
}

Building a JAR

To create an executable JAR:

mvn clean package

Dependencies

  • Jackson Databind 2.16.1 - JSON processing
  • Java 21 HttpClient - Built-in HTTP/2 client

Troubleshooting

Authentication Error

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 key

UnrecognizedPropertyException

Solution: Already handled with @JsonIgnoreProperties(ignoreUnknown = true) on all model classes.

Connection Timeout

The client is configured with:

  • 30 second connection timeout
  • 2 minute request timeout

Adjust in DeepSeekClient.java if needed.

License

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.

Resources

About

SImple client library for the Deepseek AI API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages