首页 > 其他分享 >agent_scratchpad

agent_scratchpad

时间:2024-12-01 19:44:03浏览次数:4  
标签:scratchpad format agent action input thoughts

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}

 

标签:scratchpad,format,agent,action,input,thoughts
From: https://www.cnblogs.com/lightsong/p/18580227

相关文章

  • Multi-Agent 系统架构综述与项目代码实际案例讲解
    Multi-Agent系统架构综述与项目代码实际案例讲解文章目录Multi-Agent系统架构综述与项目代码实际案例讲解1.背景介绍2.MAS系统要素拆解分析Agent的角色资料常见角色用户代理(UserProxy):环境代理(EnvProxy):意图识别者(IntentRecognizer):总结者(Su......
  • Multi-Agent 系统架构设计综述与项目代码实践
    Multi-Agent系统架构设计综述与项目代码实践关键词:Multi-Agent系统、分布式AI、协作智能、系统架构、通信协议、决策机制、项目实践摘要:本文深入探讨了Multi-Agent系统的架构设计原理和实践应用。从背景介绍开始,详细阐述了Multi-Agent系统的核心概念、设计原则和关键......
  • 拥抱 OpenTelemetry:阿里云 Java Agent 演进实践
    作者:陈承背景在2018年的2月,ARMSJavaAgent的第一个版本正式发布,为用户提供无侵入的的可观测数据采集服务。6年后的今天,随着软件技术的迅猛发展、业务场景的逐渐丰富、用户规模的快速增长,我们逐渐发现过去的功能以及架构的设计逐渐难以合理、优雅的满足今天的需求,重构越来......
  • agent introduced in Cursor v0.43
    InthelatestCursorIDE,AgentsintheComposerpanelarecustomizableAIassistantsthatcanbeconfiguredforspecifictasks.Here'sabreakdown:WhatAgentsAre:SpecializedAIassistantswithdefinedbehaviorsCanbecustomizedforspecificp......
  • 针对Qwen-Agent框架的Function Call及ReAct的源码阅读与解析:Agent基类篇
    文章目录Agent继承链Agent类总体架构初始化方法`__init__`方法:`_init_tool`方法:对话生成方法`_call_llm`方法:工具调用方法`_call_tool`方法:`_detect_tool`方法:整体执行方法`run`方法:`_run`方法:`run_nonstream`方法......
  • Perplexity 计划推出低价语音问答硬件;/dev/agents:AI Agents 的操作系统,种子轮估值 5
       开发者朋友们大家好: 这里是「RTE开发者日报」,每天和大家一起看新闻、聊八卦。我们的社区编辑团队会整理分享RTE(Real-TimeEngagement)领域内「有话题的新闻」、「有态度的观点」、「有意思的数据」、「有思考的文章」、「有看点的会议」,但内容仅代表编......
  • 语音 Agent 平台 PlayAI 融资 2100 万美元;英伟达音频模型 Fugatto:输入文本音频生成人
       开发者朋友们大家好: 这里是「RTE开发者日报」,每天和大家一起看新闻、聊八卦。我们的社区编辑团队会整理分享RTE(Real-TimeEngagement)领域内「有话题的新闻」、「有态度的观点」、「有意思的数据」、「有思考的文章」、「有看点的会议」,但内容仅代表编......
  • L2G2000 - Lagent:从零搭建你的 Multi-Agent
    Lagent是一个轻量级开源智能体框架,旨在让用户可以高效地构建基于大语言模型的智能体。同时它也提供了一些典型工具以增强大语言模型的能力。环境配置开发机选择30%A100,镜像选择为Cuda12.2-conda。#创建环境condacreate-nlagentpython=3.10-y#激活环境cond......
  • 微软将推出10个自主AI Agent,称相当于增加187名全职员工产出
    如今,我们每天都能看到各种AI新成果出炉,尤其是生成式AI和大模型领域,几乎每隔几天就有更强大的模型问世。然而在这样的大背景下,坐拥ChatGPT、DALL-E等流行应用(模型)的OpenAI却仍未找到合适的商业盈利模式。不久前,就有知情人士透露OpenAI“烧钱太狠”,今年或面临高达......
  • 一文彻底理解大模型 Agent 智能体原理和案例
    什么是大模型Agent?大模型Agent,作为一种人工智能体,是具备环境感知能力、自主理解、决策制定及执行行动能力的智能实体。简而言之,它是构建于大模型之上的计算机程序,能够模拟独立思考过程,灵活调用各类工具,逐步达成预设目标的智能存在。Agent是AI大模型应用的主要新形态......