Using Java and DeepSeek to Effectively Call AI Models

Using Java and DeepSeek to Effectively Call AI Models

This article discusses how to use Java to call AI models, especially the recently popular DeepSeek-R1. You may have heard that the DeepSeek model has a long reasoning chain, strong inference capabilities, and is free to use, running quickly on local machines. On the Python side, many people jump straight into commands like <span>ollama pull</span>, <span>chatgpt</span>, and <span>fastapi</span>… which can be overwhelming. Today, I will present something substantial: using Java, we can achieve the same results, integrate it into projects, with stable performance, smooth debugging, and reliable deployment.

How to Run the Model? The Key is to “Invoke” the Effect

DeepSeek is a large model, but it is open-source and supports Ollama, which means it can run locally. While the command line operations on the Python side are indeed convenient, don’t think that Java can’t handle it. Java can also invoke the model via HTTP calls, and it can be done in a more engineered and controllable manner.

Tip: Don’t think that “HTTP requests” are outdated; they are the Swiss Army knife in the hands of backend developers, ready to tackle any challenge.

How to Call the Ollama Model with Java? Use HttpClient Directly

Let’s jump straight to the code:

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class DeepSeekCaller {
    public static void main(String[] args) throws Exception {
        String prompt = "Write a Java implementation of the Fibonacci sequence, with comments";
        String requestPayload = """
            {
              "model": "deepseek-r1:1.5b",
              "messages": [
                {"role": "user", "content": "%s"}
              ],
              "stream": false
            }
        """.formatted(prompt);

        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("http://localhost:11434/api/chat"))
            .header("Content-Type", "application/json")
            .POST(HttpRequest.BodyPublishers.ofString(requestPayload))
            .build();

        HttpClient client = HttpClient.newHttpClient();
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

        System.out.println("Model response:");
        System.out.println(response.body());
    }
}

What does this code do? In short: it sends our question to the DeepSeek model, and after it thinks, it spits out the answer. No beating around the bush, we go straight to the model.

Output Effect: The model will return a JSON containing the information you need. You can directly paste this content into your IDE for analysis, or even return it to the frontend via an interface.

Model Deployment: Simple Yet Complex

To deploy the model locally, we use the Ollama tool. After installation, just one command:

ollama pull deepseek-r1:1.5b

Then you can use your local inference engine. You can run it without a GPU, with a latency just over one second, reminiscent of love.

Common Pitfalls: Remember that Ollama runs by default on port <span>11434</span>, so don’t change it arbitrarily. Also, make sure to write the model name correctly as <span>deepseek-r1:1.5b</span>; it won’t recognize it if you add or omit anything.

How to Integrate the Model into a Java Project? Let’s Create a Simple REST Interface

You might ask: How do I use DeepSeek in my project? Just create a controller to encapsulate the calling logic and easily connect it to the frontend.

@RestController
@RequestMapping("/deepseek")
public class DeepSeekController {

    @PostMapping("/chat")
    public String chatWithModel(@RequestBody String userInput) throws Exception {
        // Call the logic from DeepSeekCaller
        return DeepSeekCaller.callModel(userInput);
    }
}

With just a few lines, you can integrate AI into your project. Unlike Python, where you have to set up Flask, manage environments, and handle CORS, we can do it all in one go.

Tip: Remember to set up cross-origin resource sharing (CORS), or else the frontend will be blocked by the browser when trying to access this interface.

How to Process Model Output? Just Wrap It Up

The model returns a complete JSON, with the data hidden in the <span>"message.content"</span> field. Create an entity class to handle it:

public class ChatResponse {
    private String model;
    private Message message;

    // getters and setters

    public static class Message {
        private String role;
        private String content;
        // getters and setters
    }
}

Then use Jackson or Gson to parse it, and you can immediately access the model output.

ObjectMapper mapper = new ObjectMapper();
ChatResponse chatResponse = mapper.readValue(response.body(), ChatResponse.class);
System.out.println(chatResponse.getMessage().getContent());

Code is Extensible: You can add caching, rate limiting, audit logs, or even token validation on top of this, all with stability.

To Truly Utilize the Model, You Need “Business Capability”

With the model in hand, the question arises: What should the model do?

Here are a few things I have actually done:

  • • Automatically generate test code (directly improving efficiency)
  • • Write comments (making it understandable for low-code colleagues)
  • • Analyze logs (the model helps find keywords)
  • • Smartly complete API documentation (Swagger can take a back seat)

Java is not incapable of AI; rather, we have not realized: the model is a tool, not a magic wand.

If you treat DeepSeek as a co-pilot, it can help you soar; if you expect it to write a complete system, you’re doomed.

My Colleague Uses Python to Call the Model, While I Use Java to Build the System

One of my colleagues initially used Python to call the model, which seemed quick, but in the end:

  • • Debugging via command line, output relying entirely on print statements
  • • Environment changes caused package failures
  • • Attempting to deploy to a server took three days to figure out Docker

As for me using Java?

  • • One-click startup in IDEA
  • • SpringBoot directly connects to the interface
  • • Model output encapsulated into classes, automatically logging
  • • Adding a Redis cache halved the model call frequency
  • • Deployment? The business line directly integrates, testing and going live in one go

Being able to call the model is not a skill; knowing how to “use” the model is what truly matters. Java in AI is not about taking detours, but about following the right path.

What Have We Learned This Time?

  • • Calling the local DeepSeek model with Java is completely feasible
  • • HttpClient is a powerful tool, veterans never die
  • • Integrating the model into a project requires a system; don’t just write it and throw it away
  • • The model is a tool, not the main character; the business is the main character
  • • Python is fast, but Java is stable; especially in engineering, Java’s advantages become apparent

If you also use Java and want to try AI, then DeepSeek is indeed a great starting point. It’s free, can run locally, responds quickly, and can think; when combined with Java, it truly is—stable yet robust, robust yet stable.

Leave a Comment