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
No comments:
Post a Comment