链载Ai

标题: 如何用GraphRAG GNN解锁知识图谱的隐藏价值? [打印本页]

作者: 链载Ai    时间: 3 小时前
标题: 如何用GraphRAG GNN解锁知识图谱的隐藏价值?

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;display: table;padding-right: 0.2em;padding-left: 0.2em;color: rgb(255, 255, 255);background: rgb(1, 155, 252);">如何通过 GNN 提升 GraphRAG 的上下文相关性

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;margin: 0.1em auto 0.5em;border-radius: 4px;" title="null"/>

ingFang SC";letter-spacing: 0.1em;color: rgb(63, 63, 63);">GraphRAG 为大语言模型(LLMs)提供了“推理支架”,通过结构化的知识图谱(KG)支持其推理过程。试想一下,追踪“因果路径”——从发达经济体的财政紧缩到新兴市场的通货膨胀。借助 GraphRAG,可以减少生成幻觉内容(hallucination),并获得意想不到的洞见。但能否更进一步?通过将图注意网络(GATs)集成到 GraphRAG 中,我们可以动态地将上下文与每个查询对齐,从而释放前所未有的推理能力。

ingFang SC";letter-spacing: 0.1em;color: rgb(63, 63, 63);">(本文将使用 IMF 2024 年 10 月的《世界经济展望》报告作为因果推理的示例。)

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;display: table;padding-right: 0.2em;padding-left: 0.2em;color: rgb(255, 255, 255);background: rgb(1, 155, 252);">引言——基线 GraphRAG

ingFang SC";letter-spacing: 0.1em;color: rgb(63, 63, 63);">基线 RAG 在推理任务中表现不佳,因为它主要依赖语义相似性。推断因果关系(通常未明确编码在非结构化文档中)尤其具有挑战性。对于缺乏高级模型精度的小型语言模型,这一问题尤为突出。

ingFang SC";letter-spacing: 0.1em;color: rgb(63, 63, 63);">GraphRAG 通过利用知识图谱(KG)中的结构化关系克服了这些局限性。关键实体(如“发达经济体”、“财政紧缩”和“新兴市场”)被表示为节点,节点之间通过边连接,映射出因果链。例如:

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;color: rgb(63, 63, 63);">ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;margin: 0.1em auto 0.5em;border-radius: 4px;" title="null"/>

通过 Leiden 等算法,GraphRAG 根据关系强度将节点聚类为社区,分组相关概念(如财政政策和通胀)。这种聚类通过减少噪声和增强上下文相关性来改善检索,从而使 LLM 能生成准确、扎实的响应。

然而,基线 GraphRAG 的静态聚类存在局限性。固定的社区可能无法很好地适应跨多个主题的查询,例如财政紧缩和全球贸易中断对通胀的综合影响。这种刚性可能导致:

集成图注意网络(GATs)的 GraphRAG:

GATs集成到 GraphRAG 中,增强了其对节点和边相关性的权重计算能力,从而实现上下文敏感的推理。与传统 GraphRAG 的静态聚类不同,GAT 能根据查询动态调整,通过为节点和边分配注意力分数来反映其与查询的相关性。这使 GraphRAG 能够实时优化上下文,使检索更精准,洞见更贴合查询意图。

在 GAT 中:

  1. 1.节点嵌入:每个节点以其特征向量开始。

  2. 2.注意力分数:在每一层,节点从其邻居处聚合信息,权重由注意力分数决定(通过相似度计算,例如点积)。

  3. 3.多跳扩展:该过程在每一层重复,扩展节点的上下文以包含越来越远的邻居(例如,1 跳、2 跳、3 跳连接)。

  4. 4.最终嵌入:在最后一层,每个节点的嵌入捕获了其自身特征和相关邻居的聚合影响,形成了图知识的整体表示。

为什么这很重要?

GAT 增强的嵌入充当 LLM 的“精炼镜头”,将推理聚焦于图中最相关的部分。这些嵌入作为结构化输入提供给 LLM,将图的知识与查询对齐。这种动态协同使 GraphRAG 能够:

引入 G-Retriever:基于 GNN 的增强图检索

PyG 是一个开源的几何深度学习和基于图神经网络(GNN)的库,在其 2.6 版本中引入了G-Retriever。G-Retriever 仅需几行代码即可将 PyG GNN 与 HuggingFace 的 LLM 无缝整合。基于论文“G-Retriever: Retrieval-Augmented Generation for Textual Graph Understanding and Question Answering”,该工具可实现复杂文本推理任务的高级图检索。

gnn=GAT(
in_channels=768,
hidden_channels=1024,
out_channels=768,
num_layers=4,
heads=8
).to(device)

llm=LLM(model_name='meta-llama/Llama-2-7b-chat-hf',num_params=7).to(device)

g_retriever=GRetriever(llm=llm,gnn=gnn,mlp_out_channels=4096)

G-Retriever 通过结合 GNN 和 LLM 的优势来实现 RAG。在推理时,它通过以下四步处理查询:

  1. 1.LLM 编码:将自然语言查询分词并使用 LLM 编码器编码为嵌入。

  2. 2.子图检索:检索 KG 的相关子图,并使用 GNN 编码器对其节点和边进行编码。

  3. 3.嵌入融合:将 GNN 嵌入(表示 KG 上下文)与 LLM 嵌入(表示查询)连接以形成联合表示。

  4. 4.LLM 解码:将联合嵌入传递给 LLM 解码器,根据丰富的上下文生成自然语言响应。

此工作流确保 G-Retriever 能通过动态整合基于图的推理与 LLM 的上下文理解来处理复杂查询,从而生成更准确、更扎实的响应。

使用 PyG G-Retriever 的示例实现

我已分别在三个笔记本中实现了基线 GraphRAG 的全局查询、使用 GAT 的 G-Retriever 和不使用 GAT 的 G-Retriever,并在附录中引用了这些笔记本。然后我们将使用 gpt-4o 来评估响应。示例简化知识图谱来自 IMF 2024 年 10 月的《世界经济展望》报告。(G-Retriever 已在 llama2–7b-chat-hf 和 gemma-7b 上测试,因此在此示例中使用 llama2–7b-chat-hf。)

以下是结果……

EVALUATION
Wewillevaluatetheanswersusinggpt-4o.

