返回顶部

实战: 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 实战

安装依赖包

pip install langchain_experimental
# 一个向量数据库
!pip install faiss-cpu

构建tools

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

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

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

运行目标

agent.run(["2023年成都大运会,中国金牌数是多少"])

 

输出日志如下

 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.'

 

posted @ 2023-10-22 21:35  Crazymagic  阅读(635)  评论(0编辑  收藏  举报