向量数据库:加载md进数据库
This commit is contained in:
parent
071f710f5c
commit
274608a471
@ -12,4 +12,6 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public interface DBService {
|
public interface DBService {
|
||||||
List<Document> similaritySearch();
|
List<Document> similaritySearch();
|
||||||
|
|
||||||
|
List<Document> loadMdToDd();
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.huangge1199.aiagent.Service.impl;
|
package com.huangge1199.aiagent.Service.impl;
|
||||||
|
|
||||||
import com.huangge1199.aiagent.Service.DBService;
|
import com.huangge1199.aiagent.Service.DBService;
|
||||||
|
import com.huangge1199.aiagent.rag.DocumentLoaderUtils;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.ai.document.Document;
|
import org.springframework.ai.document.Document;
|
||||||
import org.springframework.ai.vectorstore.SearchRequest;
|
import org.springframework.ai.vectorstore.SearchRequest;
|
||||||
@ -22,6 +23,9 @@ public class DBServiceImpl implements DBService {
|
|||||||
@Resource
|
@Resource
|
||||||
VectorStore pgVectorVectorStore;
|
VectorStore pgVectorVectorStore;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
DocumentLoaderUtils documentLoaderUtils;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Document> similaritySearch() {
|
public List<Document> similaritySearch() {
|
||||||
@ -34,4 +38,11 @@ public class DBServiceImpl implements DBService {
|
|||||||
// 相似度查询
|
// 相似度查询
|
||||||
return pgVectorVectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());
|
return pgVectorVectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Document> loadMdToDd() {
|
||||||
|
List<Document> documents = documentLoaderUtils.loadMarkdowns();
|
||||||
|
pgVectorVectorStore.add(documents);
|
||||||
|
return pgVectorVectorStore.similaritySearch(SearchRequest.builder().topK(5).build());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package com.huangge1199.aiagent.config;
|
package com.huangge1199.aiagent.config;
|
||||||
|
|
||||||
import com.huangge1199.aiagent.rag.DocumentLoaderUtils;
|
|
||||||
import jakarta.annotation.Resource;
|
|
||||||
import org.springframework.ai.embedding.EmbeddingModel;
|
import org.springframework.ai.embedding.EmbeddingModel;
|
||||||
import org.springframework.ai.vectorstore.VectorStore;
|
import org.springframework.ai.vectorstore.VectorStore;
|
||||||
import org.springframework.ai.vectorstore.pgvector.PgVectorStore;
|
import org.springframework.ai.vectorstore.pgvector.PgVectorStore;
|
||||||
@ -22,18 +20,9 @@ import static org.springframework.ai.vectorstore.pgvector.PgVectorStore.PgIndexT
|
|||||||
@Configuration
|
@Configuration
|
||||||
public class PgVectorVectorStoreConfig {
|
public class PgVectorVectorStoreConfig {
|
||||||
|
|
||||||
@Resource
|
|
||||||
private DocumentLoaderUtils documentLoaderUtils;
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public VectorStore vectorStore(JdbcTemplate jdbcTemplate, @Qualifier("ollamaEmbeddingModel") EmbeddingModel embeddingModel) {
|
public VectorStore vectorStore(JdbcTemplate jdbcTemplate, @Qualifier("ollamaEmbeddingModel") EmbeddingModel embeddingModel) {
|
||||||
// 设置向量维度,默认为模型维度或1536
|
|
||||||
// 设置距离类型,默认为 COSINE_DISTANCE
|
|
||||||
// 设置索引类型,默认为 HNSW
|
|
||||||
// 是否初始化模式,默认为 false
|
|
||||||
// 设置模式名称,默认为 "public"
|
|
||||||
// 设置向量表名称,默认为 "vector_store"
|
|
||||||
// 设置最大文档批处理大小,默认为 10000
|
|
||||||
return PgVectorStore.builder(jdbcTemplate, embeddingModel)
|
return PgVectorStore.builder(jdbcTemplate, embeddingModel)
|
||||||
// 设置向量维度,默认为模型维度或1536
|
// 设置向量维度,默认为模型维度或1536
|
||||||
.dimensions(1024)
|
.dimensions(1024)
|
||||||
|
@ -32,4 +32,11 @@ public class DBController {
|
|||||||
List<Document> results = dbService.similaritySearch();
|
List<Document> results = dbService.similaritySearch();
|
||||||
return R.ok(results);
|
return R.ok(results);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/loadMdToDB")
|
||||||
|
@Operation(summary = "加载md进数据库")
|
||||||
|
public R<List<Document>> loadMdToDd() {
|
||||||
|
List<Document> result = dbService.loadMdToDd();
|
||||||
|
return R.ok(result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package com.huangge1199.aiagent.rag;
|
package com.huangge1199.aiagent.rag;
|
||||||
|
|
||||||
import jakarta.annotation.Resource;
|
|
||||||
import org.springframework.ai.chat.client.ChatClient;
|
import org.springframework.ai.chat.client.ChatClient;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@ -14,15 +13,6 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
@Configuration
|
@Configuration
|
||||||
public class RagConfig {
|
public class RagConfig {
|
||||||
|
|
||||||
@Resource
|
|
||||||
private DocumentLoaderUtils documentLoaderUtils;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private MyTokenTextSplitter myTokenTextSplitter;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private MyKeywordEnricher myKeywordEnricher;
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
ChatClient chatClient(ChatClient.Builder builder) {
|
ChatClient chatClient(ChatClient.Builder builder) {
|
||||||
return builder.defaultSystem("你将作为一名恋爱大师,对于用户的问题作出解答")
|
return builder.defaultSystem("你将作为一名恋爱大师,对于用户的问题作出解答")
|
||||||
|
Loading…
Reference in New Issue
Block a user