【LangChain-04】利用权重和偏差跟踪和检查LangChain代理的提示
作者:mmseoamin日期:2024-02-24

目录

  • 一、说明
  • 二、一些背景信息
    • 2.1 LangChain和代理工具101
    • 2.2 权重和偏差 101
    • 三、 LangChain代理追踪检查提示
      • 3.1 代码和模型注释
      • 3.2 一个基本的 LangChain 代理
      • 3.3 我们需要更详细的内容
      • 3.4 权重和偏差项目详细
        • 提示 1 – 初始提示和工具
        • 提示 2 – 初始提示和工具
        • 提示 3 – 做数学
        • 提示 4 – 将所有东西整合在一起
        • 四、结论

          一、说明

          考虑到(生成)人工智能空间,(自主)代理现在无处不在!除了更强大且幸运的是开放的大型语言模型(LLM)之外,LangChain已成为开发人工智能驱动的应用程序和代理的主要工具

          Langchain 是一个功能强大且功能丰富的开源框架,适用于LLM。正如我在另一篇文章中所演示的,它可以用于使用LLM轻松构建问答系统。然而,浪链(LangChain)的另一个关键特性是能够创建和使用上面提到的这些所谓的代理。简而言之,它们是结合了LLM、其他工具和接口(例如浏览器或 API)以及内存来解决复杂任务的应用程序。

          基于LLM的代理通常使用一系列提示(基于提示模板)来检索必要的数据和信息,并准备数据作为他们可以使用的工具的输入(见下文)。当然,LLM还用于首先决定使用哪些工具来完成给定的任务。例如,使用Yao等人提出的ReAct框架来完成自主选择工具。 (2022)。

          当使用和/或开发(自主)代理时,了解代理如何使用 LLM 至关重要,即正在执行哪些提示及其输出。了解游戏中的提示可以提高透明度和可解释性,对于调试至关重要。

          此外,考虑教育,仔细考虑代理生成和使用的提示对于理解决策过程以及与各种工具以及所涉及的数据的交互是必要的。特别是在结合使用多个工具和数据源时,了解各个步骤是成为此类系统的合格用户的关键组成部分。

          因此,本文演示了如何跟踪和检查LangChain代理使用的提示。在简单介绍之后,我们将首先了解如何仅使用 LangChain 检查提示,然后再使用权重和偏差(W&B) 来更好、更清晰地了解正在发生的情况。

          二、一些背景信息

          在查看提示之前,我将提供一些有关 LangChain 工具和权重和偏差的背景信息。

          如果您对权重和偏差以及工具在 LangChain 中的工作原理有基本的了解,您可以安全地跳过。

          2.1 LangChain和代理工具101

          从根本上理解代理如何与工具交互非常重要。虽然这将使我们暂时远离跟踪和检查提示,但它将证明为什么检查已执行的提示如此重要。

          根据 LangChain文档,“工具是代理可以用来与世界交互的功能。”在最简单的情况下,工具是一个接受单个字符串(查询)并返回字符串输出的函数。这使得LLM能够使用与工具进行交互,例如通过语言与信息源进行交互。

          这是一个非常简单的示例,使用可以反转字符串的自定义工具。使用@tool装饰器虽然受到限制,但目前是在 LangChain 中创建基本工具的最简单的方法。

          @tool
          def reverse_string(query: str) -> str:
              '''Reverses a string.'''
              return query[::-1]
          llm = OpenAI(temperature=0)
          tools = [reverse_string]
          agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
          agent.run('What is LangChain in reverse?')
          

          文档字符串(“反转字符串”)在这里至关重要,因为它将作为有关该工具能够执行的操作的信息传递给 LLM。

          执行时,LangChain使用该工具反转字符串。虽然任务本身非常无聊,但代理可以选择正确的工具并应用它的事实非常令人印象深刻。

          Entering new AgentExecutor chain…

          I need to reverse the string

          Action: reverse_string

          Action Input: LangChain

          Observation: niahCgnaL

          Thought: I now know the final answer

          Final Answer: niahCgnaL

          Finished chain.

          代理输出

          重要的是一切都基于提示和文本输入和输出。通过LLM,代理需要找出工具的输入(即函数)并处理其输出。在此示例中,正在执行两个提示来解决问题。

          首先,代理人向LLM提示有关可用工具的信息。正如您所看到的,文档字符串 forreverse_string用作提示的一部分,以告知 LLM 该工具的用途。如果这是一个更复杂的工具,我们将需要提供额外的信息,例如关于预期输入格式的信息。

          Answer the following questions as best you can. You have access to the following tools:
          reverse_string: reverse_string(query: str) -> str - Reverses a string.
          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 [reverse_string]
          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: What is LangChain in reverse?
          Thought:
          

          迅速的

          LLM 以操作(即要使用的工具)和操作输入(即对函数的查询)进行响应:

          [Thought:] I need to reverse the string

          Action: reverse_string

          Action Input: LangChain

          完成/代理输出

          在后台,LangChain 现在正在使用“操作输入”运行所选工具。现在,正在执行的第二个提示可以使用以下结果reverse_string:

          Answer the following questions as best you can. You have access to the following tools:
          reverse_string: reverse_string(query: str) -> str - Reverses a string.
          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 [reverse_string]
          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: What is LangChain in reverse?
          Thought: I need to reverse the string
          Action: reverse_string
          Action Input: LangChain
          Observation: niahCgnaL
          Thought:
          

          迅速的

          正如预期的那样,LLM 正确返回:

          [Thought:] I now know the final answer

          Final Answer: niahCgnaL

          完成/代理输出

          在内部,LangChain 在这里大量使用了链。代理环境中最重要的补充是允许LangChain和LLM与世界互动的工具。

          最终,查看和理解提示及其输出对于理解工具的使用方式(尤其是在调试代理时)至关重要。例如,如果LLM为工具生成“错误”输入,我们需要了解导致该输入的提示。

          2.2 权重和偏差 101

          Weights & Biases是一个非常强大的商业 MLOps 平台,可免费用于个人项目。

          虽然基于云的平台有许多令人兴奋的功能(也用于提示工程),但我们将使用它来跟踪我们的代理并显示其提示。

          简而言之,我们可以将 W&B(使用该wandb库)添加到现有的 Python 脚本中,并开始详细记录发生的情况。幸运的是,W&B 和 LangChain 之间已经有了我们可以利用的强大集成。

          默认情况下,所有信息都将存储在本地(wandb文件夹)以及 W&B 平台上的云端。这也非常有用,因为我们在迭代代码时不必考虑保存日志文件。

          三、 LangChain代理追踪检查提示

          下面,我们将以一个非常简单的 LangChain 代理为例。

          特工的任务是查明欧洲歌曲大赛第一次举行的时间以及距今已经过去了多少年。

          为此,代理将可以使用两种工具:wikipedia在互联网上查找信息和llm-math使用 Python 进行计算。

          我们将首先查看没有任何日志记录的代理。然后我们将使用 LangChain 的详细设置,然后再进行权重和偏差。

          3.1 代码和模型注释

          在此示例中,我们使用 OpenAI 的text-davinci-003模型。也就是说,任何相当强大的模型都适用于这个特定的例子——使用任何更强大的模型(例如,gpt-4)都会浪费资源。

          此外,我们正在使用zero-shot-react-description 代理,它仅根据工具的描述来确定要使用的工具。

          要使这些示例正常工作,需要两个 API 密钥(OpenAI 和权重和偏差)。它们存储在一个.env文件中:

          OPENAI_API_KEY=XYZ

          WANDB_API_KEY=XYZ

          在 Python 中,我们使用dotenv如下方式提供这些:

          from dotenv import load_dotenv

          load_dotenv()

          显而易见,我们使用的是 Python 版本的 LangChain。

          3.2 一个基本的 LangChain 代理

          让我们从一个非常简单的代理开始,无需任何额外的日志记录。最简单的形式,代理看起来像这样:

          from dotenv import load_dotenv
          from langchain.agents import AgentType, initialize_agent, load_tools
          from langchain.llms import OpenAI
          # Load environment variables; especially the OpenAI API key
          load_dotenv()
          llm = OpenAI(model_name='text-davinci-003', temperature=0)
          tools = load_tools(['wikipedia', 'llm-math'], llm=llm)
          agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION)
          output = agent.run('When did the first Eurovision Song Contest take place? What is 2023 minus this year?')
          print(output)
          

          代理完成后,输出如下:

          第一届欧洲歌唱大赛于 1956 年举行,2023 年减去今年为 67 场。

          这是对我们问题的完全正确的答案!然而,我们不知道我们是如何到达那里的!根据您的使用案例和情况,这可能根本不是问题!

          3.3 我们需要更详细的内容

          特别是对于相对简单的代理,verbose只需使用标志即可很好地了解幕后发生的情况。这是代理的稍微修改版本,它将产生更多输出,包括正在使用的提示。

          from dotenv import load_dotenv
          from langchain.agents import AgentType, initialize_agent, load_tools
          from langchain.callbacks import StdOutCallbackHandler
          from langchain.llms import OpenAI
          # Load environment variables; especially the OpenAI API key
          load_dotenv()
          llm = OpenAI(model_name='text-davinci-003', temperature=0, verbose=True)
          tools = load_tools(['wikipedia', 'llm-math'], llm=llm)
          agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
          agent.agent.llm_chain.verbose=True
          callbacks = [StdOutCallbackHandler()]
          agent.run('When did the first Eurovision Song Contest take place? What is 2023 minus this year?', callbacks=callbacks)
          

          正如您所看到的,llm和 都agent将标志verbose设置为True。此外,我们手动将其设置llm_chain为详细并向该方法添加了回调run。

          回调很重要,因为它使我们能够在更深层次上“了解 LLM 申请的各个阶段”(LangChain )。

          3.4 权重和偏差项目详细

          LangChain 代理输出

          在不诉诸更复杂的方法的情况下,这与 LangChain 目前所得到的一样冗长。

          现在我们可以看到,例如,以下输出,其中包括“格式化后的提示”,即填充的提示模板。

          Entering new LLMChain chain…

          Prompt after formatting:

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

          维基百科:维基百科的包装。当您需要回答有关人物、地点、公司、事实、历史事件或其他主题的一般问题时非常有用。输入应该是搜索查询。

          计算器:当您需要回答数学问题时很有用。

          使用以下格式:

          Question:您必须回答的输入问题

          想法:你应该时刻思考该做什么

          操作:要采取的操作,应该是[维基百科,计算器]之一

          动作输入:动作的输入

          观察:行动的结果

          …(这个想法/行动/行动输入/观察可以重复N次)

          想法:我现在知道了最终答案

          Final Answer:原始输入问题的最终答案

          开始!

          Question: When did the first Eurovision Song Contest take place? What is 2023 minus this year?

          Thought:

          迅速的

          这是第一个提示,因为它正在发送给LLM。正如我们所看到的,可用的工具和问题已被注入到提示模板中。

          在许多情况下,这种冗长程度绝对足以理解正在发生的事情。也就是说,输出可能更容易阅读,并且对于更复杂的代理,以这种方式跟踪提示是相当乏味的。

          使用权重和偏差的见解

          当然,这就是权重和偏差终于发挥作用的地方!

          通过对代码进行一些修改,我们可以将代理所做的所有事情记录到 W&B 中以供进一步分析。正如上面提到的,我们可以利用现有的资源WandbCallbackHandler来快速整合LangChain和W&B。

          from datetime import datetime
          from dotenv import load_dotenv
          from langchain.agents import AgentType, initialize_agent, load_tools
          from langchain.callbacks import StdOutCallbackHandler, WandbCallbackHandler
          from langchain.llms import OpenAI
          # Load environment variables; especially the OpenAI API key
          load_dotenv()
          # Weights & Biases
          session_group = datetime.now().strftime("%m.%d.%Y_%H.%M.%S")
          wandb_callback = WandbCallbackHandler(
              job_type="inference",
              project="LangChain Demo",
              group=f"minimal_{session_group}",
              name="llm",
              tags=["demo"],
          )
          callbacks = [StdOutCallbackHandler(), wandb_callback]
          llm = OpenAI(model_name='text-davinci-003', temperature=0)
          tools = load_tools(['wikipedia', 'llm-math'], llm=llm)
          agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION)
          agent.run('When did the first Eurovision Song Contest take place? What is 2023 minus this year?', callbacks=callbacks)
          wandb_callback.flush_tracker(agent, reset=False, finish=True)
          

          这里的关键部分是添加另一个回调(wandb_callback),它将用于跟踪我们的代理。如下所示,启动代理后,数据很快就会在 W&B 平台上可用。配置回调时,我们还可以定义项目名称、标签等。

          【LangChain-04】利用权重和偏差跟踪和检查LangChain代理的提示,在这里插入图片描述,第1张

          权重和偏差项目权重和偏差新数据

          在下面的屏幕截图中,我们可以在顶部看到“session_analysis”(我们的提示),在底部看到“action_records”。使用这些表,我们可以准确地看到代理执行期间发生的情况。

          我们还可以看到 W&B 如何按“步骤”对一切进行排序。因此,例如,对于每个提示,我们可以看到一个“prompt_step”和一个“output_step”,它们与“action_records”中的步骤匹配。

          【LangChain-04】利用权重和偏差跟踪和检查LangChain代理的提示,权重和偏差分析权重和偏差分析,第2张

          有了这些能力,让我们试着理解一下我们是如何一步步得到“第一届欧洲歌唱大赛发生在 1956 年,2023 年减去今年是 67”这样的结果的。

          为此,我们将重点关注已发送给LLM的四个提示以及我们收到的输出。

          为此,我们将重点关注已发送到 LLM 的四个提示以及我们收到的输出。

          提示 1 – 初始提示和工具

          在第一步中,LLM 会提示问题(任务)以及可用的工具及其描述。

          Answer the following questions as best you can. You have access to the following tools:
          Wikipedia: A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, facts, historical events, or other subjects. Input should be a search query.
          Calculator: Useful for when you need to answer questions about math.
          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 [Wikipedia, Calculator]
          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: When did the first Eurovision Song Contest take place? What is 2023 minus this year?
          Thought:
          

          提示

          完成(输出)内容如下:

          [Thought:] I need to find out when the first Eurovision Song Contest took place and then use a calculator to subtract this year from 2023.

          Action: Wikipedia

          Action Input: “Eurovision Song Contest”

          完成/代理输出

          LLM选择wikipedia作为第一个选择的工具,并希望使用“欧洲歌唱大赛歌曲上下文”对其进行查询。

          在“action_records”中,我们可以看到在此之后(步骤8)wikipedia被调用。让我们看一下下一个提示。

          权重和偏差步骤权重和偏见行动记录

          【LangChain-04】利用权重和偏差跟踪和检查LangChain代理的提示,在这里插入图片描述,第3张

          提示 2 – 初始提示和工具

          如下所示,下一个提示包含该工具检索到的维基百科页面(在下面缩短)。这为LLM提供了进步所需的外部信息。

          Answer the following questions as best you can. You have access to the following tools:
          Wikipedia: A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, facts, historical events, or other subjects. Input should be a search query.
          Calculator: Useful for when you need to answer questions about math.
          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 [Wikipedia, Calculator]
          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: When did the first Eurovision Song Contest take place? What is 2023 minus this year?
          Thought: I need to find out when the first Eurovision Song Contest took place and then use a calculator to subtract this year from 2023.
          Action: Wikipedia
          Action Input: "Eurovision Song Contest"
          Observation: Page: Eurovision Song Contest
          Summary: The Eurovision Song Contest (French: Concours Eurovision de la chanson), often known simply as Eurovision, is an international song competition [...]
          Thought:
          

          提示

          LLM承认这些新发现的知识,并确定以下步骤:

          I now know when the first Eurovision Song Contest took place.

          Action: Calculator

          Action Input: 2023 - 1956

          完成/代理输出

          前半段任务已经圆满完成!现在我们需要做一些计算。

          提示 3 – 做数学

          在下一步中,LLM需要llm-math根据之前的“操作输入”创建有效的查询。作为其核心llm-math用途numexpr.evaluate,法学硕士会被提示提供有效的输入。换句话说:我们需要 LLM 根据之前生成的输入为我们的 Python 函数生成正确的输入。

          Translate a math problem into a expression that can be executed using Python's numexpr library. Use the output of running this code to answer the question.
          

          Question: ${Question with math problem.}

          ${single line mathematical expression that solves the problem}
          

          …numexpr.evaluate(text)…

          ${Output of running the code}
          

          Answer: ${Answer}

          Begin.

          Question: What is 37593 * 67?

          37593 * 67
          

          …numexpr.evaluate(“37593 * 67”)…

          2518731
          

          Answer: 2518731

          Question: 2023 - 1956

          提示

          正如我们所看到的,智能体正在使用单次或几次提示,为 LLM 提供了一个示例。 这将产生以下内容,作为计算器的输入(即和):llm-mathnumexpr.evaluate

          2023 - 1956
          

          …numexpr.evaluate(“2023 - 1956”)…

          完成/代理输出

          提示 4 – 将所有东西整合在一起

          如何获得正确的输入,该工具可以执行计算。这样一来,所有信息都会再次传递给 LLM:

          Answer the following questions as best you can. You have access to the following tools:
          Wikipedia: A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, facts, historical events, or other subjects. Input should be a search query.
          Calculator: Useful for when you need to answer questions about math.
          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 [Wikipedia, Calculator]
          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: When did the first Eurovision Song Contest take place? What is 2023 minus this year?
          Thought: I need to find out when the first Eurovision Song Contest took place and then use a calculator to subtract this year from 2023.
          Action: Wikipedia
          Action Input: "Eurovision Song Contest"
          Observation: Page: Eurovision Song Contest
          Summary: The Eurovision Song Contest (French: Concours Eurovision de la chanson), often known simply as Eurovision, is an international song competition [...]
          Thought: I now know when the first Eurovision Song Contest took place.
          Action: Calculator
          Action Input: 2023 - 1956
          Observation: Answer: 67
          Thought:
          

          提示

          有了所有必要的信息,LLM现在可以得出最终答案:

          I now know the final answer.

          Final Answer: The first Eurovision Song Contest took place in 1956 and 2023 minus this year is 67.

          完成/代理输出

          看看我们的W&B“action_records”,我们可以看到这些是执行的最后一步。

          【LangChain-04】利用权重和偏差跟踪和检查LangChain代理的提示,在这里插入图片描述,第4张

          权重和偏差步骤权重和偏见行动记录

          知道 W&B 也会跟踪常规日志也很有帮助。这使我们能够在任何时间点通过标准输出记录整个执行过程。

          【LangChain-04】利用权重和偏差跟踪和检查LangChain代理的提示,在这里插入图片描述,第5张

          权重和偏差日志权重和偏差对数(标准输出)

          如上所述,所有信息也都存储在本地文件夹中。wandb

          四、结论

          (自主)代理是目前生成式人工智能领域最令人兴奋的发展之一!它们有可能使 LLM 可用于执行许多需要访问“世界”的任务。

          也就是说,它们通常被视为无法解释或理解的近乎神奇的黑匣子。然而,在引擎盖下,这些只是巧妙地使用提示工程和强大的 LLM 将各种工具和接口联系在一起的系统。

          能够调查正在执行的提示,不仅可以让我们更好地了解这些系统,还可以更有效地优化和调试它们。

          然而,可能更重要的是,在引擎盖下观察可以让我们批判性地检查这些智能体是如何运作的,并质疑它们如何与世界互动。这一点至关重要,尤其是考虑有关安全、信任和代理的问题。