Skip to content

Commit 9cb1458

Browse files
jisySTwzh1994
andauthored
add deploy cli and docs for mineru (#817)
Co-authored-by: wangzhihong <wangzhihong@sensetime.com>
1 parent c2f139c commit 9cb1458

File tree

4 files changed

+131
-23
lines changed

4 files changed

+131
-23
lines changed

docs/zh/Cookbook/rag.md

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,55 @@ print(f"answer: {res}")
218218

219219
</details>
220220

221-
## 版本-3:使用 flow
221+
## 版本-3:自定义文档解析工具,以MinerU为例
222+
223+
lazyllm 内置了一套默认的文档解析算法。如果需要更高定制化的文档解析,可以使用自定义的文档解析器。
224+
[MinerU](https://github.com/opendatalab/MinerU) 是业界领先的 PDF 文档解析工具。我们为 MinerU 提供了专门的接入组件,无需额外定制,即可顺畅集成。
225+
226+
目前提供一键启动的 MinerU 服务端(server)以及配套的 PDF 客户端。使用流程如下:先在本地启动 MinerU 解析服务,再通过接入 `MineruPDFReader` 获取解析后的文档内容。
227+
228+
### 启动 MinerU 服务
229+
在开始之前,请先安装 MinerU 依赖:
230+
231+
```bash
232+
lazyllm install mineru
233+
```
234+
> **提示**:为确保解析结果稳定,当前固定 MinerU 版本为2.5.4。服务运行所需资源请参考 [MinerU](https://github.com/opendatalab/MinerU) 官方文档。
235+
236+
环境准备完毕后,通过以下命令一键部署服务:
237+
238+
```bash
239+
lazyllm deploy mineru [--port <port>] [--cache_dir <cache_dir>] [--image_save_dir <image_save_dir>] [--model_source <model_source>]
240+
```
241+
242+
** 参数说明 **
243+
244+
| 参数 | 说明 | 默认值 |
245+
|------|------|--------|
246+
| `--port` | 服务端口号 | **随机分配** |
247+
| `--cache_dir` | 文档解析缓存目录(设置后相同文档无需重复解析) | **None** |
248+
| `--image_save_dir` | 图片输出目录(设置后保存文档内提取的图片) | **None** |
249+
| `--model_source` | 模型来源(可选:`huggingface``modelscope`| **huggingface** |
250+
251+
252+
> **提示**:所有参数均为可缺省,执行 lazyllm deploy mineru 即可启动默认服务。若希望持久化缓存解析结果和图片,请自行指定目录路径。
253+
254+
### 通过 reader 无缝接入 MinerU 服务
255+
256+
在RAG流程中,我们在 Part1 为 `documents` 对象注册用于PDF文件解析的解析器:
257+
258+
```python
259+
from lazyllm.tools.rag.readers import MineruPDFReader
260+
261+
# 注册 PDF 解析器,url 替换为已启动的 MinerU 服务地址
262+
documents.add_reader("*.pdf", MineruPDFReader(url="http://127.0.0.1:8888"))
263+
264+
```
265+
266+
其余流程保持不变,即可将 MinerU 服务集成到 RAG 流程中,实现 PDF 文档解析。
267+
268+
269+
## 版本-4:使用 flow
222270

223271
从 版本-2 的流程图可以看到整个流程已经比较复杂了。我们注意到两个(或多个)`Retriever` 的检索过程互不影响,它们可以并行执行。同时整个流程上下游之间也有着明确的依赖关系,需要保证上游执行完成之后才可以进行下一步。
224272

@@ -286,7 +334,7 @@ print(f"answer: {res}")
286334

287335
`pipeline``parallel` 只是方便流程搭建以及可能的一些性能优化,并不会改成程序的逻辑。另外用户查询作为重要的提示内容,基本是中间每个模块都会用到,我们在这里还用了 `LazyLLM` 提供的 [bind()](../Best%20Practice/flow.md#use-bind) 函数将用户查询 `query` 作为参数传给 `ppl.reranker``ppl.formatter`
288336

289-
## 版本-4:自定义检索和排序策略
337+
## 版本-5:自定义检索和排序策略
290338

291339
上面的例子使用的都是 `LazyLLM` 内置的组件。现实中总有我们覆盖不到的用户需求,为了满足这些需求,`Retriever``Reranker` 提供了插件机制,用户可以自定义检索和排序策略,通过 `LazyLLM` 提供的注册接口添加到框架中。
292340

@@ -325,7 +373,7 @@ my_reranker = Reranker(name="MyReranker")
325373

326374
这里只是简单介绍了怎么使用 `LazyLLM` 注册扩展的机制。可以参考 [Retriever](../Best%20Practice/rag.md#Retriever)[Reranker](../Best%20Practice/rag.md#Reranker) 的文档,在遇到不能满足需求的时候通过编写自己的相似度计算和排序策略来实现自己的应用。
327375

328-
## 版本-5:自定义存储后端
376+
## 版本-6:自定义存储后端
329377

330378
在定义好 Node Group 的转换规则之后,`LazyLLM` 会把检索过程中用到的转换得到的 Node Group 内容保存起来,这样后续使用的时候可以避免重复执行转换操作。为了方便用户存取不同种类的数据,`LazyLLM` 支持用户自定义存储后端。
331379

@@ -466,7 +514,7 @@ if __name__ == '__main__':
466514

467515
</details>
468516

469-
## 版本-6:离在线分离,接入远程部署的 `Document`
517+
## 版本-7:离在线分离,接入远程部署的 `Document`
470518

471519
RAG系统往往包含文档解析与在线问答两阶段,其中文档解析阶段耗时较长,但可以离线执行,而问答阶段则需要快速响应。为了满足这一需求,`LazyLLM` 提供了 `Document` 的远程部署与接入功能,支持用户将 `Document` 部署在远程服务器上,并使用 url 的方式接入。
472520

lazyllm/cli/deploy.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,30 @@ def deploy(commands):
5757
)
5858
)
5959
)
60+
elif commands and commands[0] == 'mineru':
61+
commands = commands[1:]
62+
parser = argparse.ArgumentParser(description='lazyllm deploy command for deploying a mineru server.')
63+
parser.add_argument('--port', help='Port for the mineru server.', default=None)
64+
parser.add_argument('--cache_dir', help='Cache directory for the mineru server.', default=None)
65+
parser.add_argument('--image_save_dir', help='Image save directory for the mineru server.', default=None)
66+
parser.add_argument('--default_backend', help='Default backend for the mineru server.\
67+
(pipeline|vlm-vllm-async-engine|vlm-transformers)', default='pipeline')
68+
parser.add_argument('--model_source', help='Model source for the mineru server.(huggingface|modelscope)',
69+
default='huggingface')
70+
args = parser.parse_args(commands)
71+
os.environ['MINERU_MODEL_SOURCE'] = args.model_source
72+
73+
lazyllm.LOG.info('Starting mineru server')
74+
lazyllm.LOG.info(f'Current model source: {args.model_source}')
75+
from lazyllm.tools.servers.mineru import MineruServer
76+
server = MineruServer(
77+
cache_dir=args.cache_dir,
78+
image_save_dir=args.image_save_dir,
79+
default_backend=args.default_backend,
80+
port=args.port,
81+
)
82+
server.start()
83+
server.wait()
6084
else:
6185
parser = argparse.ArgumentParser(description='lazyllm deploy command for deploying a model.')
6286
parser.add_argument('model', help='model name')

lazyllm/docs/tools.py

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,29 +1362,60 @@
13621362
''')
13631363

13641364
add_chinese_doc('rag.readers.MineruPDFReader', '''\
1365-
用于通过 MinerU 服务解析 PDF 文件内容的模块。支持上传文件或通过 URL 方式调用解析接口,解析结果经过回调函数处理成文档节点列表。
1366-
1367-
Args:
1368-
url (str): MineruPDFReader 服务的接口 URL。
1369-
upload_mode (bool): 是否采用文件上传模式调用接口,默认为 False,即通过 JSON 请求文件路径。
1370-
extract_table (bool): 是否提取表格,默认为 True。
1371-
extract_formula (bool): 是否提取公式,默认为 True。
1372-
split_doc (bool): 是否分割文档,默认为 True。
1373-
post_func (Optional[Callable]): 后处理函数。
1365+
基于Mineru服务的PDF解析器,通过调用Mineru服务的API来解析PDF文件,支持丰富的文档结构识别。
1366+
1367+
Args:
1368+
url (str): Mineru服务的完整API端点URL。
1369+
backend (str, optional): 解析引擎类型。可选值:
1370+
- 'pipeline': 标准处理流水线
1371+
- 'vlm-transformers': 基于Transformers的视觉语言模型
1372+
- 'vlm-vllm-async-engine': 基于异步VLLM的视觉语言模型
1373+
默认为 'pipeline'。
1374+
upload_mode (bool, optional): 文件传输模式。
1375+
- True: 使用multipart/form-data上传文件内容
1376+
- False: 通过文件路径传递(需确保服务端可访问该路径)
1377+
默认为 False。
1378+
extract_table (bool, optional): 是否提取表格内容并转换为Markdown格式。默认为 True。
1379+
extract_formula (bool, optional): 是否提取公式文本。
1380+
- True: 提取为LaTeX等文本格式
1381+
- False: 将公式保留为图片
1382+
默认为 True。
1383+
split_doc (bool, optional): 是否将文档分割为多个DocNode节点。默认为 True。
1384+
clean_content (bool, optional): 是否清理冗余内容(页眉、页脚、页码等)。默认为 True。
1385+
post_func (Optional[Callable[[List[DocNode]], Any]], optional): 后处理函数,
1386+
接收DocNode列表作为参数,用于自定义结果处理。默认为 None。
13741387
''')
13751388

13761389
add_english_doc('rag.readers.MineruPDFReader', '''\
1377-
Module to parse PDF content via the MineruPDFReader service. Supports file upload or URL-based parsing, with a callback to process the parsed elements into document nodes.
1378-
1379-
Args:
1380-
url (str): The MineruPDFReader service API URL.
1381-
upload_mode (bool): Whether to use file upload mode for the API call. Default is False, meaning JSON request with file path.
1382-
extract_table (bool): Whether to extract tables. Default is True.
1383-
extract_formula (bool): Whether to extract formulas. Default is True.
1384-
split_doc (bool): Whether to split the document. Default is True.
1385-
post_func (Optional[Callable]): Post-processing function.
1390+
Reader for PDF files by calling the Mineru service's API.
1391+
1392+
Args:
1393+
url (str): The complete API endpoint URL for the Mineru service.
1394+
backend (str, optional): Type of parsing engine. Available options:
1395+
- 'pipeline': Standard processing pipeline
1396+
- 'vlm-transformers': Vision-language model based on Transformers
1397+
- 'vlm-vllm-async-engine': Vision-language model based on async VLLM engine
1398+
Defaults to 'pipeline'.
1399+
upload_mode (bool, optional): File transfer mode.
1400+
- True: Upload file content using multipart/form-data
1401+
- False: Pass by file path (ensure the server can access the path)
1402+
Defaults to False.
1403+
extract_table (bool, optional): Whether to extract table content and convert
1404+
to Markdown format. Defaults to True.
1405+
extract_formula (bool, optional): Whether to extract formula text.
1406+
- True: Extract as text format (e.g., LaTeX)
1407+
- False: Keep formulas as images
1408+
Defaults to True.
1409+
split_doc (bool, optional): Whether to split the document into multiple
1410+
DocNode nodes. Defaults to True.
1411+
clean_content (bool, optional): Whether to clean redundant content
1412+
(headers, footers, page numbers, etc.). Defaults to True.
1413+
post_func (Optional[Callable[[List[DocNode]], Any]], optional): Post-processing
1414+
function that takes a list of DocNodes as input for custom result handling.
1415+
Defaults to None.
13861416
''')
13871417

1418+
13881419
add_chinese_doc('rag.readers.MarkdownReader', '''\
13891420
用于读取和解析 Markdown 文件的模块。支持去除超链接和图片,按标题和内容将 Markdown 划分成若干文本段落节点。
13901421
@@ -2864,6 +2895,11 @@ def _lazy_load_data(self, file_paths: list, **kwargs) -> Iterable[DocNode]:
28642895
documents = reader.forward(file_paths=["doc1.txt", "doc2.txt"])
28652896
''')
28662897

2898+
add_example('rag.readers.MineruPDFReader', '''\
2899+
from lazyllm.tools.rag.readers import MineruPDFReader
2900+
reader = MineruPDFReader("http://0.0.0.0:8888") # Mineru server address
2901+
nodes = reader("path/to/pdf")
2902+
''')
28672903

28682904
add_chinese_doc('rag.doc_node.QADocNode', '''\
28692905
问答文档节点类,用于存储问答对数据。

0 commit comments

Comments
 (0)