LangChain智能体开发:为规则配置 Webhook 通知

LangChain智能体开发:为规则配置 Webhook 通知 当你在自动化操作中添加 webhook URL 时每当定义的规则与任何新的运行匹配时我们将向你的 webhook 端点发送一个 POST 请求。Webhook 有效负载我们发送到你的 webhook 端点的有效负载包含rule_id这是发送此有效负载的自动化的 IDstart_time和end_time这些是我们在其中找到匹配运行的时间边界runs这是一个运行数组每个运行都是一个字典。如果你需要有关每个运行的更多信息我们建议在你的端点中使用我们的 SDK 从我们的 API 中获取它。feedback_stats这是一个包含运行的反馈统计信息的字典。此字段的示例有效负载如下所示。feedback_stats: { about_langchain: { n: 1, avg: 0.0, show_feedback_arrow: true, values: {} }, category: { n: 0, avg: null, show_feedback_arrow: true, values: { CONCEPTUAL: 1 } }, user_score: { n: 2, avg: 0.0, show_feedback_arrow: false, values: {} }, vagueness: { n: 1, avg: 0.0, show_feedback_arrow: true, values: {} } }这是我们发送到你的 webhook 端点的整个有效负载的示例{ rule_id: d75d7417-0c57-4655-88fe-1db3cda3a47a, start_time: 2024-04-05T01:28:54.73449100:00, end_time: 2024-04-05T01:28:56.49256300:00, runs: [ { status: success, is_root: true, trace_id: 6ab80f10-d79c-4fa2-b441-922ed6feb630, dotted_order: 20230505T051324571809Z6ab80f10-d79c-4fa2-b441-922ed6feb630, run_type: tool, modified_at: 2024-04-05T01:28:54.145062, tenant_id: 2ebda79f-2946-4491-a9ad-d642f49e0815, end_time: 2024-04-05T01:28:54.085649, name: Search, start_time: 2024-04-05T01:28:54.085646, id: 6ab80f10-d79c-4fa2-b441-922ed6feb630, session_id: 6a3be6a2-9a8c-4fc8-b4c6-a8983b286cc5, parent_run_ids: [], child_run_ids: null, direct_child_run_ids: null, total_tokens: 0, completion_tokens: 0, prompt_tokens: 0, total_cost: null, completion_cost: null, prompt_cost: null, first_token_time: null, app_path: /o/2ebda79f-2946-4491-a9ad-d642f49e0815/projects/p/6a3be6a2-9a8c-4fc8-b4c6-a8983b286cc5/r/6ab80f10-d79c-4fa2-b441-922ed6feb630?trace_id6ab80f10-d79c-4fa2-b441-922ed6feb630start_time2023-05-05T05:13:24.571809, in_dataset: false, last_queued_at: null, inputs: null, inputs_s3_urls: null, outputs: null, outputs_s3_urls: null, extra: null, events: null, feedback_stats: null, serialized: null, share_token: null } ] }安全我们强烈建议你在 webhook URL 中添加一个秘密查询字符串参数并在任何传入请求上进行验证。这确保了如果有人发现你的 webhook URL你可以区分这些调用与真实的 webhook 通知。一个例子是https://api.example.com/langsmith_webhook?secret38ee77617c3a489ab6e871fbeb2ec87dWebhook 自定义 HTTP 标头如果你想使用任何特定的标头发送 webhook可以针对每个 URL 进行配置。要设置此项请单击 URL 字段旁边的Headers选项并添加你的标头。Webhook 传递在将事件传递到你的 webhook 端点时我们遵循以下准则如果无法连接到你的端点我们将重试传输连接最多 2 次然后声明传递失败。如果你的端点回复时间超过 5 秒我们将声明传递失败并且不进行重试。如果你的端点在 5 秒内返回 5xx 状态代码我们将以指数退避的方式重试最多 2 次。如果你的端点返回 4xx 状态代码我们将声明传递失败并且不进行重试。你的端点在正文中返回的任何内容都将被忽略使用 Modal 的示例设置作为一个设置示例我们将使用 Modal。Modal 提供用于推理和微调的自动缩放 GPU用于代码代理的安全容器化以及无服务器 Python Web 端点。我们将重点介绍这里的 Web 端点。首先创建一个 Modal 帐户。然后在本地安装 Modal SDKpip install modal要完成帐户设置请运行命令modal setup并按照说明操作密钥接下来你需要在 Modal 中设置一些密钥。首先LangSmith 需要通过传递一个密钥来验证 Modal。最简单的方法是在查询参数中传递一个密钥。为了验证此密钥我们需要在Modal中添加一个密钥进行验证。我们将通过创建一个 Modal 密钥来执行此操作。你可以在 此处 找到有关密钥的说明。为此让我们将我们的密钥称为ls-webhook并使其设置一个名为LS_WEBHOOK的环境变量。我们还可以设置一个 LangSmith 密钥 - 幸运的是已经有一个集成模板了服务之后你可以创建一个 Python 文件作为你的端点。下面是一个示例其中包含解释正在发生的事情的注释from fastapi import HTTPException, status, Request, Query from modal import Secret, Stub, web_endpoint, Image stub Stub(auth-example, imageImage.debian_slim().pip_install(langsmith)) stub.function( secrets[Secret.from_name(ls-webhook), Secret.from_name(my-langsmith-secret)] ) # We want this to be a POST endpoint since we will post data here web_endpoint(methodPOST) # We set up a secret query parameter def f(data: dict, secret: str Query(...)): # You can import dependencies you dont have locally inside Modal functions from langsmith import Client # First, we validate the secret key we pass import os if secret ! os.environ[LS_WEBHOOK]: raise HTTPException( status_codestatus.HTTP_401_UNAUTHORIZED, detailIncorrect bearer token, headers{WWW-Authenticate: Bearer}, ) # This is where we put the logic for what should happen inside this webhook ls_client Client() runs data[runs] ids [r[id] for r in runs] feedback list(ls_client.list_feedback(run_idsids)) for r, f in zip(runs, feedback): try: ls_client.create_example( inputsr[inputs], outputs{output: f.correction}, dataset_nameclassifier-github-issues, ) except Exception: raise ValueError(f{r} and {f}) # Function body return success!现在我们可以使用modal deploy ...轻松部署它请参阅文档 此处。你现在应该得到类似以下内容✓ Created objects. ├── Created mount /Users/harrisonchase/workplace/langsmith-docs/example-webhook.py ├── Created mount PythonPackage:langsmith └── Created f https://hwchase17--auth-example-f.modal.run ✓ App deployed! View Deployment: https://modal.com/apps/hwchase17/auth-example重要的是要记住https://hwchase17--auth-example-f.modal.run- 我们创建的函数。注意这不是最终部署 URL请确保不要意外使用它。连接它现在我们可以获取我们创建的函数 URL并将其添加为 webhook。我们必须记住也要将秘密密钥作为查询参数传递。综上所述它应该如下所示https://hwchase17--auth-example-f-dev.modal.run?secret{SECRET}将{SECRET}替换为你创建的用于访问 Modal 服务的密钥。《AI提示工程必知必会》为读者提供了丰富的AI提示工程知识与实战技能。《AI提示工程必知必会》主要内容包括各类提示词的应用如问答式、指令式、状态类、建议式、安全类和感谢类提示词以及如何通过实战演练掌握提示词的使用技巧使用提示词进行文本摘要、改写重述、语法纠错、机器翻译等语言处理任务以及在数据挖掘、程序开发等领域的应用AI在绘画创作上的应用百度文心一言和阿里通义大模型这两大智能平台的特性与功能以及市场调研中提示词的实战应用。通过阅读《AI提示工程必知必会》读者可掌握如何有效利用AI提示工程提升工作效率创新工作流程并在职场中脱颖而出。