ข้ามไปที่เนื้อหา

Messaging Hub

Tutorial นี้พาไปดู examples/messaging_hub/smart_inbox.py — สคริปต์ที่เชื่อมต่อ OpenJarvis กับแพลตฟอร์มส่งข้อความ คัดกรองข้อความที่เข้ามาตามความสำคัญ ร่างคำตอบที่เข้าใจบริบท และสร้างสรุปประจำวัน สาธิตการเชื่อมต่อ channel, ผลลัพธ์แบบมีโครงสร้างจากเอเจนต์ และการรวมข้อมูลด้วย memory ข้ามหลายข้อความ

สิ่งที่ต้องมีก่อน

  • Python 3.10 ขึ้นไป
  • ติดตั้ง OpenJarvis แล้ว: uv sync --extra dev จาก root ของ repository
  • มี inference engine กำลังรันอยู่ (Ollama พร้อม qwen3:8b ที่ดึงมาแล้ว หรือ cloud API key)
  • สำหรับโหมด channel จริง: credential เฉพาะของ channel นั้นๆ (ดู การตั้งค่า Channel จริง)

Quick Start: โหมด Demo

โหมด demo ประมวลผลข้อความตัวอย่าง 5 ข้อความ โดยไม่ต้องตั้งค่า channel หรือ credential ใดๆ เป็นวิธีที่เร็วที่สุดในการดู pipeline การคัดกรองทำงานจริง:

Terminal
python examples/messaging_hub/smart_inbox.py --demo

ผลลัพธ์ที่คาดหวัง (แบบย่อ):

Smart Inbox — Demo Mode
Model: qwen3:8b  |  Engine: ollama
============================================================
Processing 5 messages...

  [1/5] Classifying: URGENT: Server is down in production...
           -> URGENT
  [2/5] Classifying: Hey, just wanted to share this interest...
           -> FYI
  [3/5] Classifying: Can you review my PR #42 by end of day...
           -> ACTION_REQUIRED
  [4/5] Classifying: Meeting reminder: Team standup at 10am...
           -> FYI
  [5/5] Classifying: Buy now! Limited time offer on premium...
           -> SPAM

  #   Category          Message                             Reply
  ---------------------------------------------------------------
  1   URGENT            URGENT: Server is down...           On it — escalating now.
  2   FYI               Hey, just wanted to share...        Thanks for sharing!
  3   ACTION_REQUIRED   Can you review my PR #42...         Will review before EOD.
  4   FYI               Meeting reminder: Team standup...   N/A
  5   SPAM              Buy now! Limited time offer...      N/A

Generating end-of-day summary...

Override โมเดลหรือ engine:

Terminal
python examples/messaging_hub/smart_inbox.py --demo --model gpt-4o --engine cloud

การจำแนกข้อความทำงานอย่างไร

แต่ละข้อความที่เข้ามาจะผ่าน prompt แบบมีโครงสร้างที่ให้เอเจนต์สร้างผลลัพธ์เป็นสองฟิลด์เท่านั้น — หมวดหมู่และคำตอบ — ในรูปแบบที่ parse ได้ จากนั้นสคริปต์จะดึงฟิลด์เหล่านั้นออกมาและสร้างตารางคัดกรอง

graph TD
    A[Incoming message] --> B[OrchestratorAgent]
    B --> C{think tool: internal reasoning}
    C --> D{memory_store: persist context}
    D --> E[Structured response]
    E --> F{Parse CATEGORY and REPLY}
    F -->|URGENT| G[Flag for immediate attention]
    F -->|ACTION_REQUIRED| H[Add to action list]
    F -->|FYI| I[Log for reference]
    F -->|SPAM| J[Discard]
    G --> K[Triage table]
    H --> K
    I --> K
    J --> K
    K --> L[memory_search: cross-reference]
    L --> M[End-of-day summary]

หลังจากประมวลผลข้อความทั้งหมดแล้ว การเรียก orchestrator ครั้งที่สองจะใช้ memory_search เพื่อดึง log การคัดกรองที่เก็บไว้ และสร้างสรุปแบบจัดกลุ่ม พร้อมเน้นรายการที่ยังต้องดำเนินการอยู่

Prompt สำหรับการจำแนก

เอเจนต์ได้รับ prompt แบบมีโครงสร้างที่ระบุรูปแบบผลลัพธ์อย่างชัดเจน วิธีนี้ทำให้คำตอบ parse ได้อย่างน่าเชื่อถือโดยไม่ต้องใช้ schema ที่ซับซ้อน:

examples/messaging_hub/smart_inbox.py
CLASSIFICATION_PROMPT = (
    "You are a smart inbox assistant. Classify the following message into "
    "exactly one category: URGENT, ACTION_REQUIRED, FYI, or SPAM.\n"
    "Then draft a short reply if appropriate (not for SPAM).\n\n"
    "Respond in this exact format:\n"
    "CATEGORY: <category>\n"
    "REPLY: <reply or N/A>\n\n"
    "Message:\n{message}"
)

