构建LangChain应用程序的示例代码:13、基于在BabyAGI的基础上如何替换执行链的教程

BabyAGI 与工具

示例是在 baby agi 的基础上构建,但展示了如何替换执行链。之前的执行链仅仅是一个 LLM,它编造东西。通过将其替换为具有工具访问权限的代理,我们可以希望获得真实可靠的信息。

安装并导入所需模块
from typing import Optional

from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain_experimental.autonomous_agents import BabyAGI
from langchain_openai import OpenAI, OpenAIEmbeddings
连接到向量存储

根据您使用的向量存储不同,这一步可能看起来不同。

%pip install faiss-cpu > /dev/null
%pip install google-search-results > /dev/null
from langchain.docstore import InMemoryDocstore
from langchain_community.vectorstores import FAISS
# 定义您的嵌入模型
embeddings_model = OpenAIEmbeddings()

# 初始化为空的向量存储
import faiss

embedding_size = 1536
index = faiss.IndexFlatL2(embedding_size)
vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {})
定义链

BabyAGI 依赖于三个 LLM 链:

  • 任务创建链,用于选择添加到列表中的新任务
  • 任务优先级链,用于重新确定任务的优先级
  • 执行链,用于执行任务

注意:在这本笔记本中,执行链现在将是一个代理。

from langchain.agents import AgentExecutor, Tool, ZeroShotAgent
from langchain.chains import LLMChain
from langchain_community.utilities import SerpAPIWrapper
from langchain_openai import OpenAI

todo_prompt = PromptTemplate.from_template(
    "你是擅长为给定目标制定待办事项清单的规划者。为这个目标制定一个待办事项清单:{objective}"
)
todo_chain = LLMChain(llm=OpenAI(temperature=0), prompt=todo_prompt)
search = SerpAPIWrapper()
tools = [
    Tool(
        name="搜索",
        func=search.run,
        description="当你需要回答有关当前事件的问题时很有用",
    ),
    Tool(
        name="待办事项",
        func=todo_chain.run,
        description="当你需要制定待办事项清单时很有用。输入:一个目标来创建待办事项清单。输出:该目标的待办事项清单。请非常清楚目标是什么!",
    ),
]

prefix = "你是根据以下目标执行任务的 AI:{objective}。考虑这些先前完成的任务:{context}。"
suffix = "问题:{task}\n{agent_scratchpad}"
prompt = ZeroShotAgent.create_prompt(
    tools,
    prefix=prefix,
    suffix=suffix,
    input_variables=["objective", "task", "context", "agent_scratchpad"],
)
llm = OpenAI(temperature=0)
llm_chain = LLMChain(llm=llm, prompt=prompt)
tool_names = [tool.name for tool in tools]
agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names)
agent_executor = AgentExecutor.from_agent_and_tools(
    agent=agent, tools=tools, verbose=True
)
运行 BabyAGI

现在到了创建 BabyAGI 控制器并观察它尝试完成您的目标的时候了。

OBJECTIVE = "为今天的旧金山编写一份天气报告"
# LLMChains 的日志记录
verbose = False

# 如果为 None,将永远继续下去
max_iterations: Optional[int] = 3
baby_agi = BabyAGI.from_llm(
    llm=llm,
    vectorstore=vectorstore,
    task_execution_chain=agent_executor,
    verbose=verbose,
    max_iterations=max_iterations,
)
baby_agi({"objective": OBJECTIVE})

介绍了如何使用BabyAGI并替换其执行链,通过引入具有工具访问权限的代理来提高信息的可靠性。首先,安装必要的模块并连接向量存储,然后定义嵌入模型和向量存储。接着,定义了三个链:任务创建链、任务优先级链和执行链,其中执行链在此示例中被替换为一个代理。通过创建提示模板和工具,然后初始化代理执行器和BabyAGI控制器,BabyAGI能够根据给定的目标执行任务,并通过日志记录选项提供详细的执行过程。这种方法展示了如何通过集成不同的工具来扩展BabyAGI的功能,使其能够更有效地完成任务。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hugo_Hoo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值