Example1:
Query:"Evaluatethepotentiallong-termeconomicandenvironmentalimpactsofshiftingfromfossilfuelconsumptiontorenewableenergyadoption,consideringfactorssuchasjobcreation,technologicalinnovation,energypolicy,andpublichealth.Additionally,discusshowthistransitionaffectsinternationalagreementsandclimatechangepolicies,andidentifypotentialchallengesininfrastructureinvestmentandsustainableagriculture."
AnswerwithvanillaGraphRAG:"['Shiftingfromfossilfuelconsumptiontorenewableenergyadoptionhasthepotentialtobringaboutsignificantlong-termeconomicandenvironmentalbenefits,whilealsoaddressingclimatechange.Herearesomeofthekeyfactorstoconsider:Addressingtheimpactsofclimatechange,suchasincreasedfrequencyandseverityofnaturaldisasters,canhelptoreducetheriskofdisastersandimprovedisasterriskmanagement.Inconclusion,shiftingfromfossilfuelconsumptiontorenewableenergyadoptionhasthepotentialtobringaboutsignificantlong-termeconomicandenvironmentalbenefits,whilealsoaddressingclimatechange.However,therearealsochallengestobeaddressed,includinginfrastructureinvestmentandsustainableagriculture.Byprioritizingrenewableenergyadoptionandaddressingthesechallenges,wecancreateamoresustainableandresilientfutureforall.']
AnswerwithG-RetrieverGNN:"['Shiftingfromfossilfuelconsumptiontorenewableenergyadoptionhasthepotentialtobringaboutsignificantlong-termeconomicandenvironmentalbenefits.Herearesomeofthekeyfactorstoconsider:\n\n1.JobCreation:Thetransitiontorenewableenergycancreatenewjobopportunitiesinindustriessuchassolarpanelinstallation,windturbinemaintenance,andenergyefficiencyconsulting.AccordingtotheInternationalRenewableEnergyAgency(IRENA),therenewableenergysectorcouldsupportupto46millionjobsworldwideby2050,upfromaround11milliontoday.\n2.TechnologicalInnovation:Theshifttorenewableenergycandriveinnovationinareassuchasenergystorage,gridmanagement,andsmartgrids.Thiscanleadtonewtechnologiesandproductsthatcanbeexportedglobally,creatingneweconomicopportunities.\n3.EnergyPolicy:Thetransitiontorenewableenergycanleadtomoreflexibleanddecentralizedenergysystems,withgreateremphasisonlocalgenerationanddistribution.Thiscanleadtomoreefficientandresilientenergysystems,withreducedtransmissionlossesandincreasedenergysecurity.\n4.PublicHealth:Thetransitiontorenewableenergycanleadtosignificantimprovementsinpublichealth,particularlyinurbanareas.Airpollutionfromfossilfuelsisamajorcontributortorespiratorydiseasesandotherhealthproblems,andtheuseofrenewableenergycanhelptoreducetheseimpacts.\n\nIntermsofinternationalagreementsandclimatechangepolicies,thetransitiontorenewableenergyiscriticalformeetingglobalclimatetargets.TheParisAgreement,signedbynearly200countriesin2015,setsagoaloflimitingglobalwarmingtowellbelow2degreesCelsius(andpursuingeffortstolimititto1.5degrees)abovepre-industriallevels.Renewableenergyisakeycomponentofthiseffort,asitcanhelptoreducegreenhousegasemissionswhileprovidingenergysecurityandeconomicbenefits.\n\nHowever,therearealsopotentialchallengestoconsiderinthetransitiontorenewableenergy,including:\n\n1.InfrastructureInvestment:Thetransitiontorenewableenergywillrequiresignificantinvestmentinnewinfrastructure,includingwindfarms,solarpanels,andenergystoragefacilities.Thiscanbeachallenge,particularlyindevelopingcountrieswheretheremaybelimitedfinancialresourcesandexistinginfrastructure.\n2.SustainableAgriculture:Thetransitiontorenewableenergymayalsohaveimpactsonsustainableagriculture,particularlyinareaswherelandisusedforbiofuelproduction.Thiscanleadtocompetitionforlandandwaterresources,andmayimpactfoodsecurityandrurallivelihoods.\n3.EnergyStorage:Thetransitiontorenewableenergywillalsorequirethedevelopmentofenergystoragetechnologies,suchasbatteriesandhydrogenfuelcells,toensureareliableandstableenergysupply.Thiscanbeachallenge,particularlyinareaswheretheremaybelimitedinfrastructureandresources.\n4.GridIntegration:Theintegrationofrenewableenergyintothegridcanalsobeachallenge,particularlyinareaswheretheremaybelimitedgridcapacityandinfrastructure.Thiscanleadtoissueswithenergytransmissionanddistribution,andmayimpactthereliabilityandstabilityoftheenergysupply.\n\nOverall,thetransitiontorenewableenergyhasthepotentialtobringaboutsignificantlong-termeconomicandenvironmentalbenefits,butitwillrequirecarefulplanning,investment,andcoordinationtoensureasuccessfultransition.']
AnswerwithG-RetrieverwithoutGNN:"['Shiftingfromfossilfuelconsumptiontorenewableenergyadoptionhasthepotentialtohavesignificantlong-termeconomicandenvironmentalimpacts.Herearesomeofthefactorstoconsider:\n\n1.JobCreation:Thetransitiontorenewableenergycancreatenewjobopportunitiesinindustriessuchassolarpanelinstallation,windturbinemaintenance,andenergyefficiency.AccordingtotheInternationalRenewableEnergyAgency(IRENA),therenewableenergysectorcouldsupportupto24millionjobsworldwideby2050.\n2.TechnologicalInnovation:Theshifttorenewableenergycandrivetechnologicalinnovation,particularlyinareassuchasenergystorage,gridmanagement,andsmartgrids.Thiscanleadtonewproductsandservices,andimprovedenergyefficiency.\n3.EnergyPolicy:Thetransitiontorenewableenergycanleadtochangesinenergypolicy,suchasincentivesforrenewableenergyproduction,carbonpricing,andenergyefficiencystandards.Thesepoliciescanhelptoleveltheplayingfieldforrenewableenergyandcreateamoresustainableenergysystem.\n4.PublicHealth:Thetransitiontorenewableenergycanleadtoimprovedairqualityandreducedgreenhousegasemissions,whichcanhavesignificantpublichealthbenefits.Forexample,theWorldHealthOrganization(WHO)estimatesthatairpollutionfromfossilfuelscausesaround7millionprematuredeathsworldwideeachyear.\n5.InternationalAgreementsandClimateChangePolicies:ThetransitiontorenewableenergycanhelpcountriesmeettheircommitmentsunderinternationalagreementssuchastheParisAgreement,whichaimstolimitglobalwarmingtowellbelow2°Cabovepre-industriallevels.Thetransitioncanalsohelptoreducegreenhousegasemissionsandmitigatetheimpactsofclimatechange.\n\nHowever,therearealsopotentialchallengestoconsider:\n\n1.InfrastructureInvestment:Thetransitiontorenewableenergymayrequiresignificantinvestmentininfrastructure,suchasnewpowertransmissionlines,energystoragefacilities,anddistributedenergysystems.Thiscanbeachallengeinareaswithlimitedresourcesorincountrieswithlessdevelopedenergyinfrastructure.\n2.SustainableAgriculture:Thetransitiontorenewableenergymayalsohaveimplicationsforsustainableagriculture,particularlyinareaswherelandisusedforbiofuelproduction.Thiscanleadtocompetitionforlanduseandpotentialimpactsonfoodsecurity.\n3.EnergySecurity:Thetransitiontorenewableenergymayalsoraiseconcernsaboutenergysecurity,particularlyincountrieswithlimitedrenewableenergyresources.Thiscanleadtodependenceonimportedenergysourcesandpotentialvulnerabilitytopricevolatility.\n4.EconomicDisruption:Thetransitiontorenewableenergycanalsoleadtoeconomicdisruptioninindustriessuchasfossilfuelproductionandtransportation.Thiscanleadtojoblossesandeconomicinstabilityincommunitiesreliantontheseindustries.\n5.SocialAcceptance:Finally,thetransitiontorenewableenergymayalsobeinfluencedbysocialacceptance,particularlyinareaswithlimitedpublicsupportforrenewableenergy.Thiscanleadtodelaysorchallengesinimplementingrenewableenergyprojects.\n\nInconclusion,thetransitiontorenewableenergyhasthepotentialtohavesignificantlong-termeconomicandenvironmentalimpacts.Whiletherearechallengestoconsider,thebenefitsofthistransitionfaroutweighthecosts,particularlyintermsofimprovedpublichealth,jobcreation,andreducedgreenhousegasemissions.Withcarefulplanningandpolicysupport,thetransitiontorenewableenergycanbeasuccessfulandsustainablepathforward.']"

