xparse-client 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
logging.basicConfig(
|
|
4
|
+
level=logging.INFO,
|
|
5
|
+
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s',
|
|
6
|
+
encoding='utf-8'
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
from .pipeline.config import ParseConfig, ChunkConfig, EmbedConfig, PipelineStats
|
|
10
|
+
from .pipeline.sources import Source, S3Source, LocalSource, FtpSource, SmbSource
|
|
11
|
+
from .pipeline.destinations import Destination, MilvusDestination, LocalDestination, S3Destination
|
|
12
|
+
from .pipeline.pipeline import Pipeline, create_pipeline_from_config
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
'ParseConfig',
|
|
16
|
+
'ChunkConfig',
|
|
17
|
+
'EmbedConfig',
|
|
18
|
+
'PipelineStats',
|
|
19
|
+
'Source',
|
|
20
|
+
'S3Source',
|
|
21
|
+
'LocalSource',
|
|
22
|
+
'FtpSource',
|
|
23
|
+
'SmbSource',
|
|
24
|
+
'Destination',
|
|
25
|
+
'MilvusDestination',
|
|
26
|
+
'LocalDestination',
|
|
27
|
+
'S3Destination',
|
|
28
|
+
'Pipeline',
|
|
29
|
+
'create_pipeline_from_config',
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
__version__ = '0.2.0'
|
|
@@ -0,0 +1,863 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: xparse-client
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: 面向Agent和RAG的新一代文档处理 AI Infra
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Project-URL: Homepage, https://gitlab.intsig.net/xparse1/xparse-pipeline
|
|
7
|
+
Project-URL: Repository, https://gitlab.intsig.net/xparse1/xparse-pipeline
|
|
8
|
+
Keywords: xparse,pipeline,rag
|
|
9
|
+
Requires-Python: >=3.8
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Requires-Dist: boto3
|
|
13
|
+
Requires-Dist: pymilvus[milvus_lite]
|
|
14
|
+
Requires-Dist: requests
|
|
15
|
+
Requires-Dist: pysmb
|
|
16
|
+
Dynamic: license-file
|
|
17
|
+
|
|
18
|
+
# xParse
|
|
19
|
+
|
|
20
|
+
面向Agent和RAG的新一代文档处理 AI Infra。
|
|
21
|
+
|
|
22
|
+
xParse的同步pipeline实现,支持多种数据源与输出。
|
|
23
|
+
|
|
24
|
+
## 🌟 特点
|
|
25
|
+
|
|
26
|
+
- **灵活的数据源**:支持兼容 S3 协议的对象存储、本地文件系统以及 FTP/SMB 协议文件系统
|
|
27
|
+
- **灵活的输出**:支持 Milvus/Zilliz 向量数据库、兼容 S3 协议的对象存储以及本地文件系统
|
|
28
|
+
- **统一 Pipeline API**:使用 `/api/xparse/pipeline` 一次性完成 parse → chunk → embed 全流程
|
|
29
|
+
- **配置化处理**:支持灵活配置 parse、chunk、embed 参数
|
|
30
|
+
- **详细统计信息**:返回每个阶段的处理统计数据
|
|
31
|
+
- **易于扩展**:基于抽象类,可轻松添加新的 Source 和 Destination
|
|
32
|
+
- **完整日志**:详细的处理日志和错误追踪
|
|
33
|
+
|
|
34
|
+
## 📋 架构
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
┌──────────────┐
|
|
38
|
+
│ Source │ 数据源(S3/本地/FTP)
|
|
39
|
+
└──────┬───────┘
|
|
40
|
+
│ read_file()
|
|
41
|
+
▼
|
|
42
|
+
┌──────────────────────────────────────┐
|
|
43
|
+
│ Pipeline API │
|
|
44
|
+
│ /api/xparse/pipeline │
|
|
45
|
+
│ │
|
|
46
|
+
│ ┌────────┐ ┌────────┐ ┌────────┐ │ ┌────────┐
|
|
47
|
+
│ │ Parse │→ │ Chunk │→ │ Embed │ |────→│ Deduct │ 计费
|
|
48
|
+
│ └────────┘ └────────┘ └────────┘ │ └────────┘
|
|
49
|
+
│ │
|
|
50
|
+
└──────────────┬───────────────────────┘
|
|
51
|
+
│ [embeddings + stats]
|
|
52
|
+
▼
|
|
53
|
+
┌──────────────┐
|
|
54
|
+
│ Destination │ 目的地(Milvus/Zilliz/本地)
|
|
55
|
+
└──────────────┘
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## 🚀 快速开始
|
|
59
|
+
|
|
60
|
+
### 1. 安装依赖
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
pip install --upgrade xparse-client
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### 2. 运行
|
|
67
|
+
|
|
68
|
+
`xparse-client`支持两种配置方式,即通过代码配置,以及直接通过config字典配置
|
|
69
|
+
|
|
70
|
+
#### 代码配置
|
|
71
|
+
```python
|
|
72
|
+
from xparse_client import ParseConfig, ChunkConfig, EmbedConfig, Pipeline, S3Source, MilvusDestination
|
|
73
|
+
|
|
74
|
+
# 创建配置对象
|
|
75
|
+
parse_config = ParseConfig(
|
|
76
|
+
provider='textin'
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
chunk_config = ChunkConfig(
|
|
80
|
+
strategy='by_title',
|
|
81
|
+
include_orig_elements=False,
|
|
82
|
+
new_after_n_chars=512,
|
|
83
|
+
max_characters=1024,
|
|
84
|
+
overlap=50
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
embed_config = EmbedConfig(
|
|
88
|
+
provider='qwen',
|
|
89
|
+
model_name='text-embedding-v4'
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
# 创建 Pipeline
|
|
93
|
+
source = S3Source(...)
|
|
94
|
+
destination = MilvusDestination(...)
|
|
95
|
+
|
|
96
|
+
pipeline = Pipeline(
|
|
97
|
+
source=source,
|
|
98
|
+
destination=destination,
|
|
99
|
+
api_base_url='https://api.textin.com/api/xparse',
|
|
100
|
+
api_headers={...},
|
|
101
|
+
parse_config=parse_config,
|
|
102
|
+
chunk_config=chunk_config,
|
|
103
|
+
embed_config=embed_config
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
pipeline.run()
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
#### 字典配置
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
config = {
|
|
113
|
+
'source': {...},
|
|
114
|
+
'destination': {...},
|
|
115
|
+
'api_base_url': 'https://api.textin.com/api/xparse',
|
|
116
|
+
'api_headers': {...},
|
|
117
|
+
|
|
118
|
+
# Parse 配置(可选)
|
|
119
|
+
'parse_config': {
|
|
120
|
+
'provider': 'textin' # 当前支持textin文档解析,未来可扩展
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
# Chunk 配置(可选)
|
|
124
|
+
'chunk_config': {
|
|
125
|
+
'strategy': 'basic', # 分块策略: 'basic' | 'by_title' | 'by_page'
|
|
126
|
+
'include_orig_elements': False, # 是否包含原始元素
|
|
127
|
+
'new_after_n_chars': 512, # 多少字符后创建新块
|
|
128
|
+
'max_characters': 1024, # 最大字符数
|
|
129
|
+
'overlap': 0 # 重叠字符数
|
|
130
|
+
},
|
|
131
|
+
|
|
132
|
+
# Embed 配置(可选)
|
|
133
|
+
'embed_config': {
|
|
134
|
+
'provider': 'qwen', # 向量化供应商: 'qwen'/'doubao'
|
|
135
|
+
'model_name': 'text-embedding-v3' # 模型名称
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
# 使用配置创建 pipeline
|
|
140
|
+
from xparse_client import create_pipeline_from_config
|
|
141
|
+
pipeline = create_pipeline_from_config(config)
|
|
142
|
+
pipeline.run()
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
详见下文的 [使用示例](#-使用示例) 一章,或参考`example/run_pipeline.py`文件。
|
|
146
|
+
|
|
147
|
+
## 📝 配置说明
|
|
148
|
+
|
|
149
|
+
### Source 配置
|
|
150
|
+
|
|
151
|
+
#### MinIO / S3兼容数据源(OSS/COS/TOS/OBS/S3)
|
|
152
|
+
|
|
153
|
+
1. MinIO
|
|
154
|
+
|
|
155
|
+
接入代码如下:
|
|
156
|
+
```python
|
|
157
|
+
source = S3Source(
|
|
158
|
+
endpoint='https://your-minio-endpoint',
|
|
159
|
+
access_key='IEQspf******mp3AZWl',
|
|
160
|
+
secret_key='kLj96I8FGb**********zBijOJWKWOt1',
|
|
161
|
+
bucket='textin',
|
|
162
|
+
prefix='',
|
|
163
|
+
region='us-east-1',
|
|
164
|
+
pattern='*.pdf' # 可选,使用 Shell 通配符过滤对象
|
|
165
|
+
)
|
|
166
|
+
```
|
|
167
|
+
请确保配置的访问凭证至少包括以下几项权限:
|
|
168
|
+
```
|
|
169
|
+
s3:ListBucket
|
|
170
|
+
s3:GetObject
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
2. 阿里云OSS
|
|
174
|
+
|
|
175
|
+
接入代码示例如下:
|
|
176
|
+
```python
|
|
177
|
+
source = S3Source(
|
|
178
|
+
endpoint='https://s3.oss-cn-shanghai.aliyuncs.com',
|
|
179
|
+
access_key='LTAI5tBg**********bPyuB17',
|
|
180
|
+
secret_key='JFIIaTGiX**********SStofF0S98',
|
|
181
|
+
bucket='textin',
|
|
182
|
+
prefix='',
|
|
183
|
+
region='cn-shanghai',
|
|
184
|
+
pattern='*.pdf' # 可选,使用 Shell 通配符过滤对象
|
|
185
|
+
)
|
|
186
|
+
```
|
|
187
|
+
请确保配置的访问凭证至少包括以下几项权限:
|
|
188
|
+
```
|
|
189
|
+
oss:HeadBucket
|
|
190
|
+
oss:ListObjects
|
|
191
|
+
oss:GetObject
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
3. 腾讯云COS
|
|
195
|
+
|
|
196
|
+
接入代码示例如下:
|
|
197
|
+
```python
|
|
198
|
+
source = S3Source(
|
|
199
|
+
endpoint='https://cos.ap-shanghai.myqcloud.com',
|
|
200
|
+
access_key='AKIDRnws********nlUzHLAmAJ',
|
|
201
|
+
secret_key='we7KJ4bux**********UKxWu3yeDZi',
|
|
202
|
+
bucket='textin',
|
|
203
|
+
prefix='',
|
|
204
|
+
region='ap-shanghai',
|
|
205
|
+
pattern='*.pdf' # 可选,使用 Shell 通配符过滤对象
|
|
206
|
+
)
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
请确保配置的访问凭证至少包括以下几项权限:
|
|
210
|
+
```
|
|
211
|
+
cos:HeadBucket
|
|
212
|
+
cos:GetBucket
|
|
213
|
+
cos:GetObject
|
|
214
|
+
```
|
|
215
|
+
4. 火山引擎TOS
|
|
216
|
+
|
|
217
|
+
接入代码示例如下:
|
|
218
|
+
```python
|
|
219
|
+
source = S3Source(
|
|
220
|
+
endpoint='https://tos-s3-cn-shanghai.volces.com',
|
|
221
|
+
access_key='AKLTMzNkZ**************BjYjZjYzA',
|
|
222
|
+
secret_key='TnpWaE0yRTVa**************RrMFlqVQ==',
|
|
223
|
+
bucket='textin',
|
|
224
|
+
prefix='',
|
|
225
|
+
region='cn-shanghai',
|
|
226
|
+
pattern='*.pdf' # 可选,使用 Shell 通配符过滤对象
|
|
227
|
+
)
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
请确保配置的访问凭证至少包括以下几项权限:
|
|
231
|
+
```
|
|
232
|
+
tos:HeadBucket
|
|
233
|
+
tos:ListBucket
|
|
234
|
+
tos:GetObject
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
5. 华为云OBS
|
|
238
|
+
|
|
239
|
+
接入代码示例如下:
|
|
240
|
+
```python
|
|
241
|
+
source = S3Source(
|
|
242
|
+
endpoint='https://obs.cn-east-3.myhuaweicloud.com',
|
|
243
|
+
access_key='HPUAL6********YAT7JMWY',
|
|
244
|
+
secret_key='z9cm95UXCw**********bwDYz8PVoBGDI',
|
|
245
|
+
bucket='textin',
|
|
246
|
+
prefix='',
|
|
247
|
+
region='cn-east-3',
|
|
248
|
+
pattern='*.pdf' # 可选,使用 Shell 通配符过滤对象
|
|
249
|
+
)
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
请确保配置的访问凭证至少包括以下几项权限:
|
|
253
|
+
```
|
|
254
|
+
HeadBucket
|
|
255
|
+
ListBucket
|
|
256
|
+
GetObject
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
6. AWS S3
|
|
260
|
+
|
|
261
|
+
接入代码示例如下:
|
|
262
|
+
```python
|
|
263
|
+
source = S3Source(
|
|
264
|
+
endpoint='https://s3.us-east-1.amazonaws.com',
|
|
265
|
+
access_key='AKIA6Q******UWA4PO',
|
|
266
|
+
secret_key='OfV4r9/u+CmlLx**************WLADKdPek7',
|
|
267
|
+
bucket='textin-xparse',
|
|
268
|
+
prefix='',
|
|
269
|
+
region='us-east-1',
|
|
270
|
+
pattern='*.pdf' # 可选,使用 Shell 通配符过滤对象
|
|
271
|
+
)
|
|
272
|
+
```
|
|
273
|
+
请确保配置的访问凭证至少包括以下几项权限:
|
|
274
|
+
```
|
|
275
|
+
s3:ListBucket
|
|
276
|
+
s3:GetObject
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
#### 本地文件系统数据源
|
|
281
|
+
|
|
282
|
+
```python
|
|
283
|
+
source = LocalSource(
|
|
284
|
+
directory='./input',
|
|
285
|
+
pattern='*.pdf' # 支持通配符: *.pdf, *.docx, **/*.txt
|
|
286
|
+
)
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
#### FTP数据源
|
|
290
|
+
|
|
291
|
+
```python
|
|
292
|
+
source = FtpSource(
|
|
293
|
+
host='127.0.0.1',
|
|
294
|
+
port=21,
|
|
295
|
+
username='', # 用户名,按照实际填写
|
|
296
|
+
password='', # 密码,按照实际填写
|
|
297
|
+
pattern='*.pdf' # 可选,过滤指定类型文件
|
|
298
|
+
)
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
#### SMB数据源
|
|
302
|
+
|
|
303
|
+
```python
|
|
304
|
+
source = SmbSource(
|
|
305
|
+
host='your-smb-host',
|
|
306
|
+
share_name='your-smb-share-name',
|
|
307
|
+
username='', # 用户名,按照实际填写
|
|
308
|
+
password='', # 密码,按照实际填写
|
|
309
|
+
domain='your-smb-domain',
|
|
310
|
+
pattern='**/*.pdf' # 可选,支持多级匹配
|
|
311
|
+
)
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
> 提示:所有 Source 均支持 `pattern` 参数,使用 Shell 通配符(`*.pdf`、`**/*.txt` 等)来过滤需要处理的文件;默认为 `*`,即处理全部文件。
|
|
315
|
+
|
|
316
|
+
### Destination 配置
|
|
317
|
+
|
|
318
|
+
#### 本地 Milvus 向量存储
|
|
319
|
+
|
|
320
|
+
```python
|
|
321
|
+
destination = MilvusDestination(
|
|
322
|
+
db_path: './milvus_pipeline.db', # 本地数据库文件
|
|
323
|
+
collection_name: 'my_collection', # 数据库collection名称
|
|
324
|
+
dimension: 1024 # 向量维度,需与 embed API 返回一致
|
|
325
|
+
)
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
#### Zilliz 向量存储
|
|
329
|
+
|
|
330
|
+
```python
|
|
331
|
+
destination = MilvusDestination(
|
|
332
|
+
db_path: 'https://xxxxxxx.serverless.xxxxxxx.cloud.zilliz.com.cn', # zilliz连接地址
|
|
333
|
+
collection_name: 'my_collection', # 数据库collection名称
|
|
334
|
+
dimension: 1024, # 向量维度,需与 embed API 返回一致
|
|
335
|
+
api_key: 'your-api-key' # Zilliz Cloud API Key
|
|
336
|
+
)
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
#### 本地文件系统目的地
|
|
340
|
+
|
|
341
|
+
将在配置的本地文件地址中写入`json`文件。
|
|
342
|
+
|
|
343
|
+
```python
|
|
344
|
+
destination = LocalDestination(
|
|
345
|
+
output_dir: './output'
|
|
346
|
+
)
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
#### MinIO / S3兼容数据源(OSS/COS/TOS/OBS/S3)
|
|
350
|
+
|
|
351
|
+
将在配置的本地文件地址中写入`json`文件。
|
|
352
|
+
|
|
353
|
+
配置可参考上文中 Source 的配置,需要注意的是,需要确保配置的访问凭证在上述权限的基础上包括 `PutObject` 权限,例如在使用阿里云OSS时,需要包括以下权限:
|
|
354
|
+
```
|
|
355
|
+
oss:HeadBucket
|
|
356
|
+
oss:ListObjects
|
|
357
|
+
oss:GetObject
|
|
358
|
+
oss:PutObject
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
### API 配置
|
|
362
|
+
|
|
363
|
+
该配置即为pipeline主逻辑接口的请求配置,api_base_url固定为 `https://api.textin.com/api/xparse` ,api_headers中需要填入 [TextIn 开发者信息](https://www.textin.com/console/dashboard/setting) 中获取的 `x-ti-app-id` 与 `x-ti-secret-code`。
|
|
364
|
+
|
|
365
|
+
```python
|
|
366
|
+
'api_base_url': 'https://api.textin.com/api/xparse',
|
|
367
|
+
'api_headers': {
|
|
368
|
+
'x-ti-app-id': 'your-app-id',
|
|
369
|
+
'x-ti-secret-code': 'your-secret-code'
|
|
370
|
+
}
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
## 🔌 API 接口规范
|
|
374
|
+
|
|
375
|
+
### Pipeline 接口(统一接口)
|
|
376
|
+
|
|
377
|
+
**Endpoint:** `POST /api/xparse/pipeline`
|
|
378
|
+
|
|
379
|
+
**请求格式:**
|
|
380
|
+
```
|
|
381
|
+
Content-Type: multipart/form-data
|
|
382
|
+
|
|
383
|
+
file: <binary file>
|
|
384
|
+
stages: [
|
|
385
|
+
{
|
|
386
|
+
"type": "parse",
|
|
387
|
+
"config": {
|
|
388
|
+
"provider": "textin",
|
|
389
|
+
...
|
|
390
|
+
}
|
|
391
|
+
},
|
|
392
|
+
{
|
|
393
|
+
"type": "chunk",
|
|
394
|
+
"config": {
|
|
395
|
+
"strategy": "basic",
|
|
396
|
+
"include_orig_elements": false,
|
|
397
|
+
"new_after_n_chars": 512,
|
|
398
|
+
"max_characters": 1024,
|
|
399
|
+
"overlap": 0
|
|
400
|
+
}
|
|
401
|
+
},
|
|
402
|
+
{
|
|
403
|
+
"type": "embed",
|
|
404
|
+
"config": {
|
|
405
|
+
"provider": "qwen",
|
|
406
|
+
"model_name": "text-embedding-v3"
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
]
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
**Stages 说明:**
|
|
413
|
+
|
|
414
|
+
Pipeline 接口使用 stages 数组来定义处理流程,每个 stage 包含:
|
|
415
|
+
- `type`: 阶段类型,可选值:`parse`、`chunk`、`embed`
|
|
416
|
+
- `parse`节点必选,且顺序必须在第一位
|
|
417
|
+
- `chunk`/`embed`节点可选,若二者同时存在`embed`节点需在`chunk`后面
|
|
418
|
+
- 若不存在`embed`节点且Destination为向量数据库类型(例如`Milvus`),运行时会报错
|
|
419
|
+
- `config`: 该阶段的配置,具体字段取决于阶段类型
|
|
420
|
+
|
|
421
|
+
**各阶段配置:**
|
|
422
|
+
|
|
423
|
+
1. **Parse Stage** (`type: "parse"`)
|
|
424
|
+
|
|
425
|
+
Parse 参数中有必填项`Provider`,表示文档解析服务的供应商,目前可选项如下:
|
|
426
|
+
- textin: 合合信息提供的文档解析服务,在速度、准确性上均为行业领先
|
|
427
|
+
- 支持的文档解析参数参考 [TextIn 文档解析官方API文档](https://docs.textin.com/api-reference/endpoint/parse)
|
|
428
|
+
- 接口调用将按照 `TextIn 通用文档解析` 服务的计费标准进行计费
|
|
429
|
+
- mineru: 敬请期待
|
|
430
|
+
- paddle: 敬请期待
|
|
431
|
+
|
|
432
|
+
2. **Chunk Stage** (`type: "chunk"`)
|
|
433
|
+
|
|
434
|
+
| 参数名 | 类型 / 可选性 | 说明 | 默认值 | 使用场景 / 注意事项 |
|
|
435
|
+
| ------ | ------------- | ---- | ------ | -------------------- |
|
|
436
|
+
| **strategy** | string/必填 | 分块策略 | basic | <br>- `basic`: 基础分块,按字符数分割<br>- `by_title`: 按标题分块,保持章节完整性<br>- `by_page`: 按页面分块,保持页面完整性 |
|
|
437
|
+
| **combine_text_under_n_chars** | `int` / 可选 | 将同一部分中的元素合并成一个数据块,直到该部分的总长度达到指定字符数。 | `None` | 可用于将过短的小块合并成较长文本,提高语义连贯性。 |
|
|
438
|
+
| **include_orig_elements** | `bool` / 可选 | 如果为 `true`,用于构成数据块的原始元素会出现在该数据块的 `.metadata.orig_elements` 中。 | `False` | 用于调试或需要保留原始元素追溯的场景。 |
|
|
439
|
+
| **new_after_n_chars** | `int` / 可选 | 当文本长度达到指定字符数时,强制结束当前章节并开始新的章节(近似限制)。 | `None` | 适用于需要控制章节最大长度的情况下。 |
|
|
440
|
+
| **max_characters** | `int` / 可选 | 数据块中允许的最大字符数上限。 | `None` | 用于硬性限制块大小,避免过大块带来的处理延迟或内存占用。 |
|
|
441
|
+
| **overlap** | `int` / 可选 | 将前一个文本分块末尾指定数量的字符,作为前缀应用到由过大元素分割而成的第二个及后续文本块。 | `None` | 常用于确保分块之间的上下文连续性。 |
|
|
442
|
+
| **overlap_all** | `bool` / 可选 | 如果为 `true`,重叠也会应用到由完整元素组合而成的“普通”块。 | `False` | 谨慎使用,可能在语义上引入噪声。 |
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
3. **Embed Stage** (`type: "embed"`)
|
|
446
|
+
|
|
447
|
+
`xparse-pipeline`当前支持的文本向量化模型如下:
|
|
448
|
+
- `qwen` 供应商,即通义千问:
|
|
449
|
+
- `text-embedding-v3`
|
|
450
|
+
- `text-embedding-v4`
|
|
451
|
+
- `doubao` 供应商,即火山引擎:
|
|
452
|
+
- `doubao-embedding-large-text-250515`
|
|
453
|
+
- `doubao-embedding-text-240715`
|
|
454
|
+
|
|
455
|
+
**返回格式:**
|
|
456
|
+
```json
|
|
457
|
+
{
|
|
458
|
+
"code": 200,
|
|
459
|
+
"msg": "success",
|
|
460
|
+
"data": {
|
|
461
|
+
"elements": [
|
|
462
|
+
{
|
|
463
|
+
"element_id": "f6d5beee53d4f3d90589472974abd7f75c54988c72375cd206f74089391c92b2",
|
|
464
|
+
"type": "plaintext",
|
|
465
|
+
"text": "文本内容",
|
|
466
|
+
"metainfo": {
|
|
467
|
+
"record_id": "08f8e327d05f97e545d04c81d2ef8de1",
|
|
468
|
+
...
|
|
469
|
+
},
|
|
470
|
+
"embeddings": [0.1, 0.2, 0.3, ...]
|
|
471
|
+
}
|
|
472
|
+
],
|
|
473
|
+
"stats": {
|
|
474
|
+
"original_elements": 10, // 原始解析的元素数量
|
|
475
|
+
"chunked_elements": 15, // 分块后的元素数量
|
|
476
|
+
"embedded_elements": 15, // 向量化后的元素数量
|
|
477
|
+
"parse_config": { // 使用的 parse 配置
|
|
478
|
+
"provider": "textin"
|
|
479
|
+
},
|
|
480
|
+
"chunk_config": { // 使用的 chunk 配置
|
|
481
|
+
"strategy": "basic",
|
|
482
|
+
"include_orig_elements": false,
|
|
483
|
+
"new_after_n_chars": 512,
|
|
484
|
+
"max_characters": 1024,
|
|
485
|
+
"overlap": 0
|
|
486
|
+
},
|
|
487
|
+
"embed_config": { // 使用的 embed 配置
|
|
488
|
+
"provider": "qwen",
|
|
489
|
+
"model_name": "text-embedding-v3"
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
## 💡 使用示例
|
|
497
|
+
|
|
498
|
+
### 示例 1: 使用 config 字典配置(推荐)
|
|
499
|
+
|
|
500
|
+
```python
|
|
501
|
+
from xparse_client import create_pipeline_from_config
|
|
502
|
+
|
|
503
|
+
# 完整的配置示例
|
|
504
|
+
config = {
|
|
505
|
+
# S3 数据源配置
|
|
506
|
+
'source': {
|
|
507
|
+
'type': 's3',
|
|
508
|
+
'endpoint': 'https://your-minio.com',
|
|
509
|
+
'access_key': 'your-access-key',
|
|
510
|
+
'secret_key': 'your-secret-key',
|
|
511
|
+
'bucket': 'documents',
|
|
512
|
+
'prefix': 'pdfs/',
|
|
513
|
+
'region': 'us-east-1',
|
|
514
|
+
'pattern': '*.pdf' # 仅处理匹配的文件
|
|
515
|
+
},
|
|
516
|
+
|
|
517
|
+
# Milvus 目的地配置
|
|
518
|
+
'destination': {
|
|
519
|
+
'type': 'milvus',
|
|
520
|
+
'db_path': './vectors.db',
|
|
521
|
+
'collection_name': 'documents',
|
|
522
|
+
'dimension': 1024
|
|
523
|
+
},
|
|
524
|
+
|
|
525
|
+
# API 配置
|
|
526
|
+
'api_base_url': 'https://api.textin.com/api/xparse',
|
|
527
|
+
'api_headers': {
|
|
528
|
+
'x-ti-app-id': 'your-app-id',
|
|
529
|
+
'x-ti-secret-code': 'your-secret-code'
|
|
530
|
+
},
|
|
531
|
+
|
|
532
|
+
# Parse 配置(可选)
|
|
533
|
+
'parse_config': {
|
|
534
|
+
'provider': 'textin'
|
|
535
|
+
},
|
|
536
|
+
|
|
537
|
+
# Chunk 配置(可选)
|
|
538
|
+
'chunk_config': {
|
|
539
|
+
'strategy': 'by_title', # 按标题分块
|
|
540
|
+
'include_orig_elements': False,
|
|
541
|
+
'new_after_n_chars': 512,
|
|
542
|
+
'max_characters': 1024,
|
|
543
|
+
'overlap': 50 # 块之间重叠 50 字符
|
|
544
|
+
},
|
|
545
|
+
|
|
546
|
+
# Embed 配置(可选)
|
|
547
|
+
'embed_config': {
|
|
548
|
+
'provider': 'qwen',
|
|
549
|
+
'model_name': 'text-embedding-v3'
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
# 使用配置创建并运行 pipeline
|
|
554
|
+
pipeline = create_pipeline_from_config(config)
|
|
555
|
+
pipeline.run()
|
|
556
|
+
```
|
|
557
|
+
|
|
558
|
+
### 示例 2: 本地到本地(测试)
|
|
559
|
+
|
|
560
|
+
```python
|
|
561
|
+
from datetime import datetime, timezone
|
|
562
|
+
from xparse_client import create_pipeline_from_config
|
|
563
|
+
|
|
564
|
+
config = {
|
|
565
|
+
'source': {
|
|
566
|
+
'type': 'local',
|
|
567
|
+
'directory': './test_files',
|
|
568
|
+
'pattern': '*.pdf'
|
|
569
|
+
},
|
|
570
|
+
'destination': {
|
|
571
|
+
'type': 'local',
|
|
572
|
+
'output_dir': './test_output'
|
|
573
|
+
},
|
|
574
|
+
'api_base_url': 'https://api.textin.com/api/xparse',
|
|
575
|
+
# 使用默认的 chunk 和 embed 配置
|
|
576
|
+
'chunk_config': {
|
|
577
|
+
'strategy': 'basic',
|
|
578
|
+
'max_characters': 1024
|
|
579
|
+
},
|
|
580
|
+
'embed_config': {
|
|
581
|
+
'provider': 'qwen',
|
|
582
|
+
'model_name': 'text-embedding-v3'
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
pipeline = create_pipeline_from_config(config)
|
|
587
|
+
pipeline.run()
|
|
588
|
+
```
|
|
589
|
+
|
|
590
|
+
### 示例 3: 不同分块策略的配置
|
|
591
|
+
|
|
592
|
+
```python
|
|
593
|
+
from xparse_client import create_pipeline_from_config
|
|
594
|
+
|
|
595
|
+
# 配置 1:按页面分块(适合 PDF 文档)
|
|
596
|
+
config_by_page = {
|
|
597
|
+
'source': {...},
|
|
598
|
+
'destination': {...},
|
|
599
|
+
'api_base_url': 'https://api.textin.com/api/xparse',
|
|
600
|
+
'api_headers': {...},
|
|
601
|
+
'chunk_config': {
|
|
602
|
+
'strategy': 'by_page', # 按页面分块
|
|
603
|
+
'max_characters': 2048, # 增大块大小
|
|
604
|
+
'overlap': 100 # 页面间重叠 100 字符
|
|
605
|
+
},
|
|
606
|
+
'embed_config': {
|
|
607
|
+
'model_name': 'text-embedding-v4' # 使用更高精度的模型
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
# 配置 2:按标题分块(适合结构化文档)
|
|
612
|
+
config_by_title = {
|
|
613
|
+
'source': {...},
|
|
614
|
+
'destination': {...},
|
|
615
|
+
'api_base_url': 'https://api.textin.com/api/xparse',
|
|
616
|
+
'api_headers': {...},
|
|
617
|
+
'chunk_config': {
|
|
618
|
+
'strategy': 'by_title', # 按标题分块
|
|
619
|
+
'include_orig_elements': True, # 保留原始元素信息
|
|
620
|
+
'max_characters': 1536
|
|
621
|
+
},
|
|
622
|
+
'embed_config': {
|
|
623
|
+
'provider': 'qwen',
|
|
624
|
+
'model_name': 'text-embedding-v3'
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
# 根据文档类型选择配置
|
|
629
|
+
pipeline = create_pipeline_from_config(config_by_page)
|
|
630
|
+
pipeline.run()
|
|
631
|
+
```
|
|
632
|
+
|
|
633
|
+
### 示例 4: FTP 数据源配置
|
|
634
|
+
|
|
635
|
+
```python
|
|
636
|
+
from xparse_client import create_pipeline_from_config
|
|
637
|
+
|
|
638
|
+
config = {
|
|
639
|
+
# FTP 数据源
|
|
640
|
+
'source': {
|
|
641
|
+
'type': 'ftp',
|
|
642
|
+
'host': 'ftp.example.com',
|
|
643
|
+
'port': 21,
|
|
644
|
+
'username': 'user',
|
|
645
|
+
'password': 'pass'
|
|
646
|
+
},
|
|
647
|
+
|
|
648
|
+
# Milvus 目的地
|
|
649
|
+
'destination': {
|
|
650
|
+
'type': 'milvus',
|
|
651
|
+
'db_path': './vectors.db',
|
|
652
|
+
'collection_name': 'ftp_docs',
|
|
653
|
+
'dimension': 1024
|
|
654
|
+
},
|
|
655
|
+
|
|
656
|
+
'api_base_url': 'https://api.textin.com/api/xparse',
|
|
657
|
+
'api_headers': {
|
|
658
|
+
'x-ti-app-id': 'app-id',
|
|
659
|
+
'x-ti-secret-code': 'secret'
|
|
660
|
+
},
|
|
661
|
+
|
|
662
|
+
# 配置处理参数
|
|
663
|
+
'chunk_config': {
|
|
664
|
+
'strategy': 'basic',
|
|
665
|
+
'max_characters': 1024
|
|
666
|
+
},
|
|
667
|
+
'embed_config': {
|
|
668
|
+
'provider': 'qwen',
|
|
669
|
+
'model_name': 'text-embedding-v3'
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
pipeline = create_pipeline_from_config(config)
|
|
674
|
+
pipeline.run()
|
|
675
|
+
```
|
|
676
|
+
|
|
677
|
+
### 示例 5: 获取处理统计信息
|
|
678
|
+
|
|
679
|
+
```python
|
|
680
|
+
from xparse_client import create_pipeline_from_config
|
|
681
|
+
|
|
682
|
+
config = {
|
|
683
|
+
'source': {
|
|
684
|
+
'type': 'local',
|
|
685
|
+
'directory': './docs',
|
|
686
|
+
'pattern': '*.pdf'
|
|
687
|
+
},
|
|
688
|
+
'destination': {
|
|
689
|
+
'type': 'local',
|
|
690
|
+
'output_dir': './output'
|
|
691
|
+
},
|
|
692
|
+
'api_base_url': 'https://api.textin.com/api/xparse',
|
|
693
|
+
'chunk_config': {
|
|
694
|
+
'strategy': 'basic',
|
|
695
|
+
'max_characters': 1024
|
|
696
|
+
},
|
|
697
|
+
'embed_config': {
|
|
698
|
+
'provider': 'qwen',
|
|
699
|
+
'model_name': 'text-embedding-v3'
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
pipeline = create_pipeline_from_config(config)
|
|
704
|
+
|
|
705
|
+
# 处理单个文件并获取统计信息
|
|
706
|
+
file_bytes, data_source = pipeline.source.read_file('document.pdf')
|
|
707
|
+
data_source['date_processed'] = datetime.now(timezone.utc).timestamp()
|
|
708
|
+
result = pipeline.process_with_pipeline(file_bytes, 'document.pdf', data_source)
|
|
709
|
+
|
|
710
|
+
if result:
|
|
711
|
+
elements, stats = result
|
|
712
|
+
print(f"原始元素: {stats.original_elements}")
|
|
713
|
+
print(f"分块后: {stats.chunked_elements}")
|
|
714
|
+
print(f"向量化: {stats.embedded_elements}")
|
|
715
|
+
print(f"使用配置:")
|
|
716
|
+
print(f" - 分块策略: {stats.chunk_config.strategy}")
|
|
717
|
+
print(f" - 向量模型: {stats.embed_config.model_name}")
|
|
718
|
+
|
|
719
|
+
# 写入目的地
|
|
720
|
+
metadata = {
|
|
721
|
+
'file_name': 'document.pdf',
|
|
722
|
+
'data_source': data_source
|
|
723
|
+
}
|
|
724
|
+
pipeline.destination.write(elements, metadata)
|
|
725
|
+
```
|
|
726
|
+
|
|
727
|
+
## 📊 Pipeline 统计信息
|
|
728
|
+
|
|
729
|
+
Pipeline 接口会返回详细的处理统计信息:
|
|
730
|
+
|
|
731
|
+
| 字段 | 类型 | 说明 |
|
|
732
|
+
|------|------|------|
|
|
733
|
+
| `original_elements` | int | 原始解析的元素数量 |
|
|
734
|
+
| `chunked_elements` | int | 分块后的元素数量 |
|
|
735
|
+
| `embedded_elements` | int | 向量化后的元素数量 |
|
|
736
|
+
| `parse_config` | ParseConfig | 使用的解析配置 |
|
|
737
|
+
| `chunk_config` | ChunkConfig | 使用的分块配置 |
|
|
738
|
+
| `embed_config` | EmbedConfig | 使用的向量化配置 |
|
|
739
|
+
|
|
740
|
+
**示例输出:**
|
|
741
|
+
```
|
|
742
|
+
✓ Pipeline 完成:
|
|
743
|
+
- 原始元素: 25
|
|
744
|
+
- 分块后: 42
|
|
745
|
+
- 向量化: 42
|
|
746
|
+
✓ 写入 Milvus: 42 条
|
|
747
|
+
```
|
|
748
|
+
|
|
749
|
+
## 🔧 扩展开发
|
|
750
|
+
|
|
751
|
+
### 添加新的 Source
|
|
752
|
+
|
|
753
|
+
```python
|
|
754
|
+
from typing import List, Dict, Any, Tuple
|
|
755
|
+
from xparse_client import Source
|
|
756
|
+
|
|
757
|
+
class MyCustomSource(Source):
|
|
758
|
+
def __init__(self, custom_param):
|
|
759
|
+
self.custom_param = custom_param
|
|
760
|
+
|
|
761
|
+
def list_files(self) -> List[str]:
|
|
762
|
+
# 实现文件列表逻辑
|
|
763
|
+
return ['file1.pdf', 'file2.pdf']
|
|
764
|
+
|
|
765
|
+
def read_file(self, file_path: str) -> Tuple[bytes, Dict[str, Any]]:
|
|
766
|
+
# 实现文件读取逻辑并返回数据来源信息
|
|
767
|
+
data_source = {
|
|
768
|
+
'url': f'custom://{file_path}',
|
|
769
|
+
'version': None,
|
|
770
|
+
'date_created': None,
|
|
771
|
+
'date_modified': None,
|
|
772
|
+
'record_locator': {
|
|
773
|
+
'protocol': 'custom',
|
|
774
|
+
'remote_file_path': file_path
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
return b'file content', data_source
|
|
778
|
+
```
|
|
779
|
+
|
|
780
|
+
### 添加新的 Destination
|
|
781
|
+
|
|
782
|
+
```python
|
|
783
|
+
from xparse_client import Destination
|
|
784
|
+
|
|
785
|
+
class MyCustomDestination(Destination):
|
|
786
|
+
def __init__(self, custom_param):
|
|
787
|
+
self.custom_param = custom_param
|
|
788
|
+
|
|
789
|
+
def write(self, data: List[Dict], metadata: Dict) -> bool:
|
|
790
|
+
# 实现数据写入逻辑
|
|
791
|
+
return True
|
|
792
|
+
```
|
|
793
|
+
|
|
794
|
+
## 📊 数据格式
|
|
795
|
+
|
|
796
|
+
### 元素格式
|
|
797
|
+
|
|
798
|
+
每个处理步骤都使用统一的元素格式:
|
|
799
|
+
|
|
800
|
+
```python
|
|
801
|
+
{
|
|
802
|
+
"element_id": str, # 唯一标识符
|
|
803
|
+
"type": str, # 元素类型: plaintext, table, image, etc.
|
|
804
|
+
"text": str, # 文本内容
|
|
805
|
+
"metainfo": { # 元数据
|
|
806
|
+
"filename": str,
|
|
807
|
+
"orig_elements": list, # chunk处理后添加
|
|
808
|
+
# 其他字段
|
|
809
|
+
},
|
|
810
|
+
"embeddings": list # 向量(embed 步骤后添加)
|
|
811
|
+
}
|
|
812
|
+
```
|
|
813
|
+
|
|
814
|
+
## ⚠️ 注意事项
|
|
815
|
+
|
|
816
|
+
1. **API 端点**:确保 API 服务正常运行并可访问,目前需要固定使用`https://api.textin.com/api/xparse`,同时需要配置请求头上的app-id/secret-code
|
|
817
|
+
2. **向量维度**:Milvus 的 dimension 必须与 pipeline API 返回的向量维度一致,目前pipeline API使用的是1024维度
|
|
818
|
+
3. **写入Milvus**:确保目标collection中包含`element_id`,`text`,`record_id`,`embeddings`,`metadata`这些字段
|
|
819
|
+
4. **错误重试**:默认每个 API 调用失败会重试 3 次
|
|
820
|
+
|
|
821
|
+
## 💰 计费
|
|
822
|
+
|
|
823
|
+
Pipeline接口调用将按页进行计费,具体计费标准可以参考:[通用文档解析](https://www.textin.com/market/detail/xparse)。
|
|
824
|
+
|
|
825
|
+
## 🐛 故障排除
|
|
826
|
+
|
|
827
|
+
### API 连接失败
|
|
828
|
+
|
|
829
|
+
- 检查 `api_base_url` 是否正确
|
|
830
|
+
- 确认网络连接正常
|
|
831
|
+
- 查看 API 服务日志
|
|
832
|
+
|
|
833
|
+
### S3 连接失败
|
|
834
|
+
|
|
835
|
+
- 验证 endpoint、access_key、secret_key
|
|
836
|
+
- 确认 bucket 存在且有访问权限
|
|
837
|
+
|
|
838
|
+
### FTP 连接失败
|
|
839
|
+
|
|
840
|
+
- 验证路径端口是否正确
|
|
841
|
+
- 确认用户名密码是否正确
|
|
842
|
+
|
|
843
|
+
### 本地文件找不到
|
|
844
|
+
|
|
845
|
+
- 确认路径正确
|
|
846
|
+
- 检查文件匹配模式
|
|
847
|
+
- 验证文件权限
|
|
848
|
+
|
|
849
|
+
### Milvus 写入失败
|
|
850
|
+
|
|
851
|
+
- 检查向量维度是否匹配
|
|
852
|
+
- 确认必须字段是否存在
|
|
853
|
+
- 查看 Milvus 日志
|
|
854
|
+
|
|
855
|
+
## 🔗 相关文件
|
|
856
|
+
|
|
857
|
+
- `core.py` - 核心 Pipeline 实现
|
|
858
|
+
- `run_pipeline.py` - 运行示例
|
|
859
|
+
|
|
860
|
+
## 📄 License
|
|
861
|
+
|
|
862
|
+
MIT License
|
|
863
|
+
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
xparse_client/__init__.py,sha256=9xmthqT95qlxUT0i2KNwTR4ze4031aj9he1dEv6haHg,843
|
|
2
|
+
xparse_client-0.2.0.dist-info/licenses/LICENSE,sha256=ckIP-MbocsP9nqYnta5KgfAicYF196B5TNdHIR6kOO0,1075
|
|
3
|
+
xparse_client-0.2.0.dist-info/METADATA,sha256=zwQomwSi4EatL0oBfpercGcI7IAinHuXvuuVpCkYrVY,24415
|
|
4
|
+
xparse_client-0.2.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
5
|
+
xparse_client-0.2.0.dist-info/top_level.txt,sha256=W5PeQwOyfo_Od3d26-gcOtan7rHYk1q3SP1phYedat4,14
|
|
6
|
+
xparse_client-0.2.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 ACG Xparse Authors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
xparse_client
|