ZERO_SHOT_REACT_DESCRIPTION
代码逻辑
1、initialize_agent函数
langchain\agents\initialize.py
ZeroShotAgent : from_llm_and_tools
langchain\agents\mrkl\base.py
Construct an agent from an LLM and tools
prompt = cls.create_prompt
Create prompt in the style of the zero shot agent.
tools: Sequence[BaseTool],
prefix: str = PREFIX,
suffix: str = SUFFIX,
将prefix、工具名称以及工具描述、suffix组装成prompt给LLMChain
llm_chain = LLMChain 构建LLMChain
_output_parser = output_parser or cls._get_default_output_parser()
return AgentExecutor.from_agent_and_tools
2、 run函数
langchain\chains\base.py
Chain :__call__函数: Execute the chain.
AgentExecuter: _call
langchain\agents\agent.py
Run text through and get agent response
while self._should_continue(iterations, time_elapsed) 通过该while循环
_take_next_step
_iter_next_step
Take a single step in the thought-action-observation loop
output = self.agent.plan
Call the LLM to see what to do
LLMChain : predict
langchain\chains\llm.py
Format prompt with kwargs and pass to LLM
Chain : __call__函数
LLMChain:_call函数
response = self.generate([inputs], run_manager=run_manager)
Generate LLM result from inputs 将问题输入给LLM,获取LLM的结果
生成的response的内容包括:
text": " I need to find out the age of Yao Ming's wife first.\n
Action: Search\n
Action Input: \"Yao Ming's wife age
执行工具,获取工具调用的结果
langchain ZERO_SHOT_REACT运行逻辑
于 2023-12-17 22:21:32 首次发布