|Metric|GraphRAGGlobalQuery|G-retwithGNN|G-retwoGNN|
|-------------------|-----------------------------------------------------------------|
|Groundedness|4|8|7|
|Correctness|6|9|8|
|Coherence|5|9|8|
|Novelty|3|8|7|
|Relevance|6|9|8|
|OverallScore.|4.8|8.6|7.6.|

Example2:
Query:"Whataretheeconomicandenvironmentalimplicationsofrenewableenergyadoption,consideringtherolesofjobcreation,technologicalinnovation,anddeforestationmitigation?"
AnswerwithvanillaGraphRAG:["Renewableenergyadoptionhassignificanteconomicandenvironmentalimplications,includingjobcreation,technologicalinnovation,anddeforestationmitigation.EconomicImplications:JobCreation:Renewableenergyadoptioncancreatenewjobopportunitiesinindustriessuchassolarpanelinstallation,windturbinemaintenance,andbiofuelsproduction.AccordingtotheInternationalRenewableEnergyAgency(IRENA),therenewableenergysectorcouldsupportupto46millionjobsgloballyby2050,upfrom11millionin2020.TechnologicalInnovation:Thetransitiontorenewableenergysourcesrequiressignificanttechnologicaladvancements,leadingtoinnovationsinfieldssuchasenergystorage,gridmanagement,andsmartgrids.Thiscandriveeconomicgrowthbycreatingnewindustriesandopportunitiesforinvestment.InfrastructureInvestment:Thedeploymentofrenewableenergyinfrastructure,suchaswindfarmsandsolarparks,requiressignificantinvestmentininfrastructure.Thiscanleadtojobcreation,economicgrowth,andimprovedenergysecurity.EnvironmentalImplicationseforestationMitigation:Renewableenergyadoptioncanhelpmitigatedeforestationbyreducingthedemandforfossilfuels,whichareoftenlinkedtodeforestationindevelopingcountries.AccordingtotheWorldWildlifeFund,theuseofrenewableenergycanhelpreducedeforestationbyupto70CarbonEmissionsReduction:Thetransitiontorenewableenergysourcescansignificantlyreducecarbonemissions,whichcontributetoclimatechange.AccordingtotheIntergovernmentalPanelonClimateChange(IPCC),theuseofrenewableenergycanreducecarbonemissionsbyupto70%by2050.AirPollutionReduction:Renewableenergyadoptioncanalsoreduceairpollution,asrenewableenergysourcesemitsignificantlylessairpollutantsthanfossilfuels.PublicHealthImprovement:Thereductionofairpollutionandcarbonemissionsthroughrenewableenergyadoptioncanleadtoimprovedpublichealthoutcomes,asreducedairpollutioncanreducetheincidenceofrespiratorydiseasesandotherhealthproblems.EnergySecurity:Theadoptionofrenewableenergysourcescanimproveenergysecuritybyreducingdependenceonimportedfossilfuels,whichcanbesubjecttopricevolatilityandgeopoliticalrisks.Inconclusion,renewableenergyadoptionhassignificanteconomicandenvironmentalimplications,includingjobcreation,technologicalinnovation,deforestationmitigation,carbonemissionsreduction,airpollutionreduction,publichealthimprovement,andenergysecurity.Thetransitiontorenewableenergysourcesisessentialtomitigatetheimpactsofclimatechangeandensureasustainablefutureforgenerationstocome."]
AnswerwithGNN:["Renewableenergyadoptionhassignificanteconomicandenvironmentalimplications,includingjobcreation,technologicalinnovation,anddeforestationmitigation.Herearesomeofthekeyimplications:\n\nEconomicImplications:\n\n1.JobCreation:Therenewableenergysectoriscreatingnewjobopportunitiesinareassuchassolarpanelinstallation,windturbinemaintenance,andenergyefficiencyconsulting.AccordingtotheInternationalRenewableEnergyAgency(IRENA),therenewableenergysectorcouldsupportupto46millionjobsgloballyby2050,upfrom11millionin2019.\n2.InvestmentOpportunities:Investinginrenewableenergycanprovideattractivefinancialreturns,aswellascontributetosustainableeconomicgrowth.AccordingtoBloombergNewEnergyFinance,theglobalinvestmentinrenewableenergyreached$2.6trillionin2020,upfrom$1.1trillionin2010.\n3.EconomicEfficiency:Renewableenergycanreduceenergycostsforconsumersandbusinesses,asthefuelcostsforrenewableenergysourcesaregenerallylowerthanforfossilfuels.AccordingtotheNationalRenewableEnergyLaboratory(NREL),thecostofwindenergyhasdecreasedby69%since2009,makingitmorecompetitivewithfossilfuels.\n\nEnvironmentalImplications:\n\n1.ReducedGreenhouseGasEmissions:Renewableenergysourcessuchassolar,wind,andhydroelectricpowerproducelittletonogreenhousegasemissions,whichcontributetoclimatechange.AccordingtotheIntergovernmentalPanelonClimateChange(IPCC),renewableenergycanhelpreducegreenhousegasemissionsbyupto70%by2050,comparedto2010levels.\n2.DeforestationMitigation:Renewableenergycanhelpmitigatedeforestationbyreducingthedemandforlandforenergyproduction.AccordingtotheWorldWildlifeFund(WWF),theuseofrenewableenergycanhelpreducetheglobaldemandforlandbyupto30%,whichcanhelpprotectforestsandbiodiversity.\n3.ResourceConservation:Renewableenergycanhelpconservenon-renewableresources,suchasfossilfuels,byreducingthedemandfortheseresources.AccordingtotheU.S.DepartmentofEnergy,theuseofrenewableenergycanhelpreducetheUnitedStates'dependenceonfossilfuelsbyupto50%by2050.\n\nTechnologicalInnovation:\n\n1.IncreasedEfficiency:Thedevelopmentofnewtechnologies,suchasadvancedsolarpanelsandwindturbines,hasincreasedtheefficiencyofrenewableenergyproduction.AccordingtotheNREL,theefficiencyofsolarpanelshasincreasedby40%since2000,whiletheefficiencyofwindturbineshasincreasedby10%since2000.\n2.Decentralization:Thedevelopmentofdecentralizedrenewableenergysystems,suchasrooftopsolarandcommunitywind,canincreasetheresilienceofenergysystemsandreducetheriskofpoweroutages.AccordingtotheInternationalEnergyAgency(IEA),decentralizedrenewableenergysystemscanprovideupto20%ofglobalelectricitydemandby2050.\n3.EnergyStorage:Thedevelopmentofenergystoragetechnologies,suchasbatteriesandhydrogenfuelcells,canimprovethereliabilityofrenewableenergysystemsandreducetheneedforfossilfuels.AccordingtotheIEA,energystoragecanprovideupto10%ofglobalelectricitydemandby2050.\n\nInconclusion,theadoptionofrenewableenergyhassignificanteconomicandenvironmentalimplications,includingjobcreation,technologicalinnovation,anddeforestationmitigation.Astheworldtransitionstoalow-carboneconomy,theadoptionofrenewableenergysourceswillplayacriticalroleinreducinggreenhousegasemissions,conservingnon-renewableresources,andpromotingsustainableeconomicgrowth."]
AnswerwithoutGNN:["Renewableenergyadoptionhassignificanteconomicandenvironmentalimplications,bothpositiveandnegative,acrossvarioussectors.Herearesomekeyconsiderations:\n\n1.Jobcreation:\n\t*Renewableenergycancreatenewjobopportunitiesinindustriessuchassolarpanelinstallation,windturbinemaintenance,andenergyefficiencyconsulting.AccordingtotheInternationalRenewableEnergyAgency(IRENA),therenewableenergysectorcouldsupportupto46millionjobsgloballyby2050,comparedto11millionjobsinthefossilfuelsector.\n\t*However,thetransitiontorenewableenergymayalsoleadtojoblossesintraditionalfossilfuelindustries,suchascoalminingandoildrilling.\n2.Technologicalinnovation:\n\t*Thedevelopmentanddeploymentofrenewableenergytechnologies,suchasadvancedsolarpanelsandwindturbines,candriveinnovationandeconomicgrowth.\n\t*Investinginrenewableenergycanalsofosterresearchanddevelopmentinothersectors,suchasenergystorageandgridmanagement.\n3.Deforestationmitigation:\n\t*Renewableenergycanhelpreducedeforestationbyreducingthedemandforfossilfuels,whichareoftenlinkedtodeforestationintropicalregions.\n\t*Large-scalerenewableenergyprojects,suchashydroelectricdamsandbioenergyplantations,canalsocontributetodeforestation,soit'sessentialtoensurethattheseprojectsaredesignedandimplementedsustainably.\n4.Environmentalimpact:\n\t*Renewableenergysources,suchassolarandwind,haveasignificantlylowerenvironmentalimpactthanfossilfuels,astheyproducelittletonogreenhousegasemissionsduringoperation.\n\t*However,theproductionofrenewableenergytechnologies,suchassolarpanelsandwindturbines,canhaveenvironmentalimpacts,suchaslandusechangesandmaterialdepletion.\n5.Economicbenefits:\n\t*Renewableenergycanprovideeconomicbenefitsbyreducingenergycosts,improvingenergysecurity,andcreatingnewjobopportunities.\n\t*AccordingtotheNationalRenewableEnergyLaboratory(NREL),thecostofwindandsolarenergyhasdecreasedsignificantlyoverthepastdecade,makingthemmorecompetitivewithfossilfuels.\n6.Economicchallenges:\n\t*Thetransitiontorenewableenergycanbecostly,particularlyfordevelopingcountriesorregionswithlimitedfinancialresources.\n\t*Theintermittencyofrenewableenergysources,suchassolarandwind,cancreatechallengesforgridmanagementandenergystorage,whichcanbeexpensivetoaddress.\n7.Policyandregulation:\n\t*Effectivepolicyandregulationcanhelpdrivethetransitiontorenewableenergybyprovidingincentivesforinvestment,promotingresearchanddevelopment,andensuringalevelplayingfieldforrenewableenergytechnologies.\n\t*Governmentscanalsoplayaroleinaddressingtheeconomicchallengesassociatedwithrenewableenergyadoptionbyprovidingfinancialsupport,suchastaxcreditsorfeed-intariffs,toencourageinvestment.\n8.Gridintegration:\n\t*Theintegrationofrenewableenergysourcesintothegridcanbechallenging,particularlywhenthesesourcesareintermittent.\n\t*Advancedgridmanagementsystemsandenergystoragetechnologiescanhelpaddressthesechallenges,buttheirdeploymentcanbeexpensive.\n9.Energystorage:\n\t*Energystoragetechnologies,suchasbatteriesandhydrogenfuelcells,canhelpaddresstheintermittencyofrenewableenergysourcesbystoringexcessenergyforusewhenit'sneeded.\n\t*Thecostofenergystoragetechnologiesisexpectedtodecreaseovertime,makingthemmorecompetitivewithtraditionalenergystoragesolutions.\n10.Internationalcooperation:\n\t*Renewableenergyadoptioncanhavesignificantimplicationsforinternationaltradeandcooperation,particularlyinthecontextofglobalclimatechange.\n\t*Collaborationbetweencountriescanhelpshareknowledge,technologies,andbestpractices,acceleratingthetransitiontorenewableenergy.\n\nInconclusion,theeconomicandenvironmentalimplicationsofrenewableenergyadoptionarecomplexandmultifaceted.Whiletherearesignificantchallengestobeaddressed,thetransitiontorenewableenergycanprovidenumerousbenefits,includingjobcreation,technologicalinnovation,anddeforestationmitigation.Effectivepolicyandregulation,alongwithinternationalcooperation,canhelpdrivethistransitionandensureasustainableenergyfuture."]

