Skip to main content

使用 webhooks 觸發 Flow

您可以使用 Webhook Components來回應外部事件啟動 Flow執行。

使用 Webhook Components, Flow可以直接從外部來源接收資料。然後, Flow可以解析資料並將其傳遞給 Flow中的其他Components,以啟動其他動作,例如呼叫 API、寫入資料庫和與 LLM 聊天。 如果輸入不是有效的 JSON,Webhook Components會將其包裝在 payload 物件中,以便可以接受它作為輸入來觸發 Flow。

Webhook Components提供了一個多功能的入口點,可以讓您的 Flow更加事件驅動並與您的整個應用程式和服務堆疊整合。 例如:

  • 使用 LLM 分析客戶回饋或調查回應的情緒和內容。
  • 從監控系統接收通知,然後根據警報類型和嚴重性觸發自動回應。
  • 與電子商務平台整合以處理訂單和更新庫存。

配置 Webhook Components

要在 Flow中使用 Webhook Components,請執行以下操作:

  1. 在 AgentBuilder 中,開啟您要使用 Webhook Components的 Flow。

  2. Webhook ComponentsParser Components 新增到您的 Flow。

    這兩個Components通常一起配對,因為 Parser Components從 Webhook Components接收的原始承載中提取相關資料。

  3. Webhook Components的 Data 輸出連接到 Parser Components的 Data 輸入。

  4. Parser Components的 Template 欄位中,輸入一個範本來將原始承載解析為結構化文字。

    在範本中,以您在 Prompt Template Components 中定義變數的方式使用承載金鑰的變數。

    例如,假設您期望您的 Webhook Components接收以下 JSON 資料:


    _10
    {
    _10
    "id": "",
    _10
    "name": "",
    _10
    "email": ""
    _10
    }

    然後,您可以在解析器範本中的任何地方 使用大括號 來參考 JSON 金鑰:


    _10
    ID: {id} - Name: {name} - Email: {email}

  5. Parser Components的 Parsed Text 輸出連接到 Flow中下一個邏輯Components,例如 Chat Input Components。

    如果您只想測試 WebhookParser Components,您可以將 Parsed Text 輸出直接連接到 Chat Output Components的 Text 輸入。然後,您可以在執行 Flow後在 Playground 中看到解析的資料。

  6. Webhook Components的 Endpoint 欄位複製您將用來傳送資料到 Webhook Components並觸發 Flow的 API EndPoint。

    或者,要取得完整的 POST /v1/webhook/$FLOW_ID 程式碼片段,請開啟 Flow的 API 存取窗格,然後按一下 Webhook curl 標籤。 您也可以修改 Webhook Components的 curl 欄位中的預設 curl 命令。 如果此欄位預設不可見,請按一下 Webhook Components,然後在 Components的標題選單 中按一下 控制項

  7. 傳送帶有 data 的 POST 請求到 Flow的 webhook EndPoint 來觸發 Flow。

    以下範例傳送包含 idnameemail 字串的承載:


    _10
    curl -X POST "http://localhost:7860/api/v1/webhook/FLOW_ID" \
    _10
    -H "Content-Type: application/json" \
    _10
    -H "x-api-key: LANGFLOW_API_KEY" \
    _10
    -d '{"id": "12345", "name": "alex", "email": "alex@email.com"}'

    成功的回應表示 AgentBuilder 啟動了 Flow。 回應不包括整個 Flow的輸出,只表示 Flow已啟動。


    _10
    {
    _10
    "message": "Task started in the background",
    _10
    "status": "in progress"
    _10
    }

  8. 要檢視 Flow最新的解析承載,請按一下 Parser Components,然後按一下 檢查輸出。 對於前面的範例,解析的承載會是像 ID: 12345 - Name: alex - Email: alex@email.com 這樣的字串。

使用 Composio webhooks 觸發 Flow

通常,您不會手動觸發 Webhook Components。 要學習如何使用來自外部應用程式的承載觸發 Flow,請參閱影片教學 How to Use Webhooks in AgentBuilder

疑難排解具有 Webhook Components的 Flow

使用以下資訊來幫助解決 Webhook Components可能發生的常見問題。

驗證 Webhook Components接收的資料

要疑難排解具有 Webhook Components的 Flow並驗證Components是否正在接收資料,您可以建立一個只輸出解析承載的小 Flow:

  1. 使用 WebhookParserChat Output Components建立 Flow。

  2. Webhook Components的 Data 輸出連接到 Parser Components的 Data 輸入。

  3. Parser Components的 Parsed Text 輸出連接到 Chat Output Components的 Text 輸入。

  4. 編輯 Parser Components以將 Mode 設定為 Stringify

    此模式將 Webhook Components接收的資料作為字串傳遞,由 Chat Output Components列印。

  5. 按一下 分享,選取 API 存取,然後複製 Webhook curl 程式碼片段。

  6. 選用:如果您想要傳遞不同的承載,請編輯程式碼片段中的 data

  7. 傳送 POST 請求來觸發 Flow。

  8. 按一下 Playground 以驗證 Chat Output Components是否列印了來自您的 POST 請求的 JSON 資料。

Parser Components建置失敗

如果 Parser Components沒有從 Webhook Components接收資料或傳入資料有問題,Parser Components可能會建置失敗。

如果發生這種情況,請嘗試將 Parser Components的 Mode 變更為 Stringify,以便Components將解析的承載作為單一字串輸出。 然後,您可以檢查字串輸出並疑難排解您的解析範本,或以字串形式處理解析的資料。

另請參閱

Search