在用spring-ai对接ollama本地大模型时,@Autowired OllamaChatModel 无法注入

引入包

<dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>

注入OllamaChatModel :

@Autowired
    private OllamaChatModel ollamaChatModel ;

启动时报的异常:

15:05:52.151 [restartedMain] WARN  o.a.c.l.WebappClassLoaderBase - [log,173] - The web application [ROOT] appears to have started a thread named [Thread-4] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.base@17.0.13/sun.net.dns.ResolverConfigurationImpl.notifyAddrChange0(Native Method)
 java.base@17.0.13/sun.net.dns.ResolverConfigurationImpl$AddressChangeListener.run(ResolverConfigurationImpl.java:176)
15:05:52.184 [restartedMain] ERROR o.s.b.d.LoggingFailureAnalysisReporter - [report,40] - 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field aiClient in com.ruoyi.AI.service.AiService required a bean of type 'org.springframework.ai.ollama.OllamaChatModel' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.ai.ollama.OllamaChatModel' in your configuration.

Disconnected from the target VM, address: '127.0.0.1:56199', transport: 'socket'

Process finished with exit code 1

spring.ai.ollama 等配置信息,配置了没?

spring:
ai:
    ollama:
      base-url: http://localhost:11434/v1/
      api-key: ollama
      chat:
        model: deepseek-r1:7b

在application.yml已经配置了AI模型的信息了,问题是这个 org.springframework.ai.ollama.OllamaChatModel 在注入时,找不到它的bean,项目启动不起来。把 @Autowired(required = false) 注入改成false,项目就能启动起来。说明 OllamaChatModel 无法注入。难道还有什么其它配置吗?

yml文件格式,怎么并行了

yml文件格式是正确的,只是我复制过来时格式有点变样而已,ai:这一行是缩进去两格。yml就是下面这样的格式:

spring:
  ai:
    ollama:
      base-url: http://localhost:11434/v1/
      api-key: ollama
      chat:
        model: deepseek-r1:7b

有哪们大神用过spring-ai这个功能吗?你们那能用吗?我这里找了很多资料,还是用不了。 :joy:

在回复框里排好了样式,点回复后看到的样式又不对了。 :rofl:

这是 markdown 编辑器,你要用代码块包裹(我帮你修改了)。

我没用过这个依赖,跟我我排除这种问题的经验:

1、开启 DEBUG 日志,注意看看看是不是有异常、警告信息。
2、查看这个 starter 的自动配置类,也许可以找出一些端倪。

异常警告信息就是前面那4句了。试了好多的方法还是不行,觉得这个Jar包的这个类不支持注入了。 :flushed:

首先,你这个依赖的版本 1.0.0-SNAPSHOT 不对吧?Maven 仓库 中这个依赖的最新版本是:

<!-- https://mvnrepository.com/artifact/org.springframework.ai/spring-ai-ollama-spring-boot-starter -->
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
    <version>1.0.0-M6</version>
</dependency>

我把上述依赖添加到应用后,复制了你的配置信息:

spring:
  ai:
    ollama:
      base-url: http://localhost:11434/v1/
      api-key: ollama
      chat:
        model: deepseek-r1:7b

执行测试:

@Autowired
OllamaChatModel  ollamaChatModel ;
	
	
@Test
public void test() {
	System.out.println(ollamaChatModel);
}

输出如下:

org.springframework.ai.ollama.OllamaChatModel@4babe06f

一切 OK,你检查下是不是搞错了版本号?

我也遇到了这个问题,请问有解决吗

请参考

1.0.0-M1,1.0.0-M5的版本试过了也不行,1.0.0-M6这个版本的我再试试看

还没解决呢 :rofl:

我新建了一个springAI的项目,然后运行好,把两个依赖结合起来,莫名其妙解决了,我猜测还是有东西没加好,可能就是下载包的地址?