import org.springframework.ai.ollama.OllamaChatModel; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class AiController private final OllamaChatModel chatModel; public AiController(OllamaChatModel chatModel) this.chatModel = chatModel; @GetMapping("/ai/generate") public String generate(@RequestParam(value = "message") String message) return chatModel.call(message); Use code with caution.
– For a first Java test, a small but capable model like qwen2.5:0.5b or llama3:8b works well:
An overview of Ollama and Java integration, focusing on how developers can run large language models (LLMs) locally within the Java ecosystem, followed by an architectural breakdown and code implementation examples. ollamac java work
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-ollama-spring-boot-starter</artifactId> <version>1.0.0-M6</version> </dependency>
ollama pull llama3:8b ollama serve
For developers building Spring Boot microservices, is the natural choice. It provides a model-agnostic ChatClient and ChatModel API, allowing you to swap out different LLM providers (e.g., Ollama, OpenAI, or Hugging Face) with a simple configuration change. This is invaluable for enterprise applications that value flexibility and decoupling.
(For Windows, a graphical installer is available.) import org
To work with , you generally use one of several community-driven libraries or higher-level frameworks like