xparse-client 0.2.2__tar.gz → 0.2.4__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xparse-client
3
- Version: 0.2.2
3
+ Version: 0.2.4
4
4
  Summary: 面向Agent和RAG的新一代文档处理 AI Infra
5
5
  License-Expression: MIT
6
6
  Project-URL: Homepage, https://gitlab.intsig.net/xparse1/xparse-pipeline
@@ -0,0 +1,479 @@
1
+ #!/usr/bin/env python
2
+ # -*- encoding: utf-8 -*-
3
+ '''
4
+ Pipeline 运行脚本
5
+ 快速启动和运行 Pipeline 的示例
6
+ '''
7
+
8
+ from datetime import datetime, timezone
9
+ from xparse_client import create_pipeline_from_config, S3Source, LocalSource, MilvusDestination, LocalDestination, Pipeline, SmbSource, S3Destination, FtpSource
10
+
11
+
12
+ # ============================================================================
13
+ # 常量配置
14
+ # ============================================================================
15
+
16
+ # API 请求头配置
17
+ API_HEADERS = {
18
+ 'x-ti-app-id': '4c0032d9e4d93b0ad674cac0d75256e7',
19
+ 'x-ti-secret-code': '7104f599ad02b8468fc619f7605d2d8d'
20
+ }
21
+
22
+
23
+ # ============================================================================
24
+ # 方式 1: 使用配置字典
25
+ # ============================================================================
26
+
27
+ def run_with_config():
28
+ """使用配置字典运行 pipeline"""
29
+
30
+ config = {
31
+ 'source': {
32
+ 'type': 's3',
33
+ 'endpoint': 'https://textin-minio-api.ai.intsig.net',
34
+ 'access_key': 'IEQspf8C7fVcgmp3AZWl',
35
+ 'secret_key': 'kLj96I8FGbIrPFW08meXivCy4AVdzBijOJWKWOt1',
36
+ 'bucket': 'textin-test',
37
+ 'prefix': '', # 留空处理所有文件,或指定如 'milvus/'
38
+ 'region': 'us-east-1'
39
+ },
40
+ 'destination': {
41
+ 'type': 'milvus',
42
+ 'db_path': './milvus_pipeline.db',
43
+ 'collection_name': 'pipeline_collection',
44
+ 'dimension': 1024
45
+ },
46
+ 'api_base_url': 'https://api.textin.com/api/xparse',
47
+ 'api_headers': API_HEADERS,
48
+ # Stages 配置
49
+ 'stages': [
50
+ {
51
+ 'type': 'parse',
52
+ 'config': {
53
+ 'provider': 'textin'
54
+ }
55
+ },
56
+ {
57
+ 'type': 'chunk',
58
+ 'config': {
59
+ 'strategy': 'basic', # 分块策略: 'basic' | 'by_title' | 'by_page'
60
+ 'include_orig_elements': False, # 是否包含原始元素
61
+ 'new_after_n_chars': 512, # 多少字符后创建新块
62
+ 'max_characters': 1024, # 最大字符数
63
+ 'overlap': 0 # 重叠字符数
64
+ }
65
+ },
66
+ {
67
+ 'type': 'embed',
68
+ 'config': {
69
+ 'provider': 'qwen', # 向量化供应商: 'qwen'
70
+ 'model_name': 'text-embedding-v3' # 模型名称: 'text-embedding-v3' | 'text-embedding-v4'
71
+ }
72
+ }
73
+ ]
74
+ }
75
+
76
+ pipeline = create_pipeline_from_config(config)
77
+ pipeline.run()
78
+
79
+
80
+ # ============================================================================
81
+ # 方式 2: 手动创建组件
82
+ # ============================================================================
83
+
84
+ def run_with_manual_setup():
85
+ """手动创建 Source、Destination 和 Pipeline"""
86
+ from xparse_client import ChunkConfig, EmbedConfig, ParseConfig, Stage, PipelineConfig, LocalDestination
87
+
88
+ # 创建 S3 数据源
89
+ # source = S3Source(
90
+ # endpoint='https://textin-minio-api.ai.intsig.net',
91
+ # access_key='IEQspf8C7fVcgmp3AZWl',
92
+ # secret_key='kLj96I8FGbIrPFW08meXivCy4AVdzBijOJWKWOt1',
93
+ # bucket='textin-test',
94
+ # prefix='',
95
+ # region='us-east-1'
96
+ # )
97
+ # source = S3Source(
98
+ # endpoint='https://s3.oss-cn-shanghai.aliyuncs.com',
99
+ # access_key='',
100
+ # secret_key='',
101
+ # bucket='textin',
102
+ # prefix='',
103
+ # region='cn-shanghai',
104
+ # pattern='*.png'
105
+ # )
106
+ # source = S3Source(
107
+ # endpoint='https://cos.ap-shanghai.myqcloud.com',
108
+ # access_key='',
109
+ # secret_key='',
110
+ # bucket='textin-1300705866',
111
+ # prefix='',
112
+ # region='ap-shanghai'
113
+ # )
114
+ # source = S3Source(
115
+ # endpoint='https://tos-s3-cn-shanghai.volces.com',
116
+ # access_key='',
117
+ # secret_key='',
118
+ # bucket='textin',
119
+ # prefix='',
120
+ # region='cn-shanghai'
121
+ # )
122
+ # source = S3Source(
123
+ # endpoint='https://obs.cn-east-3.myhuaweicloud.com',
124
+ # access_key='',
125
+ # secret_key='',
126
+ # bucket='textin',
127
+ # prefix='',
128
+ # region='cn-east-3'
129
+ # )
130
+ # source = S3Source(
131
+ # endpoint='https://s3.us-east-1.amazonaws.com',
132
+ # access_key='',
133
+ # secret_key='',
134
+ # bucket='textin-xparse',
135
+ # prefix='',
136
+ # region='us-east-1'
137
+ # )
138
+ # source = S3Source(
139
+ # endpoint='http://127.0.0.1:9000',
140
+ # access_key='',
141
+ # secret_key='',
142
+ # bucket='textin',
143
+ # prefix='',
144
+ # region='us-east-1'
145
+ # )
146
+ # source = SmbSource(
147
+ # host='internal-storage.intsig.net',
148
+ # share_name='ke_wang',
149
+ # username='ke_wang',
150
+ # password='',
151
+ # domain='INTSIG.COM'
152
+ # )
153
+ # source = FtpSource(
154
+ # host='127.0.0.1',
155
+ # port=21,
156
+ # username='', # 用户名,按照实际填写
157
+ # password='' # 密码,按照实际填写
158
+ # )
159
+ source = LocalSource(
160
+ directory='/Users/ke_wang/Documents/doc',
161
+ pattern='*.pdf' # 支持通配符: *.pdf, *.docx, **/*.txt
162
+ )
163
+
164
+ # 创建 Milvus 目的地
165
+ # destination = MilvusDestination(
166
+ # db_path='./milvus_pipeline1.db',
167
+ # collection_name='pipeline_collection',
168
+ # dimension=1024
169
+ # )
170
+
171
+ # destination = LocalDestination(
172
+ # output_dir='./result'
173
+ # )
174
+
175
+ destination = MilvusDestination(
176
+ db_path='https://in03-5388093d0db1707.serverless.ali-cn-hangzhou.cloud.zilliz.com.cn', # zilliz连接地址
177
+ collection_name='textin_test_2', # 数据库collection名称
178
+ dimension=1024, # 向量维度,需与 embed API 返回一致
179
+ api_key='872c3f5b3f3995c80dcda5c3d34f1f608815aef7671b6ee391ab37e40e79c892ce56d9c8c6565a03a3fd66da7e11b67f384c5c46' # Zilliz Cloud API Key
180
+ )
181
+
182
+ # destination = S3Destination(
183
+ # endpoint='https://cos.ap-shanghai.myqcloud.com',
184
+ # access_key='',
185
+ # secret_key='',
186
+ # bucket='textin-1300705866',
187
+ # prefix='result',
188
+ # region='ap-shanghai'
189
+ # )
190
+
191
+ # 使用新的 stages 格式创建配置
192
+ stages = [
193
+ Stage(
194
+ type='parse',
195
+ config=ParseConfig(provider='textin-lite')
196
+ ),
197
+ Stage(
198
+ type='chunk',
199
+ config=ChunkConfig(
200
+ strategy='by_title', # 按标题分块
201
+ include_orig_elements=False,
202
+ new_after_n_chars=512,
203
+ max_characters=1024,
204
+ overlap=50 # 块之间重叠 50 字符
205
+ )
206
+ ),
207
+ Stage(
208
+ type='embed',
209
+ config=EmbedConfig(
210
+ provider='qwen',
211
+ model_name='text-embedding-v3'
212
+ )
213
+ )
214
+ ]
215
+
216
+ # 配置中间结果保存
217
+ intermediate_results_destination = LocalDestination(
218
+ output_dir='./intermediate_results'
219
+ )
220
+
221
+ pipeline_config = PipelineConfig(
222
+ include_intermediate_results=True,
223
+ intermediate_results_destination=intermediate_results_destination
224
+ )
225
+
226
+ # 创建 Pipeline
227
+ pipeline = Pipeline(
228
+ source=source,
229
+ destination=destination,
230
+ api_base_url='https://textin-api-go-pre.ai.intsig.net/api/xparse',
231
+ api_headers=API_HEADERS,
232
+ stages=stages,
233
+ pipeline_config=pipeline_config
234
+ )
235
+
236
+ # 运行
237
+ pipeline.run()
238
+
239
+
240
+ # ============================================================================
241
+ # 方式 3: 本地测试(本地文件 -> 本地输出)
242
+ # ============================================================================
243
+
244
+ def run_local_test():
245
+ """使用本地文件进行测试"""
246
+
247
+ config = {
248
+ 'source': {
249
+ 'type': 'local',
250
+ 'directory': '/Users/ke_wang/Documents/doc',
251
+ 'pattern': '*.pdf'
252
+ },
253
+ 'destination': {
254
+ 'type': 's3',
255
+ 'endpoint': 'https://textin-minio-api.ai.intsig.net',
256
+ 'access_key': '',
257
+ 'secret_key': '',
258
+ 'bucket': 'textin-test',
259
+ 'prefix': '',
260
+ 'region': 'us-east-1'
261
+ },
262
+ 'api_base_url': 'https://api.textin.com/api/xparse',
263
+ 'api_headers': API_HEADERS,
264
+ # Stages 配置
265
+ 'stages': [
266
+ {
267
+ 'type': 'parse',
268
+ 'config': {
269
+ 'provider': 'textin'
270
+ }
271
+ },
272
+ {
273
+ 'type': 'embed',
274
+ 'config': {
275
+ 'provider': 'qwen',
276
+ 'model_name': 'text-embedding-v3'
277
+ }
278
+ }
279
+ ]
280
+ }
281
+
282
+ pipeline = create_pipeline_from_config(config)
283
+ pipeline.run()
284
+
285
+
286
+ # ============================================================================
287
+ # 方式 4: 处理单个文件
288
+ # ============================================================================
289
+
290
+ def run_single_file():
291
+ """只处理单个文件"""
292
+ from xparse_client import ChunkConfig, EmbedConfig, ParseConfig, Stage
293
+
294
+ # 创建 pipeline
295
+ source = LocalSource(directory='/Users/ke_wang/Documents/doc', pattern='*.pdf')
296
+ destination = LocalDestination(output_dir='./output')
297
+
298
+ # 使用新的 stages 格式创建配置
299
+ stages = [
300
+ Stage(
301
+ type='parse',
302
+ config=ParseConfig(provider='textin')
303
+ ),
304
+ Stage(
305
+ type='chunk',
306
+ config=ChunkConfig(
307
+ strategy='by_page', # 按页面分块
308
+ max_characters=2048, # 增大块大小
309
+ overlap=100
310
+ )
311
+ ),
312
+ Stage(
313
+ type='embed',
314
+ config=EmbedConfig(
315
+ provider='qwen',
316
+ model_name='text-embedding-v4' # 使用更高精度的模型
317
+ )
318
+ )
319
+ ]
320
+
321
+ pipeline = Pipeline(
322
+ source=source,
323
+ destination=destination,
324
+ api_base_url='https://api.textin.com/api/xparse',
325
+ api_headers=API_HEADERS,
326
+ stages=stages
327
+ )
328
+
329
+ # 只处理指定文件
330
+ file_path = '4e3250f00210431fb29ca0c808.pdf' # 相对于 source directory 的路径
331
+ success = pipeline.process_file(file_path)
332
+
333
+ if success:
334
+ print(f"\n✅ 文件 {file_path} 处理成功!")
335
+ else:
336
+ print(f"\n❌ 文件 {file_path} 处理失败!")
337
+
338
+
339
+ # ============================================================================
340
+ # 方式 5: 自定义处理流程
341
+ # ============================================================================
342
+
343
+ def run_custom_flow():
344
+ """自定义处理流程,手动控制文件处理"""
345
+ from xparse_client import ChunkConfig, EmbedConfig, ParseConfig, Stage
346
+
347
+ # 创建组件
348
+ source = S3Source(
349
+ endpoint='https://textin-minio-api.ai.intsig.net',
350
+ access_key='',
351
+ secret_key='',
352
+ bucket='textin-test',
353
+ prefix='',
354
+ region='us-east-1',
355
+ pattern='*.pdf'
356
+ )
357
+
358
+ destination = MilvusDestination(
359
+ db_path='./milvus_custom.db',
360
+ collection_name='custom_collection',
361
+ dimension=1024
362
+ )
363
+
364
+ # 使用新的 stages 格式创建配置
365
+ stages = [
366
+ Stage(
367
+ type='parse',
368
+ config=ParseConfig(provider='textin')
369
+ ),
370
+ Stage(
371
+ type='chunk',
372
+ config=ChunkConfig(
373
+ strategy='by_title',
374
+ include_orig_elements=True,
375
+ max_characters=1536,
376
+ overlap=80
377
+ )
378
+ ),
379
+ Stage(
380
+ type='embed',
381
+ config=EmbedConfig(
382
+ provider='qwen',
383
+ model_name='text-embedding-v4'
384
+ )
385
+ )
386
+ ]
387
+
388
+ pipeline = Pipeline(
389
+ source=source,
390
+ destination=destination,
391
+ api_base_url='https://api.textin.com/api/xparse',
392
+ api_headers=API_HEADERS,
393
+ stages=stages
394
+ )
395
+
396
+ # 手动控制文件处理
397
+ files = source.list_files()
398
+
399
+ for file_path in files[:2]: # 只处理前2个文件
400
+ print(f"\n处理: {file_path}")
401
+ file_bytes, data_source = source.read_file(file_path)
402
+ data_source['date_processed'] = datetime.now(timezone.utc).timestamp()
403
+
404
+ # 使用 pipeline 接口处理
405
+ result = pipeline.process_with_pipeline(file_bytes, file_path, data_source)
406
+
407
+ if result:
408
+ embedded, stats = result
409
+ print(f" - 原始元素: {stats.original_elements}")
410
+ print(f" - 分块后: {stats.chunked_elements}")
411
+ print(f" - 向量化: {stats.embedded_elements}")
412
+
413
+ # 写入
414
+ metadata = {
415
+ 'file_name': file_path,
416
+ 'data_source': data_source,
417
+ 'stats': {
418
+ 'original_elements': stats.original_elements,
419
+ 'chunked_elements': stats.chunked_elements,
420
+ 'embedded_elements': stats.embedded_elements
421
+ }
422
+ }
423
+ destination.write(embedded, metadata)
424
+ print(f"✓ 完成: {file_path}")
425
+ else:
426
+ print(f"✗ 失败: {file_path}")
427
+
428
+
429
+ # ============================================================================
430
+ # 主函数
431
+ # ============================================================================
432
+
433
+ def main():
434
+ """主函数 - 选择运行方式"""
435
+
436
+ print("=" * 60)
437
+ print("Pipeline 运行脚本")
438
+ print("=" * 60)
439
+ print("\n请选择运行方式:")
440
+ print("1. 使用配置字典 (S3 -> Milvus) [基础配置]")
441
+ print("2. 手动创建组件 (S3 -> Milvus) [按标题分块 + 自定义配置]")
442
+ print("3. 本地测试 (本地文件 -> 本地输出) [基础配置]")
443
+ print("4. 处理单个文件 [按页面分块 + V4模型]")
444
+ print("5. 自定义处理流程 [手动控制 + 统计信息]")
445
+ print()
446
+
447
+ try:
448
+ choice = input("请输入选项 (1-5) [默认: 1]: ").strip() or '1'
449
+
450
+ if choice == '1':
451
+ print("\n使用配置字典运行...")
452
+ run_with_config()
453
+ elif choice == '2':
454
+ print("\n手动创建组件运行...")
455
+ run_with_manual_setup()
456
+ elif choice == '3':
457
+ print("\n本地测试模式...")
458
+ run_local_test()
459
+ elif choice == '4':
460
+ print("\n处理单个文件...")
461
+ run_single_file()
462
+ elif choice == '5':
463
+ print("\n自定义处理流程...")
464
+ run_custom_flow()
465
+ else:
466
+ print("无效的选项,使用默认方式运行...")
467
+ run_with_config()
468
+
469
+ except KeyboardInterrupt:
470
+ print("\n\n用户中断执行")
471
+ except Exception as e:
472
+ print(f"\n程序异常: {str(e)}")
473
+ import traceback
474
+ traceback.print_exc()
475
+
476
+
477
+ if __name__ == '__main__':
478
+ main()
479
+