1.5B
ollama run https://www.modelscope.cn/unsloth/DeepSeek-R1-Distill-Qwen-1.5B-GGUF
pom
注意不能用阿里云的MAVEN
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.fan</groupId>
<artifactId>ollamaV2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ollamaV2</name>
<description>ollamaV2</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
<spring-ai.version>1.0.0-M6</spring-ai.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<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>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
配置
spring:
ai:
ollama:
base-url: http://localhost:11434
chat:
options:
model: www.modelscope.cn/unsloth/DeepSeek-R1-Distill-Qwen-1.5B-GGUF:latest
config
package com.fan.config;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@RequiredArgsConstructor
public class AiConfig {
final OllamaChatModel model;
@Bean
public ChatClient chatClient() {
return ChatClient.builder(model)
// .defaultSystem("You are a helpful assistant.")
.build();
}
@Bean
public ChatMemory chatMemory() {
return new InMemoryChatMemory();
}
}
接口
package com.fan.controller;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RequiredArgsConstructor
@RestController
public class ChatController {
final ChatClient chatClient;
@RequestMapping("/chat")
public String chat(@RequestParam(value = "msg") String msg) {
try {
return chatClient.prompt().user(msg).call().content();
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
}
模型向量化
ollama run https://www.modelscope.cn/Embedding-GGUF/all-MiniLM-L12-v2-GGUF
数据库
https://www.postgresql.org/download/linux/ubuntu/
安装教程:https://blog.csdn.net/weixin_44594317/article/details/144029200
加pgvector
sudo apt install postgresql-server-dev-16 build-essential
git clone --branch v0.8.0 https://github.com/pgvector/pgvector.git
cd pgvector
# 编译
make
# 安装到 PostgreSQL 扩展目录
sudo make install
# 连接到目标数据库(以 postgres 用户为例)
sudo -u postgres psql -d your_database_name
# 创建扩展
CREATE EXTENSION vector;
# 验证安装
\dx
启用 uuid-ossp 扩展
sudo -u postgres psql -d mydb
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
\df uuid_generate_v4
sql
create table vector_store (
id uuid default uuid_generate_v4() not null primary key,
content text,
metadata jsonb,
embedding vector(384)
);