tool think ให้เอเจนต์ให้เหตุผลภายในก่อนตัดสินใจเลือกหมวดหมู่ และ memory_store เก็บผลการจำแนกแต่ละครั้งไว้ เพื่อให้ prompt สรุปสามารถอ้างอิง log การคัดกรองทั้งหมดได้

การตั้งค่า Channel จริง

  1. เพิ่ม Slack MCP server เข้าไปในการตั้งค่าของคุณ:

    Terminal
    jarvis add slack
    
  2. ตั้งค่า credential ของคุณใน .env (อยู่ใน gitignore):

    .env
    SLACK_BOT_TOKEN=xoxb-...
    SLACK_APP_TOKEN=xapp-...
    
  3. เชิญ bot เข้า Slack channel เป้าหมายในการตั้งค่า Slack workspace

  4. รันสคริปต์ในโหมด live channel:

    Terminal
    python examples/messaging_hub/smart_inbox.py --channel slack
    
  1. ตรวจสอบว่าติดตั้ง Node.js 22 ขึ้นไปแล้ว

  2. ตั้งค่า WhatsApp Baileys bridge ดูขั้นตอนตั้งค่าแบบเต็มที่ เอกสาร channel

  3. เริ่ม bridge — มันจะแสดง QR code สแกนด้วยแอป WhatsApp บนมือถือเพื่อยืนยันตัวตน

  4. รันสคริปต์:

    Terminal
    python examples/messaging_hub/smart_inbox.py --channel whatsapp
    

OpenJarvis รองรับ LINE, Viber, Mastodon, Rocket.Chat, Zulip, XMPP, Twitch, Nostr และอื่นๆ ดูรายการ channel ที่มีทั้งหมด:

Terminal
jarvis channel list
jarvis channel status

แต่ละ channel ต้องการ environment variable ของตัวเอง รัน jarvis add <channel> ในกรณีที่รองรับ เพื่อสร้าง template การตั้งค่าอัตโนมัติ

โหมด Live Channel

โหมด live channel ต้องมี credential ของ channel และระบบย่อยของ channel นั้นๆ กำลังรันอยู่ ใช้ --demo เพื่อตรวจสอบ logic การคัดกรองก่อนเชื่อมต่อกับ channel จริง

การตั้งค่า Channel ผ่าน TOML

recipe messaging.toml ใน examples/messaging_hub/ เก็บค่าเริ่มต้นของเอเจนต์และ channel ไว้แบบ declarative:

examples/messaging_hub/messaging.toml
[channel]
default = "slack"

[agent]
type = "orchestrator"
max_turns = 5
temperature = 0.3
tools = ["think", "memory_store", "memory_search"]

คุณสามารถโหลด recipe นี้เชิงโปรแกรมได้:

Loading the messaging recipe
from openjarvis.recipes import load_recipe
from openjarvis import SystemBuilder

recipe = load_recipe("examples/messaging_hub/messaging.toml")
system = SystemBuilder(**recipe.to_builder_kwargs()).build()
response = system.ask(CLASSIFICATION_PROMPT.format(message=incoming_message))
system.close()

เพิ่มกฎการคัดกรองแบบกำหนดเอง

ขยายหมวดหมู่การจำแนกได้โดยแก้ CLASSIFICATION_PROMPT ตัวอย่างเช่น เพิ่มหมวดหมู่ FOLLOW_UP สำหรับข้อความที่ต้องตอบภายใน 48 ชั่วโมง:

Custom classification prompt
CLASSIFICATION_PROMPT = (
    "Classify into: URGENT, ACTION_REQUIRED, FOLLOW_UP, FYI, or SPAM.\n"
    "Then draft a short reply if appropriate (not for SPAM).\n\n"
    "Respond in this exact format:\n"
    "CATEGORY: <category>\n"
    "REPLY: <reply or N/A>\n\n"
    "Message:\n{message}"
)

คุณยังเพิ่มกฎเฉพาะโดเมนใน system prompt ผ่าน messaging.toml ได้ด้วย — เช่น จัดเส้นทางข้อความที่มีคำว่า "P0" หรือ "incident" ไปที่ URGENT ทันทีไม่ว่าจะใช้ถ้อยคำแบบใด

ตั้งเวลาสรุปประจำวัน

หลังจากประมวลผลข้อความทั้งหมดแล้ว การเรียกสรุปประจำวันจะรันทันทีในสคริปต์ สำหรับการใช้งานจริง ให้ตั้งเวลาแยกต่างหากผ่าน scheduler ของ OpenJarvis:

Terminal
jarvis scheduler create "Daily inbox summary" \
    --type cron --value "0 17 * * *"

หรือใช้รูปแบบ operator recipe เพื่อรันเอเจนต์คัดกรองแบบต่อเนื่องตามตารางเวลา ดูตัวอย่าง operator recipe สำเร็จรูปได้ที่ src/openjarvis/recipes/data/operators/

ดูเพิ่มเติม