核心提示词管理功能Webhook

Webhook 和 Slack 集成

使用 webhook,在 Litefuse 中 prompt 版本被创建、更新或删除时实时接收通知。这样你可以触发 CI/CD 流水线、同步 prompt 目录或审计变更,无需轮询 API。

为什么使用 webhook?

  • 生产环境监控:当 production prompt 被更新时及时获取告警
  • 团队协同:让所有人都了解 prompt 的变更
  • 同步:把 prompt 目录与其他系统同步

快速开始

进入 Prompts 并点击 Automations

选择事件

点击 Create Automation

选择事件

选择要监听的事件。

选择事件

选择应该触发 webhook 的 prompt 版本动作:

  • Created: 新增了一个版本。
  • Updated: 标签或 tag 发生变更(会触发两个事件:一个对应获得标签/tag 的版本,一个对应失去的版本)。
  • Deleted: 某个版本被移除。

(可选)过滤为只对特定 prompt 触发。

配置请求

配置请求

  • URL:接受 POST 请求的 HTTPS 端点。
  • Headers:默认 header 包括:
    • Content-Type: application/json
    • User-Agent: Langfuse/1.0
    • x-langfuse-signature: <sig>(关于 HMAC 签名校验,参见下方说明)
  • 如有需要可添加自定义静态 header。

检视 payload

你的端点会收到如下 JSON body:

webhook-payload.json
{
  "id": "550e8400-e29b-41d4-a716-446655440000",
  "timestamp": "2024-07-10T10:30:00Z",
  "type": "prompt-version",
  "apiVersion": "v1",
  "action": "created",
  "prompt": {
    "id": "prompt_abc123",
    "name": "movie-critic",
    "version": 3,
    "projectId": "xyz789",
    "labels": ["production", "latest"],
    "prompt": "As a {{criticLevel}} movie critic, rate {{movie}} out of 10.",
    "type": "text",
    "config": { "key": "value" },
    "commitMessage": "Improved critic persona",
    "tags": ["entertainment"],
    "createdAt": "2024-07-10T10:30:00Z",
    "updatedAt": "2024-07-10T10:30:00Z"
  }
}

确认接收

你的处理函数必须:

  • 返回 HTTP 2xx 状态码以确认收到。
  • 保持幂等 —— Litefuse 会以指数退避方式重试,直到收到成功响应。

校验真实性(推荐)

每次请求都会在 x-langfuse-signature 中携带一个 HMAC SHA-256 签名。 在创建 webhook 时获取 secret(之后也可以重新生成)。

import hmac
import hashlib
from typing import Optional
 
 
def verify_langfuse_signature(
    raw_body: str,
    signature_header: str,
    secret: str,
) -> bool:
    """
    Validate a Langfuse webhook/event signature.
 
    Parameters
    ----------
    raw_body : str
        The request body exactly as received (no decoding or reformatting).
    signature_header : str
        The value of the `Langfuse-Signature` header, e.g. "t=1720701136,s=0123abcd...".
    secret : str
        Your Langfuse signing secret.
 
    Returns
    -------
    bool
        True if the signature is valid, otherwise False.
    """
    # Split "t=timestamp,s=signature" into the two expected key/value chunks
    try:
        ts_pair, sig_pair = signature_header.split(",", 1)
    except ValueError:  # wrong format / missing comma
        return False
 
    # Extract values (everything after the first "=")
    if "=" not in ts_pair or "=" not in sig_pair:
        return False
    timestamp = ts_pair.split("=", 1)[1]
    received_sig_hex = sig_pair.split("=", 1)[1]
 
    # Recreate the message and compute the expected HMAC-SHA256 hex digest
    message = f"{timestamp}.{raw_body}".encode("utf-8")
    expected_sig_hex = hmac.new(
        secret.encode("utf-8"), message, hashlib.sha256
    ).hexdigest()
 
    # Use constant-time comparison on the *decoded* byte strings
    try:
        return hmac.compare_digest(
            bytes.fromhex(received_sig_hex), bytes.fromhex(expected_sig_hex)
        )
    except ValueError:  # received_sig_hex isn't valid hex
        return False
这个页面对你有帮助吗?