
如何快速實現REST API集成以優化業務流程
得益于llm模型的強大能力,將llm作為大腦,ai agent可以做到根據任務目標,進行思考,分解任務,然后調用相應工具完成任務。
一般ai agent的結構如下:
一個agent(智能體)主要包含以下3個部分:
?(1) Perception:感知,主要就是信息的輸入,比如文本,語言等信息。
?(2) Brain:這個是核心,基于llm,根據輸入信息,制定任務計劃等。
?(3) Action:執行,根據計劃執行對應的任務,比如調用第三方api,從工具集(tools)選中合適的tool執行任務。
agents支持多種形式,如下圖所示,single anget就是將anget作為一個ai助手,類似目前gpt的問答操作;agent-agent就是多個agent可以相互交互,比如chatdev,定義了多個角色,實現了一個虛擬的開發公司;human-agent就是加入人工提示或者反饋,根據人工信息,agent可以調整任務,實現更好的完成任務。
目前開源的ai agent有很多,如AutoGPT,BabyAGI,openagents等,本文以agentgpt為例,介紹一下其大概流程和結構。
agentgpt主要包含3個部分,其他如AutoGPT,BabyAGI等llm-based agent基本結構都是類似的。
?Reasoning and Planning:推理和計劃,如果定義一個目標,只是簡單的輸入給llm模型,只能得到一個粗略的答案;如果使用”prompt engineering”(提示工程),則可以將目標分解成多個更容易理解的步驟,并用思維鏈提示的方法進行反思。
?** Memory**:記憶,分為短期記憶和長期記憶。短期記憶是根據上下文學習,受限于llm的token長度;長期記憶是當執行一個復雜任務時,需要考慮到歷史任務的情況,當代理的任務長時間運行時,超過token長度時,就會會過期的記憶,agentgpt此處采用了向量數據庫存儲歷史信息的特征向量。?Tools:工具集,llm只支持文本的輸出,對于復雜任務,比如定個機票,llm就無法完成。解決方法就結合”prompt engineering”,定義一系列工具集,通過prompt和工具功能的描述,agent就可以根據任務調用相應的工具,比如定義一個“search”工具,功能描述是用“Google Search”的api搜索內容,描述中還可以可以包含api調用的輸入輸出以及內容格式等。
promts的重點就是設計合適的prompt模板,不同的llm對prompt的格式可能不同。
常用的方法如下:
?zero-shot方法(只提供prompt,不提供示例)
Prompt:
Classify the text into neutral, negative or positive.
Text: I think the vacation is okay.
Sentiment:
Output:
Neutral
?one-shot,two-shot,N-shot等方法,即提供1、2 或 N 個示例,以提高模型的準確性
?Chain-of-Thought (CoT) Prompting(思維鏈),通過在增加邏輯推理步驟,實現比較復雜的任務。
?ReAct技術(Reasoning + Action) ReAct是將推理和執行合并,可以動態推理和執行,通過不斷的think和action,獲取最終的答案。
?Plan and Solve(sp任務) 也是一種思維鏈方法,主要要求模型理解問題,提取變量和相關值,并逐步指定計劃。
具體prompt的技術可以參考以下文章,有很詳細的例子。
?Prompt Engineering Guidehttps://www.promptingguide.ai
?Learn Prompting: Your Guide to Communicating with AIhttps://learnprompting.org/docs/intro
也可以參考agentgpt代碼庫的prompts.py文件,里邊有一些prompt的模板。
# 分析任務的prompt
analyze_task_prompt = PromptTemplate(
template="""
High level objective: "{goal}"
Current task: "{task}"
Based on this information, use the best function to make progress or accomplish the task entirely.
Select the correct function by being smart and efficient. Ensure "reasoning" and only "reasoning" is in the
{language} language.
Note you MUST select a function.
""",
input_variables=["goal", "task", "language"],
)
agent執行多次時,會忘記之前的操作,agentgpt用Weaviate向量數據庫解決這個問題,向量數據庫能夠存儲任務執行的歷史記錄作為memory,agent就在任務循環中訪問memory的數據。
向量數據庫簡介如下圖:
關于memory,可以參考langchain種的memory機制:用戶輸入之后,再執行核心邏輯之前,先從memory中讀取之前的信息,作為past_messages,修改用戶輸入,然后再一起送入llm模型,得到當前任務的輸出,再得到最終任務的結果前,將輸入輸出先存入memory,在之后的任務中可以參考這部分任務的信息。
agentgpt中關于promt的思維鏈技術采用了langchain,目前基本這塊都使用langchain,具體langchain可以參考文章【Kevin:LangChain:一個讓你的LLM變得更強大的開源框架】,寫得很詳細,也可以看langchain官網文檔【Introduction | ? Langchain】。
https://zhuanlan.zhihu.com/p/636043995https://python.langchain.com/docs/get_started/introduction
目前agentGPT僅支持chatgpt模型,暫時不支持本地llm模型,不過可以參考代碼model_factory.py#L37和agent_service_provider.py#L18,修改此處,添加本地模型的調用接口。
agentGPT測試,以獲取paperwithcode網站最新目標檢測的sota算法為例。
輸入對應的任務“Get the latest sota models on the paper with code website about object detection on coco dataset”。
搜索網站->進入網站->網站內部搜索->過濾->排序
[1]AgentGPT原文blog: Autonomous AI in your browser
https://reworkd.ai/blog/Understanding-AgentGPT
[2]agentgpt demo網址
https://agentgpt.reworkd.ai/zh
[3]Plan-and-Solve-Prompting技術
https://github.com/AGI-Edgerunners/Plan-and-Solve-Prompting
[4]Prompt Engineering Guide文檔
https://www.promptingguide.ai/
[5]Learn Prompting: Your Guide to Communicating with AI
https://learnprompting.org/docs/intro
[6]kevin:LangChain:一個讓你的LLM變得更強大的開源框架
https://zhuanlan.zhihu.com/p/636043995
[7]Memory | ? Langchain
https://python.langchain.com/docs/modules/memory/
[8]開源ai-agents列表:awesome-ai-agents
https://github.com/e2b-dev/awesome-ai-agents
[9]葉賽文:ChatGPT api里的system,user,assistant有什么作用?如何使用?
https://zhuanlan.zhihu.com/p/638552350
[10]AutoGPT
https://github.com/Significant-Gravitas/AutoGPT
[11]BabyAGI
https://github.com/yoheinakajima/babyagi
[12]openagents
https://github.com/xlang-ai/openagents
[13]langchain中文教程
https://github.com/liaokongVFX/LangChain-Chinese-Getting-Started-Guide
[14]awesome-langchain
https://github.com/kyrolabs/awesome-langchain
文章轉自微信公眾號@智猩猩GenAI