पाठ 22 / 29
Supervisor पैटर्न
एक समन्वयक को विशेषज्ञ workers तक काम route करने दें।
एक प्रबंधक और विशेषज्ञ
Supervisor पैटर्न में एक agent (अक्सर छोटे routing prompt वाला LLM, या सादा कोड) कार्य और अब तक की प्रगति देखकर चुनता है कि अगला कौन-सा worker कार्य करे, या तय करता है कि काम पूरा हुआ। Workers अपना विशेषज्ञ चरण करके supervisor को रिपोर्ट करते हैं, जो समग्र नियंत्रण-प्रवाह रखता है। इसे समझना आसान है क्योंकि हर हस्तांतरण एक जगह से गुज़रता है जहाँ आप उसे log, सीमित और अनुमोदित कर सकते हैं। LangGraph में supervisor ऐसा node है जो Command(goto=worker) लौटाता है, और हर worker Command(goto="supervisor", update=...) लौटाता है। Supervisor को स्पष्ट समाप्ति शर्त और चरण सीमा दें।
एक supervisor और दो workers, चलाकर
मैंने यह Python virtual environment में langgraph 1.2.12 और langchain-core 1.6.6 के साथ ऑफ़लाइन चलाया। किसी API key या मॉडल की ज़रूरत नहीं क्योंकि मॉडल की जगह सादे Python functions हैं, इसलिए आउटपुट दोहराने योग्य है। यह नियंत्रण-प्रवाह खंड के Command उदाहरण वाला ही graph है: "find" वाले कार्य researcher को, बाक़ी writer को जाते हैं, और result बनने पर supervisor run समाप्त करता है।
from typing import Literal, TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.types import Command
class State(TypedDict):
task: str
result: str
def supervisor(state) -> Command[Literal["researcher", "writer", "__end__"]]:
if not state.get("result"):
return Command(goto="researcher" if "find" in state["task"] else "writer")
return Command(goto=END)
def researcher(state) -> Command[Literal["supervisor"]]:
return Command(update={"result": "facts about " + state["task"]}, goto="supervisor")
def writer(state) -> Command[Literal["supervisor"]]:
return Command(update={"result": "draft for " + state["task"]}, goto="supervisor")
g = StateGraph(State)
g.add_node("supervisor", supervisor); g.add_node("researcher", researcher); g.add_node("writer", writer)
g.add_edge(START, "supervisor")
app = g.compile()
for task in ("find the 2025 leave policy", "write a welcome note"):
print(task, "->", app.invoke({"task": task, "result": ""})["result"])
Output:
find the 2025 leave policy -> facts about find the 2025 leave policy write a welcome note -> draft for write a welcome note
त्वरित जाँच: Supervisor पैटर्न में समग्र नियंत्रण-प्रवाह कौन रखता है?
- कोई नहीं
- हर worker स्वतंत्र रूप से
- User का browser
- Supervisor
Answer
Supervisor — Workers रिपोर्ट लौटाते हैं; आगे क्या हो यह supervisor तय करता है।