Điều phối Multi-Agent
Điều phối nhiều LLM agent sử dụng mind, tác vụ và chat Synapse chung.
Điều phối Multi-Agent
Khi bạn có nhiều LLM agent làm việc trên các tác vụ liên quan, Synapse cung cấp lớp điều phối — bộ nhớ chung, gán tác vụ và chat bất đồng bộ.
Mẫu
Mẫu 1: Mind chung (Nguồn sự thật duy nhất)
Tất cả agent chia sẻ một Mind Key. Chúng đọc/ghi cùng kho bộ nhớ.
┌──────────┐ ┌──────────┐ ┌──────────┐
│ Agent A │ │ Agent B │ │ Agent C │
└────┬─────┘ └────┬─────┘ └────┬─────┘
│ │ │
└─────────────┼─────────────┘
▼
┌──────────────┐
│ Shared Mind │
│ (one key) │
└──────────────┘Trường hợp sử dụng: Nhóm nhỏ agent làm việc trên một dự án.
Thiết lập:
# All agents use the same Mind Key
export SYNAPSE_MIND_KEY=mk_shared_key...Điều phối qua tác vụ:
# Agent A creates a task
create_task("Review PR #42", priority="high")
# Agent B picks it up
tasks = list_tasks(status="pending")
if tasks:
task = tasks[0]
update_task(task["id"], status="in_progress")
# ... do work ...
update_task(task["id"], status="done")Mẫu 2: Mind chuyên biệt (Ngữ cảnh tách biệt)
Mỗi agent có mind riêng. Chúng giao tiếp qua một mind "điều phối" chung.
┌──────────┐ ┌──────────┐ ┌──────────┐
│ Coder │ │ Reviewer │ │ Deployer │
│ Agent │ │ Agent │ │ Agent │
└────┬─────┘ └────┬─────┘ └────┬─────┘
│ │ │
▼ ▼ ▼
┌─────────┐ ┌─────────┐ ┌─────────┐
│ Mind C │ │ Mind R │ │ Mind D │
└─────────┘ └─────────┘ └─────────┘
│ │ │
└─────────────┼─────────────┘
▼
┌──────────────────┐
│ Coordination Mind│
│ (shared) │
└──────────────────┘Trường hợp sử dụng: Agent với chuyên môn khác nhau (lập trình, review, triển khai).
Thiết lập:
# Coder agent
SYNAPSE_MIND_KEY=mk_coder... MCP_TRANSPORT=stdio npx synapse-mcp-api@latest
# Reviewer agent
SYNAPSE_MIND_KEY=mk_reviewer... MCP_TRANSPORT=stdio npx synapse-mcp-api@latest
# Deployer agent
SYNAPSE_MIND_KEY=mk_deployer... MCP_TRANSPORT=stdio npx synapse-mcp-api@latestĐiều phối qua mind chung:
# Coder stores "ready for review"
COORDINATION_KEY = "mk_coordination..."
requests.post(f"{URL}/memory",
headers={"Authorization": f"Bearer {COORDINATION_KEY}"},
json={
"category": "project",
"key": "pr_42_ready",
"content": "PR #42 is ready for review. Branch: feature/docs-system",
"tags": ["review", "pr-42"],
"priority": "high"
})
# Reviewer polls for review requests
r = requests.get(f"{URL}/memory/search?q=ready+for+review",
headers={"Authorization": f"Bearer {COORDINATION_KEY}"})Mẫu 3: Hub-and-Spoke (Orchestrator)
Một agent orchestrator trung tâm gán tác vụ cho agent worker.
┌──────────────┐
│ Orchestrator │
│ Agent │
└──────┬───────┘
│
┌──────────┼──────────┐
▼ ▼ ▼
┌──────┐ ┌──────┐ ┌──────┐
│Worker│ │Worker│ │Worker│
│ A │ │ B │ │ C │
└──────┘ └──────┘ └──────┘Trường hợp sử dụng: Quy trình phức tạp với công việc song song.
Triển khai:
# Orchestrator
class Orchestrator:
def assign_task(self, worker_id, task_description):
# Store task in worker's mind (or shared coordination mind)
create_task(task_description, priority="high")
# Notify worker via chat
reply(f"@{worker_id}: New task — {task_description}")
def check_progress(self):
tasks = list_tasks(status="in_progress")
for t in tasks:
print(f"{t['title']}: {t['status']}")
# Workers poll for assigned tasks
class Worker:
def run(self):
while True:
tasks = list_tasks(status="pending")
for t in tasks:
if assigned_to_me(t):
update_task(t["id"], status="in_progress")
result = do_work(t)
update_task(t["id"], status="done")
reply(f"Completed: {t['title']}")
time.sleep(60)Điều phối qua Chat
Agent có thể giao tiếp qua hệ thống chat:
# Agent A sends to Agent B
reply("@agent-b: Can you review my PR?")
# Agent B polls and responds
for msg in poll_messages():
if "@agent-b" in msg["content"]:
reply(f"@agent-a: Sure, looking at it now.")Điều phối qua Biến
Sử dụng biến cho điều phối nhẹ (lock, cờ):
# Acquire a lock
def acquire_lock(name):
r = requests.post(f"{URL}/var",
headers={"Authorization": f"Bearer {KEY}"},
json={"key": f"lock_{name}", "value": "acquired"})
return True
def release_lock(name):
requests.delete(f"{URL}/var/lock_{name}",
headers={"Authorization": f"Bearer {KEY}"})
# Use
if acquire_lock("deploy"):
try:
deploy_to_production()
finally:
release_lock("deploy")