准备工作
- 注册获取openAI的key,这个自己弄。
- 注册并获取
Google Search API
的 key,为了可以在工具中使用google查询,https://serpapi.com/search-api
概述
本笔记本介绍了如何创建自定义 的 MRKL 代理。 MRKL 代理由三部分组成:
- Tools: 代理可使用的工具
- LLMChain: 生成文本,该文本以某种方式解析以确定要采取哪些操作。
- 代理类本身: 解析 LLMChain 的输出以确定要采取哪些操作。
在本笔记本中,我们将介绍如何通过创建自定义 LLMChain 来创建自定义 MRKL 代理。
内容
自定义 LLMChain(Custom LLMChain)
创建自定义代理的第一种方法是使用现有的 Agent
类,即:使用自定义的 LLMChain
。
这是创建自定义代理的最简单方法。强烈建议您使用 ZeroShotAgent
,因为它是迄今为止最通用的一种。
创建自定义 LLMChain
的大部分工作都归结为prompt
。因为我们使用现有的代理类来解析输出,所以对于prompt
来说以该格式生成文本非常重要。此外,我们目前需要一个 agent_scratchpad
输入变量来记录之前的操作和观察结果。这几乎总是prompt
的最后部分。但是,除了这些说明之外,您还可以根据需要自定义prompt
。
作者:上面看不懂,可以直接看最后总结,都是套路。
为了确保prompt
包含适当的指令,我们将在该类上使用辅助方法。 ZeroShotAgent
的辅助方法采用以下参数:
- 工具:代理可以访问的工具列表,用于格式化提示。
- 前缀(prefix):放置在工具列表之前的字符串。
- 后缀(suffix):放置在工具列表之后的字符串。
- input_variables:最终提示所需的输入变量列表。
在本次练习中,我们将授予代理访问 Google
搜索的权限,并对其进行自定义,使其能进行回答。
import os
# 自己的openai 的Key
os.environ["OPENAI_API_KEY"] = "xxx"
# https://serpapi.com/search-api
os.environ["SERPAPI_API_KEY"] = "xxx"
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!"
"""
# 多输入
# 代理还可以处理需要多个输入的提示。
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
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"
)
"""
> 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.'
"""
总结
- 创建prompt(提示语),建议使用ZeroShotAgent
ZeroShotAgent.create_prompt(
tools, prefix=prefix, suffix=suffix, input_variables=["input", "agent_scratchpad"]
)
- 得到大模型链:
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?")
参考地址:
https://python.langchain.com/docs/modules/agents/how_to/custom_mrkl_agent