|Metric|GraphRAGGlobalQuery|G-retwithGNN|G-retwoGNN|
|-------------------|-----------------------------------------------------------------|
|Groundedness|7|9|8|
|Correctness|8|9|8|
|Coherence|7|9|8|
|Novelty|6|9|8|
|Relevance|7|9|8|
|OverallScore.|7|9|8|

Example3:
Query:["Howdoesinfrastructureinvestmentinrenewableenergycorrelatewithdisasterriskmanagement,waterscarcitysolutions,andpublichealthimprovements?"
AnswerwithvanillaGraphRAG:["Answer:Investmentinrenewableenergycanpositivelycorrelatewithdisasterriskmanagement,waterscarcitysolutions,andpublichealthimprovementsinseveralways:1.ReducedCarbonEmissions:Transitioningtorenewableenergysourcesreducestheamountofcarbondioxideemittedintotheatmosphere,whichisamajorcontributortoclimatechange.Climatechangeexacerbatesthefrequencyandseverityofnaturaldisasters,suchashurricanes,wildfires,andfloods.Byreducingcarbonemissions,investmentinrenewableenergycanhelpmitigatetheimpactofdisasters2.ImprovedEnergyEfficiency:Renewableenergysourcesaregenerallymoreenergy-efficientthanfossilfuels.Byinvestinginrenewableenergy,energyconsumptioncanbereduced,leadingtocostsavingsandareductioninenergywaste.Thiscanalsocontributetodisasterriskmanagementbyreducingthestrainonenergyinfrastructureduringnaturaldisasters.3.EnhancedResilience:Investinginrenewableenergycanenhancetheresilienceofenergysystems,makingthembetterequippedtowithstandtheimpactsofnaturaldisasters.Forexample,solarandwindpowercancontinuetooperateevenduringpoweroutagescausedbystormsorfloods.4.WaterConservation:Renewableenergysourcesoftenrequirelesswaterthantraditionalfossilfuel-basedpowerplants.Thiscanhelpconservewaterresources,whichareessentialforaddressingwaterscarcityissues.5.JobCreation:Thetransitiontorenewableenergycancreatenewjobopportunitiesinindustriessuchassolarpanelinstallation,windturbinemaintenance,andenergyefficiencyconsulting.Thiscanhelpreduceunemploymentandimproveeconomicgrowth.6.PublicHealthImprovements:Investinginrenewableenergycanleadtoimprovedairquality,asrenewableenergysourcesproducelittletonoairpollution.Thiscanimprovepublichealthoutcomesbyreducingthenumberofrespiratoryillnessesandotherhealthproblemsassociatedwithairpollution.7.ReducedWaterPollution:Renewableenergysourcescanhelpreducewaterpollutionbyminimizingtheamountofwastewatergeneratedduringenergyproduction.8.ClimateChangeMitigation:Investinginrenewableenergycanhelpmitigatetheimpactsofclimatechangebyreducinggreenhousegasemissions.Thiscanhelpreducetheriskofclimate-relateddisasters,suchassealevelrise,droughts,andheatwaves.9.InternationalCooperation:Investinginrenewableenergycanpromoteinternationalcooperationonclimatechangeissues.Thiscanhelpfacilitatethesharingofknowledge,technologies,andbestpracticesforaddressingclimatechangeanditsassociatedimpacts.10.EconomicGrowth:Investinginrenewableenergycanpromoteeconomicgrowthbycreatingnewjobopportunitiesandattractinginvestmentintherenewableenergysector.Thiscanhelpstimulatelocaleconomiesandpromotesustainabledevelopment.Inconclusion,investinginrenewableenergycanpositivelycorrelatewithdisasterriskmanagement,waterscarcitysolutions,andpublichealthimprovementsbyreducingcarbonemissions,improvingenergyefficiency,enhancingresilience,conservingwaterresources,creatingjobopportunities,improvingairquality,reducingwaterpollution,promotinginternationalcooperation,andstimulatingeconomicgrowth."]
AnswerwithGNN:["Renewableenergyadoptionhasseveraleconomicandenvironmentalimplications,includingjobcreation,technologicalinnovation,anddeforestationmitigation.Herearesomeofthekeyimplications:\n\nEconomicImplications:\n\n1.JobCreation:Renewableenergyadoptioncancreatenewjobopportunitiesinindustriessuchassolarpanelinstallation,windturbinemaintenance,andbiofuelproduction.AccordingtotheInternationalRenewableEnergyAgency(IRENA),therenewableenergysectorcouldsupportupto46millionjobsgloballyby2050.\n2.InvestmentOpportunities:Investinginrenewableenergycancreatenewinvestmentopportunities,particularlyinemergingmarkets.Asrenewableenergytechnologiesbecomemorecost-competitivewithfossilfuels,thereispotentialforincreasedinvestmentinrenewableenergyinfrastructure.\n3.EconomicGrowth:Renewableenergycancontributetoeconomicgrowthbyreducingthedependenceonfossilfuelsandpromotingenergysecurity.AccordingtotheUnitedNations,renewableenergycangenerateupto11%ofglobalGDPby2050.\n\nEnvironmentalImplications:\n\n1.ReducedGreenhouseGasEmissions:Renewableenergysourcessuchassolarandwindpowerproducelittletonogreenhousegasemissionsduringoperation,whichcanhelpmitigateclimatechange.AccordingtotheIntergovernmentalPanelonClimateChange(IPCC),renewableenergycanreducegreenhousegasemissionsbyupto70%by2050.\n2.DeforestationMitigation:Theuseofrenewableenergycanhelpmitigatedeforestationbyreducingthedemandforfossilfuels,whichareoftenlinkedtodeforestationpractices.AccordingtotheWorldWildlifeFund,theadoptionofrenewableenergycanhelpreducedeforestationbyupto50%by2050.\n3.ResourceConservation:Renewableenergysourcescanhelpconservenaturalresourcessuchaswaterandland,astheyrequirelesslandandwaterthantraditionalenergysources.\n\nTechnologicalInnovation:\n\n1.ResearchandDevelopment:Thetransitiontorenewableenergyrequiressignificantresearchanddevelopmenttoimprovetheefficiencyandaffordabilityofrenewableenergytechnologies.Thiscanleadtonewtechnologicalinnovationsandadvancementsinfieldssuchasenergystorage,gridmanagement,andmaterialsscience.\n2.IncreasedEfficiency:Asrenewableenergytechnologiesbecomemorewidespread,theirefficiencyislikelytoincrease,leadingtogreaterenergyoutputfromthesameamountofresources.\n3.DecentralizationofEnergyProduction:Renewableenergycandecentralizeenergyproduction,allowingforgreatercontroloverenergyproductionanddistribution,andreducingtheneedforcentralizedpowerplants.\n\nInconclusion,theadoptionofrenewableenergycanhavesignificanteconomicandenvironmentalimplications,includingjobcreation,technologicalinnovation,anddeforestationmitigation.Astheworldtransitionstoamorerenewableenergy-basedeconomy,itisimportanttoconsidertheseimplicationsandworktowardsasustainableandequitablefuture.']
AnswerwithoutGNN:["Investinginrenewableenergyinfrastructurecanhaveapositivecorrelationwithdisasterriskmanagement,waterscarcitysolutions,andpublichealthimprovementsinseveralways:\n\n1.Reducedrelianceonfossilfuels:Renewableenergysources,suchassolarandwindpower,canreducetherelianceonfossilfuels,whichareamajorcontributortogreenhousegasemissions.Thiscanhelpmitigatetheimpactofnaturaldisasters,suchashurricanesandwildfires,whicharebecomingmorefrequentandsevereduetoclimatechange.\n2.Resilienceandadaptation:Renewableenergyinfrastructurecanprovideamoreresilientandadaptableenergysystem,particularlyinareaspronetonaturaldisasters.Forexample,solarpanelsandwindturbinescancontinuetooperateduringpoweroutagescausedbyhurricanesorwildfires,providingcriticalenergytoemergencyrespondersandaffectedcommunities.\n3.Waterconservation:Renewableenergycanalsocontributetowaterconservationbyreducingtheamountofwaterusedintraditionalfossilfuel-basedpowergeneration.Forexample,solarpanelsandwindturbinesrequireverylittlewaterforoperation,comparedtotraditionalpowerplantsthatusesignificantamountsofwaterforcooling.\n4.Improvedairquality:Renewableenergycanalsoimproveairqualitybyreducingtheamountofairpollutantsemittedbyfossilfuel-basedpowergeneration.Thiscanhavepositiveimpactsonpublichealth,particularlyinareaswithhighlevelsofairpollution.\n5.Jobcreationandeconomicgrowth:Investinginrenewableenergyinfrastructurecancreatejobsandstimulateeconomicgrowth,particularlyinruralareaswherewindandsolarfarmsareoftenlocated.Thiscanhelptoreducepovertyandimproveeconomicopportunitiesindisaster-proneareas.\n6.Disasterriskreduction:Renewableenergyinfrastructurecanalsohelptoreducetheriskofdisastersbyprovidingreliableandsustainableenergysources.Forexample,renewableenergycanhelptopowercriticalinfrastructure,suchashospitalsandemergencyshelters,duringnaturaldisasters.\n7.Climatechangemitigation:Investinginrenewableenergycanhelptomitigatetheimpactsofclimatechangebyreducingtheamountofgreenhousegasemissionsfromfossilfuel-basedpowergeneration.Thiscanhelptoreducetheriskofmorefrequentandseverenaturaldisasters,suchashurricanes,floods,andwildfires.\n8.Waterscarcitysolutions:Renewableenergycanalsohelptoaddresswaterscarcityissuesbyreducingtheamountofwaterusedintraditionalpowergeneration.Forexample,solarpanelsandwindturbinescanprovideenergyforwatertreatmentanddesalinationplants,helpingtomakewatermoreaccessibleandsustainableinareaswithlimitedwaterresources.\n9.Publichealthimprovements:Investinginrenewableenergycanalsoimprovepublichealthbyreducingtheamountofairpollutantsemittedbyfossilfuel-basedpowergeneration.Thiscanhavepositiveimpactsonrespiratoryhealth,cardiovascularhealth,andoverallwell-being.\n10.Energyindependence:Renewableenergycanalsoprovideenergyindependencebyreducingrelianceonimportedfossilfuels.Thiscanhelptoimproveenergysecurityandreducetheriskofdisruptionstoenergysupplyduringnaturaldisasters.\n\nInsummary,investinginrenewableenergyinfrastructurecanhavenumerousbenefitsfordisasterriskmanagement,waterscarcitysolutions,andpublichealthimprovements.Byreducinggreenhousegasemissions,improvingairquality,andprovidingreliableandsustainableenergysources,renewableenergycanhelptomitigatetheimpactsofnaturaldisastersandimprovetheresilienceofcommunities.']

|Metric|GraphRAGGlobalQuery|G-retwithGNN|G-retwoGNN|
|-------------------|-----------------------------------------------------------------|
|Groundedness|7|9|8|
|Correctness|8|9|8|
|Coherence|7|9|8|
|Novelty|6|9|8|
|Relevance|7|9|8|
|OverallScore.|7|9.|8|

评估显示,带有 GAT 的 G-Retriever 在扎实性、逻辑正确性、一致性、新颖性和相关性方面始终优于基线 GraphRAG 和不带 GAT 的检索方法。基线 GraphRAG 通常提供一般性洞见,但缺乏深度和具体性,导致得分较低。不带 GAT 的检索方法提供了一种平衡的方法,但在提供综合数据和创新视角方面不及 GAT 增强的答案。GAT 的响应因其丰富的引用、细致入微的解释以及经济和环境因素的无缝整合而脱颖而出,是样本集中最可靠和最具洞察力的。

结论

GNN 是图的理想编码器。图注意显著增强了基线 GraphRAG 的能力,尤其是在需要深度图遍历和细致推理的复杂因果查询中,通过以极低的计算成本提炼图中编码的信息。G-Retriever 在需要精确、结构化知识的领域(如科学、法律、医疗)中表现突出,并且在需要多跳推理和上下文权重的复杂查询中表现卓越。

附录

#MicrosoftGraphRAGwithGlobalSearchasdescribedinhttps://microsoft.github.io/graphrag/examples_notebooks/global_search/

#Copyright(c)2024MicrosoftCorporation.
#LicensedundertheMITLicense.

importos
importtorch
importpandasaspd
importnetworkxasnx
fromsentence_transformersimportSentenceTransformer
fromtransformersimportAutoTokenizer,AutoModelForCausalLM
fromnetworkx.algorithms.communityimportasyn_lpa_communities

#Devicesetup
device=torch.device("cuda"iftorch.cuda.is_available()else"cpu")

#HuggingFaceLLMwrapper
classLLM:
def__init__(self,model_name,num_params):
self.model_name=model_name
self.num_params=num_params
self.tokenizer=AutoTokenizer.from_pretrained(model_name)
self.model=AutoModelForCausalLM.from_pretrained(model_name).to(device)

defgenerate(self,prompt,max_length=512,temperature=0.7):
inputs=self.tokenizer(prompt,return_tensors="pt",truncation=True).to(device)
outputs=self.model.generate(
inputs.input_ids,
max_length=max_length,
temperature=temperature,
pad_token_id=self.tokenizer.eos_token_id,
)
returnself.tokenizer.decode(outputs[0],skip_special_tokens=True)

#InitializetheLLM
llm=LLM(model_name="meta-llama/Llama-2-7b-chat-hf",num_params=7)

#Embeddingmodel
embedding_model=SentenceTransformer('all-MPNet-base-v2')

#Expandednodedescriptions
node_descriptions=[
"ClimateChangePoliciesoliciesaimedatreducinggreenhousegasemissionsandmitigatingclimatechangeeffects.",
"RenewableEnergyAdoption:Transitiontorenewableenergysourceslikesolar,wind,andhydroelectricpower.",
"FossilFuelConsumption:Useoffossilfuelssuchascoal,oil,andnaturalgasforenergy.",
"ElectricVehicles:Vehiclespoweredbyelectricmotorsusingenergystoredinrechargeablebatteries.",
"CarbonEmissions:Releaseofcarbondioxideintotheatmospherefromvarioussources.",
"AirPollution:Contaminationofairbyharmfulsubstancesincludinggasesandparticulates.",
"PublicHealth:Healthoutcomesofpopulationsinfluencedbyenvironmentalandsocialfactors.",
"EconomicGrowth:Increaseinthemarketvalueofgoodsandservicesproducedbyaneconomyovertime.",
"JobCreation:Generationofnewemploymentopportunitiesinaneconomy.",
"EnergyEfficiency:Usinglessenergytoperformthesametask,reducingenergywaste.",
"EnergyPolicy:Governmentpolicyregardingtheproduction,distribution,andconsumptionofenergy.",
"InternationalAgreements:Agreementsbetweennationstocooperateonissueslikeclimatechange.",
"TechnologicalInnovationevelopmentofnewtechnologiesorimprovementstoexistingones.",
"InfrastructureInvestment:Allocationoffundstobuildorimprovephysicalinfrastructure.",
"SustainableAgriculture:Farmingpracticesthatmeetcurrentfoodneedswithoutcompromisingfuturegenerations.",
"Deforestation:Removalofaforestorstandoftreesfromlandwhichisthenconvertedtonon-forestuse.",
"BiodiversityLosseclineinthenumberandvarietyofspeciesinagivenarea.",
"ClimateRefugeeseopleforcedtoleavetheirhomesduetotheimpactsofclimatechange.",
"WaterScarcityackofsufficientavailablewaterresourcestomeetthedemandswithinaregion.",
"DisasterRiskManagement:Strategiestoreducethedamagecausedbynaturaldisasters.",
]

#Generatenodeembeddings
node_features=torch.tensor(embedding_model.encode(node_descriptions),dtype=torch.float)

#Expandedrelationships(edge_index)
edge_index=torch.tensor([
#Sourcenodes(from)
[0,0,0,0,0,1,1,1,2,2,2,3,3,4,4,5,5,6,7,7,7,8,8,9,9,10,10,11,12,12,13,14,15,16,17,18],
#Targetnodes(to)
[1,3,9,11,19,2,8,13,4,5,6,2,12,5,6,6,7,7,8,13,14,7,10,2,10,0,6,0,1,3,2,15,16,17,6,6]
],dtype=torch.long)

#Buildagraphfromnodesandedges
defbuild_graph(node_descriptions,edge_index):
graph=nx.DiGraph()
#Addnodeswithdescriptions
fori,descriptioninenumerate(node_descriptions):
graph.add_node(i,description=description,embedding=node_features[i])

#Addedges
forsource,targetinzip(edge_index[0],edge_index[1]):
graph.add_edge(source.item(),target.item())

returngraph

graph=build_graph(node_descriptions,edge_index)

#CommunitydetectionusingLabelPropagation(Leidenproxy)
defdetect_communities(graph):
communities=list(asyn_lpa_communities(graph,weight="weight"))
returncommunities

detected_communities=detect_communities(graph)
print(f"Detected{len(detected_communities)}communities.")

#Preparecontextfromdetectedcommunities
defprepare_context(communities,graph):
context=[]
forcommunityincommunities:
community_text="\n".join([graph.nodes[node]["description"]fornodeincommunity])
context.append(community_text)
return"\n".join(context)

context=prepare_context(detected_communities,graph)

#ProcessquerywiththeLLM
defprocess_query_with_llm(query,context,llm):
prompt=f"Query:{query}\nContext:\n{context}\nAnswer:"
returnllm.generate(prompt,max_length=512)

#Examplequery
query="Whataretheeconomicandenvironmentalimpactsoftransitioningtorenewableenergy?"
response=process_query_with_llm(query,context,llm)

print("\nGeneratedResponse:")
print(response)
##G-Retriever-GraphRAGwithoutGAT
#UseseaDummyGNN
fromsentence_transformersimportSentenceTransformer
importtorch
fromtorch_geometric.nn.modelsimportGRetriever
fromtorch_geometric.nn.nlpimportLLM
fromtorch.nnimportModule
importwarnings

#Suppressspecificwarnings(e.g.,CPUusagewarnings)
warnings.filterwarnings("ignore",category=UserWarning,module="torch_geometric.nn.nlp.llm")

#DefineDummyGNNclasswithout_channels
classDummyGNN(Module):
def__init__(self,out_channels):
super().__init__()
self.out_channels=out_channels#Definetheexpectedattribute

defforward(self,x,edge_index=None,edge_attr=None):
returnx#Passthroughinputwithoutmodification

#InitializeSentenceTransformerforembeddings
model=SentenceTransformer('all-MPNet-base-v2')

#Expandednodedescriptionswithmorecomplexity
node_descriptions=[
"ClimateChangePoliciesoliciesaimedatreducinggreenhousegasemissionsandmitigatingclimatechangeeffects.",
"RenewableEnergyAdoption:Transitiontorenewableenergysourceslikesolar,wind,andhydroelectricpower.",
"FossilFuelConsumption:Useoffossilfuelssuchascoal,oil,andnaturalgasforenergy.",
"ElectricVehicles:Vehiclespoweredbyelectricmotorsusingenergystoredinrechargeablebatteries.",
"CarbonEmissions:Releaseofcarbondioxideintotheatmospherefromvarioussources.",
"AirPollution:Contaminationofairbyharmfulsubstancesincludinggasesandparticulates.",
"PublicHealth:Healthoutcomesofpopulationsinfluencedbyenvironmentalandsocialfactors.",
"EconomicGrowth:Increaseinthemarketvalueofgoodsandservicesproducedbyaneconomyovertime.",
"JobCreation:Generationofnewemploymentopportunitiesinaneconomy.",
"EnergyEfficiency:Usinglessenergytoperformthesametask,reducingenergywaste.",
"EnergyPolicy:Governmentpolicyregardingtheproduction,distribution,andconsumptionofenergy.",
"InternationalAgreements:Agreementsbetweennationstocooperateonissueslikeclimatechange.",
"TechnologicalInnovationevelopmentofnewtechnologiesorimprovementstoexistingones.",
"InfrastructureInvestment:Allocationoffundstobuildorimprovephysicalinfrastructure.",
"SustainableAgriculture:Farmingpracticesthatmeetcurrentfoodneedswithoutcompromisingfuturegenerations.",
"Deforestation:Removalofaforestorstandoftreesfromlandwhichisthenconvertedtonon-forestuse.",
"BiodiversityLosseclineinthenumberandvarietyofspeciesinagivenarea.",
"ClimateRefugeeseopleforcedtoleavetheirhomesduetotheimpactsofclimatechange.",
"WaterScarcityackofsufficientavailablewaterresourcestomeetthedemandswithinaregion.",
"DisasterRiskManagement:Strategiestoreducethedamagecausedbynaturaldisasters.",
#Addmorenodesifneededtoincreasecomplexity
]

#Generateembeddingsforthenodes
node_features=torch.tensor(model.encode(node_descriptions),dtype=torch.float)

#Expandedrelationships(edge_index)tocreateamorecomplexgraph
edge_index=torch.tensor([
#Sourcenodes(from)
[0,0,0,0,0,1,1,1,2,2,2,3,3,4,4,5,5,6,7,7,7,8,8,9,9,10,10,11,12,12,13,14,15,16,17,18],
#Targetnodes(to)
[1,3,9,11,19,2,8,13,4,5,6,2,12,5,6,6,7,7,8,13,14,7,10,2,10,0,6,0,1,3,2,15,16,17,6,6]
],dtype=torch.long)

#Batchvectorforasinglegraph
batch=torch.zeros(len(node_descriptions),dtype=torch.long)

#CheckifGPUisavailableandsetdeviceaccordingly
device=torch.device('cuda'iftorch.cuda.is_available()else'cpu')
print(f"Usingdevice:{device}")

#InitializeDummyGNNwithout_channelsmatchingtheembeddingsize
dummy_gnn=DummyGNN(out_channels=node_features.size(1)).to(device)

#InitializeLLM
llm=LLM(model_name='meta-llama/Llama-2-7b-chat-hf',num_params=7)

#MoveLLMtotheappropriatedevice
llm.to(device)

#InitializeG-RetrieverwithDummyGNN
g_retriever=GRetriever(llm=llm,gnn=dummy_gnn,mlp_out_channels=4096)

#Complexquerywithoutadditionalcontext
query=[
"Evaluatethepotentiallong-termeconomicandenvironmentalimpactsofshiftingfromfossilfuelconsumptiontorenewableenergyadoption,consideringfactorssuchasjobcreation,technologicalinnovation,energypolicy,andpublichealth.Additionally,discusshowthistransitionaffectsinternationalagreementsandclimatechangepolicies,andidentifypotentialchallengesininfrastructureinvestmentandsustainableagriculture."
]

#Movedatatothecorrectdevice
node_features=node_features.to(device)
edge_index=edge_index.to(device)
batch=batch.to(device)

#Performinferencewithoutadditionalcontext
withtorch.no_grad():
subgraph_context=g_retriever.inference(
question=query,
x=node_features,#Nodeembeddings
edge_index=edge_index,#Complexedgeconnections
batch=batch,
max_out_tokens=1024
)

#Printresults
print("Subgraphcontextretrieved(WithoutGNN):",subgraph_context)
#G-Retriever-GraphRAGwithGAT

fromsentence_transformersimportSentenceTransformer
importtorch
fromtorch_geometric.nnimportGAT
fromtorch_geometric.nn.modelsimportGRetriever
fromtorch_geometric.nn.nlpimportLLM
importwarnings

#Suppressspecificwarnings(e.g.,CPUusagewarnings)
warnings.filterwarnings("ignore",category=UserWarning,module="torch_geometric.nn.nlp.llm")

#InitializeSentenceTransformerforembeddings
model=SentenceTransformer('all-MPNet-base-v2')

#Expandednodedescriptionswithmorecomplexity
node_descriptions=[
"ClimateChangePoliciesoliciesaimedatreducinggreenhousegasemissionsandmitigatingclimatechangeeffects.",
"RenewableEnergyAdoption:Transitiontorenewableenergysourceslikesolar,wind,andhydroelectricpower.",
"FossilFuelConsumption:Useoffossilfuelssuchascoal,oil,andnaturalgasforenergy.",
"ElectricVehicles:Vehiclespoweredbyelectricmotorsusingenergystoredinrechargeablebatteries.",
"CarbonEmissions:Releaseofcarbondioxideintotheatmospherefromvarioussources.",
"AirPollution:Contaminationofairbyharmfulsubstancesincludinggasesandparticulates.",
"PublicHealth:Healthoutcomesofpopulationsinfluencedbyenvironmentalandsocialfactors.",
"EconomicGrowth:Increaseinthemarketvalueofgoodsandservicesproducedbyaneconomyovertime.",
"JobCreation:Generationofnewemploymentopportunitiesinaneconomy.",
"EnergyEfficiency:Usinglessenergytoperformthesametask,reducingenergywaste.",
"EnergyPolicy:Governmentpolicyregardingtheproduction,distribution,andconsumptionofenergy.",
"InternationalAgreements:Agreementsbetweennationstocooperateonissueslikeclimatechange.",
"TechnologicalInnovationevelopmentofnewtechnologiesorimprovementstoexistingones.",
"InfrastructureInvestment:Allocationoffundstobuildorimprovephysicalinfrastructure.",
"SustainableAgriculture:Farmingpracticesthatmeetcurrentfoodneedswithoutcompromisingfuturegenerations.",
"Deforestation:Removalofaforestorstandoftreesfromlandwhichisthenconvertedtonon-forestuse.",
"BiodiversityLosseclineinthenumberandvarietyofspeciesinagivenarea.",
"ClimateRefugeeseopleforcedtoleavetheirhomesduetotheimpactsofclimatechange.",
"WaterScarcityackofsufficientavailablewaterresourcestomeetthedemandswithinaregion.",
"DisasterRiskManagement:Strategiestoreducethedamagecausedbynaturaldisasters.",
#Addmorenodesifneededtoincreasecomplexity
]

#Generateembeddingsforthenodes
node_features=torch.tensor(model.encode(node_descriptions),dtype=torch.float)

#Expandedrelationships(edge_index)tocreateamorecomplexgraph
edge_index=torch.tensor([
#Sourcenodes(from)
[0,0,0,0,0,1,1,1,2,2,2,3,3,4,4,5,5,6,7,7,7,8,8,9,9,10,10,11,12,12,13,14,15,16,17,18],
#Targetnodes(to)
[1,3,9,11,19,2,8,13,4,5,6,2,12,5,6,6,7,7,8,13,14,7,10,2,10,0,6,0,1,3,2,15,16,17,6,6]
],dtype=torch.long)

#Batchvectorforasinglegraph
batch=torch.zeros(len(node_descriptions),dtype=torch.long)

#CheckifGPUisavailableandsetdeviceaccordingly
device=torch.device('cuda'iftorch.cuda.is_available()else'cpu')
print(f"Usingdevice:{device}")

#Movedatatothecorrectdevice
node_features=node_features.to(device)
edge_index=edge_index.to(device)
batch=batch.to(device)

#InitializeGNN
gnn=GAT(
in_channels=768,
hidden_channels=1024,
out_channels=768,
num_layers=4,
heads=8
).to(device)#**MoveGNNtodevice**

#InitializeLLM
llm=LLM(model_name='meta-llama/Llama-2-7b-chat-hf',num_params=7).to(device)#**MoveLLMtodevice**

#InitializeG-RetrieverwiththerealGNN
g_retriever=GRetriever(llm=llm,gnn=gnn,mlp_out_channels=4096)

#Complexquerywithoutadditionalcontext
query=[
"Evaluatethepotentiallong-termeconomicandenvironmentalimpactsofshiftingfromfossilfuelconsumptiontorenewableenergyadoption,consideringfactorssuchasjobcreation,technologicalinnovation,energypolicy,andpublichealth.Additionally,discusshowthistransitionaffectsinternationalagreementsandclimatechangepolicies,andidentifypotentialchallengesininfrastructureinvestmentandsustainableagriculture."
]

#Performinferencewithoutadditionalcontext
withtorch.no_grad():
subgraph_context=g_retriever.inference(
question=query,
x=node_features,#Nodeembeddings
edge_index=edge_index,#Complexedgeconnections
batch=batch,
max_out_tokens=1024
)

#Printresults
print("Subgraphcontextretrieved(WithGNN):",subgraph_context






欢迎光临 链载Ai (https://www.lianzai.com/) Powered by Discuz! X3.5