-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPKG-INFO
More file actions
1050 lines (882 loc) · 28.1 KB
/
PKG-INFO
File metadata and controls
1050 lines (882 loc) · 28.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Metadata-Version: 2.4
Name: xparse-client
Version: 0.2.20
Summary: 面向Agent和RAG的新一代文档处理 AI Infra
License-Expression: MIT
Project-URL: Homepage, https://gitlab.intsig.net/xparse1/xparse-pipeline
Project-URL: Repository, https://gitlab.intsig.net/xparse1/xparse-pipeline
Keywords: xparse,pipeline,rag
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: boto3
Requires-Dist: pymilvus
Requires-Dist: milvus-lite; sys_platform != "win32"
Requires-Dist: requests
Requires-Dist: pysmb
Requires-Dist: qdrant-client
Dynamic: license-file
# xParse
面向Agent和RAG的新一代文档处理 AI Infra。
xParse的同步pipeline实现,支持多种数据源与输出。
## 🌟 特点
- **灵活的数据源**:支持兼容 S3 协议的对象存储、本地文件系统以及 FTP/SMB 协议文件系统
- **灵活的输出**:支持 Milvus/Zilliz/Qdrant 向量数据库、兼容 S3 协议的对象存储以及本地文件系统
- **统一 Pipeline API**:使用 `/api/xparse/pipeline` 一次性完成 parse → chunk → embed 全流程
- **配置化处理**:支持灵活配置 parse、chunk、embed 参数
- **详细统计信息**:返回每个阶段的处理统计数据
- **易于扩展**:基于抽象类,可轻松添加新的 Source 和 Destination
- **完整日志**:详细的处理日志和错误追踪
## 📋 架构
```
┌──────────────┐
│ Source │ 数据源(S3/本地/FTP)
└──────┬───────┘
│ read_file()
▼
┌──────────────────────────────────────┐
│ Pipeline API │
│ /api/xparse/pipeline │
│ │
│ ┌────────┐ ┌────────┐ ┌────────┐ │ ┌────────┐
│ │ Parse │→ │ Chunk │→ │ Embed │ |────→│ Deduct │ 计费
│ └────────┘ └────────┘ └────────┘ │ └────────┘
│ │
└──────────────┬───────────────────────┘
│ [embeddings + stats]
▼
┌──────────────┐
│ Destination │ 目的地(Milvus/Zilliz/Qdrant/本地)
└──────────────┘
```
## 🚀 快速开始
### 1. 安装依赖
```bash
pip install --upgrade xparse-client
```
### 2. 运行
`xparse-client`支持两种配置方式,即通过代码配置,以及直接通过config字典配置
#### 代码配置
```python
from xparse_client import ParseConfig, ChunkConfig, EmbedConfig, Stage, Pipeline, S3Source, MilvusDestination, QdrantDestination
# 使用新的 stages 格式创建配置
stages = [
Stage(
type='parse',
config=ParseConfig(provider='textin')
),
Stage(
type='chunk',
config=ChunkConfig(
strategy='by_title',
include_orig_elements=False,
new_after_n_chars=512,
max_characters=1024,
overlap=50
)
),
Stage(
type='embed',
config=EmbedConfig(
provider='qwen',
model_name='text-embedding-v4'
)
)
]
# 创建 Pipeline
source = S3Source(...)
destination = MilvusDestination(...)
pipeline = Pipeline(
source=source,
destination=destination,
api_base_url='https://api.textin.com/api/xparse',
api_headers={...},
stages=stages
)
pipeline.run()
```
#### 字典配置
```python
config = {
'source': {...},
'destination': {...},
'api_base_url': 'https://api.textin.com/api/xparse',
'api_headers': {...},
# Stages 配置
'stages': [
{
'type': 'parse',
'config': {
'provider': 'textin' # 当前支持textin文档解析,未来可扩展
}
},
{
'type': 'chunk',
'config': {
'strategy': 'basic', # 分块策略: 'basic' | 'by_title' | 'by_page'
'include_orig_elements': False, # 是否包含原始元素
'new_after_n_chars': 512, # 多少字符后创建新块
'max_characters': 1024, # 最大字符数
'overlap': 0 # 重叠字符数
}
},
{
'type': 'embed',
'config': {
'provider': 'qwen', # 向量化供应商: 'qwen'/'doubao'
'model_name': 'text-embedding-v3' # 模型名称
}
}
]
}
# 使用配置创建 pipeline
from xparse_client import create_pipeline_from_config
pipeline = create_pipeline_from_config(config)
pipeline.run()
```
详见下文的 [使用示例](#-使用示例) 一章,或参考`example/run_pipeline.py`文件。
## 📝 配置说明
### Source 配置
#### MinIO / S3兼容数据源(OSS/COS/TOS/OBS/S3)
1. MinIO
接入代码如下:
```python
source = S3Source(
endpoint='https://your-minio-endpoint',
access_key='IEQspf******mp3AZWl',
secret_key='kLj96I8FGb**********zBijOJWKWOt1',
bucket='textin',
prefix='',
region='us-east-1',
pattern=['*.pdf'] # 可选,通配符模式列表,支持多个扩展名
)
```
请确保配置的访问凭证至少包括以下几项权限:
```
s3:ListBucket
s3:GetObject
```
2. 阿里云OSS
接入代码示例如下:
```python
source = S3Source(
endpoint='https://s3.oss-cn-shanghai.aliyuncs.com',
access_key='LTAI5tBg**********bPyuB17',
secret_key='JFIIaTGiX**********SStofF0S98',
bucket='textin',
prefix='',
region='cn-shanghai',
pattern=['*.pdf'] # 可选,通配符模式列表,支持多个扩展名
)
```
请确保配置的访问凭证至少包括以下几项权限:
```
oss:HeadBucket
oss:ListObjects
oss:GetObject
```
3. 腾讯云COS
接入代码示例如下:
```python
source = S3Source(
endpoint='https://cos.ap-shanghai.myqcloud.com',
access_key='AKIDRnws********nlUzHLAmAJ',
secret_key='we7KJ4bux**********UKxWu3yeDZi',
bucket='textin',
prefix='',
region='ap-shanghai',
pattern=['*.pdf'] # 可选,通配符模式列表,支持多个扩展名
)
```
请确保配置的访问凭证至少包括以下几项权限:
```
cos:HeadBucket
cos:GetBucket
cos:GetObject
```
4. 火山引擎TOS
接入代码示例如下:
```python
source = S3Source(
endpoint='https://tos-s3-cn-shanghai.volces.com',
access_key='AKLTMzNkZ**************BjYjZjYzA',
secret_key='TnpWaE0yRTVa**************RrMFlqVQ==',
bucket='textin',
prefix='',
region='cn-shanghai',
pattern=['*.pdf'] # 可选,通配符模式列表,支持多个扩展名
)
```
请确保配置的访问凭证至少包括以下几项权限:
```
tos:HeadBucket
tos:ListBucket
tos:GetObject
```
5. 华为云OBS
接入代码示例如下:
```python
source = S3Source(
endpoint='https://obs.cn-east-3.myhuaweicloud.com',
access_key='HPUAL6********YAT7JMWY',
secret_key='z9cm95UXCw**********bwDYz8PVoBGDI',
bucket='textin',
prefix='',
region='cn-east-3',
pattern=['*.pdf'] # 可选,通配符模式列表,支持多个扩展名
)
```
请确保配置的访问凭证至少包括以下几项权限:
```
HeadBucket
ListBucket
GetObject
```
6. AWS S3
接入代码示例如下:
```python
source = S3Source(
endpoint='https://s3.us-east-1.amazonaws.com',
access_key='AKIA6Q******UWA4PO',
secret_key='OfV4r9/u+CmlLx**************WLADKdPek7',
bucket='textin-xparse',
prefix='',
region='us-east-1',
pattern=['*.pdf'] # 可选,通配符模式列表,支持多个扩展名
)
```
请确保配置的访问凭证至少包括以下几项权限:
```
s3:ListBucket
s3:GetObject
```
#### 本地文件系统数据源
```python
source = LocalSource(
directory='./input',
pattern=['*.pdf', '*.docx'] # 支持多个通配符模式列表
)
```
#### FTP数据源
```python
source = FtpSource(
host='127.0.0.1',
port=21,
username='', # 用户名,按照实际填写
password='', # 密码,按照实际填写
pattern=['*.pdf'] # 可选,通配符模式列表,过滤指定类型文件
)
```
#### SMB数据源
```python
source = SmbSource(
host='your-smb-host',
share_name='your-smb-share-name',
username='', # 用户名,按照实际填写
password='', # 密码,按照实际填写
domain='your-smb-domain',
pattern=['**/*.pdf'] # 可选,通配符模式列表,支持多级匹配
)
```
> 注 1:所有 Source 均支持 `pattern` 参数,使用通配符模式列表(如 `['*.pdf', '*.docx']`)来过滤需要处理的文件。支持多个通配符模式,如果列表中包含 `'*'` 则匹配所有文件。默认为 `None`,即处理全部文件。
> 注 2:所有 Source 均支持 `recursive` 参数,表示是否递归遍历,默认为 `False`。
### Destination 配置
#### 本地 Milvus 向量存储
collection 中至少需要包含 `element_id`,`text`,`embeddings`,`record_id` 四个字段。
```python
destination = MilvusDestination(
db_path='./milvus_pipeline.db', # 本地数据库文件
collection_name='my_collection', # 数据库collection名称
dimension=1024 # 向量维度,需与 embed API 返回一致
)
```
#### Zilliz 向量存储
collection 中至少需要包含 `element_id`,`text`,`embeddings`,`record_id` 四个字段。
```python
destination = MilvusDestination(
db_path='https://xxxxxxx.serverless.xxxxxxx.cloud.zilliz.com.cn', # zilliz连接地址
collection_name='my_collection', # 数据库collection名称
dimension=1024, # 向量维度,需与 embed API 返回一致
api_key='your-api-key' # Zilliz Cloud API Key
)
```
#### Qdrant 向量存储
```python
destination = QdrantDestination(
url='http://localhost:6333', # Qdrant 服务地址(本地或云端)
collection_name='my_collection', # Collection 名称
dimension=1024, # 向量维度,需与 embed API 返回一致
api_key='your-api-key', # 可选,Qdrant Cloud API Key
prefer_grpc=False # 可选,是否优先使用 gRPC(默认 False)
)
```
**Qdrant Cloud 示例:**
```python
destination = QdrantDestination(
url='https://xxxxxxx.us-east-1-0.aws.cloud.qdrant.io',
collection_name='my_collection',
dimension=1024,
api_key='your-api-key'
)
```
#### 本地文件系统目的地
将在配置的本地文件地址中写入`json`文件。
```python
destination = LocalDestination(
output_dir='./output'
)
```
#### MinIO / S3兼容数据源(OSS/COS/TOS/OBS/S3)
将在配置的本地文件地址中写入`json`文件。
配置可参考上文中 Source 的配置,需要注意的是,需要确保配置的访问凭证在上述权限的基础上包括 `PutObject` 权限,例如在使用阿里云OSS时,需要包括以下权限:
```
oss:HeadBucket
oss:ListObjects
oss:GetObject
oss:PutObject
```
### API 配置
该配置即为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`。
```python
'api_base_url': 'https://api.textin.com/api/xparse',
'api_headers': {
'x-ti-app-id': 'your-app-id',
'x-ti-secret-code': 'your-secret-code'
}
```
## 🔌 API 接口规范
### Pipeline 接口(统一接口)
**Endpoint:** `POST /api/xparse/pipeline`
**请求格式:**
```
Content-Type: multipart/form-data
file: <binary file>
stages: [
{
"type": "parse",
"config": {
"provider": "textin",
...
}
},
{
"type": "chunk",
"config": {
"strategy": "basic",
"include_orig_elements": false,
"new_after_n_chars": 512,
"max_characters": 1024,
"overlap": 0
}
},
{
"type": "embed",
"config": {
"provider": "qwen",
"model_name": "text-embedding-v3"
}
}
]
```
**Stages 说明:**
Pipeline 接口使用 stages 数组来定义处理流程,每个 stage 包含:
- `type`: 阶段类型,可选值:`parse`、`chunk`、`embed`
- `parse`节点必选,且顺序必须在第一位
- `chunk`/`embed`节点可选,若二者同时存在`embed`节点需在`chunk`后面
- 若不存在`embed`节点且Destination为向量数据库类型(例如`Milvus`),运行时会报错
- `config`: 该阶段的配置,具体字段取决于阶段类型
**各阶段配置:**
1. **Parse Stage** (`type: "parse"`)
Parse 参数中有必填项`Provider`,表示文档解析服务的供应商,目前可选项如下:
- textin: 合合信息提供的文档解析服务,在速度、准确性上均为行业领先
- 支持的文档解析参数参考 [TextIn 文档解析官方API文档](https://docs.textin.com/api-reference/endpoint/parse)
- 接口调用将按照 `TextIn 通用文档解析` 服务的计费标准进行计费
- textin-lite:
- 接口调用将按照 `TextIn 通用表格识别` 服务的计费标准进行计费
- mineru:
- 接口调用将按照 `TextIn 通用文档解析` 服务的计费标准进行计费
- paddle:
- 接口调用将按照 `TextIn 通用文档解析` 服务的计费标准进行计费
2. **Chunk Stage** (`type: "chunk"`)
| 参数名 | 类型 / 可选性 | 说明 | 默认值 | 使用场景 / 注意事项 |
| ------ | ------------- | ---- | ------ | -------------------- |
| **strategy** | string/必填 | 分块策略 | basic | <br>- `basic`: 基础分块,按字符数分割<br>- `by_title`: 按标题分块,保持章节完整性<br>- `by_page`: 按页面分块,保持页面完整性 |
| **combine_text_under_n_chars** | `int` / 可选 | 将同一部分中的元素合并成一个数据块,直到该部分的总长度达到指定字符数。 | `None` | 可用于将过短的小块合并成较长文本,提高语义连贯性。 |
| **include_orig_elements** | `bool` / 可选 | 如果为 `true`,用于构成数据块的原始元素会出现在该数据块的 `.metadata.orig_elements` 中。 | `False` | 用于调试或需要保留原始元素追溯的场景。 |
| **new_after_n_chars** | `int` / 可选 | 当文本长度达到指定字符数时,强制结束当前章节并开始新的章节(近似限制)。 | `None` | 适用于需要控制章节最大长度的情况下。 |
| **max_characters** | `int` / 可选 | 数据块中允许的最大字符数上限。 | `None` | 用于硬性限制块大小,避免过大块带来的处理延迟或内存占用。 |
| **overlap** | `int` / 可选 | 将前一个文本分块末尾指定数量的字符,作为前缀应用到由过大元素分割而成的第二个及后续文本块。 | `None` | 常用于确保分块之间的上下文连续性。 |
| **overlap_all** | `bool` / 可选 | 如果为 `true`,重叠也会应用到由完整元素组合而成的“普通”块。 | `False` | 谨慎使用,可能在语义上引入噪声。 |
3. **Embed Stage** (`type: "embed"`)
`xparse-pipeline`当前支持的文本向量化模型如下:
- `qwen` 供应商,即通义千问:
- `text-embedding-v3`
- `text-embedding-v4`
- `doubao` 供应商,即火山引擎:
- `doubao-embedding-large-text-250515`
- `doubao-embedding-text-240715`
**返回格式:**
```json
{
"code": 200,
"msg": "success",
"data": {
"elements": [
{
"element_id": "f6d5beee53d4f3d90589472974abd7f75c54988c72375cd206f74089391c92b2",
"type": "plaintext",
"text": "文本内容",
"metainfo": {
"record_id": "08f8e327d05f97e545d04c81d2ef8de1",
...
},
"embeddings": [0.1, 0.2, 0.3, ...]
}
],
"stats": {
"original_elements": 10, // 原始解析的元素数量
"chunked_elements": 15, // 分块后的元素数量
"embedded_elements": 15, // 向量化后的元素数量
"stages": [
{
"type": "parse",
"config": {
"provider": "textin-lite"
}
},
{
"type": "chunk",
"config": {
"strategy": "by_title"
}
}
]
}
}
}
```
## 💡 使用示例
### 示例 1: 手动创建 Pipeline(推荐)
```python
from xparse_client import (
Pipeline, S3Source, MilvusDestination,
ParseConfig, ChunkConfig, EmbedConfig, Stage
)
# 创建数据源
source = S3Source(
endpoint='https://your-minio.com',
access_key='your-access-key',
secret_key='your-secret-key',
bucket='documents',
prefix='pdfs/',
region='us-east-1',
pattern=['*.pdf'], # 仅处理匹配的文件
recursive=False # 不递归子目录
)
# 创建目的地
destination = MilvusDestination(
db_path='./vectors.db',
collection_name='documents',
dimension=1024
)
# 配置处理阶段
stages = [
Stage(
type='parse',
config=ParseConfig(provider='textin')
),
Stage(
type='chunk',
config=ChunkConfig(
strategy='by_title', # 按标题分块
include_orig_elements=False,
new_after_n_chars=512,
max_characters=1024,
overlap=50 # 块之间重叠 50 字符
)
),
Stage(
type='embed',
config=EmbedConfig(
provider='qwen',
model_name='text-embedding-v3'
)
)
]
# 创建并运行 Pipeline
pipeline = Pipeline(
source=source,
destination=destination,
api_base_url='https://api.textin.com/api/xparse',
api_headers={
'x-ti-app-id': 'your-app-id',
'x-ti-secret-code': 'your-secret-code'
},
stages=stages
)
pipeline.run()
```
### 示例 1.1: 输出配置字典
手动创建 Pipeline 后,可以使用 `get_config()` 方法获取配置字典:
```python
from xparse_client import (
Pipeline, LocalSource, LocalDestination,
ParseConfig, ChunkConfig, EmbedConfig, Stage
)
# 手动创建 Pipeline
source = LocalSource(
directory='./test_files',
pattern=['*.pdf'],
recursive=False
)
destination = LocalDestination(output_dir='./test_output')
stages = [
Stage(type='parse', config=ParseConfig(provider='textin')),
Stage(type='chunk', config=ChunkConfig(strategy='basic', max_characters=1024)),
Stage(type='embed', config=EmbedConfig(provider='qwen', model_name='text-embedding-v3'))
]
pipeline = Pipeline(
source=source,
destination=destination,
api_base_url='https://api.textin.com/api/xparse',
api_headers={
'x-ti-app-id': 'your-app-id',
'x-ti-secret-code': 'your-secret-code'
},
stages=stages
)
# 获取配置字典(格式与 create_pipeline_from_config 的入参一致)
config_dict = pipeline.get_config()
# 可以保存为 JSON 文件
import json
with open('pipeline_config.json', 'w', encoding='utf-8') as f:
json.dump(config_dict, f, indent=2, ensure_ascii=False)
# 或者用于创建新的 Pipeline(需要补充敏感信息如 access_key, secret_key 等)
# from xparse_client import create_pipeline_from_config
# new_pipeline = create_pipeline_from_config(config_dict)
```
### 示例 2: 本地到本地(测试)
```python
from xparse_client import (
Pipeline, LocalSource, LocalDestination,
ParseConfig, ChunkConfig, EmbedConfig, Stage
)
# 创建本地数据源
source = LocalSource(
directory='./test_files',
pattern=['*.pdf'],
recursive=False
)
# 创建本地输出目的地
destination = LocalDestination(output_dir='./test_output')
# 配置处理阶段
stages = [
Stage(
type='parse',
config=ParseConfig(provider='textin')
),
Stage(
type='chunk',
config=ChunkConfig(
strategy='basic',
max_characters=1024
)
),
Stage(
type='embed',
config=EmbedConfig(
provider='qwen',
model_name='text-embedding-v3'
)
)
]
# 创建并运行 Pipeline
pipeline = Pipeline(
source=source,
destination=destination,
api_base_url='https://api.textin.com/api/xparse',
api_headers={
'x-ti-app-id': 'your-app-id',
'x-ti-secret-code': 'your-secret-code'
},
stages=stages
)
pipeline.run()
```
### 示例 3: 不同分块策略的配置
```python
from xparse_client import (
Pipeline, S3Source, MilvusDestination,
ParseConfig, ChunkConfig, EmbedConfig, Stage
)
# 创建数据源和目的地
source = S3Source(...)
destination = MilvusDestination(...)
# 配置 1:按页面分块(适合 PDF 文档)
stages_by_page = [
Stage(
type='parse',
config=ParseConfig(provider='textin')
),
Stage(
type='chunk',
config=ChunkConfig(
strategy='by_page', # 按页面分块
max_characters=2048, # 增大块大小
overlap=100 # 页面间重叠 100 字符
)
),
Stage(
type='embed',
config=EmbedConfig(
provider='qwen',
model_name='text-embedding-v4' # 使用更高精度的模型
)
)
]
# 配置 2:按标题分块(适合结构化文档)
stages_by_title = [
Stage(
type='parse',
config=ParseConfig(provider='textin')
),
Stage(
type='chunk',
config=ChunkConfig(
strategy='by_title', # 按标题分块
include_orig_elements=True, # 保留原始元素信息
max_characters=1536
)
),
Stage(
type='embed',
config=EmbedConfig(
provider='qwen',
model_name='text-embedding-v3'
)
)
]
# 根据文档类型选择配置
pipeline = Pipeline(
source=source,
destination=destination,
api_base_url='https://api.textin.com/api/xparse',
api_headers={...},
stages=stages_by_page # 或 stages_by_title
)
pipeline.run()
```
### 示例 4: FTP 数据源配置
```python
from xparse_client import (
Pipeline, FtpSource, MilvusDestination,
ParseConfig, ChunkConfig, EmbedConfig, Stage
)
# 创建 FTP 数据源
source = FtpSource(
host='ftp.example.com',
port=21,
username='user',
password='pass',
pattern=['*.pdf'],
recursive=False
)
# 创建 Milvus 目的地
destination = MilvusDestination(
db_path='./vectors.db',
collection_name='ftp_docs',
dimension=1024
)
# 配置处理阶段
stages = [
Stage(
type='parse',
config=ParseConfig(provider='textin')
),
Stage(
type='chunk',
config=ChunkConfig(
strategy='basic',
max_characters=1024
)
),
Stage(
type='embed',
config=EmbedConfig(
provider='qwen',
model_name='text-embedding-v3'
)
)
]
# 创建并运行 Pipeline
pipeline = Pipeline(
source=source,
destination=destination,
api_base_url='https://api.textin.com/api/xparse',
api_headers={
'x-ti-app-id': 'app-id',
'x-ti-secret-code': 'secret'
},
stages=stages
)
pipeline.run()
```
### 示例 5: 获取处理统计信息
```python
from datetime import datetime, timezone
from xparse_client import (
Pipeline, LocalSource, LocalDestination,
ParseConfig, ChunkConfig, EmbedConfig, Stage
)
# 创建 Pipeline
source = LocalSource(
directory='./docs',
pattern=['*.pdf'],
recursive=False
)
destination = LocalDestination(output_dir='./output')
stages = [
Stage(
type='parse',
config=ParseConfig(provider='textin')
),
Stage(
type='chunk',
config=ChunkConfig(
strategy='basic',
max_characters=1024
)
),
Stage(
type='embed',
config=EmbedConfig(
provider='qwen',
model_name='text-embedding-v3'
)
)
]
pipeline = Pipeline(
source=source,
destination=destination,
api_base_url='https://api.textin.com/api/xparse',
api_headers={
'x-ti-app-id': 'your-app-id',
'x-ti-secret-code': 'your-secret-code'
},
stages=stages
)
# 处理单个文件并获取统计信息
file_bytes, data_source = pipeline.source.read_file('document.pdf')
data_source['date_processed'] = datetime.now(timezone.utc).timestamp()
result = pipeline.process_with_pipeline(file_bytes, 'document.pdf', data_source)
if result:
elements, stats = result
print(f"原始元素: {stats.original_elements}")
print(f"分块后: {stats.chunked_elements}")
print(f"向量化: {stats.embedded_elements}")
print(f"执行的 stages: {[s.type for s in stats.stages]}")
# 写入目的地
metadata = {
'file_name': 'document.pdf',
'data_source': data_source
}
pipeline.destination.write(elements, metadata)
```
## 📊 Pipeline 统计信息
Pipeline 接口会返回详细的处理统计信息:
| 字段 | 类型 | 说明 |
|------|------|------|
| `original_elements` | int | 原始解析的元素数量 |
| `chunked_elements` | int | 分块后的元素数量 |
| `embedded_elements` | int | 向量化后的元素数量 |
| `stages` | List[Stage] | 实际执行的 stages 配置 |
**示例输出:**
```
✓ Pipeline 完成:
- 原始元素: 25
- 分块后: 42
- 向量化: 42
✓ 写入 Milvus: 42 条
```
## 🔧 扩展开发
### 添加新的 Source
```python
from typing import List, Dict, Any, Tuple
from xparse_client import Source
class MyCustomSource(Source):
def __init__(self, custom_param):
self.custom_param = custom_param
def list_files(self) -> List[str]:
# 实现文件列表逻辑
return ['file1.pdf', 'file2.pdf']
def read_file(self, file_path: str) -> Tuple[bytes, Dict[str, Any]]:
# 实现文件读取逻辑并返回数据来源信息
data_source = {
'url': f'custom://{file_path}',
'version': None,
'date_created': None,
'date_modified': None,
'record_locator': {
'protocol': 'custom',
'remote_file_path': file_path
}
}
return b'file content', data_source
```
### 添加新的 Destination
```python
from xparse_client import Destination
class MyCustomDestination(Destination):
def __init__(self, custom_param):
self.custom_param = custom_param
def write(self, data: List[Dict], metadata: Dict) -> bool:
# 实现数据写入逻辑
return True
```
## 📊 数据格式
### 元素格式
每个处理步骤都使用统一的元素格式:
```python
{
"element_id": str, # 唯一标识符
"type": str, # 元素类型: plaintext, table, image, etc.
"text": str, # 文本内容
"metainfo": { # 元数据
"filename": str,
"orig_elements": list, # chunk处理后添加
# 其他字段
},
"embeddings": list # 向量(embed 步骤后添加)
}
```