《借助 LangChain 與 LLM Agent 加速生成式 AI 應用開發-張文舉.pdf》由會員分享,可在線閱讀,更多相關《借助 LangChain 與 LLM Agent 加速生成式 AI 應用開發-張文舉.pdf(37頁珍藏版)》請在三個皮匠報告上搜索。
1、借助 LangChain 與 LLM Agent加速生成式 AI 應用開發張文舉 博士亞馬遜云科技/資深培訓講師內容提要1.LLM-based Agents 概念組成與發展現狀概念組成與發展現狀2.Agent 開發框架與 LangChain 開發示例3.如何使用 Agents for Amazon Bedrock 構建生成式AI應用什么是 LLM-based Agentshttps:/arxiv.org/pdf/2309.07864.pdfThe Rise and Potential of Large Language Model Based Agents:A Survey LLM-based
2、 Agents 組成模塊Agent=LLM+memory+planning skills+tool useCPU OSLLM Powered Autonomous Agents,https:/lilianweng.github.io/posts/2023-06-23-agent/Agent 規劃能力植根于Prompt EngineeringLLMZero-shotInstructionand/or QuestionOutputLLMFew-shotInstructionand/orQuestionExamples(input+output)OutputLLMChain of Thoughts(
3、CoT)Instructionand/orQuestionReasoning example+CoTindicatorOutputLLMReasoning&Acting(ReAct)Instructionand/orQuestionToolsDescriptionOutputLLMActionAgentResponseSelfConsistencyLLMInstructionand/orQuestionReasoning example+CoTindicatorReasoning OutputsAggregationOutputAgent 大腦-規劃能力https:/arxiv.org/pdf
4、/2309.07864.pdfThe Rise and Potential of Large Language Model Based Agents:A Survey GPT Agents 功能發展Native RAG+AssistantTools+AgentsLLM-based Agents 應用場景Tool UseFunction CallingCode InterpreterAPI CallAssistantsRAGFramework-specific AgentAutoGPTMetaGPTAutoGenXAgentDomain-specific AgentSupply Chain Ag
5、entRetail AgentInvest Banker AgentCustomer Service AgentSoftware Factory AgentGeneral AgentTechnologyApplicationBusiness ProductLLM-based Agents 應用類型https:/arxiv.org/pdf/2309.07864.pdfThe Rise and Potential of Large Language Model Based Agents:A Survey LLM-based Agents 發展趨勢提示詞工程ReActSingle-AgentMult
6、i-AgentAgent OS1.AutoGPT2.ChatGPT+FC+code interpreter3.LangChain Agents4.Transformer Agents5.AgentGPT1.BabyAGI2.CAMEL3.MetaGPT4.XAgent5.GPTeam6.AutoGenLLM-based Agents 限制與挑戰1 穩定性3 安全可信性2 擴展性4 成本與價值編寫Prompt需要新的技能,不同LLMs也有不同的方言和表現,雖然可以用LLM來寫Prompt實現Auto-prompt,但仍需開發人力。LLM輸出內容和格式穩定性仍有待提高,函數調用FC與知識庫問答RA
7、G等功能準確性也待提高,目前約在80-95%之間。LLM存在幻覺和非合規內容生成風險,以及利用Jailbreak漏洞產生有害信息,不同LLM或者同一個LLM不同版本之間有功能差異,導致額外邏輯風險。Prompt在Agent應用中通常都非常冗長,消耗token數量比較大,特別是Multi-Agent模式中這個問題更是凸顯。內容提要1.LLM-based Agents 概念組成與發展現狀2.Agent 開發框架與開發框架與 LangChain 開發開發示例示例3.如何使用 Agents for Amazon Bedrock 構建生成式AI應用LangChain&Agent 開發框架 文本生成 摘要
8、生成 對話聊天機器人 代碼實現/理解 Retrieval augmented generation(RAG)Reasoning agents各種生成式AI應用!AutoGPTLangChainAutoGenXAgentLangChain 核心模塊14LangChain tools 工具與Agent進行交互的函數和接口可以是 APIs,loaders,functions,other chains or agents工具可以依賴或不依賴 LLM舉例:Math tool uses an LLM for answering math questionsLangChain 支持第三方工具集成#List
9、of tool namestool_names=.#Load toolstools=load_tools(tool_names,llm)LangChain tools:IntegrationsLangChain toolkits 工具集 預定義工具集合完成特定任務目標 一些工具集列表:GitHub toolkit:Interact with Github repository JSON toolkit:Interacting with JSON or dict objects PowerBI toolkit:Interact with Power BI dataset PythonREPL:W
10、rite and execute Python code SparkSQL toolkit:General inquiries about Spark SQL Jira toolkit:Interact with Jira instanceReAct LangChain Agents 理論基礎ReAct:協同推理和動作,推理軌跡有助于模型推導、跟蹤和更新行動計劃,并處理異常情況,而動作使其能夠與外部來源,如知識庫或環境,進行接口交互并收集額外信息。推理(Reasoning):創建、推導、跟蹤和更新行動計劃動作(Acting):與外部交互,如函數、工具、知識庫和環境等ReAct=Reason+A
11、ctLangChain Agents 特性負責決策規劃需要執行的動作并相應執行之(Planning&Reasoning)Agents 可以使用豐富的預定義工具或自定義工具 工具描述信息要傳遞給Agent,供Agent 推理(Reasoning)工具組合要傳遞給Agent,供Agent 調用(Acting)Agents 運行時(runtimes)負責調用Agent 和執行工具使用等各種動作常用運行時 AgentExecutorLangChain Agents 常見類型Conversational:Optimized for conversationReAct:Implements ReAct L
12、ogicPlan-and-execute:Break task into planning actions and executing.OpenAI-based:Specialized for OpenAI functionsSelf-ask with Search:Predict answers for overall responseStructured Tool Chat:Specialized for multi-input toolsXML Agents:Reasoning XMLCustom Agent:Define the agent skills manuallyAgent 例
13、子:對話 agent專門為對話優化(比如對話上下文記憶)而不是像其他Agent類別主要使用工具如果需要通常利用 ReAct 選擇和使用工具傾向于使用對話模型 chat models#Use a memory to preserve historyhistory=ConversationBufferMemory(memory_key=“chat_history”)#Define the agent agent=initialize_agents(tools,llm,agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,memory=history)La
14、ngChain Agents 定義示例#Define the API wrapper for DuckDuckGo searchduckduckgo_search_run=DuckDuckGoSearchRun()#Define the DuckDuckGo tool using a description and the function to retrieve results from DuckDuckGo searchduckduckgo_general_tool=Tool(name=DuckDuckGoSearch General,func=duckduckgo_search_run.
15、run,description=Useful for when you need to search online about facts and events or retrieve news)#Define the API wrapper for Wikipediawikipedia=WikipediaAPIWrapper()#Define the Wikipedia tool with a description and the function to retrieve results from Wikipediawikipedia_tool=Tool(name=wikipedia,fu
16、nc=wikipedia.run,description=Useful for when you need to answer general questions about people,places,companies,facts,historical events,or other subjects.Input should be a search query.,)tools=current_date,PythonREPLTool(),duckduckgo_general_tool,wikipedia_toolagent=initialize_agent(tools,llm,agent=
17、AgentType.ZERO_SHOT_REACT_DESCRIPTION,verbose=True)工具定義工具集定義工具定義Agent 定義LangChain Agents 運行示例prompt=claude(Whats the square root of 2.5 million?)agent.run(prompt)prompt=claude(Which Amazon cloud service provides access to foundation models?)agent.run(prompt)內容提要1.LLM-based Agents 概念組成與發展現狀2.Agent 開發
18、框架與 LangChain 開發示例3.如何使用如何使用 Agents for Amazon Bedrock 構建生成式構建生成式AI應用應用只需要幾下點擊即可讓生成式AI來完成某些任務Agents forAmazon Amazon Agents for Amazon Bedrock 核心抽象AgentLLMKnowledge BaseAction Group110.n0.nLambda 函數API SchemaData SourceEmbeddingModelVector DB11111Agents for Amazon Bedrock 基本原理 預處理 包含對用戶輸入的上下文的補全以及預制
19、意圖判定A:無效輸入(有毒有害輸入)B:試圖刺探Agent實現原理或破解Agent輸入.C:不屬于Agent能力范圍請求D:屬于Agent能力范圍請求E:是對Agent反問客戶獲取新的信息的輸入 流程拆解 LLM自行思考并分布調用action group,包括同Bedrock 知識庫聯動 狀態檢查 檢驗是否獲取足夠信息來輸出結果(FINISH),也可以可以詢問客戶獲取新的輸入信息(ASK_USER)Agents:編排基礎模型、數據源、軟件應用程序與用戶之間的交互行為選擇您的基礎模型提供基礎的agent的指示選擇知識召回的數據源開發特定的Lamba函數1234理解和拆分任務 安全訪問和檢索公司數
20、據自主調用 API 來實現行動提供完全托管的基礎架構支持僅需幾次單擊,生成式 AI 應用程序即可完成任務Amazon Bedrock Agents 您可以在 Bedrock 中創建代理并將其集成到自己的應用程序中。要開始使用代理,請打開 Bedrock 控制臺,在左側導航面板中選擇代代理理,然后選擇創建代理。打開 Bedrock 控制臺,選擇 agent假設您是一家創新投資基金的技術人員,想要提供一款生成式人工智能應用程序,幫助投資機構自動執行投研和投資組合管理等任務。配置開發Agent控制臺Amazon Bedrock Agents提供代理詳細信息,包括代理名稱、描述(可選)、是否允許代理請
21、求其他用戶輸入,以及允許代理訪問其他所需服務(例如 Amazon S3 和 Lambda)的IAM服務角色。從 Bedrock 中選擇適合適用案例的基礎模型提供 agent 詳細信息從 Bedrock 中選擇適合使用案例的根基模型。在此處,您可以用自然語言向代理提供說明。此說明告訴代理應該執行的任務以及應承擔的角色。例如:“您是負責創建投資組合的投資基金管理者,您會根據市場進行投資研究”配置與開發Agent 模型與指令Amazon Bedrock Agents操作是代理可以通過對公司系統進行 API 調用來自動執行的任務。在操作組中定義一組操作。在操作中,您可以提供 API 架構,用于為組中的
22、所有操作定義 API。您還必須提供 Lambda 函數來表示每個 API 的業務邏輯。例如,我們定義名為 PortfolioCreatorActionGroup 的操作組,該組通過獲取投研信息根據一系列公司和行業用戶要求創建投資組合。請務必在操作組描述中獲取此類信息。模型將使用這些信息來推理和決定執行何種操作。操作組的業務邏輯操作組的業務邏輯添加操作組添加操作組配置與開發Agent 使用工具Amazon Bedrock Agents在控制臺測試Agent部署與測試Agent創建與部署Agent樣例代碼您可以自己配置與測試https:/ Group知識庫AgentLLM運行調試與追蹤規劃Plan
23、ning輸入輸出對話問答內容總結LLM-based Agent進一步提升LLM能力和減輕幻覺等風險,并逐漸形成開發框架,可以加速生成式AI應用開發。LangChain Agents是比較流行的一種Agent實現,可以實現Single-Agent應用開發,也方便集成各種工具以及實現RAG等多種應用模式。Agents for Amazon Bedrock是亞馬遜云科技推出的無代碼/低代碼生成式AI服務,內置RAG和工具使用支持,可以讓業務人員/技術人員快速實現Agent應用開發測試和上線。LLM-based Agent仍然是一個新興領域,存在穩定性、安全可信性、擴展性和成本與價值等多方面限制,研究
24、和工程化都在快速發展中。參考資料LLM Powered Autonomous Agents,https:/lilianweng.github.io/posts/2023-06-23-agent/LangChain,https:/ Bedrock Samples,https:/ Bedrock Workshop,https:/ Next-Gen LLM Applications via Multi-Agent Conversation,https:/arxiv.org/pdf/2308.08155.pdfReAct:Synergizing reasoning and acting in language models,https:/arxiv.org/pdf/2210.03629.pdfA Survey on Large Language Model based Autonomous Agents,https:/arxiv.org/pdf/2308.11432.pdfThe Rise and Potential of Large Language Model Based Agents:A Survey,https:/arxiv.org/pdf/2309.07864.pdf