Skip to content

Commit e7adf06

Browse files
authored
Merge pull request #2 from e06084/main
feat: refine llm-webkit extractor
2 parents b7875e4 + 31ae065 commit e7adf06

File tree

5 files changed

+1127
-60
lines changed

5 files changed

+1127
-60
lines changed

docs/LLM_WEBKIT.md

Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
# LLM-WebKit Extractor 使用指南
2+
3+
## 概述
4+
5+
LLM-WebKit Extractor集成了大语言模型(LLM)推理能力,能够智能地理解HTML结构并准确提取主要内容。
6+
7+
## 安装依赖
8+
9+
```bash
10+
# 基础依赖
11+
pip install torch transformers
12+
13+
# VLLM推理引擎
14+
pip install vllm
15+
16+
# LLM-WebKit HTML处理
17+
pip install llm_web_kit
18+
19+
# 可选:加速库
20+
pip install flash-attn # GPU加速
21+
```
22+
23+
## 基本使用
24+
25+
### 1. 创建Extractor
26+
27+
```python
28+
from webmainbench.extractors import ExtractorFactory
29+
30+
# 使用默认配置
31+
extractor = ExtractorFactory.create("llm-webkit")
32+
33+
# 使用自定义配置
34+
config = {
35+
"model_path": "/Users/chupei/model/checkpoint-3296",
36+
"use_logits_processor": True,
37+
"temperature": 0.0,
38+
"max_item_count": 500
39+
}
40+
extractor = ExtractorFactory.create("llm-webkit", config=config)
41+
```
42+
43+
### 2. 提取内容
44+
45+
```python
46+
html_content = """
47+
<html>
48+
<body>
49+
<nav _item_id="1">导航菜单</nav>
50+
<main _item_id="2">主要文章内容</main>
51+
<aside _item_id="3">侧边栏广告</aside>
52+
</body>
53+
</html>
54+
"""
55+
56+
result = extractor.extract(html_content)
57+
58+
if result.success:
59+
print(f"提取的内容: {result.content}")
60+
print(f"置信度: {result.confidence_score}")
61+
print(f"分类结果: {result.metadata['classification_result']}")
62+
else:
63+
print(f"提取失败: {result.error_message}")
64+
```
65+
66+
## 配置选项
67+
68+
### LLMInferenceConfig 参数
69+
70+
| 参数 | 类型 | 默认值 | 说明 |
71+
|------|------|--------|------|
72+
| `model_path` | str | `"/share/liukaiwen/models/qwen3-0.6b/checkpoint-3296"` | LLM模型路径 |
73+
| `use_logits_processor` | bool | `True` | 是否启用JSON格式约束 |
74+
| `max_tokens` | int | `32768` | 最大输入token数 |
75+
| `temperature` | float | `0.0` | 采样温度(0=确定性输出) |
76+
| `top_p` | float | `0.95` | 核采样参数 |
77+
| `max_output_tokens` | int | `8192` | 最大输出token数 |
78+
| `tensor_parallel_size` | int | `1` | 张量并行大小 |
79+
| `dtype` | str | `"bfloat16"` | 模型精度 |
80+
| `max_item_count` | int | `1000` | 处理的最大item数量 |
81+
82+
### 模式配置示例
83+
84+
#### 快速模式(适合批量处理)
85+
```python
86+
fast_config = {
87+
"use_logits_processor": False, # 禁用格式约束提高速度
88+
"temperature": 0.0,
89+
"max_item_count": 200,
90+
"max_output_tokens": 2048,
91+
"dtype": "float16" # 更快的精度
92+
}
93+
```
94+
95+
#### 精确模式(适合高质量提取)
96+
```python
97+
precise_config = {
98+
"use_logits_processor": True, # 启用格式约束
99+
"temperature": 0.0,
100+
"max_item_count": 1000,
101+
"max_output_tokens": 8192,
102+
"dtype": "bfloat16" # 更好的精度
103+
}
104+
```
105+
106+
#### 分布式模式(多GPU)
107+
```python
108+
distributed_config = {
109+
"tensor_parallel_size": 4, # 使用4个GPU
110+
"dtype": "bfloat16",
111+
"max_item_count": 2000, # 可以处理更复杂的HTML
112+
}
113+
```
114+
115+
## 工作流程详解
116+
117+
### 1. HTML预处理
118+
```python
119+
# 使用llm_web_kit简化HTML结构
120+
simplified_html, raw_tag_html, _ = simplify_html(original_html)
121+
```
122+
123+
### 2. 复杂度检查
124+
```python
125+
item_count = simplified_html.count('_item_id')
126+
if item_count > max_item_count:
127+
# 跳过过于复杂的HTML
128+
return error_result
129+
```
130+
131+
### 3. LLM推理
132+
```python
133+
# 创建分类提示
134+
prompt = create_classification_prompt(simplified_html)
135+
136+
# 使用VLLM生成分类结果
137+
output = model.generate(prompt, sampling_params)
138+
classification = parse_json_output(output)
139+
```
140+
141+
### 4. 内容重建
142+
```python
143+
# 根据分类结果重建主要内容
144+
main_content, content_list = reconstruct_content(
145+
original_html, classification
146+
)
147+
```
148+
149+
## 提示工程
150+
151+
### 分类标准
152+
153+
**主要内容 ("main")**:
154+
- 文章正文、博客内容
155+
- 问答的问题和答案
156+
- 论坛的主要讨论内容
157+
- 嵌入的相关图片和媒体
158+
159+
**辅助内容 ("other")**:
160+
- 导航菜单、侧边栏、页脚
161+
- 元数据(作者、时间、浏览量等)
162+
- 广告和推广内容
163+
- 相关推荐和建议内容
164+
165+
### 自定义提示模板
166+
167+
如果需要修改分类逻辑,可以继承类并重写提示模板:
168+
169+
```python
170+
class CustomLlmWebkitExtractor(LlmWebkitExtractor):
171+
CLASSIFICATION_PROMPT = """
172+
您的自定义分类提示...
173+
输入HTML: {alg_html}
174+
"""
175+
```
176+
177+
## 性能优化建议
178+
179+
### 1. 模型选择
180+
- **小模型** (0.5B-1B): 适合快速批处理,准确率略低
181+
- **中等模型** (3B-7B): 平衡性能和准确率
182+
- **大模型** (13B+): 最高准确率,适合高质量需求
183+
184+
### 2. 硬件配置
185+
```python
186+
# 单GPU配置
187+
config = {
188+
"tensor_parallel_size": 1,
189+
"dtype": "bfloat16", # A100/H100推荐
190+
# "dtype": "float16", # V100/RTX推荐
191+
}
192+
193+
# 多GPU配置
194+
config = {
195+
"tensor_parallel_size": 4, # 4个GPU
196+
"dtype": "bfloat16",
197+
}
198+
```
199+
200+
### 3. 批处理优化
201+
```python
202+
# 预加载模型避免重复初始化
203+
extractor = ExtractorFactory.create("llm-webkit", config)
204+
205+
# 批量处理
206+
for html in html_list:
207+
result = extractor.extract(html)
208+
process_result(result)
209+
```
210+
211+
## 故障排除
212+
213+
### 常见问题
214+
215+
1. **模型加载失败**
216+
```
217+
RuntimeError: Failed to load LLM model
218+
```
219+
- 检查模型路径是否正确
220+
- 确保有足够的GPU内存
221+
- 验证模型格式兼容性
222+
223+
2. **JSON解析错误**
224+
```
225+
Warning: LLM output is not valid JSON
226+
```
227+
- 启用 `use_logits_processor=True`
228+
- 检查提示模板格式
229+
- 降低temperature增加确定性
230+
231+
3. **内存不足**
232+
```
233+
CUDA out of memory
234+
```
235+
- 减少 `max_item_count`
236+
- 降低 `max_output_tokens`
237+
- 使用 `dtype="float16"`
238+
- 增加 `tensor_parallel_size`
239+
240+
4. **处理速度慢**
241+
- 禁用 `use_logits_processor`
242+
- 减少 `max_output_tokens`
243+
- 使用更小的模型
244+
- 增加GPU并行度
245+
246+
### 调试技巧
247+
248+
```python
249+
# 启用详细日志
250+
import logging
251+
logging.basicConfig(level=logging.DEBUG)
252+
253+
# 检查分类结果
254+
result = extractor.extract(html)
255+
if result.success:
256+
print("分类详情:", result.metadata['classification_result'])
257+
print("LLM原始输出:", result.metadata['llm_output'])
258+
```
259+
260+
## 集成示例
261+
262+
### 与WebMainBench评测框架集成
263+
264+
```python
265+
from webmainbench.evaluator import Evaluator
266+
from webmainbench.data import BenchmarkDataset
267+
268+
# 创建数据集
269+
dataset = BenchmarkDataset.from_file("test_data.jsonl")
270+
271+
# 配置LLM-WebKit extractor
272+
config = {
273+
"model_path": "/path/to/model",
274+
"use_logits_processor": True,
275+
"max_item_count": 500
276+
}
277+
278+
# 运行评测
279+
evaluator = Evaluator(extractor_name="llm-webkit", extractor_config=config)
280+
results = evaluator.evaluate(dataset)
281+
282+
print(f"平均得分: {results['overall_score']}")
283+
print(f"处理速度: {results['processing_speed']} samples/s")
284+
```
File renamed without changes.

0 commit comments

Comments
 (0)