Stay Hungry,Stay Foolish!

agent_scratchpad

agent_scratchpad

https://cookbook.langchain.com.cn/docs/langchain-agents/

In [17]:

print(zero_shot_agent.agent.llm_chain.prompt.template)
 

Out [17]:

Answer the following questions as best you can. You have access to the following tools:
Calculator: Useful for when you need to answer questions about math.
Stock DB: Useful for when you need to answer questions about stocks and their prices.
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, Stock DB]
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!
Question: {input}
Thought:{agent_scratchpad}

 

我们首先告诉 LLM 它可以使用的工具(CalculatorStock DB)。在此之后,定义了一个示例格式,它遵循 Question(来自用户)、Thought(思考)、Action(动作)、Action Input(动作输入)、Observation(观察结果)的流程 - 并重复这个流程直到达到 Final Answer(最终答案)。

这些工具和思考过程将 LangChain 中的 agentschains 分开。

chain 定义了一种即时的输入/输出过程,agents 的逻辑允许一步一步地进行思考。这种一步一步的过程的优势在于 LLM 可以通过多个推理步骤或工具来得出更好的答案。

我们还需要讨论提示的最后一部分。最后一行是 "Thought:{agent_scratchpad}"

agent_scratchpad 是我们添加代理 (Agents) 已经执行的 每个 思考或动作的地方。所有的思考和动作(在 当前 代理 (Agents) 执行器链中)都可以被 下一个 思考-动作-观察循环访问,从而实现代理 (Agents) 动作的连续性。

 

让我们看看最后一个代理 (Agents) - self-ask-with-search 代理 (Agents) 。当连接 LLM 和搜索引擎时,这是您应该考虑的第一个代理 (Agents) 。

代理 (Agents) 将根据需要执行搜索和提问步骤,以获得最终答案。我们这样初始化代理 (Agents) :

from langchain import SerpAPIWrapper

# initialize the search chain
search = SerpAPIWrapper(serpapi_api_key ='serp_api_key')

# create a search tool
tools = [
Tool(
name = "Intermediate Answer",
func = search.run,
description ='google search'
)
]

# initialize the search enabled agent
self_ask_with_search = initialize_agent(
tools,
llm,
agent = "self-ask-with-search",
verbose = True
)

 

现在让我们问一个需要多次搜索和“自问”步骤的问题。

In [38]:

self_ask_with_search(
"who lived longer; Plato, Socrates, or Aristotle?"
)
 

Out [38]:

[1m > Entering new AgentExecutor chain...[0m
[32; 1m [1; 3m Yes.
Follow up: How old was Plato when he died?[0m
Intermediate answer: [36; 1m [1; 3meighty [0m [32; 1m [1; 3m
Follow up: How old was Socrates when he died?[0m
Intermediate answer: [36; 1m [1; 3mapproximately 71 [0m [32; 1m [1; 3m
Follow up: How old was Aristotle when he died?[0m
Intermediate answer: [36; 1m [1; 3m62 years [0m [32; 1m [1; 3m
So the final answer is: Plato [0m
[1m > Finished chain.[0m
{'input': 'who lived longer; Plato, Socrates, or Aristotle?',
'output': 'Plato'}
 

我们可以看到代理 (Agents) 的多步骤过程。它进行了多次后续问题来找到最终答案。

 

can u introduce how to use agent_scratchpad ?

https://github.com/langchain-ai/langchain/issues/14185

In the context of the LangChain framework, agent_scratchpad is a function that formats the intermediate steps of the agent's actions and observations into a string. This function is used to keep track of the agent's thoughts or actions during the execution of the program. Here is the code that supports this explanation:

def format_agent_scratchpad(intermediate_steps):
    thoughts = ""
    for action, observation in intermediate_steps:
        thoughts += action.log
        thoughts += "</search_query>" + _format_docs(observation)
    return thoughts
 

You can find this in the agent_scratchpad.py file.

The agent_scratchpad has two methods available:

  1. _format_docs(docs): This method takes a list of documents as input and formats them into a string. Each document is wrapped in <item> and <page_content> tags, with an index attribute for the item tag.

  2. format_agent_scratchpad(intermediate_steps): This method takes a list of intermediate steps as input. Each step is a tuple containing an action and an observation. The method concatenates the log of each action and the formatted observation into a string, which it then returns.

Here is the supporting code:

def _format_docs(docs):
    result = "\n".join(
        [
            f'<item index="{i+1}">\n<page_content>\n{r}\n</page_content>\n</item>'
            for i, r in enumerate(docs)
        ]
    )
    return result

def format_agent_scratchpad(intermediate_steps):
    thoughts = ""
    for action, observation in intermediate_steps:
        thoughts += action.log
        thoughts += "</search_query>" + _format_docs(observation)
    return thoughts
 

You can also find this in the agent_scratchpad.py file.

The format_agent_scratchpad method in the LangChain framework is used to format the intermediate steps of an agent's actions and observations into a string. This method takes a list of tuples, intermediate_steps, where each tuple contains an action and an observation. It iterates over this list, appending the log of each action and the formatted observation to a string, thoughts.

The observation is formatted using the _format_docs helper function, which takes a list of documents (or 'docs') and returns a string where each document is enclosed in <item> and <page_content> tags, with an index attribute for each item.

I hope this helps! If you have any other questions, feel free to ask.

 

https://smith.langchain.com/hub/hwchase17/react

Answer the following questions as best you can. You have access to the following tools:

 

{tools}

 

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

 

Begin!

 

Question: {input}

Thought:{agent_scratchpad}

 

posted @ 2024-12-01 19:34  lightsong  阅读(14)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel