作为一名对前沿技术充满热情的研究者,我深刻感受到检索增强生成(Retrieval-Augmented Generation, RAG)对大型语言模型(LLMs)的变革性影响。
RAG 是一种强大且极具潜力的工具,它通过将模型与矢量数据库集成,使LLMs能够实时获取和利用现实世界的数据,从而生成更明智、更精准的响应。这种能力为大型语言模型的实际应用打开了新大门,尤其是在需要实时学习和快速适应的场景中。
这一特性使 RAG 成为聊天机器人、虚拟助理等应用程序的首选解决方案,这些应用对实时准确且合乎逻辑的响应有着极高的需求。而 RAG 的高级形式——图检索增强生成(GraphRAG),更进一步扩展了其应用边界。GraphRAG 将基于图的知识检索与LLMs的强大生成能力相结合,进一步提升了自然语言处理的表现力。
与传统 RAG 方法主要依赖矢量相似性搜索不同,GraphRAG 从原始文本中构建结构化的知识图,精准捕捉实体、关系以及关键论断。这种结构化的知识表示赋予 LLMs 更强的能力,使其能够理解和综合复杂数据集中的信息及其内在关系,从而生成更准确、更贴合上下文的响应。
AutoGen 是微软推出的一款创新工具,旨在通过自动化和优化曾经复杂且需要大量手动操作的工作流程,简化基于多代理大型语言模型(LLMs)的复杂应用程序开发。可以将 AutoGen 想象为一个多功能平台,用户可以与多个 GPT 代理协作,而不仅仅是与单一模型交互。在 AutoGen 中,每个 GPT 都扮演着独特的“代理”角色,在综合运作中各司其职,共同实现更高效的任务完成。
当 GraphRAG 的检索能力与 AutoGen AI 代理的对话和任务导向功能相结合时,便可打造强大的 AI 助手。这些助手不仅能够处理复杂的查询,还能生成并执行代码、撰写多页科学报告,以及进行深入的数据分析,满足各类高难度需求。
值得一提的是,AutoGen 还支持集成本地 LLMs(例如来自 Ollama 或 LM Studio 的模型)。本地 LLMs 的应用为用户提供了经济高效且数据安全的选择。与在线 LLMs 相比,本地模型能够显著降低运行成本,同时消除潜在的隐私风险——敏感数据可以完全保留在组织内部,既保障了数据安全,也提升了运营效率。
通过 AutoGen 的灵活特性和 GraphRAG 的增强能力,用户能够轻松构建更智能、更可靠的 AI 解决方案,从而在多种场景中发挥巨大潜力。
本文将指导您使用 GraphRAG 检索系统构建一个多代理 AI 应用程序。该应用程序完全在您的本地计算机上运行,并且免费提供,确保经济高效和数据隐私。以下是构建该应用程序的关键组件:
GraphRAG 的知识搜索与 AutoGen 代理集成
GraphRAG 的知识搜索方法通过函数调用与 AutoGen 代理无缝集成,结合双方的优势实现高效检索与任务执行。
支持 Ollama 的本地模型
GraphRAG 配置了本地和全局搜索功能,支持 Ollama 提供的本地 LLMs,用于推理和嵌入生成。这种配置确保了对敏感数据的完全控制,同时降低了运行成本。
AutoGen 的扩展功能
AutoGen 通过 Lite-LLM 代理服务器实现了对 Ollama 提供的非 OpenAI LLMs 的函数调用支持。此扩展大幅提升了系统的灵活性,使用户可以充分利用本地模型的能力。
Chainlit 用户界面
应用程序配备了 Chainlit UI,用于处理连续对话、多线程操作以及用户输入设置,确保用户能够轻松管理复杂的任务流。
通过这些关键组件的协同工作,您可以构建一个功能强大的 AI 系统,适用于多种场景,从高效检索到复杂任务自动化,均可实现卓越性能。
#MistralforGraphRAGInferenceollamapullmistral#Nomic-Embed-TextforGraphRAGEmbeddingollamapullnomic-embed-text#LLama3forAutogenInferenceollamapullllama3#HostOllamaonalocalserver:http://localhost:11434ollamaserve
#Createandactivateacondaenvironmentcondacreate-nRAG_agentspython=3.12condaactivateRAG_agents#Lite-LLMproxyserverforOllamapipinstall'litellm[proxy]'#InstallOllamapipinstallollama#MicrosoftAutoGenpipinstallpyautogen"pyautogen[retrievechat]"#MicrosoftGraphRAGpipinstallgraphrag#Text-TokenEncoder-Decoderpipinstalltiktoken#ChainlitPythonapplicationpipinstallchainlit#ClonemyGit-hubrepositorygitclonehttps://github.com/karthik-codex/autogen_graphRAG.git#(BONUS)ToConvertPDFfilestoMarkdownforGraphRAGpipinstallmarker-pdf#(BONUS)OnlyifyouinstalledMarker-pdfsinceitremovesGPUCUDAsupportbydefaultcondainstallpytorchtorchvisiontorchaudiopytorch-cuda=12.1-cpytorch-cnvidia
#makeanewfolder"input"toplaceyourinputfilesforGraphRAG(.txtor.md)mkdir-p./input#InitializeGraphRAGtocreatetherequiredfilesandfoldersintherootdirpython-mgraphrag.index--init--root.#Movethesettings.yamlfiletoreplacetheonecreatedbyGraphRAG--initmv./utils/settings.yaml./
下面是来自settings.yaml的片段,说明了用于创建索引和嵌入的LLMs的配置。GraphRAG需要32k上下文长度进行索引,因此选择Mistral模型。对于嵌入,选择Nomic-embed-text,尽管您可以尝试Ollama的其他嵌入。无需设置${GRAPHRAG_API_KEY},因为不需要访问这些本地模型的端点。
encoding_model: cl100k_baseskip_workflows: []llm:api_key{GRAPHRAG_API_KEY}
type: openai_chat# or azure_openai_chatmodel: mistralmodel_supports_json: trueapi_base: http://localhost:11434/v1embeddings:async_mode: threaded # or asynciollm:api_key: ${GRAPHRAG_API_KEY}type: openai_embedding # or azure_openai_embeddingmodel: nomic_embed_textapi_base: http://localhost:11434/apiinput: #Change input file pattern to.md, or .txttype: file # or blobfile_type: text # or csvbase_dir: "input"file_encoding: utf-8file_pattern: ".*\\.md$"
你可以在根目录的“input”文件夹中指定包含输入文件的文件夹。文本文件和Markdown文件都可以使用。您可以使用/utils/pdf_to_markdown.py将PDF转换为Markdown文件,然后将其放置在“input”文件夹中。处理多种文件格式尚未解决,但这是一个可以解决的问题。
在运行GraphRAG进行索引、创建嵌入和执行本地查询之前,您必须修改位于GraphRAG包中的Python文件openai_embeddings_llm.py和embedding.py。如果不进行此修改,GraphRAG将在创建嵌入时抛出错误,因为它不会将“nomic-embed-text”识别为Ollama的有效嵌入模型。
在我的设置中,这些文件位于/home/karthik/miniconda3/envs/RAG_agents/lib/python3.12/site-packages/graphrag/llm/openai/openai_embeddings_llm.py和/home/karthik/miniconda3/envs/RAG_agents/lib/python3.12/site-packages/graphrag/query/llm/oai/embedding.py
可以使用以下命令找到这些文件sudo find / -name openai_embeddings_llm.py。
最后,我们创建嵌入并使用全局或局部搜索方法测试知识图。完成嵌入过程后,您可以在GraphRAG工作目录的“output”文件夹(本实例中的根文件夹)中找到输出工件(.parquet文件)和报告(.json和.logs)。
#Createknowledgegraph-thistakessometimepython-mgraphrag.index--root.#TestGraphRAGpython-mgraphrag.query--root.--methodglobal"<insertyourquery>"
以下是在运行应用程序之前初始化服务器的命令。我选择Llama3:8b来测试这个应用程序。如果您的硬件允许,您可以使用更大的型号。有关Lite- LLM的更多信息可以在此链接中找到。现在,您已准备好从另一个终端运行该应用程序。确保您处于正确的conda环境中。
#startserverfromterminallitellm--modelollama_chat/llama3#runappfromanotherterminalchainlitrunappUI.py
Import python libraries 导入Python库
importautogenfromrichimportprintimportchainlitasclfromtyping_extensionsimportAnnotatedfromchainlit.input_widgetimport(Select,Slider,Switch)fromautogenimportAssistantAgent,UserProxyAgentfromutils.chainlit_agentsimportChainlitUserProxyAgent,ChainlitAssistantAgentfromgraphrag.query.cliimportrun_global_search,run_local_search
你会注意到从chainlit_agents导入了两个类。AutoGen代理的这些包装类使Chainlit能够跟踪它们的对话并处理终止或其他用户输入。您可以在此处阅读有关此内容的更多信息。
AutoGen代理通过Lite- LLM代理服务器利用Ollama的模型。这是必要的,因为AutoGen不支持通过非OpenAI推理模型调用函数。代理服务器允许使用Ollama模型进行函数调用和代码执行。
创建了三个Chainlit小部件(开关、选择和滑块)作为用户设置来选择GraphRAG搜索类型、社区级别和内容生成类型。当打开时,开关小部件使用GraphRAG本地搜索方法进行查询。
内容生成的选择选项包括“优先列表”、“单段落”、“多段落”和“多页报告”。滑块小部件使用选项0、1和2选择社区生成级别。您可以在此处阅读有关GraphRAG社区的更多信息。
@cl.on_chat_startasyncdefon_chat_start():try:settings=awaitcl.ChatSettings([Switch(id="Search_type",label="(GraphRAG)LocalSearch",initial=True),Select(id="Gen_type",label="(GraphRAG)ContentType",values=["prioritizedlist","singleparagraph","multipleparagraphs","multiple-pagereport"],initial_index=1,),Slider(id="Community",label="(GraphRAG)CommunityLevel",initial=0,min=0,max=2,step=1,),]).send()response_type=settings["Gen_type"]community=settings["Community"]local_search=settings["Search_type"]cl.user_session.set("Gen_type",response_type)cl.user_session.set("Community",community)cl.user_session.set("Search_type",local_search)retriever=AssistantAgent(name="Retriever",llm_config=llm_config_autogen,system_message="""Onlyexecutethefunctionquery_graphRAGtolookforcontext.Output'TERMINATE'whenananswerhasbeenprovided.""",max_consecutive_auto_reply=1,human_input_mode="NEVER",description="RetrieverAgent")user_proxy=ChainlitUserProxyAgent(name="User_Proxy",human_input_mode="ALWAYS",llm_config=llm_config_autogen,is_termination_msg=lambdax:x.get("content","").rstrip().endswith("TERMINATE"),code_execution_config=False,system_message='''Ahumanadmin.Interactwiththeretrievertoprovideanycontext''',description="UserProxyAgent")print("Setagents.")cl.user_session.set("QueryAgent",user_proxy)cl.user_session.set("Retriever",retriever)msg=cl.Message(content=f"""Hello!Whattaskwouldyouliketogetdonetoday?""",author="User_Proxy")awaitmsg.send()print("Messagesent.")exceptExceptionase:print("Error:",e)pass选择不将Chainlit包装器类用于检索器助理代理。这使我能够禁用对检索器输出的跟踪并直接捕获来自GraphRAG函数的响应。原因是,当响应通过检索器时,文本会丢失其格式,包括空格和段落缩进。
在生成带有主标题和副标题的多页报告时,这个问题尤其明显。我可以通过绕过Chainlit包装器并直接检索GraphRAG函数的输出来保留原始格式。您将在下面看到我是如何实现这一目标的。
此函数检测设置中对选择、开关和滑块小部件所做的任何更改,以便它可以在后续查询中反映这些更改。
@cl.on_settings_updateasyncdefsetup_agent(settings):response_type=settings["Gen_type"]community=settings["Community"]local_search=settings["Search_type"]cl.user_session.set("Gen_type",response_type)cl.user_session.set("Community",community)cl.user_session.set("Search_type",local_search)print("on_settings_update",settings)这是应用程序的核心部分,它创建了两个代理的群聊,定义了一个函数“state_transition”来管理对话顺序,并提供异步 RAG 查询功能。
你会注意到INPUT_DIR ,ROOT_DIR, RESPONSE_TYPE, COMMUNTIY参数基于 bool 参数LOCAL_SEARCH传递到本地和全局搜索 GraphRAG 查询函数中。 ROOT_DIR,设置为'.' — 如果您在不同的目录中初始化 GraphRAG,请注意这一点。
异步函数“query_graphRAG”调用GraphRAG全局或本地搜索方法。你会注意到这条线await cl.Message(content=result.response).send()在async def query_graphRAG函数中,该函数直接检索RAG查询的输出并保留检索内容的文本格式。
@cl.on_messageasyncdefrun_conversation(message:cl.Message):print("Runningconversation")CONTEXT=message.contentMAX_ITER=10INPUT_DIR=NoneROOT_DIR='.'RESPONSE_TYPE=cl.user_session.get("Gen_type")COMMUNITY=cl.user_session.get("Community")LOCAL_SEARCH=cl.user_session.get("Search_type")print("Settinggroupchat")retriever=cl.user_session.get("Retriever")user_proxy=cl.user_session.get("QueryAgent")defstate_transition(last_speaker,groupchat):messages=groupchat.messagesiflast_speakerisuser_proxy:returnretrieveriflast_speakerisretriever:ifmessages[-1]["content"].lower()notin['math_expert','physics_expert']:returnuser_proxyelse:ifmessages[-1]["content"].lower()=='math_expert':returnuser_proxyelse:returnuser_proxyelse:passreturnNoneasyncdefquery_graphRAG(question:Annotated[str,'QuerystringcontaininginformationthatyouwantfromRAGsearch'])->str:ifLOCAL_SEARCH:result=run_local_search(INPUT_DIR,ROOT_DIR,COMMUNITY,RESPONSE_TYPE,question)else:result=run_global_search(INPUT_DIR,ROOT_DIR,COMMUNITY,RESPONSE_TYPE,question)awaitcl.Message(content=result).send()returnresultforcallerin[retriever]:d_retrieve_content=caller.register_for_llm(description="retrievecontentforcodegenerationandquestionanswering.",api_style="function")(query_graphRAG)foragentsin[user_proxy,retriever]:agents.register_for_execution()(d_retrieve_content)groupchat=autogen.GroupChat(agents=[user_proxy,retriever],messages=[],max_round=MAX_ITER,speaker_selection_method=state_transition,allow_repeat_speaker=True,)manager=autogen.GroupChatManager(groupchat=groupchat,llm_config=llm_config_autogen,is_termination_msg=lambdax:x.get("content","")andx.get("content","").rstrip().endswith("TERMINATE"),code_execution_config=False,)#--------------------ConversationLogic.EdittochangeyourfirstmessagebasedontheTaskyouwanttogetdone.-----------------------------#iflen(groupchat.messages)==0:awaitcl.make_async(user_proxy.initiate_chat)(manager,message=CONTEXT,)eliflen(groupchat.messages)<MAX_ITER:awaitcl.make_async(user_proxy.send)(manager,message=CONTEXT,)eliflen(groupchat.messages)==MAX_ITER:awaitcl.make_async(user_proxy.send)(manager,message="exit",)对于此应用程序,我们只需要两个代理。您可以添加/修改代理并配置“state_transition”函数来协调对话中的发言人选择,以实现更复杂的工作流程。
尽管这一实现尚不完美,但它为开发更复杂的应用程序提供了一个绝佳的起点。通过整合多个功能模块和编码代理,它为构建复杂的工作流程奠定了坚实基础。
同时,这种设计使您能够灵活地自定义代理交互,根据实际需求不断增强功能,为未来的创新和扩展提供了无限可能。
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |