目標:建立一個可互動的 Web App,讓使用者透過 Gradio 與 n8n 中的 LLM Agent 進行即時對話。
概念:
pip install gradio requests
/task-agent
可以正常接收請求建立一個簡單的互動介面,包含輸入框與送出按鈕,將使用者輸入的文字傳送到 n8n Webhook。
import gradio as gr
import requests
def chat_with_agent(user_input):
response = requests.post(
"http://localhost:5678/webhook/task-agent",
json={"text": user_input, "metadata": {"user": "Test"}}
)
data = response.json()
return data.get("summary", "Agent 沒有回應")
iface = gr.Interface(
fn=chat_with_agent,
inputs="text",
outputs="text",
title="LLM Agent Web App",
description="輸入文字後,Agent 將自動分析並回覆摘要或回答。"
)
iface.launch()
啟動 Gradio:
python gradio_app.py
瀏覽器開啟預設頁面:http://127.0.0.1:7860
在文字框中輸入問題或內容並送出
預期顯示 LLM Agent 回覆的摘要或答案
問題 | 解決方式 |
---|---|
Agent 無回應 | 檢查 Webhook URL 是否正確,Workflow 是否啟用 |
Gradio 顯示空白 | 檢查回傳的 JSON key 名稱是否為 summary |
Docker 端口衝突 | 確認 n8n 與 Gradio 運行於不同 port |
LLM 回傳結構錯誤 | 檢查 API Response 結構與 Function 節點 JSON 路徑 |