最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

langchain - LangChain4j @AiService ChatModel Injection Issue - NullPointerException - Stack Overflow

programmeradmin4浏览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>

发布评论

评论列表(0)

  1. 暂无评论