Preprocess¶
Preprocess是一项API服务,可以将任何类型的文档分割成最优的文本块,用于语言模型任务。
输入文档后,Preprocess会将其分割成文本块,这些文本块尊重原始文档的布局和语义。我们根据文档的节、段落、列表、图片、数据表、文本表和幻灯片来分割内容,并针对长文本遵循内容语义。
Preprocess支持:
- PDF文件
- Microsoft Office文档(Word, PowerPoint, Excel)
- OpenOffice文档(ods, odt, odp)
- HTML内容(网页、文章、电子邮件)
- 纯文本。
PreprocessLoader
与Preprocess API library
交互,提供文档转换和分块功能,或将已分块的文件加载到LangChain中。
要求¶
如果尚未安装,请安装Python Preprocess库
In [ ]
已复制!
# Install Preprocess Python SDK package
# $ pip install pypreprocess
# 安装 Preprocess Python SDK 包 # $ pip install pypreprocess
用法¶
要使用Preprocess加载器,您需要传递Preprocess API Key
。初始化PreprocessReader
时,应传递您的API Key
;如果还没有,请发送邮件至[email protected]获取。没有API Key
,加载器将引发错误。
要对文件进行分块,请传递一个有效的文件路径,读取器将开始转换和分块。Preprocess
会通过应用内部的Splitter
来对您的文件进行分块。因此,您不应使用Splitter
将文档解析为节点,也不应在IngestionPipeline
中转换文档时应用Splitter
。
In [ ]
已复制!
from llama_index.core import VectorStoreIndex
from llama_index.readers.preprocess import PreprocessReader
from llama_index.core import VectorStoreIndex from llama_index.readers.preprocess import PreprocessReader
In [ ]
已复制!
loader = PreprocessReader(
api_key="your-api-key", filepath="valid/path/to/file"
)
loader = PreprocessReader( api_key="your-api-key", filepath="valid/path/to/file" )
如果您想直接处理节点:
In [ ]
已复制!
nodes = loader.get_nodes()
# import the nodes in a Vector Store with your configuration
index = VectorStoreIndex(nodes)
query_engine = index.as_query_engine()
nodes = loader.get_nodes() # import the nodes in a Vector Store with your configuration index = VectorStoreIndex(nodes) query_engine = index.as_query_engine()
默认情况下load_data()
会为每个分块返回一个文档,请记住不要对这些文档应用任何分割操作:
In [ ]
已复制!
documents = loader.load_data()
# don't apply any Splitter parser to documents
# if you have an ingestion pipeline you should not apply a Splitter in the transformations
# import the documents in a Vector Store, if you set the service_context parameter remember to avoid including a splitter
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
documents = loader.load_data() # 不要对文档应用任何 Splitter 解析器 # 如果您有数据摄入管道,不应在转换中应用 Splitter # 将文档导入向量存储,如果设置了 service_context 参数,请记得避免包含 splitter index = VectorStoreIndex.from_documents(documents) query_engine = index.as_query_engine()
In [ ]
已复制!
data = loader.load()
data = loader.load()
如果您只想返回提取的文本并使用自定义管道处理,请设置 return_whole_document = True
:
In [ ]
已复制!
document = loader.load_data(return_whole_document=True)
document = loader.load_data(return_whole_document=True)
如果您想加载已分块的文件,可以通过将 process_id
传递给读取器来实现:
In [ ]
已复制!
# pass a process_id obtained from a previous instance and get the chunks as one string inside a Document
loader = PreprocessReader(api_key="your-api-key", process_id="your-process-id")
# 传递从先前实例获得的 process_id,并将分块作为单个字符串获取到 Document 中 loader = PreprocessReader(api_key="your-api-key", process_id="your-process-id")
其他信息¶
PreprocessReader
基于 Preprocess 库的 pypreprocess
。如需更多信息或其他集成需求,请查看文档。