业务背景
在RAG方案中,由于使用langchain按字数的切分方案,导致文本的召回结果不是很理想,此模型为某证券公司的模型方案,知识库大多是规章制度、法律条例等等,所以个性化按照默认方案即字数切分、章节切分、条切分。
技术细节
- 使用langchain读取docx、pdf、txt文档
from langchain_community.document_loaders import PyPDFLoader, TextLoader, Docx2txtLoader
if file_name.endswith(".pdf"):
loader = PyPDFLoader(file_path)
elif file_name.endswith(".txt"):
loader = TextLoader(file_path)
elif file_name.endswith(".docx"):
loader = Docx2txtLoader(file_path)
else:
raise BizException("目前只支持pdf文件与txt、docx文件")
- 按照给定的条件切分,默认、章节、条,在切分过程中如果章节或者条所含的字数超过配置的extra_word_filter_size的字数则会将此章节或词条按默认的(500,100)切分,后面转化为Document是因为最终导入向量数据库必须是这个类型无需关注。
def load_pages_(loader, file_path, split_type, chunk_size, chunk_overlap):
contents =