【论文复现】HippoRAG & HippoRAG2
模型结构: 
安装
1 | |
下载数据集:huggingface-cli download --repo-type dataset osunlp/HippoRAG_2 --local-dir dataset
下载 Embedding 模型(NV-Embed, GritLM,
Contriever):huggingface-cli download nvidia/NV-Embed-v2 --local-dir model/NV-Embed-v2
下载
LLM:huggingface-cli download --token Your_token meta-llama/Llama-3.1-8B-Instruct --local-dir model/Llama-3.1-8B-Instruct
start.py
1 | |
执行流程:
graph TD
A["载入大模型(Loading checkpoint shards)"] --> B["实体识别(NER)"]
B --> C["提取三元组(Extractin triples)"]
C --> D["(Batch Encoding)KNN for Queries"]
D --> E[Retrieving]
E --> F[Collecting QA prompts]
F --> G[QA Reading]
G --> H[Extraction Answers from LLM Response]
生成内容: 1
2
3
4
5
6
7
8
9
10
11${llm_model_name}_${embedding_model_name}/
chunk_embeddings/vdb_chunk.parquet
entity_embeddings/vdb_chunk.parquet
fact_embeddings/vdb_chunk.parquet
graph.graphml
llm_cache/
${llm_model_name}_cache.sqlite
${llm_model_name}_cache.sqlite.lock
openie_results_ner_${llm_model_name}.json
openie_results_ner_${llm_model_name}.json 结构:
1
2
3
4
5
6
7
8
9
10
11
12
13{
"docs":[
{
"idx": "段落标识符",
"passage": "段落",
"extracted_entities": ["实体", ...],
"extracted_triples": [["三元组"], ...],
},
...
]
"avg_ent_chars": 所有提取实体的平均字符数,
"avg_ent_words": 所有提取实体的平均字符数
}
调用 API
调用阿里云百炼的 DeepSeek-R1
API: 1
2
3export OPENAI_API_KEY="Your API Key"
conda activate hipporag
python start.py online
输出: 1
2
3
4
5
6
7
8
9{
'num_phrase_nodes': 17,
'num_passage_nodes': 9,
'num_total_nodes': 26,
'num_extracted_triples': 13,
'num_triples_with_passage_node': 22,
'num_synonymy_triples': 15,
'num_total_triples': 50
}
1 | |
1 | |
1 | |
1 | |
vllm 本地部署
如果发生 OOM(out of memory),调整
gpu-memory-utilization 或 max_model_len 以适应
GPU
内存:vllm serve model/Llama-3.1-8B-Instruct --tensor-parallel-size 2 --max_model_len 4096 --gpu-memory-utilization 0.95 --dtype half
运行:python start.py offline
输出: 1
{'num_phrase_nodes': 16, 'num_passage_nodes': 9, 'num_total_nodes': 25, 'num_extracted_triples': 13, 'num_triples_with_passage_node': 20, 'num_synonymy_triples': 13, 'num_total_triples': 46}
打印的结构类似,因此只展示 2 个结果: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17QA results: (
[
QuerySolution(question="What is George Rankin's occupation?", docs=['George Rankin is a politician.', 'Thomas Marwick is a politician.'], doc_scores=array([0.10445492, 0.02884537]), answer='Politician.', gold_answers=None, gold_docs=None),
QuerySolution(question='How did Cinderella reach her happy ending?', docs=['When the slipper fit perfectly, Cinderella was reunited with the prince.', 'Cinderella attended the royal ball.'], doc_scores=array([0.04447086, 0.04025739]), answer='She attended the royal ball and was reunited with the prince after the slipper fit perfectly.', gold_answers=None, gold_docs=None),
QuerySolution(question="What county is Erik Hort's birthplace a part of?", docs=["Erik Hort's birthplace is Montebello.", 'Montebello is a part of Rockland County.'], doc_scores=array([0.09898717, 0.05803498]), answer='Rockland County.', gold_answers=None, gold_docs=None)
],
[
"The text does not provide information about George Rankin's occupation. However, it is mentioned that Thomas Marwick is a politician, and George Rankin is also mentioned as a politician in the Wikipedia title. \nAnswer: Politician.",
"The provided text snippets do not contain information about Cinderella's journey to her happy ending. However, based on general knowledge of the Cinderella fairy tale, it is likely that Cinderella reached her happy ending by attending the royal ball, where she met the prince, and then being reunited with him after the slipper fit perfectly.\n\nAnswer: She attended the royal ball and was reunited with the prince after the slipper fit perfectly.",
"To determine the county Erik Hort's birthplace is a part of, we need to identify the birthplace as Montebello, and then find the county that Montebello is a part of. According to the text, Montebello is a part of Rockland County. \nAnswer: Rockland County."
],
[
{'prompt_tokens': 733, 'completion_tokens': 48, 'finish_reason': 'stop'},
{'prompt_tokens': 742, 'completion_tokens': 87, 'finish_reason': 'stop'},
{'prompt_tokens': 744, 'completion_tokens': 64, 'finish_reason': 'stop'}
]
)
1 | |
绘制 graphml
1 | |
绘制结果: 