You are currently viewing Building a Spring Boot Application and Integrating with Ollama API

Building a Spring Boot Application and Integrating with Ollama API

Introduction

Spring Boot is a popular framework for building Java-based web applications. It simplifies the development process by providing default configurations and a variety of tools for creating stand-alone, production-grade applications. Ollama is a hypothetical API service for machine learning tasks (e.g., text generation, image processing, etc.). In this tutorial, we will build a Spring Boot application and integrate it with the Ollama API to demonstrate how you can leverage external APIs within a Spring Boot project.

Prerequisites

  • JDK 11 or later installed
  • Maven or Gradle installed
  • Basic knowledge of Java and Spring Boot
  • An account with access to the Ollama API (assuming it requires authentication)

Step 1: Setting Up the Spring Boot Project

  1. Create a new Spring Boot project using Spring Initializr: Go to Spring Initializr and create a project with the following specifications:
  • Project: Maven Project
  • Language: Java
  • Spring Boot: 3.0.0 or later
  • Dependencies: Spring Web, Spring Boot DevTools, Lombok Alternatively, you can use the following command to generate a project:
   curl https://start.spring.io/starter.zip -d dependencies=web,devtools,lombok -d type=maven-project -d language=java -d bootVersion=3.0.0 -d baseDir=spring-boot-ollama -o spring-boot-ollama.zip
   unzip spring-boot-ollama.zip
   cd spring-boot-ollama
  1. Open the project in your favorite IDE (e.g., IntelliJ IDEA, Eclipse).

Step 2: Define the Ollama API Integration

  1. Create a configuration file for Ollama API: In src/main/resources, create a file named application.properties and add the following configuration:
   ollama.api.base-url=https://api.ollama.com
   ollama.api.key=your_ollama_api_key
  1. Create a model class for the API request and response: In src/main/java/com/example/demo/model, create OllamaRequest.java and OllamaResponse.java:
   // OllamaRequest.java
   package com.example.demo.model;

   import lombok.AllArgsConstructor;
   import lombok.Data;
   import lombok.NoArgsConstructor;

   @Data
   @AllArgsConstructor
   @NoArgsConstructor
   public class OllamaRequest {
       private String inputText;
   }
   // OllamaResponse.java
   package com.example.demo.model;

   import lombok.Data;

   @Data
   public class OllamaResponse {
       private String outputText;
   }
  1. Create a service class to handle the API interaction: In src/main/java/com/example/demo/service, create OllamaService.java:
   // OllamaService.java
   package com.example.demo.service;

   import com.example.demo.model.OllamaRequest;
   import com.example.demo.model.OllamaResponse;
   import org.springframework.beans.factory.annotation.Value;
   import org.springframework.stereotype.Service;
   import org.springframework.web.reactive.function.client.WebClient;
   import reactor.core.publisher.Mono;

   @Service
   public class OllamaService {

       private final WebClient webClient;

       public OllamaService(WebClient.Builder webClientBuilder, 
                            @Value("${ollama.api.base-url}") String baseUrl,
                            @Value("${ollama.api.key}") String apiKey) {
           this.webClient = webClientBuilder
                   .baseUrl(baseUrl)
                   .defaultHeader("Authorization", "Bearer " + apiKey)
                   .build();
       }

       public Mono<OllamaResponse> processText(OllamaRequest request) {
           return this.webClient.post()
                   .uri("/process-text")
                   .bodyValue(request)
                   .retrieve()
                   .bodyToMono(OllamaResponse.class);
       }
   }

Step 3: Create a Controller to Expose the API

  1. Create a REST controller: In src/main/java/com/example/demo/controller, create OllamaController.java:
   // OllamaController.java
   package com.example.demo.controller;

   import com.example.demo.model.OllamaRequest;
   import com.example.demo.model.OllamaResponse;
   import com.example.demo.service.OllamaService;
   import org.springframework.beans.factory.annotation.Autowired;
   import org.springframework.web.bind.annotation.*;
   import reactor.core.publisher.Mono;

   @RestController
   @RequestMapping("/api/ollama")
   public class OllamaController {

       @Autowired
       private OllamaService ollamaService;

       @PostMapping("/process")
       public Mono<OllamaResponse> processText(@RequestBody OllamaRequest request) {
           return ollamaService.processText(request);
       }
   }

Step 4: Run the Application

  1. Build and run the application:
   mvn spring-boot:run
  1. Test the endpoint: Use a tool like curl or Postman to send a POST request to the /api/ollama/process endpoint. Example curl command:
   curl -X POST http://localhost:8080/api/ollama/process \
        -H "Content-Type: application/json" \
        -d '{"inputText": "Hello, Ollama!"}'

Conclusion

You’ve now set up a basic Spring Boot application that integrates with the Ollama API. This tutorial covers the essentials: configuring an external API, creating service and controller layers, and running the application. From here, you can expand the functionality, add error handling, and integrate with other services as needed.

Leave a Reply