Before diving into the code, ensure you have the following installed:
In your application.properties or application.yml , configure the model: properties
import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; public class NativeOllamaClient public static void main(String[] args) throws Exception HttpClient client = HttpClient.newHttpClient(); // JSON payload targeting the local model String jsonPayload = """ "model": "llama3", "prompt": "Why is Java great?", "stream": false """; HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("http://localhost:11434/api/generate")) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(jsonPayload)) .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println("Status Code: " + response.statusCode()); System.out.println("Response Body: " + response.body()); Use code with caution. Best Practices for Java-Ollama Development ollamac java work
To facilitate this communication, the Java community has developed several libraries, most notably ollama4j . This open-source wrapper acts as a client SDK, abstracting away the raw HTTP connection details and JSON parsing. For a Java developer, this is where the "work" truly begins. In a standard implementation, a developer initializes the OllamaAPI client, points it to the local host, and specifies the model name. The complexity of managing tokens and handling model context is reduced to method calls that return Java objects. This allows developers to focus on business logic rather than networking intricacies. For instance, a Spring Boot application can easily inject an Ollama client service, transforming a standard web server into an AI-powered backend capable of text summarization, code generation, or semantic search.
curl http://localhost:11434/api/generate -d ' "model": "llama3.2:3b", "prompt": "Say hello in Java code" ' Before diving into the code, ensure you have
When building an enterprise-grade AI application in Java, you generally rely on one of three prominent frameworks to handle the Ollama integration. 1. LangChain4j
To use Ollama with Java, you can either use specialized frameworks like and LangChain4j or connect directly to its REST API using client libraries like Ollama4j . 🛠️ Main Java Integrations For a Java developer, this is where the "work" truly begins
RAG is a technique to provide an LLM with relevant context from your own documents, vastly improving the accuracy and relevance of its answers. A typical RAG pipeline involves:
: Download a model (e.g., llama3 or mistral ) via the Ollamac app interface.