# Spring AI+Ollama+pgvector实现本地RAG 1、数据准备,将待文本数据通过embedding模型转成文本向量,并存储到向量数据库中。 2、用户提问,将用户提出的文本通过embedding模型转成问题文本向量,并在向量库中进行搜索,搜索得到一些文本段落,将搜索到的文本段落组装成prompt去调用大模型来获得答案。 ## 环境准备 ```text jdk 17+ postgresql-12+ (linux环境) ollama (linux环境) ``` ###创建maven配置文件(maven.xml) ```xml D:/repository public https://maven.aliyun.com/repository/public public,!bladex central https://maven.aliyun.com/repository/central central,!bladex bladex Authorization token c02fc9fe46326c7706fb65c233ac2c7ba1af6dfe ``` ###配置idea maven环境 ```text ctrl + alt + s 打开设置 搜索 maven 找到 User settings file 勾选Override 找到配置的maven.xml ``` ### 数据库插件 ```shell (centos7) sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm (centos8) sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm (centos8) sudo dnf -qy module disable postgresql yum -y install postgresql[pg版本]-server postgresql[pg版本] yum -y install pgvector_[pg版本] yum -y install postgresql[pg版本]-contrib ``` ### Ollama和模型 ####下载ollama ```shell curl -o ~/ollama https://hub.whtrys.space/ollama/ollama/releases/download/v0.2.5/ollama-linux-amd64 ``` ####启动ollama ````shell nohup ~/ollama serve > ~/ollama.log 2>&1 & ```` ####下载qwen2:7b: ```shell ~/ollama pull sam4096/qwen2tools:1.5b ``` ####下载embedding模型: ```shell ~/ollama pull shaw/dmeta-embedding-zh ``` ####开启会话qwen2:7b ```shell ~/ollama run sam4096/qwen2tools:1.5b ``` ####打包启动 ```shell #打包 maven -> ragchat -> clean maven -> ragchat -> package # 启动 nohup java -Dloader.path='lib/' -jar ragchat.jar > ragchat.log 2>&1 & # 添加依赖包到当前lib目录(首次/或依赖包更新时) # 将lib打包并解压到与jar包同级目录 mvn dependency:copy-dependencies -DoutputDirectory=.\lib -DincludeScope=runtime ```