0

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> 

1 Answer 1

0

You should use langchain4j-ollama-spring-boot-starter or register your OllamaChatModel using Spring Boot @Bean annotation. So that the @AiService annotation could found the bean and auto-inject it.

BTW, you should not implement the @AiService interface on your own, langchain4j will implement it using Proxy automatically.

Please refer to docs.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.