文本困惑度计算
更新时间:2026-06-15
简介
困惑度计算算子 - 基于语言模型的文本质量评估解决方案
功能描述
- 语言模型评估
- 基于 KenLM 语言模型计算文本困惑度
- 支持中英文文本质量评估
- 提供文本可读性指标
- 质量评估
- 困惑度越低,文本质量越高
- 适用于文本质量筛选和评估
- 模型核心
- KenLM: 高效的语言模型推理
- SentencePiece: 中英文分词处理
- 计算优化
- 批量处理提升效率
- 内存友好的模型加载
算子参数
输入
| 输入 | 含义 |
|---|---|
| texts | 待处理的文本列,要求元素类型为字符串。 |
输出
| 输出 | 含义 |
|---|---|
| result | pyarrow.Array: 困惑度值列,元素为浮点数类型。 |
参数
| 参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| lang | str | 'zh' | 语种 描述:需要计算的文本的语种 可选值:["en", "zh"] 默认值:"zh" |
| model_path | str | '/opt/aihc/models' | 模型文件所在的路径 默认值:"/opt/aihc/models" |
| model_name | str | 'kenlm/wikipedia' | 模型名称 默认值:"kenlm/wikipedia" |
| rank | int | 0 | GPU 编号 描述:GPU 编号,用于模型加载 默认值:0 |
调用示例
Python
1from __future__ import annotations
2
3import os
4
5import daft
6from daft import col
7
8from daft.aihc.common.udf import aihc_udf
9from daft.aihc.functions.text.perplexity_calculator import PerplexityCalculator
10
11if __name__ == "__main__":
12 if os.getenv("DAFT_RUNNER", "native") == "ray":
13 import ray
14 ray.init(dashboard_host="0.0.0.0", ignore_reinit_error=True)
15 daft.set_runner_ray()
16 daft.set_execution_config(actor_udf_ready_timeout=6000, min_cpu_per_task=0)
17
18 # TODO: 根据实际场景准备样本数据
19 samples = {"texts": [...]}
20 ds = daft.from_pydict(samples)
21 constructor_kwargs = {
22 "lang": 'zh',
23 "model_path": '/opt/aihc/models',
24 "model_name": 'kenlm/wikipedia',
25 "rank": 0,
26 }
27 ds = ds.with_column(
28 "result",
29 aihc_udf(
30 PerplexityCalculator,
31 construct_args=constructor_kwargs,
32 num_cpus=1,
33 concurrency=4,
34 batch_size=8,
35 )(col("texts")),
36 )
37 ds.show()
评价此篇文章
