背景说明
"Unstructed" 是一个开源项目旨在提供一种统一的方式来解析和处理不同格式的非结构化文档,是大模型领域专业的ETL方案,"ETL for LLMs"。
目前国内的中文资料相对较少,本文记录下工作中技术调研 Unstructed 的本地安装过程。
安装方式一、本地安装(Full Installation)
执行 pip3 install "unstructured[all-docs]",首次安装可能会提示缺少系统依赖而如下报错👇
ERROR: Could not build wheels for pikepdf, pillow-heif, which is required
安装以下依赖(brew 切换国内源,例如阿里云源):
brew install qpdf
brew install libheif
brew install pillow
都安装成功后再执行 pip3 install "unstructured[all-docs]" 安装成功,如果需要本地安装完整的 unstructured 全部功能,还需按照官文安装其他系统依赖(system dependencies)
安装方式二、unstructured 本地镜像运行
本地安装方式相对复杂,也可以尝试直接使用官方 unstructured 镜像:
docker pull downloads.unstructured.io/unstructured-io/unstructured:latest
docker run -dt --name unstructured downloads.unstructured.io/unstructured-io/unstructured:latest
docker exec -it unstructured bash
安装方式三、unstructured-api(本地部署成web服务)
unstructured 提供的本地镜像功能完整,但是镜像大小略大6.2GB左右,最后我们采用了官方提供的 unstructured-api 方式本地私有化部署为 web 服务:
docker pull downloads.unstructured.io/unstructured-io/unstructured-api:latest
docker run --env HF_ENDPOINT=hf-mirror.com --user root -p 8000:8000 -d --rm --name unstructured-api downloads.unstructured.io/unstructured-io/unstructured-api:latest
设置环境变量 HF_ENDPOINT(重要)
unstructured-api 支持对PDF的高级解析模式,即启用大模型来解析,首次调用会 unstructured-api 从HF远程下载模型,由于国内HF无法访问,可以设置HF_ENDPOINT指定从国内 hf-mirror.com 镜像网站下载所需模型
Postman调用例子
设置header
设置请求body,"files"文件列表,"strategy"解析策略,"hi_res"代表启动大模型加持解析,效果会更准确(对PDF类型生效),但是接口RT会大幅降低
详细参数参考:github.com/Unstructure…
安装方式四、云服务调用(14天内 for free)
注册申请api key,详细参考官文:app.unstructured.io/keys
调用方式和本地调用类似,header新增 api key,域名改成公网 unstructured 服务域名
"unstructured-api-key: ZEaMYMsd27DXGrX6uFvtdkvYokhshsx"
接口域名:api.unstructuredapp.io/general/v0/…
参考资料
docs.unstructured.io/open-source…
blog.csdn.net/jasonhongcn… huggingface 设置国内镜像