xparse-client 0.2.5__tar.gz → 0.2.6__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.
- {xparse_client-0.2.5 → xparse_client-0.2.6}/PKG-INFO +8 -2
- {xparse_client-0.2.5 → xparse_client-0.2.6}/README.md +7 -1
- {xparse_client-0.2.5 → xparse_client-0.2.6}/example/run_pipeline.py +18 -17
- {xparse_client-0.2.5 → xparse_client-0.2.6}/pyproject.toml +1 -1
- {xparse_client-0.2.5 → xparse_client-0.2.6}/xparse_client/pipeline/config.py +1 -0
- {xparse_client-0.2.5 → xparse_client-0.2.6}/xparse_client/pipeline/destinations.py +32 -7
- {xparse_client-0.2.5 → xparse_client-0.2.6}/xparse_client/pipeline/pipeline.py +62 -23
- {xparse_client-0.2.5 → xparse_client-0.2.6}/xparse_client/pipeline/sources.py +176 -17
- {xparse_client-0.2.5 → xparse_client-0.2.6}/xparse_client.egg-info/PKG-INFO +8 -2
- {xparse_client-0.2.5 → xparse_client-0.2.6}/xparse_client.egg-info/top_level.txt +0 -1
- {xparse_client-0.2.5 → xparse_client-0.2.6}/LICENSE +0 -0
- {xparse_client-0.2.5 → xparse_client-0.2.6}/example/run_pipeline_test.py +0 -0
- {xparse_client-0.2.5 → xparse_client-0.2.6}/setup.cfg +0 -0
- {xparse_client-0.2.5 → xparse_client-0.2.6}/xparse_client/__init__.py +0 -0
- {xparse_client-0.2.5 → xparse_client-0.2.6}/xparse_client/pipeline/__init__.py +0 -0
- {xparse_client-0.2.5 → xparse_client-0.2.6}/xparse_client.egg-info/SOURCES.txt +0 -0
- {xparse_client-0.2.5 → xparse_client-0.2.6}/xparse_client.egg-info/dependency_links.txt +0 -0
- {xparse_client-0.2.5 → xparse_client-0.2.6}/xparse_client.egg-info/requires.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: xparse-client
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.6
|
|
4
4
|
Summary: 面向Agent和RAG的新一代文档处理 AI Infra
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
Project-URL: Homepage, https://gitlab.intsig.net/xparse1/xparse-pipeline
|
|
@@ -323,12 +323,16 @@ source = SmbSource(
|
|
|
323
323
|
)
|
|
324
324
|
```
|
|
325
325
|
|
|
326
|
-
>
|
|
326
|
+
> 注 1:所有 Source 均支持 `pattern` 参数,使用 Shell 通配符(`*.pdf`、`**/*.txt` 等)来过滤需要处理的文件;默认为 `*`,即处理全部文件。
|
|
327
|
+
|
|
328
|
+
> 注 2:所有 Source 均支持 `recursive` 参数,表示是否递归遍历,默认为 `False`。
|
|
327
329
|
|
|
328
330
|
### Destination 配置
|
|
329
331
|
|
|
330
332
|
#### 本地 Milvus 向量存储
|
|
331
333
|
|
|
334
|
+
collection 中至少需要包含 `element_id`,`text`,`embeddings`,`record_id` 四个字段。
|
|
335
|
+
|
|
332
336
|
```python
|
|
333
337
|
destination = MilvusDestination(
|
|
334
338
|
db_path='./milvus_pipeline.db', # 本地数据库文件
|
|
@@ -339,6 +343,8 @@ destination = MilvusDestination(
|
|
|
339
343
|
|
|
340
344
|
#### Zilliz 向量存储
|
|
341
345
|
|
|
346
|
+
collection 中至少需要包含 `element_id`,`text`,`embeddings`,`record_id` 四个字段。
|
|
347
|
+
|
|
342
348
|
```python
|
|
343
349
|
destination = MilvusDestination(
|
|
344
350
|
db_path='https://xxxxxxx.serverless.xxxxxxx.cloud.zilliz.com.cn', # zilliz连接地址
|
|
@@ -306,12 +306,16 @@ source = SmbSource(
|
|
|
306
306
|
)
|
|
307
307
|
```
|
|
308
308
|
|
|
309
|
-
>
|
|
309
|
+
> 注 1:所有 Source 均支持 `pattern` 参数,使用 Shell 通配符(`*.pdf`、`**/*.txt` 等)来过滤需要处理的文件;默认为 `*`,即处理全部文件。
|
|
310
|
+
|
|
311
|
+
> 注 2:所有 Source 均支持 `recursive` 参数,表示是否递归遍历,默认为 `False`。
|
|
310
312
|
|
|
311
313
|
### Destination 配置
|
|
312
314
|
|
|
313
315
|
#### 本地 Milvus 向量存储
|
|
314
316
|
|
|
317
|
+
collection 中至少需要包含 `element_id`,`text`,`embeddings`,`record_id` 四个字段。
|
|
318
|
+
|
|
315
319
|
```python
|
|
316
320
|
destination = MilvusDestination(
|
|
317
321
|
db_path='./milvus_pipeline.db', # 本地数据库文件
|
|
@@ -322,6 +326,8 @@ destination = MilvusDestination(
|
|
|
322
326
|
|
|
323
327
|
#### Zilliz 向量存储
|
|
324
328
|
|
|
329
|
+
collection 中至少需要包含 `element_id`,`text`,`embeddings`,`record_id` 四个字段。
|
|
330
|
+
|
|
325
331
|
```python
|
|
326
332
|
destination = MilvusDestination(
|
|
327
333
|
db_path='https://xxxxxxx.serverless.xxxxxxx.cloud.zilliz.com.cn', # zilliz连接地址
|
|
@@ -96,8 +96,8 @@ def run_with_manual_setup():
|
|
|
96
96
|
# )
|
|
97
97
|
# source = S3Source(
|
|
98
98
|
# endpoint='https://s3.oss-cn-shanghai.aliyuncs.com',
|
|
99
|
-
# access_key='',
|
|
100
|
-
# secret_key='',
|
|
99
|
+
# access_key='LTAI5tBgsaVfkbh9rbPyuB17',
|
|
100
|
+
# secret_key='JFIIaTGiXelv7DgBYNIBSStofF0S98',
|
|
101
101
|
# bucket='textin',
|
|
102
102
|
# prefix='',
|
|
103
103
|
# region='cn-shanghai',
|
|
@@ -113,8 +113,8 @@ def run_with_manual_setup():
|
|
|
113
113
|
# )
|
|
114
114
|
# source = S3Source(
|
|
115
115
|
# endpoint='https://tos-s3-cn-shanghai.volces.com',
|
|
116
|
-
# access_key='',
|
|
117
|
-
# secret_key='',
|
|
116
|
+
# access_key='AKLTMzNkZjk1OGM3MzBjNGQ1ZjhkMGQ4MThlNjBjYjZjYzA',
|
|
117
|
+
# secret_key='TnpWaE0yRTVaamRqTmpSbU5EY3pObUZrTTJVNE5qUm1NR0ppWkRrMFlqVQ==',
|
|
118
118
|
# bucket='textin',
|
|
119
119
|
# prefix='',
|
|
120
120
|
# region='cn-shanghai'
|
|
@@ -127,14 +127,14 @@ def run_with_manual_setup():
|
|
|
127
127
|
# prefix='',
|
|
128
128
|
# region='cn-east-3'
|
|
129
129
|
# )
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
130
|
+
source = S3Source(
|
|
131
|
+
endpoint='https://s3.us-east-1.amazonaws.com',
|
|
132
|
+
access_key='AKIA6QUE3TVZADUWA4PO',
|
|
133
|
+
secret_key='OfV4r9/u+CmlLxmiZDYwtiFSl0OsNdWLADKdPek7',
|
|
134
|
+
bucket='textin-xparse',
|
|
135
|
+
prefix='',
|
|
136
|
+
region='us-east-1'
|
|
137
|
+
)
|
|
138
138
|
# source = S3Source(
|
|
139
139
|
# endpoint='http://127.0.0.1:9000',
|
|
140
140
|
# access_key='',
|
|
@@ -153,13 +153,14 @@ def run_with_manual_setup():
|
|
|
153
153
|
# source = FtpSource(
|
|
154
154
|
# host='127.0.0.1',
|
|
155
155
|
# port=21,
|
|
156
|
+
# # recursive=True,
|
|
156
157
|
# username='', # 用户名,按照实际填写
|
|
157
158
|
# password='' # 密码,按照实际填写
|
|
158
159
|
# )
|
|
159
|
-
source = LocalSource(
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
)
|
|
160
|
+
# source = LocalSource(
|
|
161
|
+
# directory='/Users/ke_wang/Documents/doc',
|
|
162
|
+
# pattern='*.pdf' # 支持通配符: *.pdf, *.docx, **/*.txt
|
|
163
|
+
# )
|
|
163
164
|
|
|
164
165
|
# 创建 Milvus 目的地
|
|
165
166
|
# destination = MilvusDestination(
|
|
@@ -174,7 +175,7 @@ def run_with_manual_setup():
|
|
|
174
175
|
|
|
175
176
|
destination = MilvusDestination(
|
|
176
177
|
db_path='https://in03-5388093d0db1707.serverless.ali-cn-hangzhou.cloud.zilliz.com.cn', # zilliz连接地址
|
|
177
|
-
collection_name='
|
|
178
|
+
collection_name='textin_test_3', # 数据库collection名称
|
|
178
179
|
dimension=1024, # 向量维度,需与 embed API 返回一致
|
|
179
180
|
api_key='872c3f5b3f3995c80dcda5c3d34f1f608815aef7671b6ee391ab37e40e79c892ce56d9c8c6565a03a3fd66da7e11b67f384c5c46' # Zilliz Cloud API Key
|
|
180
181
|
)
|
|
@@ -89,8 +89,7 @@ class MilvusDestination(Destination):
|
|
|
89
89
|
schema.add_field(field_name="element_id", datatype=DataType.VARCHAR, max_length=128, is_primary=True)
|
|
90
90
|
schema.add_field(field_name="embeddings", datatype=DataType.FLOAT_VECTOR, dim=dimension)
|
|
91
91
|
schema.add_field(field_name="text", datatype=DataType.VARCHAR, max_length=65535)
|
|
92
|
-
schema.add_field(field_name="record_id", datatype=DataType.VARCHAR, max_length=
|
|
93
|
-
schema.add_field(field_name="metadata", datatype=DataType.JSON)
|
|
92
|
+
schema.add_field(field_name="record_id", datatype=DataType.VARCHAR, max_length=200)
|
|
94
93
|
|
|
95
94
|
index_params = self.client.prepare_index_params()
|
|
96
95
|
index_params.add_index(
|
|
@@ -112,6 +111,32 @@ class MilvusDestination(Destination):
|
|
|
112
111
|
|
|
113
112
|
def write(self, data: List[Dict[str, Any]], metadata: Dict[str, Any]) -> bool:
|
|
114
113
|
try:
|
|
114
|
+
# 如果 metadata 中有 record_id,先删除相同 record_id 的现有记录
|
|
115
|
+
record_id = metadata.get('record_id')
|
|
116
|
+
if record_id:
|
|
117
|
+
try:
|
|
118
|
+
# 删除相同 record_id 的所有记录
|
|
119
|
+
# MilvusClient.delete 返回删除的记录数(可能是 int 或 dict)
|
|
120
|
+
result = self.client.delete(
|
|
121
|
+
collection_name=self.collection_name,
|
|
122
|
+
filter=f'record_id == "{record_id}"'
|
|
123
|
+
)
|
|
124
|
+
# 处理返回值:可能是数字或字典
|
|
125
|
+
deleted_count = result if isinstance(result, int) else result.get('delete_count', 0) if isinstance(result, dict) else 0
|
|
126
|
+
if deleted_count > 0:
|
|
127
|
+
print(f" ✓ 删除现有记录: record_id={record_id}, 删除 {deleted_count} 条")
|
|
128
|
+
logger.info(f"删除 Milvus 现有记录: record_id={record_id}, 删除 {deleted_count} 条")
|
|
129
|
+
else:
|
|
130
|
+
print(f" → 未找到现有记录: record_id={record_id}")
|
|
131
|
+
except Exception as e:
|
|
132
|
+
print(f" ! 删除现有记录失败: {str(e)}")
|
|
133
|
+
logger.warning(f"删除 Milvus 现有记录失败: record_id={record_id}, {str(e)}")
|
|
134
|
+
# 继续执行写入,不因为删除失败而中断
|
|
135
|
+
else:
|
|
136
|
+
print(f" → 没有 record_id")
|
|
137
|
+
logger.warning(f"没有 record_id")
|
|
138
|
+
return
|
|
139
|
+
|
|
115
140
|
insert_data = []
|
|
116
141
|
for item in data:
|
|
117
142
|
# 获取元素级别的 metadata
|
|
@@ -125,7 +150,7 @@ class MilvusDestination(Destination):
|
|
|
125
150
|
'embeddings': item['embeddings'],
|
|
126
151
|
'text': item.get('text', ''),
|
|
127
152
|
'element_id': element_id,
|
|
128
|
-
'record_id':
|
|
153
|
+
'record_id': record_id
|
|
129
154
|
}
|
|
130
155
|
|
|
131
156
|
# 合并文件级别的 metadata 和元素级别的 metadata
|
|
@@ -179,8 +204,8 @@ class LocalDestination(Destination):
|
|
|
179
204
|
|
|
180
205
|
def write(self, data: List[Dict[str, Any]], metadata: Dict[str, Any]) -> bool:
|
|
181
206
|
try:
|
|
182
|
-
|
|
183
|
-
base_name = Path(
|
|
207
|
+
filename = metadata.get('filename', 'output')
|
|
208
|
+
base_name = Path(filename).stem
|
|
184
209
|
stage = metadata.get('stage') # 用于区分中间结果的阶段
|
|
185
210
|
|
|
186
211
|
# 如果是中间结果,在文件名中添加阶段标识
|
|
@@ -248,8 +273,8 @@ class S3Destination(Destination):
|
|
|
248
273
|
|
|
249
274
|
def write(self, data: List[Dict[str, Any]], metadata: Dict[str, Any]) -> bool:
|
|
250
275
|
try:
|
|
251
|
-
|
|
252
|
-
base_name = Path(
|
|
276
|
+
filename = metadata.get('filename', 'output')
|
|
277
|
+
base_name = Path(filename).stem
|
|
253
278
|
object_key = f"{self.prefix}/{base_name}.json" if self.prefix else f"{base_name}.json"
|
|
254
279
|
|
|
255
280
|
json_data = json.dumps(data, ensure_ascii=False, indent=2)
|
|
@@ -79,13 +79,13 @@ class Pipeline:
|
|
|
79
79
|
print(f" Pipeline Config: 中间结果保存已启用")
|
|
80
80
|
print("=" * 60)
|
|
81
81
|
|
|
82
|
-
def _call_pipeline_api(self, file_bytes: bytes,
|
|
82
|
+
def _call_pipeline_api(self, file_bytes: bytes, filename: str, data_source: Dict[str, Any]) -> Optional[Dict[str, Any]]:
|
|
83
83
|
url = f"{self.api_base_url}/pipeline"
|
|
84
84
|
max_retries = 3
|
|
85
85
|
|
|
86
86
|
for try_count in range(max_retries):
|
|
87
87
|
try:
|
|
88
|
-
files = {'file': (
|
|
88
|
+
files = {'file': (filename or 'file', file_bytes)}
|
|
89
89
|
form_data = {}
|
|
90
90
|
|
|
91
91
|
# 将 stages 转换为 API 格式
|
|
@@ -107,26 +107,55 @@ class Pipeline:
|
|
|
107
107
|
|
|
108
108
|
if response.status_code == 200:
|
|
109
109
|
result = response.json()
|
|
110
|
-
|
|
110
|
+
x_request_id = result.get('x_request_id', '')
|
|
111
|
+
print(f" ✓ Pipeline 接口返回 x_request_id: {x_request_id}")
|
|
111
112
|
if result.get('code') == 200 and 'data' in result:
|
|
112
113
|
return result.get('data')
|
|
114
|
+
# 如果 code 不是 200,打印错误信息
|
|
115
|
+
error_msg = result.get('message', result.get('msg', '未知错误'))
|
|
116
|
+
print(f" ✗ Pipeline 接口返回错误: code={result.get('code')}, message={error_msg}, x_request_id={x_request_id}")
|
|
117
|
+
logger.error(f"Pipeline 接口返回错误: code={result.get('code')}, message={error_msg}, x_request_id={x_request_id}")
|
|
113
118
|
return None
|
|
114
119
|
else:
|
|
115
|
-
|
|
116
|
-
|
|
120
|
+
# 尝试解析响应获取 x_request_id 和错误信息
|
|
121
|
+
x_request_id = ''
|
|
122
|
+
error_msg = ''
|
|
123
|
+
try:
|
|
124
|
+
result = response.json()
|
|
125
|
+
x_request_id = result.get('x_request_id', '')
|
|
126
|
+
error_msg = result.get('message', result.get('msg', response.text[:200]))
|
|
127
|
+
except:
|
|
128
|
+
error_msg = response.text[:200] if response.text else f'HTTP {response.status_code}'
|
|
129
|
+
|
|
130
|
+
print(f" ✗ API 错误 {response.status_code}: {error_msg}, x_request_id={x_request_id}, 重试 {try_count + 1}/{max_retries}")
|
|
131
|
+
logger.warning(f"API 错误 {response.status_code}: {error_msg}, x_request_id={x_request_id}, 重试 {try_count + 1}/{max_retries}")
|
|
117
132
|
|
|
118
133
|
except Exception as e:
|
|
119
|
-
|
|
120
|
-
|
|
134
|
+
# 如果是 requests 异常,尝试从响应中获取 x_request_id
|
|
135
|
+
x_request_id = ''
|
|
136
|
+
error_msg = str(e)
|
|
137
|
+
try:
|
|
138
|
+
if hasattr(e, 'response') and e.response is not None:
|
|
139
|
+
try:
|
|
140
|
+
result = e.response.json()
|
|
141
|
+
x_request_id = result.get('x_request_id', '')
|
|
142
|
+
error_msg = result.get('message', result.get('msg', error_msg))
|
|
143
|
+
except:
|
|
144
|
+
pass
|
|
145
|
+
except:
|
|
146
|
+
pass
|
|
147
|
+
|
|
148
|
+
print(f" ✗ 请求异常: {error_msg}, x_request_id={x_request_id}, 重试 {try_count + 1}/{max_retries}")
|
|
149
|
+
logger.error(f"API 请求异常 pipeline: {error_msg}, x_request_id={x_request_id}")
|
|
121
150
|
|
|
122
151
|
if try_count < max_retries - 1:
|
|
123
152
|
time.sleep(2)
|
|
124
153
|
|
|
125
154
|
return None
|
|
126
155
|
|
|
127
|
-
def process_with_pipeline(self, file_bytes: bytes,
|
|
128
|
-
print(f" → 调用 Pipeline 接口: {
|
|
129
|
-
result = self._call_pipeline_api(file_bytes,
|
|
156
|
+
def process_with_pipeline(self, file_bytes: bytes, filename: str, data_source: Dict[str, Any]) -> Optional[Tuple[List[Dict[str, Any]], PipelineStats]]:
|
|
157
|
+
print(f" → 调用 Pipeline 接口: {filename}")
|
|
158
|
+
result = self._call_pipeline_api(file_bytes, filename, data_source)
|
|
130
159
|
|
|
131
160
|
if result and 'elements' in result and 'stats' in result:
|
|
132
161
|
elements = result['elements']
|
|
@@ -136,31 +165,32 @@ class Pipeline:
|
|
|
136
165
|
original_elements=stats_data.get('original_elements', 0),
|
|
137
166
|
chunked_elements=stats_data.get('chunked_elements', 0),
|
|
138
167
|
embedded_elements=stats_data.get('embedded_elements', 0),
|
|
139
|
-
stages=self.stages # 使用实际执行的 stages
|
|
168
|
+
stages=self.stages, # 使用实际执行的 stages
|
|
169
|
+
record_id=stats_data.get('record_id') # 从 API 响应中获取 record_id
|
|
140
170
|
)
|
|
141
171
|
|
|
142
172
|
# 如果启用了中间结果保存,处理中间结果
|
|
143
173
|
if self.pipeline_config.include_intermediate_results and 'intermediate_results' in result:
|
|
144
|
-
self._save_intermediate_results(result['intermediate_results'],
|
|
174
|
+
self._save_intermediate_results(result['intermediate_results'], filename, data_source)
|
|
145
175
|
|
|
146
176
|
print(f" ✓ Pipeline 完成:")
|
|
147
177
|
print(f" - 原始元素: {stats.original_elements}")
|
|
148
178
|
print(f" - 分块后: {stats.chunked_elements}")
|
|
149
179
|
print(f" - 向量化: {stats.embedded_elements}")
|
|
150
|
-
logger.info(f"Pipeline 完成: {
|
|
180
|
+
logger.info(f"Pipeline 完成: {filename}, {stats.embedded_elements} 个向量")
|
|
151
181
|
|
|
152
182
|
return elements, stats
|
|
153
183
|
else:
|
|
154
184
|
print(f" ✗ Pipeline 失败")
|
|
155
|
-
logger.error(f"Pipeline 失败: {
|
|
185
|
+
logger.error(f"Pipeline 失败: {filename}")
|
|
156
186
|
return None
|
|
157
187
|
|
|
158
|
-
def _save_intermediate_results(self, intermediate_results: List[Dict[str, Any]],
|
|
188
|
+
def _save_intermediate_results(self, intermediate_results: List[Dict[str, Any]], filename: str, data_source: Dict[str, Any]) -> None:
|
|
159
189
|
"""保存中间结果
|
|
160
190
|
|
|
161
191
|
Args:
|
|
162
192
|
intermediate_results: 中间结果数组,每个元素包含 stage 和 elements 字段
|
|
163
|
-
|
|
193
|
+
filename: 文件名
|
|
164
194
|
data_source: 数据源信息
|
|
165
195
|
"""
|
|
166
196
|
try:
|
|
@@ -174,7 +204,7 @@ class Pipeline:
|
|
|
174
204
|
elements = result_item['elements']
|
|
175
205
|
|
|
176
206
|
metadata = {
|
|
177
|
-
'
|
|
207
|
+
'filename': filename,
|
|
178
208
|
'stage': stage,
|
|
179
209
|
'total_elements': len(elements),
|
|
180
210
|
'processed_at': datetime.now().isoformat(),
|
|
@@ -183,11 +213,11 @@ class Pipeline:
|
|
|
183
213
|
|
|
184
214
|
self.pipeline_config.intermediate_results_destination.write(elements, metadata)
|
|
185
215
|
print(f" ✓ 保存 {stage.upper()} 中间结果: {len(elements)} 个元素")
|
|
186
|
-
logger.info(f"保存 {stage.upper()} 中间结果成功: {
|
|
216
|
+
logger.info(f"保存 {stage.upper()} 中间结果成功: {filename}")
|
|
187
217
|
|
|
188
218
|
except Exception as e:
|
|
189
219
|
print(f" ✗ 保存中间结果失败: {str(e)}")
|
|
190
|
-
logger.error(f"保存中间结果失败: {
|
|
220
|
+
logger.error(f"保存中间结果失败: {filename}, {str(e)}")
|
|
191
221
|
|
|
192
222
|
def process_file(self, file_path: str) -> bool:
|
|
193
223
|
print(f"\n{'=' * 60}")
|
|
@@ -198,6 +228,7 @@ class Pipeline:
|
|
|
198
228
|
print(f" → 读取文件...")
|
|
199
229
|
file_bytes, data_source = self.source.read_file(file_path)
|
|
200
230
|
data_source = data_source or {}
|
|
231
|
+
print(f" data_source: {data_source}")
|
|
201
232
|
# 转换为毫秒时间戳字符串
|
|
202
233
|
timestamp_ms = int(datetime.now(timezone.utc).timestamp() * 1000)
|
|
203
234
|
data_source['date_processed'] = str(timestamp_ms)
|
|
@@ -214,6 +245,10 @@ class Pipeline:
|
|
|
214
245
|
'filename': file_path,
|
|
215
246
|
'processed_at': str(timestamp_ms),
|
|
216
247
|
}
|
|
248
|
+
|
|
249
|
+
# 如果 stats 中有 record_id,添加到 metadata 中
|
|
250
|
+
if stats.record_id:
|
|
251
|
+
metadata['record_id'] = stats.record_id
|
|
217
252
|
|
|
218
253
|
success = self.destination.write(embedded_data, metadata)
|
|
219
254
|
|
|
@@ -294,12 +329,14 @@ def create_pipeline_from_config(config: Dict[str, Any]) -> Pipeline:
|
|
|
294
329
|
bucket=source_config['bucket'],
|
|
295
330
|
prefix=source_config.get('prefix', ''),
|
|
296
331
|
region=source_config.get('region', 'us-east-1'),
|
|
297
|
-
pattern=source_config.get('pattern', '*')
|
|
332
|
+
pattern=source_config.get('pattern', '*'),
|
|
333
|
+
recursive=source_config.get('recursive', False)
|
|
298
334
|
)
|
|
299
335
|
elif source_config['type'] == 'local':
|
|
300
336
|
source = LocalSource(
|
|
301
337
|
directory=source_config['directory'],
|
|
302
|
-
pattern=source_config.get('pattern', '*')
|
|
338
|
+
pattern=source_config.get('pattern', '*'),
|
|
339
|
+
recursive=source_config.get('recursive', False)
|
|
303
340
|
)
|
|
304
341
|
elif source_config['type'] == 'ftp':
|
|
305
342
|
source = FtpSource(
|
|
@@ -307,7 +344,8 @@ def create_pipeline_from_config(config: Dict[str, Any]) -> Pipeline:
|
|
|
307
344
|
port=source_config['port'],
|
|
308
345
|
username=source_config['username'],
|
|
309
346
|
password=source_config['password'],
|
|
310
|
-
pattern=source_config.get('pattern', '*')
|
|
347
|
+
pattern=source_config.get('pattern', '*'),
|
|
348
|
+
recursive=source_config.get('recursive', False)
|
|
311
349
|
)
|
|
312
350
|
elif source_config['type'] == 'smb':
|
|
313
351
|
source = SmbSource(
|
|
@@ -318,7 +356,8 @@ def create_pipeline_from_config(config: Dict[str, Any]) -> Pipeline:
|
|
|
318
356
|
domain=source_config.get('domain', ''),
|
|
319
357
|
port=source_config.get('port', 445),
|
|
320
358
|
path=source_config.get('path', ''),
|
|
321
|
-
pattern=source_config.get('pattern', '*')
|
|
359
|
+
pattern=source_config.get('pattern', '*'),
|
|
360
|
+
recursive=source_config.get('recursive', False)
|
|
322
361
|
)
|
|
323
362
|
else:
|
|
324
363
|
raise ValueError(f"未知的 source 类型: {source_config['type']}")
|
|
@@ -62,11 +62,12 @@ class S3Source(Source):
|
|
|
62
62
|
"""S3/MinIO 数据源"""
|
|
63
63
|
|
|
64
64
|
def __init__(self, endpoint: str, access_key: str, secret_key: str,
|
|
65
|
-
bucket: str, prefix: str = '', region: str = 'us-east-1', pattern: str = '*'):
|
|
65
|
+
bucket: str, prefix: str = '', region: str = 'us-east-1', pattern: str = '*', recursive: bool = False):
|
|
66
66
|
self.endpoint = endpoint
|
|
67
67
|
self.bucket = bucket
|
|
68
68
|
self.prefix = prefix
|
|
69
69
|
self.pattern = pattern or '*'
|
|
70
|
+
self.recursive = recursive
|
|
70
71
|
|
|
71
72
|
if self.endpoint == 'https://textin-minio-api.ai.intsig.net':
|
|
72
73
|
config = Config(signature_version='s3v4')
|
|
@@ -97,8 +98,12 @@ class S3Source(Source):
|
|
|
97
98
|
params = {'Bucket': self.bucket}
|
|
98
99
|
if self.prefix:
|
|
99
100
|
params['Prefix'] = self.prefix
|
|
101
|
+
if not self.recursive:
|
|
102
|
+
# 非递归模式:使用 Delimiter 只列出当前目录下的文件
|
|
103
|
+
params['Delimiter'] = '/'
|
|
100
104
|
|
|
101
105
|
for page in paginator.paginate(**params):
|
|
106
|
+
print(page)
|
|
102
107
|
if 'Contents' in page:
|
|
103
108
|
for obj in page['Contents']:
|
|
104
109
|
key = obj['Key']
|
|
@@ -106,6 +111,11 @@ class S3Source(Source):
|
|
|
106
111
|
continue
|
|
107
112
|
if fnmatch(key, self.pattern):
|
|
108
113
|
files.append(key)
|
|
114
|
+
|
|
115
|
+
# 非递归模式下,CommonPrefixes 包含子目录,我们忽略它们
|
|
116
|
+
if not self.recursive and 'CommonPrefixes' in page:
|
|
117
|
+
# 这些是子目录,在非递归模式下忽略
|
|
118
|
+
pass
|
|
109
119
|
|
|
110
120
|
print(f"✓ S3 找到 {len(files)} 个文件")
|
|
111
121
|
return files
|
|
@@ -115,7 +125,9 @@ class S3Source(Source):
|
|
|
115
125
|
file_bytes = response['Body'].read()
|
|
116
126
|
|
|
117
127
|
headers = response.get('ResponseMetadata', {}).get('HTTPHeaders', {})
|
|
118
|
-
version =
|
|
128
|
+
version = headers.get('etag') or ""
|
|
129
|
+
if version.startswith('"') and version.endswith('"'):
|
|
130
|
+
version = version[1:-1]
|
|
119
131
|
last_modified = headers.get('last-modified')
|
|
120
132
|
server = headers.get('server') or "unknown"
|
|
121
133
|
date_modified = None
|
|
@@ -145,9 +157,10 @@ class S3Source(Source):
|
|
|
145
157
|
class LocalSource(Source):
|
|
146
158
|
"""本地文件系统数据源"""
|
|
147
159
|
|
|
148
|
-
def __init__(self, directory: str, pattern: str = '*'):
|
|
160
|
+
def __init__(self, directory: str, pattern: str = '*', recursive: bool = False):
|
|
149
161
|
self.directory = Path(directory)
|
|
150
162
|
self.pattern = pattern or '*'
|
|
163
|
+
self.recursive = recursive
|
|
151
164
|
|
|
152
165
|
if not self.directory.exists():
|
|
153
166
|
raise ValueError(f"目录不存在: {directory}")
|
|
@@ -156,11 +169,20 @@ class LocalSource(Source):
|
|
|
156
169
|
logger.info(f"本地目录: {self.directory}")
|
|
157
170
|
|
|
158
171
|
def list_files(self) -> List[str]:
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
172
|
+
if self.recursive:
|
|
173
|
+
# 递归模式:使用 rglob
|
|
174
|
+
files = [
|
|
175
|
+
str(f.relative_to(self.directory))
|
|
176
|
+
for f in self.directory.rglob(self.pattern)
|
|
177
|
+
if f.is_file()
|
|
178
|
+
]
|
|
179
|
+
else:
|
|
180
|
+
# 非递归模式:只列出根目录下的文件,使用 glob
|
|
181
|
+
files = [
|
|
182
|
+
str(f.relative_to(self.directory))
|
|
183
|
+
for f in self.directory.glob(self.pattern)
|
|
184
|
+
if f.is_file()
|
|
185
|
+
]
|
|
164
186
|
print(f"✓ 本地找到 {len(files)} 个文件")
|
|
165
187
|
return files
|
|
166
188
|
|
|
@@ -196,12 +218,13 @@ class LocalSource(Source):
|
|
|
196
218
|
class FtpSource(Source):
|
|
197
219
|
"""FTP 数据源"""
|
|
198
220
|
|
|
199
|
-
def __init__(self, host: str, port: int, username: str, password: str, pattern: str = '*'):
|
|
221
|
+
def __init__(self, host: str, port: int, username: str, password: str, pattern: str = '*', recursive: bool = False):
|
|
200
222
|
self.host = host
|
|
201
223
|
self.port = port
|
|
202
224
|
self.username = username
|
|
203
225
|
self.password = password
|
|
204
226
|
self.pattern = pattern or '*'
|
|
227
|
+
self.recursive = recursive
|
|
205
228
|
|
|
206
229
|
self.client = ftplib.FTP()
|
|
207
230
|
self.client.connect(self.host, self.port)
|
|
@@ -211,8 +234,139 @@ class FtpSource(Source):
|
|
|
211
234
|
logger.info(f"FTP 连接成功: {self.host}:{self.port}")
|
|
212
235
|
|
|
213
236
|
def list_files(self) -> List[str]:
|
|
214
|
-
|
|
215
|
-
|
|
237
|
+
if self.recursive:
|
|
238
|
+
# 递归模式:递归列出所有文件
|
|
239
|
+
files = []
|
|
240
|
+
current_dir = self.client.pwd()
|
|
241
|
+
|
|
242
|
+
def _list_recursive(path=''):
|
|
243
|
+
try:
|
|
244
|
+
# 保存当前目录
|
|
245
|
+
original_dir = self.client.pwd()
|
|
246
|
+
if path:
|
|
247
|
+
try:
|
|
248
|
+
self.client.cwd(path)
|
|
249
|
+
except:
|
|
250
|
+
return
|
|
251
|
+
|
|
252
|
+
items = []
|
|
253
|
+
try:
|
|
254
|
+
# 尝试使用 MLSD 命令(更可靠)
|
|
255
|
+
items = []
|
|
256
|
+
for item in self.client.mlsd():
|
|
257
|
+
items.append(item)
|
|
258
|
+
except:
|
|
259
|
+
# 如果不支持 MLSD,使用 LIST 命令
|
|
260
|
+
try:
|
|
261
|
+
lines = []
|
|
262
|
+
self.client.retrlines('LIST', lines.append)
|
|
263
|
+
for line in lines:
|
|
264
|
+
parts = line.split()
|
|
265
|
+
if len(parts) >= 9:
|
|
266
|
+
# 解析 LIST 输出,第一个字符表示文件类型
|
|
267
|
+
item_name = ' '.join(parts[8:])
|
|
268
|
+
is_dir = parts[0].startswith('d')
|
|
269
|
+
items.append((item_name, {'type': 'dir' if is_dir else 'file'}))
|
|
270
|
+
except:
|
|
271
|
+
# 最后回退到 nlst,但无法区分文件和目录
|
|
272
|
+
for item_name in self.client.nlst():
|
|
273
|
+
items.append((item_name, {'type': 'unknown'}))
|
|
274
|
+
|
|
275
|
+
for item_name, item_info in items:
|
|
276
|
+
if item_name in ['.', '..']:
|
|
277
|
+
continue
|
|
278
|
+
|
|
279
|
+
item_type = item_info.get('type', 'unknown')
|
|
280
|
+
full_path = f"{path}/{item_name}" if path else item_name
|
|
281
|
+
|
|
282
|
+
if item_type == 'dir' or item_type == 'unknown':
|
|
283
|
+
# 尝试切换目录来判断是否为目录
|
|
284
|
+
try:
|
|
285
|
+
self.client.cwd(item_name)
|
|
286
|
+
self.client.cwd('..')
|
|
287
|
+
# 是目录,递归处理
|
|
288
|
+
_list_recursive(full_path)
|
|
289
|
+
except:
|
|
290
|
+
# 不是目录,是文件
|
|
291
|
+
relative_path = full_path.lstrip('/')
|
|
292
|
+
if fnmatch(relative_path, self.pattern):
|
|
293
|
+
files.append(relative_path)
|
|
294
|
+
else:
|
|
295
|
+
# 是文件
|
|
296
|
+
relative_path = full_path.lstrip('/')
|
|
297
|
+
if fnmatch(relative_path, self.pattern):
|
|
298
|
+
files.append(relative_path)
|
|
299
|
+
|
|
300
|
+
# 恢复原始目录
|
|
301
|
+
self.client.cwd(original_dir)
|
|
302
|
+
except Exception as e:
|
|
303
|
+
logger.warning(f"FTP 列出路径失败 {path}: {str(e)}")
|
|
304
|
+
try:
|
|
305
|
+
self.client.cwd(current_dir)
|
|
306
|
+
except:
|
|
307
|
+
pass
|
|
308
|
+
|
|
309
|
+
_list_recursive()
|
|
310
|
+
# 确保回到原始目录
|
|
311
|
+
try:
|
|
312
|
+
self.client.cwd(current_dir)
|
|
313
|
+
except:
|
|
314
|
+
pass
|
|
315
|
+
else:
|
|
316
|
+
# 非递归模式:只列出当前目录下的文件(排除目录)
|
|
317
|
+
files = []
|
|
318
|
+
current_dir = self.client.pwd()
|
|
319
|
+
|
|
320
|
+
try:
|
|
321
|
+
# 尝试使用 MLSD 命令(更可靠)
|
|
322
|
+
items = []
|
|
323
|
+
for item_name, item_info in self.client.mlsd():
|
|
324
|
+
if item_name in ['.', '..']:
|
|
325
|
+
continue
|
|
326
|
+
item_type = item_info.get('type', 'unknown')
|
|
327
|
+
# 只添加文件,排除目录
|
|
328
|
+
if item_type == 'file' or (item_type == 'unknown' and not item_info.get('type', '').startswith('dir')):
|
|
329
|
+
if fnmatch(item_name, self.pattern):
|
|
330
|
+
files.append(item_name)
|
|
331
|
+
except:
|
|
332
|
+
# 如果不支持 MLSD,使用 LIST 命令
|
|
333
|
+
try:
|
|
334
|
+
lines = []
|
|
335
|
+
self.client.retrlines('LIST', lines.append)
|
|
336
|
+
for line in lines:
|
|
337
|
+
parts = line.split()
|
|
338
|
+
if len(parts) >= 9:
|
|
339
|
+
# 解析 LIST 输出,第一个字符表示文件类型
|
|
340
|
+
item_name = ' '.join(parts[8:])
|
|
341
|
+
if item_name in ['.', '..']:
|
|
342
|
+
continue
|
|
343
|
+
is_dir = parts[0].startswith('d')
|
|
344
|
+
# 只添加文件,排除目录
|
|
345
|
+
if not is_dir and fnmatch(item_name, self.pattern):
|
|
346
|
+
files.append(item_name)
|
|
347
|
+
except:
|
|
348
|
+
# 最后回退到 nlst,通过尝试切换目录来判断是否为目录
|
|
349
|
+
raw_items = self.client.nlst()
|
|
350
|
+
for item_name in raw_items:
|
|
351
|
+
if item_name in ['.', '..']:
|
|
352
|
+
continue
|
|
353
|
+
# 尝试切换目录来判断是否为目录
|
|
354
|
+
try:
|
|
355
|
+
self.client.cwd(item_name)
|
|
356
|
+
self.client.cwd('..')
|
|
357
|
+
# 能切换成功,说明是目录,跳过
|
|
358
|
+
continue
|
|
359
|
+
except:
|
|
360
|
+
# 不能切换,说明是文件
|
|
361
|
+
if fnmatch(item_name, self.pattern):
|
|
362
|
+
files.append(item_name)
|
|
363
|
+
|
|
364
|
+
# 确保回到原始目录
|
|
365
|
+
try:
|
|
366
|
+
self.client.cwd(current_dir)
|
|
367
|
+
except:
|
|
368
|
+
pass
|
|
369
|
+
|
|
216
370
|
print(f"✓ FTP 找到 {len(files)} 个文件 (匹配 pattern)")
|
|
217
371
|
return files
|
|
218
372
|
|
|
@@ -232,11 +386,12 @@ class FtpSource(Source):
|
|
|
232
386
|
logger.debug(f"FTP 获取文件时间失败 {file_path}: {exc}")
|
|
233
387
|
|
|
234
388
|
normalized_path = file_path.lstrip('/')
|
|
389
|
+
version = _to_millis_timestamp_string(date_modified)
|
|
235
390
|
data_source = {
|
|
236
391
|
'url': f"ftp://{self.host}:{self.port}/{normalized_path}",
|
|
237
|
-
'version':
|
|
238
|
-
'date_created':
|
|
239
|
-
'date_modified':
|
|
392
|
+
'version': version,
|
|
393
|
+
'date_created': version,
|
|
394
|
+
'date_modified': version,
|
|
240
395
|
'record_locator': {
|
|
241
396
|
'server': f"{self.host}:{self.port}",
|
|
242
397
|
'protocol': 'ftp',
|
|
@@ -251,7 +406,7 @@ class SmbSource(Source):
|
|
|
251
406
|
"""SMB/CIFS 数据源"""
|
|
252
407
|
|
|
253
408
|
def __init__(self, host: str, share_name: str, username: str, password: str,
|
|
254
|
-
domain: str = '', port: int = 445, path: str = '', pattern: str = '*'):
|
|
409
|
+
domain: str = '', port: int = 445, path: str = '', pattern: str = '*', recursive: bool = False):
|
|
255
410
|
self.host = host
|
|
256
411
|
self.share_name = share_name
|
|
257
412
|
self.username = username
|
|
@@ -260,6 +415,7 @@ class SmbSource(Source):
|
|
|
260
415
|
self.port = port
|
|
261
416
|
self.path = path.strip('/').strip('\\') if path else ''
|
|
262
417
|
self.pattern = pattern or '*'
|
|
418
|
+
self.recursive = recursive
|
|
263
419
|
|
|
264
420
|
self.conn = SMBConnection(
|
|
265
421
|
username,
|
|
@@ -291,7 +447,10 @@ class SmbSource(Source):
|
|
|
291
447
|
item_path = f"{current_path.rstrip('/')}/{item.filename}" if current_path != '/' else f"/{item.filename}"
|
|
292
448
|
relative_path = item_path[len(base_path):].lstrip('/')
|
|
293
449
|
if item.isDirectory:
|
|
294
|
-
|
|
450
|
+
if self.recursive:
|
|
451
|
+
# 递归模式:继续递归子目录
|
|
452
|
+
_list_recursive(conn, share, item_path)
|
|
453
|
+
# 非递归模式:忽略子目录
|
|
295
454
|
else:
|
|
296
455
|
if fnmatch(relative_path, self.pattern):
|
|
297
456
|
files.append(relative_path)
|
|
@@ -334,7 +493,7 @@ class SmbSource(Source):
|
|
|
334
493
|
smb_url = f"smb://{self.host}/{self.share_name}{full_path}"
|
|
335
494
|
data_source = {
|
|
336
495
|
'url': smb_url,
|
|
337
|
-
'version':
|
|
496
|
+
'version': _to_millis_timestamp_string(date_modified),
|
|
338
497
|
'date_created': _to_millis_timestamp_string(date_created),
|
|
339
498
|
'date_modified': _to_millis_timestamp_string(date_modified),
|
|
340
499
|
'record_locator': {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: xparse-client
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.6
|
|
4
4
|
Summary: 面向Agent和RAG的新一代文档处理 AI Infra
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
Project-URL: Homepage, https://gitlab.intsig.net/xparse1/xparse-pipeline
|
|
@@ -323,12 +323,16 @@ source = SmbSource(
|
|
|
323
323
|
)
|
|
324
324
|
```
|
|
325
325
|
|
|
326
|
-
>
|
|
326
|
+
> 注 1:所有 Source 均支持 `pattern` 参数,使用 Shell 通配符(`*.pdf`、`**/*.txt` 等)来过滤需要处理的文件;默认为 `*`,即处理全部文件。
|
|
327
|
+
|
|
328
|
+
> 注 2:所有 Source 均支持 `recursive` 参数,表示是否递归遍历,默认为 `False`。
|
|
327
329
|
|
|
328
330
|
### Destination 配置
|
|
329
331
|
|
|
330
332
|
#### 本地 Milvus 向量存储
|
|
331
333
|
|
|
334
|
+
collection 中至少需要包含 `element_id`,`text`,`embeddings`,`record_id` 四个字段。
|
|
335
|
+
|
|
332
336
|
```python
|
|
333
337
|
destination = MilvusDestination(
|
|
334
338
|
db_path='./milvus_pipeline.db', # 本地数据库文件
|
|
@@ -339,6 +343,8 @@ destination = MilvusDestination(
|
|
|
339
343
|
|
|
340
344
|
#### Zilliz 向量存储
|
|
341
345
|
|
|
346
|
+
collection 中至少需要包含 `element_id`,`text`,`embeddings`,`record_id` 四个字段。
|
|
347
|
+
|
|
342
348
|
```python
|
|
343
349
|
destination = MilvusDestination(
|
|
344
350
|
db_path='https://xxxxxxx.serverless.xxxxxxx.cloud.zilliz.com.cn', # zilliz连接地址
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|