Integrating Ollama with DeepSeek-R1 in Spring Boot
Are you looking to leverage the power of Ollama and DeepSeek-R1 in your Spring Boot application? This post will walk you through the entire process, from understanding what Ollama is to implementing a seamless integration. Let’s get started!
What is Ollama?
Ollama is a powerful tool designed to simplify the deployment and management of large language models (LLMs) locally. It provides an easy-to-use API for interacting with models like DeepSeek-R1, making it an excellent choice for developers who want to integrate AI capabilities into their applications without relying on external cloud services.
With Ollama, you can:
Why Integrate Ollama with DeepSeek-R1?
DeepSeek-R1 is a state-of-the-art language model that offers high performance and flexibility. By integrating it with Ollama in your Spring Boot application, you can:
Step 1: Install Ollama
To get started, you’ll need to install Ollama on your system. Run the following command in your terminal:
curl -fsSL https://ollama.com/install.sh | sh
Successful Installation Output:
>>> Cleaning up old version
>>> Installing ollama to /usr/local
>>> Downloading Linux amd64 bundle
>>> Creating ollama user...
>>> Adding ollama user to groups...
>>> Creating ollama systemd service...
Created symlink /etc/systemd/system/default.target.wants/ollama.service
>>> Nvidia GPU detected
>>> API available at 127.0.0.1:11434
Once installed, Ollama will be ready to use, and the API will be available at https://localhost:11434.
Step 2: Application Configuration
Next, configure your Spring Boot application by updating the application.properties file:
spring:
application:
name: demo-deepseek-r1.ollama
# Server configuration
server:
port: 8080
error:
include-message: always
# Ollama configuration
ollama:
endpoint: https://localhost:11434/api/generate
model: deepseek-r1:1.5b
timeout:
connect: 30000
read: 60000
This configuration sets up the Ollama endpoint, model, and timeout settings for your application.
领英推荐
Step 3: Core Implementation
Records
Create the following records to handle requests and responses:
// OllamaRequest.java
@JsonInclude(JsonInclude.Include.NON_NULL)
public record OllamaRequest(
String model,
String prompt,
boolean stream
) {}
// OllamaResponse.java
@JsonIgnoreProperties(ignoreUnknown = true)
public record OllamaResponse(
String model,
String response,
String created_at,
boolean done
) {}
Service Layer
Implement the OllamaService to interact with the Ollama API:
@Service
public class OllamaService {
private static final String OLLAMA_API_URL = "https://localhost:11434/api/generate";
private final RestTemplate restTemplate;
public OllamaService(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder
.build();
}
public String generateResponse(String prompt) {
try {
OllamaRequest request = new OllamaRequest("deepseek-r1:1.5b", prompt, false);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
ResponseEntity<OllamaResponse> response = restTemplate.exchange(
OLLAMA_API_URL,
HttpMethod.POST,
new HttpEntity<>(request, headers),
OllamaResponse.class
);
if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) {
return response.getBody().response() != null
? response.getBody().response()
: "Received empty response from model";
}
return "Ollama API returned status: " + response.getStatusCode();
} catch (RestClientException e) {
return "Error communicating with Ollama: " + e.getMessage();
}
}
}
REST Controller
Create a REST controller to expose the chat endpoint:
@RestController
@RequestMapping("/api/chat")
public class ChatController {
private final OllamaService ollamaService;
public ChatController(OllamaService ollamaService) {
this.ollamaService = ollamaService;
}
@PostMapping
public ResponseEntity<String> chat(@RequestBody String prompt) {
if (prompt == null || prompt.isBlank()) {
return ResponseEntity.badRequest().body("Prompt cannot be empty");
}
String response = ollamaService.generateResponse(prompt);
return ResponseEntity.ok(response);
}
}
Model Version Compatibility
Here’s a quick reference for DeepSeek-R1 model versions and their requirements:
*Check official model availability at:
Testing the Integration
To test the integration, use the following curl command or postman:
curl -X POST -H "Content-Type: text/plain" -d "Explain AI in simple terms" https://localhost:8080/api/chat
Ouput
Source Code
Here on GitHub.
CEO en Elianai Systems
1 个月tambien de tomar en cuenta
CEO en Elianai Systems
1 个月Para los usuarios de docker el archivo compose.yml: services: ?webui: ???image: ghcr.io/open-webui/open-webui:main ???ports: ?????- 3000:8080/tcp ???volumes: ?????- open-webui:/app/backend/data ???extra_hosts: ?????- "host.docker.internal:host-gateway" ???depends_on: ?????- ollama ?ollama: ???image: ollama/ollama ???expose: ?????- 11434/tcp ???ports: ?????- 11434:11434/tcp ???healthcheck: ?????test: ollama --version || exit 1 ???volumes: ?????- ollama:/root/.ollama volumes: ?ollama: ?open-webui: ----------------- para usuarios docker y ejecutar el modelo que se desee: docker compose exec ollama ollama pull deepseek-r1:1.5b --- saludos amigo y gracias por la info.