Views
No views yet
pip install htmlragpip install -e .1from htmlrag import clean_html
2
3question = "When was the bellagio in las vegas built?"
4html = """
5<html>
6<head>
7<title>When was the bellagio in las vegas built?</title>
8</head>
9<body>
10<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>
11</body>
12<div>
13<div>
14<p>Some other text</p>
15<p>Some other text</p>
16</div>
17</div>
18<p class="class1"></p>
19<!-- Some comment -->
20<script type="text/javascript">
21document.write("Hello World!");
22</script>
23</html>
24"""
25
26simplified_html = clean_html(html)
27print(simplified_html)
28
29# <html>
30# <title>When was the bellagio in las vegas built?</title>
31# <p>The Bellagio is a luxury hotel and casino located on the Las Vegas Strip in Paradise, Nevada. It was built in 1998.</p>
32# <div>
33# <p>Some other text</p>
34# <p>Some other text</p>
35# </div>
36# </html>1from htmlrag import build_block_tree
2
3block_tree, simplified_html = build_block_tree(simplified_html, max_node_words=10)
4for block in block_tree:
5 print("Block Content: ", block[0])
6 print("Block Path: ", block[1])
7 print("Is Leaf: ", block[2])
8 print("")
9
10# Block Content: <title>When was the bellagio in las vegas built?</title>
11# Block Path: ['html', 'title']
12# Is Leaf: True
13#
14# Block Content: <div>
15# <p>Some other text</p>
16# <p>Some other text</p>
17# </div>
18# Block Path: ['html', 'div']
19# Is Leaf: True
20#
21# 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>
22# Block Path: ['html', 'p']
23# Is Leaf: True1from htmlrag import EmbedHTMLPruner
2
3embed_html_pruner = EmbedHTMLPruner(embed_model="bm25")
4block_rankings = embed_html_pruner.calculate_block_rankings(question, simplified_html, block_tree)
5print(block_rankings)
6
7# [0, 2, 1]
8
9from transformers import AutoTokenizer
10
11chat_tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-70B-Instruct")
12
13max_context_window = 60
14pruned_html = embed_html_pruner.prune_HTML(simplified_html, block_tree, block_rankings, chat_tokenizer, max_context_window)
15print(pruned_html)
16
17# <html>
18# <title>When was the bellagio in las vegas built?</title>
19# <p>The Bellagio is a luxury hotel and casino located on the Las Vegas Strip in Paradise, Nevada. It was built in 1998.</p>
20# </html>1from htmlrag import GenHTMLPruner
2
3ckpt_path = "zstanjj/HTML-Pruner-Llama-1B"
4gen_embed_pruner = GenHTMLPruner(gen_model=ckpt_path, max_node_words=10)
5block_rankings = gen_embed_pruner.calculate_block_rankings(question, pruned_html)
6print(block_rankings)
7
8# [1, 0]
9
10max_context_window = 32
11pruned_html = gen_embed_pruner.prune_HTML(pruned_html, block_tree, block_rankings, chat_tokenizer, max_context_window)
12print(pruned_html)
13
14# <p>The Bellagio is a luxury hotel and casino located on the Las Vegas Strip in Paradise, Nevada. It was built in 1998.</p>| Dataset | ASQA | HotpotQA | NQ | TriviaQA | MuSiQue | ELI5 |
|---|---|---|---|---|---|---|
| Metrics | EM | EM | EM | EM | EM | ROUGE-L |
| BM25 | 49.50 | 38.25 | 47.00 | 88.00 | 9.50 | 16.15 |
| BGE | 68.00 | 41.75 | 59.50 | 93.00 | 12.50 | 16.20 |
| E5-Mistral | 63.00 | 36.75 | 59.50 | 90.75 | 11.00 | 16.17 |
| LongLLMLingua | 62.50 | 45.00 | 56.75 | 92.50 | 10.25 | 15.84 |
| JinaAI Reader | 55.25 | 34.25 | 48.25 | 90.00 | 9.25 | 16.06 |
| HtmlRAG-Phi-3.8B | 68.50 | 46.25 | 60.50 | 93.50 | 13.25 | 16.33 |
| HtmlRAG-Llama-1B | 66.50 | 45.00 | 60.75 | 93.00 | 10.00 | 16.25 |
1@misc{tan2024htmlraghtmlbetterplain,
2 title={HtmlRAG: HTML is Better Than Plain Text for Modeling Retrieved Knowledge in RAG Systems},
3 author={Jiejun Tan and Zhicheng Dou and Wen Wang and Mang Wang and Weipeng Chen and Ji-Rong Wen},
4 year={2024},
5 eprint={2411.02959},
6 archivePrefix={arXiv},
7 primaryClass={cs.IR},
8 url={https://arxiv.org/abs/2411.02959},
9}