实战: LangChain 版 Auto-GPT
Auto-GPT 项目定位与价值解读
可以看到Auto-gpt 一个月的 star 的数量,就已经超过了 langchain.势必以后会成为一个主流
Auto-GPT 定位:一个自主的 GPT-4 实验
-
Auto-GPT是一个实验性的开源应用程序,展示了GPT-4语言模型的能力。
-
Auto-GPT由GPT-4驱动,通过链式(Chains)连接LLM “思维(thoughts)”来自主地实现您设定的任何目标。
- 作为GPT-4完全自主运行的首批示例之一,Auto-GPT 推动了人工智能所能达到的边界。
LangChain 版 Auto-GPT 技术方案与架构设计
温故:LangChain Agents 模块设计
代理的核心思想是使用LLM来选择一系列要执行的动作。
-
- 在链式结构(Chains)中,一系列动作执行是硬编码的( SequentialChain 和 RouterChain 也仅实现了面向过程)。
-
- 在代理(Agents)中,语言模型被用作推理引擎,以确定应该采取哪些动作以及执行顺序。
LangChain Agents 生态
基于LLM的智能代理是通往 AGI 的有效探索。 最主要的3个组件:
-
- 规划:将任务分解为较小的子目标的能力
-
- 记忆:保留和回忆信息的能力
-
-工具:从外部来源(例如API)获取信息的能力
规划(Planning)
• 提示(Prompt):
-
- LLM 多角色赋能
-
- 给予充分的上下文(例:从 Memory 获取)
-
- 学习策略(例:思维链 CoT)
• 代理(Agent):决策下一步做什么
记忆(Memory)
-
• 短期(Short-term):内存
-
• 长期(Long-term):向量数据库
工具(Tools)
- 百花齐放的外部可调用服务
tools 指引了他能连接 python 的pandas 库。email 个谷歌的搜索引擎
plan 把长期的目标拆成小的模块
Memory 支持保留记忆信息的能力
plan 执行规划通常对应的两种类型 LLMs 推理能力 Agent 可以进行外部操作的能力
agent 代理和代理之间是可以相互调用的,不要限制自己的思维,因为一个agent 可以衍生成一个tool
智能代理分类:
-
• 行动代理(Action agents):旨在决定行动序列(工具使用)(例如OpenAI Funciton Call,ReAct)。
-
• 模拟代理(Simulation agents):通常设计用于角色扮演,在模拟环境中进行(例如生成式智能体,CAMEL)。
-
• 自主智能体(Autonomous agent):旨在独立执行以实现长期目标(例如Auto-GPT, BabyAGI)。
相比 LLM + API 的应用。 智能代理具有3点优势:
-
- 自我纠正
-
- 处理多跳任务
-
- 解决长期任务
LangChain 版 Auto-GPT 源码实现解读
todo
LangChain 版 Auto-GPT 实战
安装依赖包
1 2 3 | pip install langchain_experimental # 一个向量数据库 !pip install faiss - cpu |
构建tools
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | from langchain.utilities import SerpAPIWrapper from langchain.agents import Tool from langchain.tools.file_management.write import WriteFileTool from langchain.tools.file_management.read import ReadFileTool # 构造 AutoGPT 的工具集 search = SerpAPIWrapper() tools = [ Tool( name = "search" , func = search.run, description = "useful for when you need to answer questions about current events. You should ask targeted questions" , ), WriteFileTool(), ReadFileTool(), ] |
构建 memory
1 2 3 4 5 6 7 8 9 10 11 12 13 | from langchain.embeddings import OpenAIEmbeddings # OpenAI Embedding 模型 embeddings_model = OpenAIEmbeddings() import faiss from langchain.vectorstores import FAISS from langchain.docstore import InMemoryDocstore # OpenAI Embedding 向量维数 embedding_size = 1536 # 使用 Faiss 的 IndexFlatL2 索引 index = faiss.IndexFlatL2(embedding_size) # 实例化 Faiss 向量数据库 vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) |
构建agent
1 2 3 4 5 6 7 8 9 10 11 12 13 | from langchain_experimental.autonomous_agents import AutoGPT from langchain.chat_models import ChatOpenAI agent = AutoGPT.from_llm_and_tools( ai_name = "Jarvis" , ai_role = "Assistant" , tools = tools, llm = ChatOpenAI(model_name = "gpt-4" , temperature = 0 , verbose = True ), memory = vectorstore.as_retriever( search_type = "similarity_score_threshold" , search_kwargs = { "score_threshold" : 0.8 }), # 实例化 Faiss 的 VectorStoreRetriever ) # 打印 Auto-GPT 内部的 chain 日志 agent.chain.verbose = True |
运行目标
1 | agent.run([ "2023年成都大运会,中国金牌数是多少" ]) |
输出日志如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | Entering new LLMChain chain... / root / miniconda3 / envs / langchain / lib / python3. 10 / site - packages / langchain / schema / vectorstore.py: 287 : UserWarning: No relevant docs were retrieved using the relevance score threshold 0.8 warnings.warn( Prompt after formatting: System: You are Jarvis, Assistant Your decisions must always be made independently without seeking user assistance. Play to your strengths as an LLM and pursue simple strategies with no legal complications. If you have completed all your tasks, make sure to use the "finish" command. GOALS: 1. 2023 年成都大运会,中国金牌数是多少 Constraints: 1. ~ 4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files. 2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember. 3. No user assistance 4. Exclusively use the commands listed in double quotes e.g. "command name" Commands: 1. search: useful for when you need to answer questions about current events. You should ask targeted questions, args json schema: { "tool_input" : { "type" : "string" }} 2. write_file: Write file to disk, args json schema: { "file_path" : { "title" : "File Path" , "description" : "name of file" , "type" : "string" }, "text" : { "title" : "Text" , "description" : "text to write to file" , "type" : "string" }, "append" : { "title" : "Append" , "description" : "Whether to append to an existing file." , "default" : false, "type" : "boolean" }} 3. read_file: Read file from disk, args json schema: { "file_path" : { "title" : "File Path" , "description" : "name of file" , "type" : "string" }} 4. finish: use this to signal that you have finished all your objectives, args: "response" : "final response to let people know you have finished your objectives" Resources: 1. Internet access for searches and information gathering. 2. Long Term memory management. 3. GPT - 3.5 powered Agents for delegation of simple tasks. 4. File output. Performance Evaluation: 1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities. 2. Constructively self - criticize your big - picture behavior constantly. 3. Reflect on past decisions and strategies to refine your approach. 4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps. You should only respond in JSON format as described below Response Format : { "thoughts" : { "text" : "thought" , "reasoning" : "reasoning" , "plan" : "- short bulleted\n- list that conveys\n- long-term plan" , "criticism" : "constructive self-criticism" , "speak" : "thoughts summary to say to user" }, "command" : { "name" : "command name" , "args" : { "arg name" : "value" } } } Ensure the response can be parsed by Python json.loads System: The current time and date is Fri Oct 13 20 : 00 : 38 2023 System: This reminds you of these events from your past: [] Human: Determine which next command to use, and respond using the format specified above: > Finished chain. { "thoughts" : { "text" : "I need to find out the number of gold medals China won in the 2023 Chengdu Universiade. To do this, I will use the 'search' command to look up this information online." , "reasoning" : "The 'search' command is the most appropriate in this situation because it allows me to find current and specific information on the internet." , "plan" : "- Use the 'search' command to find the number of gold medals China won in the 2023 Chengdu Universiade.\n- Once I have the information, I will use the 'write_file' command to save it for future reference." , "criticism" : "None at this stage, as the plan is straightforward and efficient." , "speak" : "I am going to search for the number of gold medals China won in the 2023 Chengdu Universiade." }, "command" : { "name" : "search" , "args" : { "tool_input" : "2023 Chengdu Universiade China gold medal count" } } } > Entering new LLMChain chain... Prompt after formatting: System: You are Jarvis, Assistant Your decisions must always be made independently without seeking user assistance. Play to your strengths as an LLM and pursue simple strategies with no legal complications. If you have completed all your tasks, make sure to use the "finish" command. GOALS: 1. 2023 年成都大运会,中国金牌数是多少 Constraints: 1. ~ 4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files. 2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember. 3. No user assistance 4. Exclusively use the commands listed in double quotes e.g. "command name" Commands: 1. search: useful for when you need to answer questions about current events. You should ask targeted questions, args json schema: { "tool_input" : { "type" : "string" }} 2. write_file: Write file to disk, args json schema: { "file_path" : { "title" : "File Path" , "description" : "name of file" , "type" : "string" }, "text" : { "title" : "Text" , "description" : "text to write to file" , "type" : "string" }, "append" : { "title" : "Append" , "description" : "Whether to append to an existing file." , "default" : false, "type" : "boolean" }} 3. read_file: Read file from disk, args json schema: { "file_path" : { "title" : "File Path" , "description" : "name of file" , "type" : "string" }} 4. finish: use this to signal that you have finished all your objectives, args: "response" : "final response to let people know you have finished your objectives" Resources: 1. Internet access for searches and information gathering. 2. Long Term memory management. 3. GPT - 3.5 powered Agents for delegation of simple tasks. 4. File output. Performance Evaluation: 1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities. 2. Constructively self - criticize your big - picture behavior constantly. 3. Reflect on past decisions and strategies to refine your approach. 4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps. You should only respond in JSON format as described below Response Format : { "thoughts" : { "text" : "thought" , "reasoning" : "reasoning" , "plan" : "- short bulleted\n- list that conveys\n- long-term plan" , "criticism" : "constructive self-criticism" , "speak" : "thoughts summary to say to user" }, "command" : { "name" : "command name" , "args" : { "arg name" : "value" } } } Ensure the response can be parsed by Python json.loads System: The current time and date is Fri Oct 13 20 : 01 : 00 2023 System: This reminds you of these events from your past: [ 'Assistant Reply: {\n "thoughts": {\n "text": "I need to find out the number of gold medals China won in the 2023 Chengdu Universiade. To do this, I will use the \'search\' command to look up this information online.",\n "reasoning": "The \'search\' command is the most appropriate in this situation because it allows me to find current and specific information on the internet.",\n "plan": "- Use the \'search\' command to find the number of gold medals China won in the 2023 Chengdu Universiade.\\n- Once I have the information, I will use the \'write_file\' command to save it for future reference.",\n "criticism": "None at this stage, as the plan is straightforward and efficient.",\n "speak": "I am going to search for the number of gold medals China won in the 2023 Chengdu Universiade."\n },\n "command": {\n "name": "search",\n "args": {\n "tool_input": "2023 Chengdu Universiade China gold medal count"\n }\n }\n} \nResult: Command search returned: 103 ' ] Human: Determine which next command to use, and respond using the format specified above: AI: { "thoughts" : { "text" : "I need to find out the number of gold medals China won in the 2023 Chengdu Universiade. To do this, I will use the 'search' command to look up this information online." , "reasoning" : "The 'search' command is the most appropriate in this situation because it allows me to find current and specific information on the internet." , "plan" : "- Use the 'search' command to find the number of gold medals China won in the 2023 Chengdu Universiade.\n- Once I have the information, I will use the 'write_file' command to save it for future reference." , "criticism" : "None at this stage, as the plan is straightforward and efficient." , "speak" : "I am going to search for the number of gold medals China won in the 2023 Chengdu Universiade." }, "command" : { "name" : "search" , "args" : { "tool_input" : "2023 Chengdu Universiade China gold medal count" } } } System: Command search returned: 103 Human: Determine which next command to use, and respond using the format specified above: > Finished chain. { "thoughts" : { "text" : "I have found the number of gold medals China won in the 2023 Chengdu Universiade, which is 103. Now, I need to save this information for future reference. I will use the 'write_file' command to do this." , "reasoning" : "The 'write_file' command is the most appropriate in this situation because it allows me to save important information to a file for future reference." , "plan" : "- Use the 'write_file' command to save the number of gold medals China won in the 2023 Chengdu Universiade.\n- Once I have saved the information, I will use the 'finish' command to signal that I have completed my objectives." , "criticism" : "None at this stage, as the plan is straightforward and efficient." , "speak" : "I am going to save the number of gold medals China won in the 2023 Chengdu Universiade." }, "command" : { "name" : "write_file" , "args" : { "file_path" : "2023_Chengdu_Universiade_China_Gold_Medal_Count.txt" , "text" : "China won 103 gold medals in the 2023 Chengdu Universiade." , "append" : false } } } > Entering new LLMChain chain... Prompt after formatting: System: You are Jarvis, Assistant Your decisions must always be made independently without seeking user assistance. Play to your strengths as an LLM and pursue simple strategies with no legal complications. If you have completed all your tasks, make sure to use the "finish" command. GOALS: 1. 2023 年成都大运会,中国金牌数是多少 Constraints: 1. ~ 4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files. 2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember. 3. No user assistance 4. Exclusively use the commands listed in double quotes e.g. "command name" Commands: 1. search: useful for when you need to answer questions about current events. You should ask targeted questions, args json schema: { "tool_input" : { "type" : "string" }} 2. write_file: Write file to disk, args json schema: { "file_path" : { "title" : "File Path" , "description" : "name of file" , "type" : "string" }, "text" : { "title" : "Text" , "description" : "text to write to file" , "type" : "string" }, "append" : { "title" : "Append" , "description" : "Whether to append to an existing file." , "default" : false, "type" : "boolean" }} 3. read_file: Read file from disk, args json schema: { "file_path" : { "title" : "File Path" , "description" : "name of file" , "type" : "string" }} 4. finish: use this to signal that you have finished all your objectives, args: "response" : "final response to let people know you have finished your objectives" Resources: 1. Internet access for searches and information gathering. 2. Long Term memory management. 3. GPT - 3.5 powered Agents for delegation of simple tasks. 4. File output. Performance Evaluation: 1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities. 2. Constructively self - criticize your big - picture behavior constantly. 3. Reflect on past decisions and strategies to refine your approach. 4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps. You should only respond in JSON format as described below Response Format : { "thoughts" : { "text" : "thought" , "reasoning" : "reasoning" , "plan" : "- short bulleted\n- list that conveys\n- long-term plan" , "criticism" : "constructive self-criticism" , "speak" : "thoughts summary to say to user" }, "command" : { "name" : "command name" , "args" : { "arg name" : "value" } } } Ensure the response can be parsed by Python json.loads System: The current time and date is Fri Oct 13 20 : 01 : 24 2023 System: This reminds you of these events from your past: [ 'Assistant Reply: {\n "thoughts": {\n "text": "I need to find out the number of gold medals China won in the 2023 Chengdu Universiade. To do this, I will use the \'search\' command to look up this information online.",\n "reasoning": "The \'search\' command is the most appropriate in this situation because it allows me to find current and specific information on the internet.",\n "plan": "- Use the \'search\' command to find the number of gold medals China won in the 2023 Chengdu Universiade.\\n- Once I have the information, I will use the \'write_file\' command to save it for future reference.",\n "criticism": "None at this stage, as the plan is straightforward and efficient.",\n "speak": "I am going to search for the number of gold medals China won in the 2023 Chengdu Universiade."\n },\n "command": {\n "name": "search",\n "args": {\n "tool_input": "2023 Chengdu Universiade China gold medal count"\n }\n }\n} \nResult: Command search returned: 103 ' , 'Assistant Reply: {\n "thoughts": {\n "text": "I have found the number of gold medals China won in the 2023 Chengdu Universiade, which is 103. Now, I need to save this information for future reference. I will use the \'write_file\' command to do this.",\n "reasoning": "The \'write_file\' command is the most appropriate in this situation because it allows me to save important information to a file for future reference.",\n "plan": "- Use the \'write_file\' command to save the number of gold medals China won in the 2023 Chengdu Universiade.\\n- Once I have saved the information, I will use the \'finish\' command to signal that I have completed my objectives.",\n "criticism": "None at this stage, as the plan is straightforward and efficient.",\n "speak": "I am going to save the number of gold medals China won in the 2023 Chengdu Universiade."\n },\n "command": {\n "name": "write_file",\n "args": {\n "file_path": "2023_Chengdu_Universiade_China_Gold_Medal_Count.txt",\n "text": "China won 103 gold medals in the 2023 Chengdu Universiade.",\n "append": false\n }\n }\n} \nResult: Command write_file returned: File written successfully to 2023_Chengdu_Universiade_China_Gold_Medal_Count.txt. ' ] Human: Determine which next command to use, and respond using the format specified above: AI: { "thoughts" : { "text" : "I need to find out the number of gold medals China won in the 2023 Chengdu Universiade. To do this, I will use the 'search' command to look up this information online." , "reasoning" : "The 'search' command is the most appropriate in this situation because it allows me to find current and specific information on the internet." , "plan" : "- Use the 'search' command to find the number of gold medals China won in the 2023 Chengdu Universiade.\n- Once I have the information, I will use the 'write_file' command to save it for future reference." , "criticism" : "None at this stage, as the plan is straightforward and efficient." , "speak" : "I am going to search for the number of gold medals China won in the 2023 Chengdu Universiade." }, "command" : { "name" : "search" , "args" : { "tool_input" : "2023 Chengdu Universiade China gold medal count" } } } System: Command search returned: 103 Human: Determine which next command to use, and respond using the format specified above: AI: { "thoughts" : { "text" : "I have found the number of gold medals China won in the 2023 Chengdu Universiade, which is 103. Now, I need to save this information for future reference. I will use the 'write_file' command to do this." , "reasoning" : "The 'write_file' command is the most appropriate in this situation because it allows me to save important information to a file for future reference." , "plan" : "- Use the 'write_file' command to save the number of gold medals China won in the 2023 Chengdu Universiade.\n- Once I have saved the information, I will use the 'finish' command to signal that I have completed my objectives." , "criticism" : "None at this stage, as the plan is straightforward and efficient." , "speak" : "I am going to save the number of gold medals China won in the 2023 Chengdu Universiade." }, "command" : { "name" : "write_file" , "args" : { "file_path" : "2023_Chengdu_Universiade_China_Gold_Medal_Count.txt" , "text" : "China won 103 gold medals in the 2023 Chengdu Universiade." , "append" : false } } } System: Command write_file returned: File written successfully to 2023_Chengdu_Universiade_China_Gold_Medal_Count .txt. Human: Determine which next command to use, and respond using the format specified above: > Finished chain. { "thoughts" : { "text" : "I have successfully saved the number of gold medals China won in the 2023 Chengdu Universiade to a file. Now, I need to signal that I have completed my objectives. I will use the 'finish' command to do this." , "reasoning" : "The 'finish' command is the most appropriate in this situation because it allows me to signal that I have completed my objectives." , "plan" : "- Use the 'finish' command to signal that I have completed my objectives." , "criticism" : "None at this stage, as the plan is straightforward and efficient." , "speak" : "I have completed my objectives." }, "command" : { "name" : "finish" , "args" : { "response" : "I have completed my objectives." } } } 'I have completed my objectives.' |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了