iT邦幫忙

2025 iThome 鐵人賽

DAY 27
0

一樣沒想法,所以今天乾脆來加強之前的dc機器人,不然他只能用天氣跟地震有點爛,但由於我目前懶得再去找api了,所以這邊乾脆讓他mcp server有的功能就呼叫mcp,沒有就上網查。

先在之前的discord trigger後面新增一個gemini跟code還有一個if node,在第一個gemini node中的prompt這樣寫

{
{
  "contents": [
    {
      "role": "system",
      "parts": [
        {
          "text": "你是一個訊息分類助理,專門判斷使用者訊息應該呼叫的工具。請只輸出 JSON,不要任何文字或程式碼區塊。格式如下:\n{\n  \"tool\": \"weather\" | \"earthquake\" | \"other\",\n  \"location\": <地點名稱或null>\n}\n\n分類規則:\n1. 訊息中包含「天氣」、「氣溫」、「下雨」、「晴天」、「降雨」、「颱風」、「氣象」等字眼,即使是短句,也視為 tool = \"weather\"。\n2. 訊息中包含「地震」、「震度」、「安全」、「災害」等字眼,即使是短句,也視為 tool = \"earthquake\"。\n3. 其他情況視為 tool = \"other\"。\n\n請務必遵守規則,輸出只有 JSON。"
        }
      ]
    },
    {
      "role": "user",
      "parts": [
        {
          "text": "{{ $json.content }}"
        }
      ]
    }
  ],
  "generationConfig": {
    "temperature": 0,
    "maxOutputTokens": 150
  }
}

在code node中放置

const raw = $json["content"]["parts"][0]["text"];

const clean = raw.replace(/```json\s*|```/g, '');

const parsed = JSON.parse(clean);

return { json: parsed };

最後在IF node中放置

{{ $json.tool }}
is equal to
"weather"

OR

{{ $json.tool }}
is equal to
"earthquake"

這樣
https://ithelp.ithome.com.tw/upload/images/20251011/20177920AVfJ1P0lhz.png
最後將true接再原本的ai agent,false直接接gemini,然後再原本的ai agent中把原本的json.context改成{{ $('Discord Trigger').item.json.content }},gemini那邊則是直接放{{ $('Discord Trigger').item.json.content }}
https://ithelp.ithome.com.tw/upload/images/20251011/20177920OTIsByEiTx.png
https://ithelp.ithome.com.tw/upload/images/20251011/20177920R7OJkEtzrO.png
最後加一個SENT discord


上一篇
d26
系列文
這是一個一個一個 Python API 與 Gemini 整合、n8n入門指南27
  1. 23
    d23
  2. 24
    d24
  3. 25
    d25
  4. 26
    d26
  5. 27
    d27
完整目錄
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言