Spring AI接入Ollama本地模型

60 阅读2分钟

介绍:上篇说到Spring AI实现智能对话,但如果在工作中,会用到LlamaFactory训练已有的模型,实现需求

LLaMA Factory官方文档:https://llamafactory.readthedocs.io/zh-cn/latest/getting_started/installation.html#windows

image.png

上篇文章说过使用ChatClient调用模型,而如果想要使用模型的独有能力,则需要使用ChatModel

image.png

如引入如下依赖

<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>

则可以将原始的ChatClient改写为

@RestController
@RequestMapping("/test")
public class AiController {
    @Autowired
    private ChatClient chatClient;

    @Autowired
    private OpenAiChatModel openAiChatModel;


    /**
     * ChatModel测试
     *
     * @param userInput
     * @return
     */
    @GetMapping(value = "/chatModel")
    public String chatModel(@RequestParam(value = "userInput") String userInput) {
        System.out.println("chatModel()..");
        ChatResponse response = openAiChatModel.call(
                new Prompt(
                        new UserMessage(userInput),
                        // "我的名字是小研,跟着我学习AI,月薪过万不是梦.",
                        OpenAiChatOptions.builder()
                                .model("qwen-plus")
                                .temperature(0.4)
                                .build()
                ));
        return response.getResult().getOutput().getText();
    }

}

接口测试

image.png

以上内容是对上篇博客的补充!

由于之前写过一篇调用ollama模型的教程,但spring ai版本为1.0.0-M5

image.png

步骤一:具备Ollama

image.png

步骤二:引入依赖

版本管理

<properties>
  <java.version>17</java.version>
  <spring-boot.version>3.4.5</spring-boot.version>
  <spring-ai.version>1.0.0</spring-ai.version>
</properties>
 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.ai</groupId>
                <artifactId>spring-ai-bom</artifactId>
                <version>${spring-ai.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
</dependencyManagement>

具体依赖

<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-starter-model-ollama</artifactId>
</dependency>

此篇基于上篇博客续写

步骤三:配置信息

application.yml

spring:
  application:
    name: SpringAI-Demo
# AI配置
  ai:
    ollama:
      chat:
        model: deepseek-r1:1.5b
      base-url: http://localhost:11434

模型名称可设置为ollama已有模型,可从网上拉取模型或本地使用LlamaFactory训练好的模型,如需要训练好的模型,请在具体模型,执行以下命令

# step one
cd 到你导出的目录
# step two
ollama create 模型名称 -f Modelfile

步骤四:敲代码

AiController

@RestController
@RequestMapping("/test")
public class AiController {
    private final ChatClient chatClient;

    public AiController(ChatClient.Builder chatClientBuilder) {
        this.chatClient = chatClientBuilder.build();
    }

    /**
     * ollama测试
     *
     * @param userInput
     * @return
     */
    @GetMapping(value = "/ollmaChat")
    public String ollamaChat(@RequestParam(value = "userInput") String userInput) {
        System.out.println("ollamaChat()..");
        return chatClient
                .prompt()
                .user(userInput)
                .call()
                .content();
    }
}

步骤五:测试

image.png

调用接口

image.png

问题记录

由于之前使用到openai模型,如果不注释则会报错

image.png

报错信息如下

image.png

Parameter 1 of method chatClientBuilder in org.springframework.ai.model.chat.client.autoconfigure.ChatClientAutoConfiguration required a single bean, but 2 were found:
	- ollamaChatModel: defined by method 'ollamaChatModel' in class path resource [org/springframework/ai/model/ollama/autoconfigure/OllamaChatAutoConfiguration.class]
	- openAiChatModel: defined by method 'openAiChatModel' in class path resource [org/springframework/ai/model/openai/autoconfigure/OpenAiChatAutoConfiguration.class]

由于ChatClient是单例的,所以只能引入一个

image.png

image.png

image.png

注释后,启动成功

image.png

至此,Spring AI调用本地模型Demo版结束啦!

本人正在打造技术交流群,欢迎志同道合的朋友一起探讨,一起努力,通过自己的努力,在技术岗位这条道路上走得更远。QQ群号:952912771 备注:技术交流 即可通过!

加入技术群可以获取资料,含AI资料、Spring AI中文文档等,等你加入~