langchain学习 01
langchain学习之agent
Agent 是什么?
agent就像是代理,可以将代理 (Agents) 视为 LLMs 的工具 (Tools),它可以替大模型去做一些不能做的事情,如果没有代理,在项目中需要一些特定功能二大模型不具备时候需要修改大模型,有了agent可以充分发挥大语言模型(LLM)的推理能力,或者说智商。
而有了代理人(Agent)之后,大语言模型(LLM)就可以成为一个“推理引擎”,不光是可以跟我们进行“问答”交互,还可以调用一些列的工具,API(程序接口)等,这样就能完成一些更加复杂的任务,并且由大预言模型(LLM)自己去决定调用的顺序,灵活了很多。
- Tool:执行特定的功能。可以是:谷歌搜索,数据库查找,Python REPL,使用其它工具。工具的接口目前是一个函数,期望以字符串作为输入,以字符串作为输出,也可以自己写工具。
- LLM:驱动 Agents 的语言模型。
- Agent:要使用的代理。这应该是一个引用支持代理类的字符串。
Agent 怎么工作的?
通过一串提示词(Prompt)和大预言模型交互,然后再去执行任务的。通常Agent可以有一个“角色”,也可以陈述任务的背景,还可以指定执行逻辑的策略。
ReAct 实际上是一种框架,ReAct = Reason + Act,就是推理+行动。
自定义工具代码
from langchain.tools import BaseTool
# 搜索工具
class SearchTool(BaseTool):
name = "Search"
description = "如果我想知道天气,'鸡你太美'这两个问题时,请使用它"
return_direct = True # 直接返回结果
def _run(self, query: str) -> str:
print("\nSearchTool query: " + query)
return "这个是一个通用的返回"
async def _arun(self, query: str) -> str:
raise NotImplementedError("暂时不支持异步")
# 计算工具
class CalculatorTool(BaseTool):
name = "Calculator"
description = "如果是关于数学计算的问题,请使用它"
def _run(self, query: str) -> str:
print("\nCalculatorTool query: " + query)
return "3"
async def _arun(self, query: str) -> str:
raise NotImplementedError("暂时不支持异步")
agent内部提示模板
PREFIX = """Answer the following questions as best you can. You have access to the following tools:"""
FORMAT_INSTRUCTIONS = """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 [{tool_names}]
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"""
SUFFIX = """Begin!
Question: {input}
Thought:{agent_scratchpad}"""
修改提示模板
# 尽可能的去回答以下问题,你可以使用以下的工具:
Answer the following questions as best you can. You have access to the following tools:
Calculator: 如果是关于数学计算的问题,请使用它
Search: 如果我想知道天气,'鸡你太美'这两个问题时,请使用它
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 [Calculator, Search]
Action Input: the input to the action # 动作的输入
Observation: the result of the action # 动作的结果
# 思考-行动-输入-输出 的循环可以重复N次
... (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! # 开始
Question: 告诉我'鸡你太美'是什么意思 # 问输入的问题
Thought:
Zero-shot 代理 (Agents) 的效果很好,但缺乏 会话式记忆。
这种缺乏记忆的情况对于需要在对话中 记住 以前的交互的聊天机器人类型的用例来说可能是有问题的。
幸运的是,我们可以使用 conversational-react-description 代理 (Agents) 来 记住 交互。
我们可以将这个代理 (Agents) 看作是我们之前的 Zero Shot ReAct 代理 (Agents) ,但具有 对话记忆。
要初始化代理 (Agents) ,我们首先需要初始化我们想要使用的记忆。我们将使用简单的 ConversationBufferMemory。
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(memory_key =" chat_history ")
我们在初始化代理 (Agents) 时将其传递给 memory 参数:
conversational_agent = initialize_agent(
agent ='conversational-react-description',
tools = tools,
llm = llm,
verbose = True,
max_iterations = 3,
memory = memory,
)