GraphRAG面—一种基于知识图谱的大模型检索增强实现策略来自:AiGC面试宝典宁静致远2023年09月29日14:07地址:https://siwei.io/graph-rag/一、为什么需要GraphRAG?虽然llamaindex能够利用摘要索引进行增强的方案,但这些都是利用非结构化文本在做。对于知识图谱,是否可以将其作为一路召回,提高检索的相关性,这个可以利用好知识图谱内部的知识。知识图谱可以减少基于嵌入的语义搜索所导致的不准确性。eg:“保温大棚”与“保温杯”,尽管在语义上两者是存在相关性的,但在大多数场景下,这种通用语义(Embedding)下的相关性很高,进而作为错误的上下文而引入“幻觉”。这时候,可以利用领域知识的知识图谱来缓解这种幻觉。二、什么是GraphRAG?GraphRAG(Retrieval-AugmentedGeneration),是一种基于知识图谱的检索增强技术,通过构建图模型的知识表达,将实体和关系之间的联系用图的形式进行展示,然后利用大语言模型LLM进行检索增强。三、GraphRAG思路介绍?GraphRAG将知识图谱等价于一个超大规模的词汇表,而实体和关系则对应于单词。通过这种方式,GraphRAG在检索时能够将实体和关系作为单元进行联合建模。GraphRAG思想:对用户输入的query提取实体,然后构造子图形成上下文,最后送入大模型完成生成四、用代码介绍GraphRAG?•GraphRAG(Retrieval-AugmentedGeneration)面——一种基于知识图谱的大模型检索增强实现策略•一、为什么需要GraphRAG?•二、什么是GraphRAG?•三、GraphRAG思路介绍?•四、用代码介绍GraphRAG?•五、用示例介绍GraphRAG?•六、GraphRAG排序优化方式?•致谢defsimple_graph_rag(query_str,nebulagraph_store,llm):entities=_get_key_entities(query_str,llm)graph_rag_context=_retrieve_subgraph_context(entities)return_synthesize_answer(query_str,graph_rag_context,llm)扫码加查看更多这样一来,知识图谱召回可以作为一路和传统的召回进行融合。``五、用示例介绍GraphRAG?示例一:当用户输入,tellmeaboutPeterquill时,先识别关键词quil,编写cypher语句获得二跳结果。示例二用户输入:TellmeeventsaboutNASA得到关键词:Querykeywords:['NASA','events']召回二度逻辑:1.使用LLM(或其他)模型从问题中提取关键实体。def_get_key_entities(query_str,llm=None,with_llm=True):...return_expand_synonyms(entities)2.根据这些实体检索子图,深入到一定的深度,例如可以是2度甚至更多。def_get_key_entities(query_str,llm=None,with_llm=True):...return_expand_synonyms(entities)3.利用获得的上下文利用LLM产生答案def_synthesize_answer(query_str,graph_rag_context,llm):returnllm.predict(PROMPT_SYNTHESIZE_AND_REFINE,query_str,graph_rag_context)Extractedrelationships:Thefollowingareknowledgetripletsinmaxdepth2intheformof`subject[predicate,object,predicate_next_hop,object_next_hop...]nasa['publicreleasedate','mid-2023']送入LLM完成问答。nasa['announces','futurespacetelescopeprograms']nasa['publishesimagesof','debrisdisk']nasa['discovers','exoplanetlhs475b']INFO:llama_index.indices.knowledge_graph.retriever:>Startingquery:TellmeeventsaboutNASA>Startingquery:TellmeeventsaboutNASA>Startingquery:TellmeeventsaboutNASAINFO:llama_index.indices.knowledge_graph.retriever:>Querykeywords:['NASA','events']>Querykeywords:['NASA','events']>Querykeywords:['NASA','events']INFO:llama_index.indices.knowledge_graph.retriever:>Extractedrelationships:Thefollowingareknowledgetripletsinmaxdepth2intheformof`subject[predicate,object,predicate_next_hop,object_next_hop...]`nasa['publicreleasedate','mid-2023']nasa['announces','futurespacetelescopeprograms']nasa['publishesimagesof','debrisdisk']nasa['discovers','exoplanetlhs475b']>Extractedrelationships:Thefollowingareknowledgetripletsinmaxdepth2intheformof`subject[predicate,object,predicate_nex...
发表评论取消回复