【新增】AI 知识库:文档向量化 demo

This commit is contained in:
xiaoxin
2024-08-05 13:53:50 +08:00
parent cb59a61a04
commit 2a984504d9
7 changed files with 595 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package cn.iocoder.yudao.module.ai.service.knowledge;
/**
* AI 知识库 Service 接口
*
* @author xiaoxin
*/
public interface DocService {
/**
* 向量化文档
*/
void embeddingDoc();
}

View File

@ -0,0 +1,44 @@
package cn.iocoder.yudao.module.ai.service.knowledge;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.document.Document;
import org.springframework.ai.reader.TextReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.RedisVectorStore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* AI 知识库 Service 实现类
*
* @author xiaoxin
*/
@Service
@Slf4j
public class DocServiceImpl implements DocService {
@Resource
RedisVectorStore vectorStore;
@Resource
TokenTextSplitter tokenTextSplitter;
// TODO @xin 临时测试用,后续删
@Value("classpath:/webapp/test/Fel.pdf")
private org.springframework.core.io.Resource data;
@Override
public void embeddingDoc() {
// 读取文件
org.springframework.core.io.Resource file = data;
TextReader loader = new TextReader(file);
List<Document> documents = loader.get();
// 文档分段
List<Document> segments = tokenTextSplitter.apply(documents);
// 向量化并存储
vectorStore.add(segments);
}
}