分类目录:《大模型从入门到应用》总目录
LangChain系列文章:
- 基础知识
- 快速入门
- 安装与环境配置
- 链(Chains)、代理(Agent:)和记忆(Memory)
- 快速开发聊天模型
- 模型(Models)
- 基础知识
- 大型语言模型(LLMs)
- 基础知识
- LLM的异步API、自定义LLM包装器、虚假LLM和人类输入LLM(Human Input LLM)
- 缓存LLM的调用结果
- 加载与保存LLM类、流式传输LLM与Chat Model响应和跟踪tokens使用情况
- 聊天模型(Chat Models)
- 基础知识
- 使用少量示例和响应流式传输
- 文本嵌入模型
- Aleph Alpha、Amazon Bedrock、Azure OpenAI、Cohere等
- Embaas、Fake Embeddings、Google Vertex AI PaLM等
- 提示(Prompts)
- 基础知识
- 提示模板
- 基础知识
- 连接到特征存储
- 创建自定义提示模板和含有Few-Shot示例的提示模板
- 部分填充的提示模板和提示合成
- 序列化提示信息
- 示例选择器(Example Selectors)
- 输出解析器(Output Parsers)
- 记忆(Memory)
- 基础知识
- 记忆的类型
- 会话缓存记忆、会话缓存窗口记忆和实体记忆
- 对话知识图谱记忆、对话摘要记忆和会话摘要缓冲记忆
- 对话令牌缓冲存储器和基于向量存储的记忆
- 将记忆添加到LangChain组件中
- 自定义对话记忆与自定义记忆类
- 聊天消息记录
- 记忆的存储与应用
- 索引(Indexes)
- 基础知识
- 文档加载器(Document Loaders)
- 文本分割器(Text Splitters)
- 向量存储器(Vectorstores)
- 检索器(Retrievers)
- 链(Chains)
- 基础知识
- 通用功能
- 自定义Chain和Chain的异步API
- LLMChain和RouterChain
- SequentialChain和TransformationChain
- 链的保存(序列化)与加载(反序列化)
- 链与索引
- 文档分析和基于文档的聊天
- 问答的基础知识
- 图问答(Graph QA)和带来源的问答(Q&A with Sources)
- 检索式问答
- 文本摘要(Summarization)、HyDE和向量数据库的文本生成
- 代理(Agents)
- 基础知识
- 代理类型
- 自定义代理(Custom Agent)
- 自定义MRKL代理
- 带有ChatModel的LLM聊天自定义代理和自定义多操作代理(Custom MultiAction Agent)
- 工具
- 基础知识
- 自定义工具(Custom Tools)
- 多输入工具和工具输入模式
- 人工确认工具验证和Tools作为OpenAI函数
- 工具包(Toolkit)
- 代理执行器(Agent Executor)
- 结合使用Agent和VectorStore
- 使用Agents的异步API和创建ChatGPT克隆
- 处理解析错误、访问中间步骤和限制最大迭代次数
- 为代理程序设置超时时间和限制最大迭代次数和为代理程序和其工具添加共享内存
- 计划与执行
- 回调函数(Callbacks)
本文将介绍如何创建自己的自定义MRKL Agent。MRKL Agent由三个部分组成:
- 工具:代理可用的工具。
- LLMChain:生成以一定方式解析的文本,以确定要采取哪个动作。
- 代理类本身:解析LLMChain的输出,以确定要采取哪个动作。
自定义LLMChain(Custom LLMChain)
本部分将介绍如何通过创建自定义LLMChain来创建自定义MRKL代理。创建自定义代理的第一种方法是使用现有的代理类,但使用自定义LLMCain。这也是创建自定义代理的最简单方法。强烈建议使用ZeroShotAgent
,因为目前这是最通用的一个。
创建自定义LLMCain的大部分工作都归结为提示符。因为我们使用的是一个现有的代理类来解析输出,所以提示符中要生成该格式的文本是非常重要的。此外,我们目前需要一个agent_scratchpad
输入变量来记录以前的操作和观察结果,这几乎总是提示符的最后一部分。但是,除了这些说明之外,我们还可以根据需要自定义提示。为了确保提示符包含适当的指令,我们将在该类上使用helper
方法。ZeroShotAgent
的helper
方法接受以下参数:
-
tools
:座席将有权访问的工具列表,用于设置提示的格式。 -
prefix
:要放在工具列表前面的字符串。 -
suffix
:放在工具列表后面的字符串。 -
input_variables
:最后提示所期望的输入变量列表。
在下面的示例中,我们将给予我们的代理访问Google搜索,我们将定制它,我们将让它回答为盗版。
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
from langchain import OpenAI, SerpAPIWrapper, LLMChain
search = SerpAPIWrapper()
tools = [
Tool(
name = "Search",
func=search.run,
description="useful for when you need to answer questions about current events"
)
]
prefix = """Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools:"""
suffix = """Begin! Remember to speak as a pirate when giving your final answer. Use lots of "Args"
Question: {input}
{agent_scratchpad}"""
prompt = ZeroShotAgent.create_prompt(
tools,
prefix=prefix,
suffix=suffix,
input_variables=["input", "agent_scratchpad"]
)
如果感到好奇,我们现在可以看看最终的提示模板,看看它看起来像当它的所有放在一起:
print(prompt.template)
输出:
Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools:
Search: useful for when you need to answer questions about current events
Use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [Search]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
Begin! Remember to speak as a pirate when giving your final answer. Use lots of "Args"
Question: {input}
{agent_scratchpad}
需要注意的是,我们能够为代理提供自定义的提示模板,即不限于由create_prompt
函数生成的提示,假设它满足代理的要求。例如,对于ZeroShotAgent
,我们需要确保它应该有一个以Action:
开头的字符串和一个以Action Input:
开头的字符串,并且两者都应该用换行符分隔。
llm_chain = LLMChain(llm=OpenAI(temperature=0), 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)
agent_executor.run("How many people live in canada as of 2023?")
输出:
> Entering new AgentExecutor chain...
Thought: I need to find out the population of Canada
Action: Search
Action Input: Population of Canada 2023
Observation: The current population of Canada is 38,661,927 as of Sunday, April 16, 2023, based on Worldometer elaboration of the latest United Nations data.
Thought: I now know the final answer
Final Answer: Arrr, Canada be havin' 38,661,927 people livin' there as of 2023!
> Finished chain.
"Arrr, Canada be havin' 38,661,927 people livin' there as of 2023!"
多路输入 (Multiple Inputs)
代理还可以处理需要多个输入的提示:
prefix = """Answer the following questions as best you can. You have access to the following tools:"""
suffix = """When answering, you MUST speak in the following language: {language}.
Question: {input}
{agent_scratchpad}"""
prompt = ZeroShotAgent.create_prompt(
tools,
prefix=prefix,
suffix=suffix,
input_variables=["input", "language", "agent_scratchpad"]
)
llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools)
agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)
agent_executor.run(input="How many people live in canada as of 2023?", language="italian")
输出:文章来源:https://www.toymoban.com/news/detail-596705.html
> Entering new AgentExecutor chain...
Thought: I should look for recent population estimates.
Action: Search
Action Input: Canada population 2023
Observation: 39,566,248
Thought: I should double check this number.
Action: Search
Action Input: Canada population estimates 2023
Observation: Canada's population was estimated at 39,566,248 on January 1, 2023, after a record population growth of 1,050,110 people from January 1, 2022, to January 1, 2023.
Thought: I now know the final answer.
Final Answer: La popolazione del Canada è stata stimata a 39.566.248 il 1° gennaio 2023, dopo un record di crescita demografica di 1.050.110 persone dal 1° gennaio 2022 al 1° gennaio 2023.
> Finished chain.
'La popolazione del Canada è stata stimata a 39.566.248 il 1° gennaio 2023, dopo un record di crescita demografica di 1.050.110 persone dal 1° gennaio 2022 al 1° gennaio 2023.'
参考文献:
[1] LangChain 🦜️🔗 中文网,跟着LangChain一起学LLM/GPT开发:https://www.langchain.com.cn/
[2] LangChain中文网 - LangChain 是一个用于开发由语言模型驱动的应用程序的框架:http://www.cnlangchain.com/文章来源地址https://www.toymoban.com/news/detail-596705.html
到了这里,关于自然语言处理从入门到应用——LangChain:代理(Agents)-[自定义MRKL代理]的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!