Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,192 @@
|
|
1 |
-
---
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
language:
|
3 |
+
- en
|
4 |
+
library_name: transformers
|
5 |
+
base_model: meta-llama/Llama-3.2-1B
|
6 |
+
license: apache-2.0
|
7 |
+
---
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
## β¨ Latest News
|
12 |
+
|
13 |
+
- [11/06/2024]: Our paper is available on arXiv. You can access it [here](https://arxiv.org/abs/2411.02959).
|
14 |
+
- [11/05/2024]: The open-source toolkit and models are released. You can apply HtmlRAG in your own RAG systems now.
|
15 |
+
|
16 |
+
|
17 |
+
## Model Information
|
18 |
+
|
19 |
+
<p align="left">
|
20 |
+
β’ π <a href="https://arxiv.org/abs/2411.02959" target="_blank">Paper</a> β’ π€ <a href="https://huggingface.co/zstanjj/SlimPLM-Query-Rewriting/" target="_blank">Hugging Face</a> ⒠𧩠<a href="https://github.com/plageon/SlimPLM" target="_blank">Github</a>
|
21 |
+
</p>
|
22 |
+
|
23 |
+
We propose HtmlRAG, which uses HTML instead of plain text as the format of external knowledge in RAG systems. To tackle the long context brought by HTML, we propose **Lossless HTML Cleaning** and **Two-Step Block-Tree-Based HTML Pruning**.
|
24 |
+
|
25 |
+
- **Lossless HTML Cleaning**: This cleaning process just removes totally irrelevant contents and compress redundant structures, retaining all semantic information in the original HTML. The compressed HTML of lossless HTML cleaning is suitable for RAG systems that have long-context LLMs and are not willing to loss any information before generation.
|
26 |
+
|
27 |
+
- **Two-Step Block-Tree-Based HTML Pruning**: The block-tree-based HTML pruning consists of two steps, both of which are conducted on the block tree structure. The first pruning step uses a embedding model to calculate scores for blocks, while the second step uses a path generative model. The first step processes the result of lossless HTML cleaning, while the second step processes the result of the first pruning step.
|
28 |
+
|
29 |
+
|
30 |
+
πΉ If you use this model, please star our **[GitHub repository](https://github.com/plageon/HTMLRAG)** to support us. Your star means a lot!
|
31 |
+
|
32 |
+
## π¦ Installation
|
33 |
+
|
34 |
+
Install the package using pip:
|
35 |
+
```bash
|
36 |
+
pip install htmlrag
|
37 |
+
```
|
38 |
+
Or install the package from source:
|
39 |
+
```bash
|
40 |
+
pip install -e .
|
41 |
+
```
|
42 |
+
|
43 |
+
---
|
44 |
+
|
45 |
+
## π User Guide
|
46 |
+
|
47 |
+
### π§Ή HTML Cleaning
|
48 |
+
|
49 |
+
```python
|
50 |
+
from htmlrag import clean_html
|
51 |
+
|
52 |
+
question = "When was the bellagio in las vegas built?"
|
53 |
+
html = """
|
54 |
+
<html>
|
55 |
+
<head>
|
56 |
+
<title>When was the bellagio in las vegas built?</title>
|
57 |
+
</head>
|
58 |
+
<body>
|
59 |
+
<p class="class0">The Bellagio is a luxury hotel and casino located on the Las Vegas Strip in Paradise, Nevada. It was built in 1998.</p>
|
60 |
+
</body>
|
61 |
+
<div>
|
62 |
+
<div>
|
63 |
+
<p>Some other text</p>
|
64 |
+
<p>Some other text</p>
|
65 |
+
</div>
|
66 |
+
</div>
|
67 |
+
<p class="class1"></p>
|
68 |
+
<!-- Some comment -->
|
69 |
+
<script type="text/javascript">
|
70 |
+
document.write("Hello World!");
|
71 |
+
</script>
|
72 |
+
</html>
|
73 |
+
"""
|
74 |
+
|
75 |
+
simplified_html = clean_html(html)
|
76 |
+
print(simplified_html)
|
77 |
+
|
78 |
+
# <html>
|
79 |
+
# <title>When was the bellagio in las vegas built?</title>
|
80 |
+
# <p>The Bellagio is a luxury hotel and casino located on the Las Vegas Strip in Paradise, Nevada. It was built in 1998.</p>
|
81 |
+
# <div>
|
82 |
+
# <p>Some other text</p>
|
83 |
+
# <p>Some other text</p>
|
84 |
+
# </div>
|
85 |
+
# </html>
|
86 |
+
```
|
87 |
+
|
88 |
+
|
89 |
+
### π² Build Block Tree
|
90 |
+
|
91 |
+
```python
|
92 |
+
from htmlrag import build_block_tree
|
93 |
+
|
94 |
+
block_tree, simplified_html = build_block_tree(simplified_html, max_node_words=10)
|
95 |
+
for block in block_tree:
|
96 |
+
print("Block Content: ", block[0])
|
97 |
+
print("Block Path: ", block[1])
|
98 |
+
print("Is Leaf: ", block[2])
|
99 |
+
print("")
|
100 |
+
|
101 |
+
# Block Content: <title>When was the bellagio in las vegas built?</title>
|
102 |
+
# Block Path: ['html', 'title']
|
103 |
+
# Is Leaf: True
|
104 |
+
#
|
105 |
+
# Block Content: <div>
|
106 |
+
# <p>Some other text</p>
|
107 |
+
# <p>Some other text</p>
|
108 |
+
# </div>
|
109 |
+
# Block Path: ['html', 'div']
|
110 |
+
# Is Leaf: True
|
111 |
+
#
|
112 |
+
# Block Content: <p>The Bellagio is a luxury hotel and casino located on the Las Vegas Strip in Paradise, Nevada. It was built in 1998.</p>
|
113 |
+
# Block Path: ['html', 'p']
|
114 |
+
# Is Leaf: True
|
115 |
+
```
|
116 |
+
|
117 |
+
### βοΈ Prune HTML Blocks with Embedding Model
|
118 |
+
|
119 |
+
```python
|
120 |
+
from htmlrag import EmbedHTMLPruner
|
121 |
+
|
122 |
+
embed_html_pruner = EmbedHTMLPruner(embed_model="bm25")
|
123 |
+
block_rankings = embed_html_pruner.calculate_block_rankings(question, simplified_html, block_tree)
|
124 |
+
print(block_rankings)
|
125 |
+
|
126 |
+
# [0, 2, 1]
|
127 |
+
|
128 |
+
from transformers import AutoTokenizer
|
129 |
+
|
130 |
+
chat_tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-70B-Instruct")
|
131 |
+
|
132 |
+
max_context_window = 60
|
133 |
+
pruned_html = embed_html_pruner.prune_HTML(simplified_html, block_tree, block_rankings, chat_tokenizer, max_context_window)
|
134 |
+
print(pruned_html)
|
135 |
+
|
136 |
+
# <html>
|
137 |
+
# <title>When was the bellagio in las vegas built?</title>
|
138 |
+
# <p>The Bellagio is a luxury hotel and casino located on the Las Vegas Strip in Paradise, Nevada. It was built in 1998.</p>
|
139 |
+
# </html>
|
140 |
+
```
|
141 |
+
|
142 |
+
|
143 |
+
### βοΈ Prune HTML Blocks with Generative Model
|
144 |
+
|
145 |
+
```python
|
146 |
+
from htmlrag import GenHTMLPruner
|
147 |
+
|
148 |
+
ckpt_path = "zstanjj/HTML-Pruner-Llama-1B"
|
149 |
+
gen_embed_pruner = GenHTMLPruner(gen_model=ckpt_path, max_node_words=10)
|
150 |
+
block_rankings = gen_embed_pruner.calculate_block_rankings(question, pruned_html)
|
151 |
+
print(block_rankings)
|
152 |
+
|
153 |
+
# [1, 0]
|
154 |
+
|
155 |
+
max_context_window = 32
|
156 |
+
pruned_html = gen_embed_pruner.prune_HTML(pruned_html, block_tree, block_rankings, chat_tokenizer, max_context_window)
|
157 |
+
print(pruned_html)
|
158 |
+
|
159 |
+
# <p>The Bellagio is a luxury hotel and casino located on the Las Vegas Strip in Paradise, Nevada. It was built in 1998.</p>
|
160 |
+
```
|
161 |
+
|
162 |
+
## Results
|
163 |
+
|
164 |
+
- **Results for [HTML-Pruner-Phi-3.8B](https://huggingface.co/zstanjj/HTML-Pruner-Phi-3.8B) and [HTML-Pruner-Llama-1B](https://huggingface.co/zstanjj/HTML-Pruner-Llama-1B) with Llama-3.1-70B-Instruct as chat model**.
|
165 |
+
|
166 |
+
| Dataset | ASQA | HotpotQA | NQ | TriviaQA | MuSiQue | ELI5 |
|
167 |
+
|------------------|-----------|-----------|-----------|-----------|-----------|-----------|
|
168 |
+
| Metrics | EM | EM | EM | EM | EM | ROUGE-L |
|
169 |
+
| BM25 | 49.50 | 38.25 | 47.00 | 88.00 | 9.50 | 16.15 |
|
170 |
+
| BGE | 68.00 | 41.75 | 59.50 | 93.00 | 12.50 | 16.20 |
|
171 |
+
| E5-Mistral | 63.00 | 36.75 | 59.50 | 90.75 | 11.00 | 16.17 |
|
172 |
+
| LongLLMLingua | 62.50 | 45.00 | 56.75 | 92.50 | 10.25 | 15.84 |
|
173 |
+
| JinaAI Reader | 55.25 | 34.25 | 48.25 | 90.00 | 9.25 | 16.06 |
|
174 |
+
| HtmlRAG-Phi-3.8B | **68.50** | **46.25** | 60.50 | **93.50** | **13.25** | **16.33** |
|
175 |
+
| HtmlRAG-Llama-1B | 66.50 | 45.00 | **60.75** | 93.00 | 10.00 | 16.25 |
|
176 |
+
|
177 |
+
---
|
178 |
+
|
179 |
+
## π Citation
|
180 |
+
|
181 |
+
```bibtex
|
182 |
+
@misc{tan2024htmlraghtmlbetterplain,
|
183 |
+
title={HtmlRAG: HTML is Better Than Plain Text for Modeling Retrieved Knowledge in RAG Systems},
|
184 |
+
author={Jiejun Tan and Zhicheng Dou and Wen Wang and Mang Wang and Weipeng Chen and Ji-Rong Wen},
|
185 |
+
year={2024},
|
186 |
+
eprint={2411.02959},
|
187 |
+
archivePrefix={arXiv},
|
188 |
+
primaryClass={cs.IR},
|
189 |
+
url={https://arxiv.org/abs/2411.02959},
|
190 |
+
}
|
191 |
+
```
|
192 |
+
|