Monday, 30 June 2025

Build a "Hello World" Chatbot using Spring Boot, Langchain4j, and Ollama (LLaMA3.2)

In this tutorial, we’ll build a simple Hello World chatbot using Spring Boot, Langchain4j, and Ollama. This chatbot uses the LLaMA 3.2 model running locally with Ollama, making it a great choice for developers exploring open-source LLMs in a lightweight environment.

 

We will:

·      Set up a Spring Boot project with Langchain4j

·      Use Ollama to run the LLaMA 3.2 model locally

·      Build an endpoint that receives user input and returns the model’s response

 

Prerequisites

·      JDK 17+

·      Maven

·      Setup Ollama and run the model llama3.2 (ollama run llama3.2). Refer this post for ollama setup (https://self-learning-java-tutorial.blogspot.com/2025/03/how-to-install-ollama-on-your-computer.html)

·      Basic knowledge of Spring Boot

 

LangChain4j offers Spring Boot starters that streamline the integration of popular AI services and simplify the declaration of AI-powered components using familiar Spring Boot conventions. These starters enable seamless configuration and initialization of core LangChain4j components such as:

 

·      Language models

·      Embedding models

·      Embedding stores

·      Other essential services

 

All of this can be configured via standard Spring Boot properties, reducing the need for manual setup and boilerplate code.

 

How to Use LangChain4j Spring Boot Starters?

To get started with any integration, simply add the corresponding dependency to your project. The naming convention for the Spring Boot starter artifacts follows this pattern.

 

langchain4j-{integration-name}-spring-boot-starter

 

Example: Using the Ollama Integration

To use Ollama with LangChain4j in your Spring Boot application, include the following dependency in your pom.xml. 

<dependency>
  <groupId>dev.langchain4j</groupId>
  <artifactId>langchain4j-ollama-spring-boot-starter</artifactId>
</dependency>

After including the dependency, you can define the language model configuration using Spring annotations, like below.  

 

@Configuration
public class LangchainConfig {

  @Bean
  public OllamaLanguageModel ollamaLanguageModel() {
    return OllamaLanguageModel.builder()
        .baseUrl("http://localhost:11434")
        .modelName("llama3.2")
        .httpClientBuilder(new SpringRestClientBuilderFactory().create()) // Uses Spring’s HTTP client
        .build();
  }
}

This configuration sets up an OllamaLanguageModel bean that connects to a local Ollama server instance using the specified model.

 

Autowiring LangChain4j Components

Once configured, LangChain4j automatically registers the corresponding beans (such as ChatModel implementations like OpenAiChatModel or OllamaLanguageModel). You can then inject these components into your services using Spring's @Autowired annotation:

@Autowired
private ChatModel chatModel;

This approach promotes clean separation of configuration and usage, making it easy to build scalable and maintainable AI-powered applications in the Spring Boot ecosystem.

 

Follow step-by-step procedure to build a complete working application.

 

Step 1: Create new maven project ‘langchain-spring-hello-world’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sample.app</groupId>
  <artifactId>langchain-spring-hello-world</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <maven.compiler.source>21</maven.compiler.source>
    <maven.compiler.target>21</maven.compiler.target>
    <java.version>21</java.version>
  </properties>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.3.10</version>
  </parent>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-bom</artifactId>
        <version>1.0.1</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>dev.langchain4j</groupId>
      <artifactId>langchain4j-ollama-spring-boot-starter</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
      <version>2.6.0</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal> <!-- Important -->
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

Step 3: Create a package com.sample.app.dto and define ChatResponse class.

 

ChatResponse.java

package com.sample.app.dto;

public class ChatResponse {
    private String message;

    public ChatResponse() {
    }

    public ChatResponse(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

 

Step 4: Create a package com.sample.app.config and define LangchainConfig and SwaggerConfig classes.

 

LangchainConfig.java

 

package com.sample.app.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import dev.langchain4j.http.client.spring.restclient.SpringRestClientBuilderFactory;
import dev.langchain4j.model.ollama.OllamaLanguageModel;

@Configuration
public class LangchainConfig {

    @Bean
    public OllamaLanguageModel ollamaLanguageModel() {
        return OllamaLanguageModel.builder().baseUrl("http://localhost:11434").modelName("llama3.2")
                .httpClientBuilder(new SpringRestClientBuilderFactory().create()) // explicitly use Spring's HTTP client
                .build();
    }
}

SwaggerConfig.java

package com.sample.app.config;

import org.springframework.context.annotation.Configuration;

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;

@Configuration
@OpenAPIDefinition(info = @Info(title = "Chat service Application", version = "v1"))
public class SwaggerConfig {

}

Step 5: Create com.sample.app.service package and define ChatService class.

 

ChatService.java

 

package com.sample.app.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sample.app.dto.ChatResponse;

import dev.langchain4j.model.language.LanguageModel;

@Service
public class ChatService {

  @Autowired
    private LanguageModel model;

    public ChatResponse chat(String userMessage) {
        String responseMessage = model.generate(userMessage).content();
        return new ChatResponse(responseMessage);
    }
}

 

Step 6: Create com.sample.app.controller package and define ChatController class.

 

ChatController.java

package com.sample.app.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sample.app.dto.ChatResponse;
import com.sample.app.service.ChatService;

import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;

@RestController
@RequestMapping("/api/chat")
@CrossOrigin("*")
@Tag(name = "Chat Controller", description = "This section contains APIs related to Chat APIs Powered by Ollama")
public class ChatController {

  @Autowired
  private ChatService chatService;

  @PostMapping
  public ChatResponse chat(@RequestBody @Valid ChatRequestBody chatRequestBody) {
    return chatService.chat(chatRequestBody.getMessage());
  }

  private static class ChatRequestBody {
    @NotEmpty
    private String message;

    public String getMessage() {
      return message;
    }

    public void setMessage(String message) {
      this.message = message;
    }

  }
}

Step 7: Define App class in com.sample.app package.

 

App.java

package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}

 

Build the project

Navigate to the folder where pom.xml is located, and execute following command to generate an executable.

mvn clean install

 

Upon successful execution of the command, you can see langchain-spring-hello-world-0.0.1-SNAPSHOT.jar file in target folder.

 

Run the Application

Execute following command to run the application.

java -jar ./target/langchain-spring-hello-world-0.0.1-SNAPSHOT.jar --server.port=9092

Open the url ‘http://localhost:9092/swagger-ui/index.html’ in browser to interact with the application.

 

Execute the api /api/chat with the following payload.

{
  "message": "Tell me a Joke"
}

You will receive the following response.

{
  "message": "Here's one:\n\nWhat do you call a fake noodle?\n\n(wait for it...)\n\nAn impasta!\n\nHope that made you laugh! Do you want to hear another one?"
}

 

You can download the application from this link.


 

References

https://docs.langchain4j.dev/tutorials/spring-boot-integration/

 

Previous                                                    Next                                                    Home

Simplify LLM Integration with LangChain4j AI Services

Until now in this tutorial series, we've focused on low-level components such as ChatModel, ChatMessage, and ChatMemory. While this gives you fine-grained control and maximum flexibility, it also leads to writing a lot of boilerplate code. Building LLM-powered applications typically requires coordinating several components like prompt templates, memory management, LLMs, output parsers, and RAG components (embedding models and stores) and often involves multiple steps or interactions. Managing all this complexity at a low level can quickly become tedious.

 

To help you concentrate on what truly matters, your business logic, not the low-level mechanics AI Services are here to help.

 

1. What Are AI Services?

AI Services abstract away the complexities of working with LLMs and related components by offering a simplified, declarative API.

 

Think of them as similar in concept to Spring Data JPA or Retrofit, you define an interface for your desired functionality, and LangChain4j provides a proxy implementation behind the scenes. In essence, AI Services are part of your application's service layer they deliver AI capabilities with minimal setup.

 

Following are the Key Capabilities of AI Services:

·      Input formatting for LLMs

·      Output parsing from LLMs

·      Support for advanced features like:

o   Chat memory

o   Tool integration

o   Retrieval-Augmented Generation (RAG)

 

AI Services are well-suited for building both stateful chatbots with multi-turn interactions and stateless automation tasks where each LLM call is independent.

 

2. Your First and Simplest AI Service

Let’s walk through creating a basic AI Service using LangChain4j. This is the most minimal setup to get started, perfect for quick experimentation or learning.

 

2.1. Define the AI Service Interface

Start by declaring a plain Java interface that describes the interaction you want with your AI. In this example, the interface has a single method chat, which accepts a String as input and returns a String as the response. 

public interface MyAssistant {
    String chat(String message);
}

LangChain4j will generate the implementation of this interface for you behind the scenes.

 

2.2. Configure the Chat Model

Next, configure a low-level component, specifically, a ChatModel. This model will power your AI Service and handle communication with the underlying LLM (in this case, a model served via Ollama).

 

ChatModel model = OllamaChatModel.builder()
    .baseUrl("http://localhost:11434")   // The local endpoint where Ollama is running
    .modelName("llama3.2")               // The model name you want to use
    .build();

 

This sets up everything the service needs to communicate with your LLM.

 

2.3 Create the AI Service Instance

Now, with your interface and model ready, you can create the AI service using AiServices.create(...). This dynamically generates a proxy implementation of your interface.

 

MyAssistant assistant = AiServices.create(MyAssistant.class, model);

2.4 Use the Assistant

Once the assistant is created, you can use it just like any other Java object. Here's an example of sending a message and printing the AI’s response.

 

String response = assistant.chat("Hi, tell me about LLMs in 30 words maximum");
System.out.println(response);

This simple setup lets you start building AI-powered features without worrying about the low-level plumbing. You get a clean, declarative interface and LangChain4j handles the rest.

 

Find the below working application.

 

MyAssistant.java

 

package com.sample.app.aiservices;

public interface MyAssistant {
    String chat(String message);
}

 

AiServiceHelloWorld.java

package com.sample.app.aiservices;

import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.ollama.OllamaChatModel;
import dev.langchain4j.service.AiServices;

public class AiServiceHelloWorld {
    public static void main(String[] args) {
        ChatModel model = OllamaChatModel.builder().baseUrl("http://localhost:11434").modelName("llama3.2").build();

        MyAssistant assistant = AiServices.create(MyAssistant.class, model);
        String response = assistant.chat("Hi, tell me about LLMs in 30 words maximum");
        System.out.println(response);

    }
}

 

Output

Large Language Models (LLMs) are AI algorithms that process and generate human-like language based on vast amounts of training data. They're trained to predict the next word or character.

How Does It Work?

In the previous example, we passed the interface class (MyAssistant.class) along with a configured low-level component (ChatModel) to the AiServices.create(...) method. Behind the scenes, LangChain4j uses this information to generate a proxy object that implements the provided interface. Currently, this proxy is created using Java reflection.

 

This proxy handles all necessary input and output conversions automatically.

 

·      Your interface method (chat(String message)) accepts a simple String.

·      However, under the hood, the ChatModel expects input in the form of a ChatMessage.

·      The proxy takes care of this mismatch by wrapping the input string into a UserMessage and then passing it to the model.

·      The model processes the request and returns an AiMessage.

·      Since the return type of the method is declared as String, the proxy converts the AiMessage content back into a String before returning it to the caller.

 

This seamless conversion makes it possible to interact with complex LLM models using simple Java method calls abstracting away the complexity and making development faster and cleaner.

 

Previous                                                    Next                                                    Home