广州制作外贸网站,asp.net中文官方网站,上谷网络网站建设,中山市做网站实力利用权重和偏差跟踪和检查LangChain代理的提示
一、说明
考虑到#xff08;生成#xff09;人工智能空间#xff0c;#xff08;自主#xff09;代理现在无处不在#xff01;除了更强大且幸运的是开放的大型语言模型#xff08;LLM#xff09;之外#xff0c;LangCh… 利用权重和偏差跟踪和检查LangChain代理的提示
一、说明
考虑到生成人工智能空间自主代理现在无处不在除了更强大且幸运的是开放的大型语言模型LLM之外LangChain已成为开发人工智能驱动的应用程序和代理的主要工具。
Langchain 是一个功能强大且功能丰富的开源框架适用于LLM。正如我在另一篇文章中所演示的它可以用于使用LLM轻松构建问答系统。然而浪链LangChain的另一个关键特性是能够创建和使用上面提到的这些所谓的代理。简而言之它们是结合了LLM、其他工具和接口例如浏览器或 API以及内存来解决复杂任务的应用程序。
基于LLM的代理通常使用一系列提示基于提示模板来检索必要的数据和信息并准备数据作为他们可以使用的工具的输入见下文。当然LLM还用于首先决定使用哪些工具来完成给定的任务。例如使用Yao等人提出的ReAct框架来完成自主选择工具。 2022。
当使用和/或开发自主代理时了解代理如何使用 LLM 至关重要即正在执行哪些提示及其输出。了解游戏中的提示可以提高透明度和可解释性对于调试至关重要。
此外考虑教育仔细考虑代理生成和使用的提示对于理解决策过程以及与各种工具以及所涉及的数据的交互是必要的。特别是在结合使用多个工具和数据源时了解各个步骤是成为此类系统的合格用户的关键组成部分。
因此本文演示了如何跟踪和检查LangChain代理使用的提示。在简单介绍之后我们将首先了解如何仅使用 LangChain 检查提示然后再使用权重和偏差(WB) 来更好、更清晰地了解正在发生的情况。
二、一些背景信息
在查看提示之前我将提供一些有关 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(temperature0)
tools [reverse_string]
agent initialize_agent(tools, llm, agentAgentType.ZERO_SHOT_REACT_DESCRIPTION, verboseTrue)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 questionBegin!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 questionBegin!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 平台可免费用于个人项目。
虽然基于云的平台有许多令人兴奋的功能也用于提示工程但我们将使用它来跟踪我们的代理并显示其提示。
简而言之我们可以将 WB使用该wandb库添加到现有的 Python 脚本中并开始详细记录发生的情况。幸运的是WB 和 LangChain 之间已经有了我们可以利用的强大集成。
默认情况下所有信息都将存储在本地wandb文件夹以及 WB 平台上的云端。这也非常有用因为我们在迭代代码时不必考虑保存日志文件。
三、 LangChain代理追踪检查提示
下面我们将以一个非常简单的 LangChain 代理为例。
特工的任务是查明欧洲歌曲大赛第一次举行的时间以及距今已经过去了多少年。
为此代理将可以使用两种工具wikipedia在互联网上查找信息和llm-math使用 Python 进行计算。
我们将首先查看没有任何日志记录的代理。然后我们将使用 LangChain 的详细设置然后再进行权重和偏差。
3.1 代码和模型注释
在此示例中我们使用 OpenAI 的text-davinci-003模型。也就是说任何相当强大的模型都适用于这个特定的例子——使用任何更强大的模型例如gpt-4都会浪费资源。
此外我们正在使用zero-shot-react-description 代理它仅根据工具的描述来确定要使用的工具。
要使这些示例正常工作需要两个 API 密钥OpenAI 和权重和偏差。它们存储在一个.env文件中
OPENAI_API_KEYXYZ WANDB_API_KEYXYZ 在 Python 中我们使用dotenv如下方式提供这些
from dotenv import load_dotenv
load_dotenv() 显而易见我们使用的是 Python 版本的 LangChain。
3.2 一个基本的 LangChain 代理
让我们从一个非常简单的代理开始无需任何额外的日志记录。最简单的形式代理看起来像这样
from dotenv import load_dotenvfrom 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_nametext-davinci-003, temperature0)
tools load_tools([wikipedia, llm-math], llmllm)
agent initialize_agent(tools, llm, agentAgentType.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_dotenvfrom 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_nametext-davinci-003, temperature0, verboseTrue)
tools load_tools([wikipedia, llm-math], llmllm)
agent initialize_agent(tools, llm, agentAgentType.ZERO_SHOT_REACT_DESCRIPTION, verboseTrue)
agent.agent.llm_chain.verboseTruecallbacks [StdOutCallbackHandler()]agent.run(When did the first Eurovision Song Contest take place? What is 2023 minus this year?, callbackscallbacks)正如您所看到的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。正如我们所看到的可用的工具和问题已被注入到提示模板中。
在许多情况下这种冗长程度绝对足以理解正在发生的事情。也就是说输出可能更容易阅读并且对于更复杂的代理以这种方式跟踪提示是相当乏味的。
使用权重和偏差的见解 当然这就是权重和偏差终于发挥作用的地方
通过对代码进行一些修改我们可以将代理所做的所有事情记录到 WB 中以供进一步分析。正如上面提到的我们可以利用现有的资源WandbCallbackHandler来快速整合LangChain和WB。
from datetime import datetimefrom dotenv import load_dotenvfrom 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_typeinference,projectLangChain Demo,groupfminimal_{session_group},namellm,tags[demo],
)callbacks [StdOutCallbackHandler(), wandb_callback]llm OpenAI(model_nametext-davinci-003, temperature0)
tools load_tools([wikipedia, llm-math], llmllm)
agent initialize_agent(tools, llm, agentAgentType.ZERO_SHOT_REACT_DESCRIPTION)agent.run(When did the first Eurovision Song Contest take place? What is 2023 minus this year?, callbackscallbacks)wandb_callback.flush_tracker(agent, resetFalse, finishTrue)这里的关键部分是添加另一个回调wandb_callback它将用于跟踪我们的代理。如下所示启动代理后数据很快就会在 WB 平台上可用。配置回调时我们还可以定义项目名称、标签等。
权重和偏差项目权重和偏差新数据
在下面的屏幕截图中我们可以在顶部看到“session_analysis”我们的提示在底部看到“action_records”。使用这些表我们可以准确地看到代理执行期间发生的情况。
我们还可以看到 WB 如何按“步骤”对一切进行排序。因此例如对于每个提示我们可以看到一个“prompt_step”和一个“output_step”它们与“action_records”中的步骤匹配。
有了这些能力让我们试着理解一下我们是如何一步步得到“第一届欧洲歌唱大赛发生在 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 questionBegin!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”中我们可以看到在此之后步骤8wikipedia被调用。让我们看一下下一个提示。
权重和偏差步骤权重和偏见行动记录
提示 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 questionBegin!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 Pythons 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”)…
2518731Answer: 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 questionBegin!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. 完成/代理输出
看看我们的WB“action_records”我们可以看到这些是执行的最后一步。
权重和偏差步骤权重和偏见行动记录
知道 WB 也会跟踪常规日志也很有帮助。这使我们能够在任何时间点通过标准输出记录整个执行过程。
权重和偏差日志权重和偏差对数标准输出
如上所述所有信息也都存储在本地文件夹中。wandb
四、结论
自主代理是目前生成式人工智能领域最令人兴奋的发展之一它们有可能使 LLM 可用于执行许多需要访问“世界”的任务。
也就是说它们通常被视为无法解释或理解的近乎神奇的黑匣子。然而在引擎盖下这些只是巧妙地使用提示工程和强大的 LLM 将各种工具和接口联系在一起的系统。
能够调查正在执行的提示不仅可以让我们更好地了解这些系统还可以更有效地优化和调试它们。
然而可能更重要的是在引擎盖下观察可以让我们批判性地检查这些智能体是如何运作的并质疑它们如何与世界互动。这一点至关重要尤其是考虑有关安全、信任和代理的问题。