I am developing an AI-assisted interface using LangChain4j and injecting OllamaChatModel, but I am encountering an issue where the application fails to start due to a NullPointerException related to @AiService.
2025-02-16T21:47:29.387-07:00 WARN 25236 --- [ main] o.s.w.c.s.GenericWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: java.lang.NullPointerException: Cannot invoke "dev.langchain4j.service.spring.AiService.chatModel()" because "aiServiceAnnotation" is null
@Service
public class LlamaService implements Assistant {
private final OllamaChatModel model;
public LlamaService() {
this.model = OllamaChatModel.builder()
.temperature(0.4)
.responseFormat(JSON) // Ensure JSON is correctly imported
.modelName("llama2")
.baseUrl("http://127.0.0.1:11434")
.build();
}
public String chat(String userMessage) {
return model.chat(userMessage);
}}
---
@AiService(wiringMode = EXPLICIT, chatModel = "ollamaChatModel")
interface Assistant {
@SystemMessage("You are a polite assistant")
String chat(String userMessage);
}
How should I properly configure @AiService so that the chatModel is correctly injected?
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-ollama</artifactId>
<version>1.0.0-beta1</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-spring-boot-starter</artifactId>
<version>1.0.0-beta1</version>
</dependency>