数据处理
数据处理算子
基类算子
lazyllm.tools.data.LazyLLMDataBase
数据处理算子基类。为注册到 data_register 的算子提供统一行为,包括并发执行、结果保存/恢复、进度记录和错误收集。
主要方法和行为:
- forward(self, input, **kwargs): 处理单条数据(子类/函数实现)。
- forward_batch_input(self, inputs, **kwargs): 处理批量数据并返回最终结果(子类/函数实现)。
- call(self, inputs): 统一入口,会根据子类是否实现 forward 或 forward_batch_input 选择执行逻辑;支持并发执行、断点续传和保存结果。
- set_output(self, path): 设置导出路径,调用后 call 返回导出文件路径而不是内存结果。
构造函数参数:
- _concurrency_mode (str): 并发模式,'process'|'thread'|'single'。
- _save_data (bool): 是否保存中间结果到磁盘以便 Resume。
- _max_workers (int|None): 最大并发工作进程/线程数,None 表示使用默认。
- _ignore_errors (bool): 是否忽略任务异常。
- **kwargs (dict): 其它传递给算子的参数。
配置项(通过 lazyllm.config):
- data_process_path (str): 存储处理结果的根路径。
- data_process_resume (bool): 是否开启 Resume 功能,从进度文件继续处理。
Examples:
from lazyllm.tools.data import LazyLLMDataBase
# simple usage: subclass and implement forward
class EchoOp(LazyLLMDataBase):
def forward(self, data):
return {'text': data.get('text', '')}
op = EchoOp(_save_data=True)
res = op([{'text': 'hello'}]) # returns list or exported path depending on set_output
Source code in lazyllm/tools/data/base_data.py
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 | |
forward(input_data, **kwargs)
子类需要实现的方法,处理单条数据。返回值支持:
- dict: 表示处理后的单条结果。
- list: 表示将一条输入展开为多条输出。
- None: 表示保留原始输入(不修改)。
- 抛出异常或返回错误对象会被记录到错误文件并跳过(依赖配置和调用者)。
Parameters:
-
input(dict) –单条输入数据字典。
-
**kwargs(dict, default:{}) –其它用户传入的参数。
Examples:
from lazyllm.tools.data import LazyLLMDataBase
class MyOp(LazyLLMDataBase):
def forward(self, data):
# return dict or list or None
return {'text': data.get('text', '').upper()}
op = MyOp()
print(op([{'text': 'a'}]))
Source code in lazyllm/tools/data/base_data.py
forward_batch_input(inputs, **kwargs)
子类可实现的批量处理方法,用于在非逐条并发场景下直接接收整个输入列表并返回最终结果列表(可用于自定义批量逻辑或外部服务一次性处理)。
Parameters:
-
inputs(list[dict]) –输入数据列表。
-
**kwargs(dict, default:{}) –其它用户传入的参数。
Examples:
from lazyllm.tools.data import LazyLLMDataBase
class BatchOp(LazyLLMDataBase):
def forward_batch_input(self, inputs):
# implement batch processing and return a list
return [{'text': i.get('text', '').lower()} for i in inputs]
op = BatchOp()
print(op([{'text': 'A'}, {'text': 'B'}]))
Source code in lazyllm/tools/data/base_data.py
set_output(output_path)
设置输出路径,用于把最终结果导出为 jsonl 文件并返回文件路径。
Parameters:
-
output_path(str) –文件夹路径或具体 .jsonl 文件路径。若为文件夹,则在该文件夹下创建以类名命名的 jsonl 文件。
行为:
- 如果传入的是文件夹路径,则在该文件夹下创建以类名命名的 jsonl 文件。
- 如果传入的是以 .jsonl 结尾的路径,则直接写入该文件(必要时会创建目录)。
- 返回写入的绝对路径字符串。
Examples:
from lazyllm.tools.data import Demo2
# export to a directory (will create DemoClass.jsonl)
op = Demo2.rich_content(input_key='text').set_output('./out_dir')
path = op([{'text': 'sample'}])
print(path) # ./out_dir/RichContent.jsonl or similar
# export to a specific file
op = Demo2.rich_content(input_key='text').set_output('./out_dir/results.jsonl')
path = op([{'text': 'sample'}])
print(path) # ./out_dir/results.jsonl
Source code in lazyllm/tools/data/base_data.py
演示算子
lazyllm.tools.data.operators.demo_ops
AddSuffix
Bases: Demo2
通过类方式实现的算子,为指定字段添加后缀。支持并发配置(通过构造参数)。
Parameters:
-
suffix(str) –要添加的后缀
-
input_key(str, default:'content') –文本字段名
-
_max_workers(int | None) –可选,最大并发数
-
_concurrency_mode(str, default:'process') –可选,并发模式
-
_save_data(bool) –可选,是否保存结果
Examples:
from lazyllm.tools.data.operators.demo_ops import AddSuffix
op = AddSuffix(suffix='!!!', input_key='text', _max_workers=2)
print(op([{'text': 'wow'}])) # [{'text': 'wow!!!'}]
Source code in lazyllm/tools/data/operators/demo_ops.py
build_pre_suffix(data, input_key='content', prefix='', suffix='')
对输入列表中每项在指定字段前后添加前缀和后缀。此算子以批处理函数注册(forward_batch_input)。
Parameters:
-
data(list[dict]) –输入列表
-
input_key(str, default:'content') –文本字段名
-
prefix(str, default:'') –要添加的前缀
-
suffix(str, default:'') –要添加的后缀
Examples:
from lazyllm.tools.data.operators.demo_ops import build_pre_suffix
op = build_pre_suffix(input_key='text', prefix='Hello, ', suffix='!')
print(op([{'text': 'world'}]))
# [{'text': 'Hello, world!'}]
Source code in lazyllm/tools/data/operators/demo_ops.py
error_prone_op(data, input_key='content')
一个用于测试的算子:在特定输入(content == 'fail')时抛出异常,否则返回处理后的字典结果。用于验证错误收集与跳过逻辑。
Parameters:
-
data(dict) –单条数据字典
-
input_key(str, default:'content') –文本字段名
Examples:
from lazyllm.tools.data.operators.demo_ops import error_prone_op
op = error_prone_op(input_key='text', _save_data=True, _concurrency_mode='single')
res = op([{'text': 'ok'}, {'text': 'fail'}, {'text': 'ok2'}])
# valid results skip the failed item; error details written to error file
Source code in lazyllm/tools/data/operators/demo_ops.py
process_uppercase(data, input_key='content')
将输入文本字段转换为大写。适用于单条处理函数注册(forward)。
Parameters:
-
data(dict) –单条数据字典
-
input_key(str, default:'content') –文本字段名,默认 'content'
Examples:
from lazyllm.tools.data.operators.demo_ops import process_uppercase
op = process_uppercase(input_key='text')
print(op({'text': 'hello'})) # {'text': 'HELLO'}
Source code in lazyllm/tools/data/operators/demo_ops.py
rich_content(data, input_key='content')
将单条输入拆分为多条输出,生成富内容表示(原始 + 若干派生)。适用于返回 list 的 forward。
Parameters:
-
data(dict) –单条数据字典
-
input_key(str, default:'content') –文本字段名
Examples:
from lazyllm.tools.data.operators.demo_ops import rich_content
op = rich_content(input_key='text')
print(op({'text': 'This is a test.'}))
# [
# {'text': 'This is a test.'},
# {'text': 'This is a test. - part 1'},
# {'text': 'This is a test. - part 2'}
# ]
Source code in lazyllm/tools/data/operators/demo_ops.py
数据处理 Pipeline
演示Pipeline
lazyllm.tools.data.pipelines.demo_pipelines
build_demo_pipeline(input_key='text')
构建演示用数据处理流水线(Pipeline),包含若干示例算子并展示如何在 pipeline 上组合使用这些算子。
Parameters:
-
input_key(str, default:'text') –要处理的文本字段名,默认 'text'
Returns:
一个可调用的 pipeline 对象,调用时会按顺序执行其中注册的算子。
Examples:
from lazyllm.tools.data.pipelines.demo_pipelines import build_demo_pipeline
ppl = build_demo_pipeline(input_key='text')
data = [{'text': 'lazyLLM'}]
res = ppl(data)
print(res) # demonstrates how operators are combined and applied