基于springboot+ollama实现大模型接入项目的小白笔记

org.springframework.ai无法被maven加载的问题

问题原因:
阿里镜像没有这个库
共用库中只保存了0.8.1的测试版本
解决方法:
参考了这个视频
https://www.youtube.com/watch?v=dffEF9ORVUg
学习视频中方法使用springinitializr这个工具进行项目的生成
具体参数如下图
基于springboot+ollama实现大模型接入项目的小白笔记_第1张图片
在右边依赖中搜索ollama加载ai依赖,以及spring web依赖,然后导出项目使用idea打开。

org.springframework.ai需要从快照库中加载

在pom中加入如下代码

<repositories>
		<repository>
			<id>spring-milestonesid>
			<name>Spring Milestonesname>
			<url>https://repo.spring.io/milestoneurl>
			<snapshots>
				<enabled>trueenabled>
			snapshots>
		repository>
		<repository>
			<id>spring-snapshotsid>
			<name>Spring Snapshotsname>
			<url>https://repo.spring.io/snapshoturl>
			<releases>
				<enabled>trueenabled>
			releases>
		repository>
	repositories>

然后将spring-ai的版本修改为如下

<properties>
		<java.version>17java.version>
		<spring-ai.version>1.0.0-SNAPSHOTspring-ai.version>
	properties>

模型选择

首先通过在cmd中输入

ollama list

获取你已经安装的大模型。
然后修改图中配置文件
基于springboot+ollama实现大模型接入项目的小白笔记_第2张图片

然后加入你想要用的模型

spring.ai.ollama.chat.model=llama3

新建控制类验证功能

新建类代码:

package dev.ying;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ChatController {

    private final ChatClient chatClient;

    public ChatController(ChatClient.Builder chatClient){
        this.chatClient=chatClient.build();
    }
	//默认输入,根目录就能验证接入效果
    @GetMapping("")
    public String joke(){
        return chatClient.prompt()
                .user("讲个笑话")
                .call()
                .content();
    }
    //http://localhost:8080/ollama/chat?msg=如何使用spring-ai
    //模拟chat
    @GetMapping("/ollama/chat")
    public String ollama(@RequestParam String msg){
        return chatClient.prompt()
                .user(msg)
                .call()
                .content();
    }

}

然后访问localhost就行啦

碎碎念

问题的主要原因的错误配置pom导致的ai包下载失败,由于这个功能是今年刚刚加入springboot的,所以国内教程少,讲的东西也大概率随着版本更新失效了。也是通过学习才知道了springinitializr这个工具,更具视频一步步做下来也是成功实现了。
jav的版本也有可能影响,spring框架的版本推荐是3.3.x,我式了3.4.0但没跑起来,还是退回到了3.3.2

你可能感兴趣的:(spring,boot,笔记,后端,llama)