hee_!J commited on
Commit ยท
580db8f
1
Parent(s): 1d550b9
feat(orchestrator): Supervisor agent - LLM-driven dynamic workflow routing
Browse filesFiles changed (3)
- agents/orchestrator.py+71-19
- agents/supervisor.pyAdded+113
- docs/orchestrator_graph.mmd+7-2
1234567891011121314151617185 unmodified lines24252627282930313233344 unmodified lines3940414243445 unmodified lines505152535455568 unmodified lines65666768697071727374757677787980818283848586878889903 unmodified lines9495969798991003 unmodified lines104105106107108109110111112113114"""4-Tier ๋ฉํฐ์์ด์ ํธ ์ค์ผ์คํธ๋ ์ดํฐ (LangGraph ๊ธฐ๋ฐ, ์กฐ๊ฑด๋ถ ๋ผ์ฐํ )๊ฐ Tier๊ฐ tool-using LLM agent์ด๋ฉฐ, ์ค์ผ์คํธ๋ ์ดํฐ๋ LangGraph๋ก ์กฐ์ ํฉ๋๋ค.[๊ทธ๋ํ ๊ตฌ์กฐ]START -> detectdetect --(score >= 0.3)--> causedetect --(score < 0.3)---> noise -> END (severity gate: ๋ ธ์ด์ฆ๋ ๋ถ์ skip)cause --(max pct >= 40)--> impactcause --(max pct < 40)---> cause_retry -> impact (confidence retry)impact -> response -> END[์บ์ฑ]- build_graph(): ๊ทธ๋ํ ์ปดํ์ผ 1ํ (lru_cache)- run_orchestrator(alarm_id): ์๋๋น ๊ฒฐ๊ณผ ์บ์ 8๊ฐ (LLM ๋น์ฉ ์ ๊ฐ)"""from functools import lru_cachefrom typing import TypedDict5 unmodified linesfrom agents.detection import run_detectionfrom agents.impact import run_impactfrom agents.response import run_responsefrom core.schema import Tier1, Tier2, Tier3, Tier4, TierDatafrom data.demo import DEFAULT_ALARMSSEVERITY_THRESHOLD = 0.30 # tier1 score ๋ฏธ๋ง์ด๋ฉด ๋ ธ์ด์ฆ๋ก ๋ถ๋ฅCAUSE_CONFIDENCE_THRESHOLD = 40 # tier2 ์ต๋ ๊ธฐ์ฌ๋(%) ๋ฏธ๋ง์ด๋ฉด ์ฌ์๋class _GraphState(TypedDict, total=False):4 unmodified linestier4: Tier4skipped: boolcause_retried: booldef _node_detect(state: _GraphState) -> dict:5 unmodified linesdef _node_noise(state: _GraphState) -> dict:"""Tier 1 ์ ์๊ฐ ๋ฎ์ ํ์ ๋ถ์ skip - ๊ฒฐ๊ณผ๋ง ํ์์ฉ์ผ๋ก ์ฑ์"""return {"skipped": True,"tier2": {"causes": []},8 unmodified linesdef _route_after_cause(state: _GraphState) -> str:if state.get("cause_retried"):return "impact" # ์ด๋ฏธ 1ํ ์ฌ์๋ - ๋ฌดํ ๋ฃจํ ๋ฐฉ์งcauses = state["tier2"]["causes"]max_pct = max((c.get("pct", 0) for c in causes), default=0)return "cause_retry" if max_pct < CAUSE_CONFIDENCE_THRESHOLD else "impact"def _node_cause_retry(state: _GraphState) -> dict:"""๊ธฐ์ฌ๋ ๋ฎ์ ๋ถ์ ์ฌ์คํ - retry_hint ์ ํธ๋ก ๋๊ตฌ ํธ์ถ ๊ฐํ ์ ๋"""return {"tier2": run_cause(state["alarm"], state["tier1"], retry_hint=True),"cause_retried": True,}def _node_impact(state: _GraphState) -> dict:return {"tier3": run_impact(state["alarm"], state["tier1"], state["tier2"])}def _node_response(state: _GraphState) -> dict:return {"tier4": run_response(state["alarm"], state["tier1"], state["tier2"], state["tier3"])}@lru_cache(maxsize=1)3 unmodified linesg.add_node("noise", _node_noise)g.add_node("cause", _node_cause)g.add_node("cause_retry", _node_cause_retry)g.add_node("impact", _node_impact)g.add_node("response", _node_response)g.add_edge(START, "detect")3 unmodified lines})g.add_edge("noise", END)g.add_conditional_edges("cause", _route_after_cause, {"impact": "impact","cause_retry": "cause_retry",})g.add_edge("cause_retry", "impact")g.add_edge("impact", "response")g.add_edge("response", END)return g.compile()12345678910111213141516171819202122235 unmodified lines293031323334353637383940414 unmodified lines464748495051525 unmodified lines5859606162638 unmodified lines727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341353 unmodified lines1391401411421431441451461473 unmodified lines151152153154155156157158159160161162163164165166"""4-Tier ๋ฉํฐ์์ด์ ํธ ์ค์ผ์คํธ๋ ์ดํฐ (LangGraph ๊ธฐ๋ฐ)๊ฐ Tier๊ฐ tool-using LLM agent์ด๋ฉฐ, LangGraph๋ก ์กฐ์ ํฉ๋๋ค.[๊ทธ๋ํ ๊ตฌ์กฐ]START -> detectdetect --(score >= 0.3)--> causedetect --(score < 0.3)---> noise -> END (severity gate)cause --(max pct < 40)---> cause_retry -> supervisorcause --(max pct >= 40)--> supervisorsupervisor --(LLM ๊ฒฐ์ )-+-> impact -> response -> END (proceed_full / escalate)+-> fast_impact -> response -> END (fast_track: Tier 3 skip)Supervisor (`agents.supervisor`)๋ LLM์ด Tier 2 ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ณ ๋์ ์ผ๋ก routing ๊ฒฐ์ .- proceed_full: ํ์ค (Tier 3 + 4)- fast_track: ์์ธ ๋จ๋ ์ฐ์ธ ์ Tier 3 skip- escalate: ๊ณ ์ํ - ์ ์ ์งํ + human review ํ๋๊ทธ (tier4.metadata์ ๊ธฐ๋ก)[์บ์ฑ] build_graph 1ํ ์ปดํ์ผ, run_orchestrator ์๋๋น 8๊ฐ LRU[Observability] @traceable + wrap_openai (ํ๊ฒฝ๋ณ์ LANGSMITH_TRACING=true ์ ํ์ฑ)"""from functools import lru_cachefrom typing import TypedDict5 unmodified linesfrom agents.detection import run_detectionfrom agents.impact import run_impactfrom agents.response import run_responsefrom agents.supervisor import run_supervisorfrom core.schema import Tier1, Tier2, Tier3, Tier4, TierDatafrom data.demo import DEFAULT_ALARMSfrom data.wip import get_affected_wipSEVERITY_THRESHOLD = 0.30CAUSE_CONFIDENCE_THRESHOLD = 40class _GraphState(TypedDict, total=False):4 unmodified linestier4: Tier4skipped: boolcause_retried: boolsupervisor_decision: dict # {action, severity, reasoning}def _node_detect(state: _GraphState) -> dict:5 unmodified linesdef _node_noise(state: _GraphState) -> dict:return {"skipped": True,"tier2": {"causes": []},8 unmodified linesdef _route_after_cause(state: _GraphState) -> str:if state.get("cause_retried"):return "supervisor" # ์ด๋ฏธ ์ฌ์๋ - supervisor๋ก ์งํcauses = state["tier2"]["causes"]max_pct = max((c.get("pct", 0) for c in causes), default=0)return "cause_retry" if max_pct < CAUSE_CONFIDENCE_THRESHOLD else "supervisor"def _node_cause_retry(state: _GraphState) -> dict:return {"tier2": run_cause(state["alarm"], state["tier1"], retry_hint=True),"cause_retried": True,}def _node_supervisor(state: _GraphState) -> dict:decision = run_supervisor(state["alarm"], state["tier1"], state["tier2"])return {"supervisor_decision": decision}def _route_after_supervisor(state: _GraphState) -> str:action = state["supervisor_decision"]["action"]if action == "fast_track":return "fast_impact"return "impact" # proceed_full + escalate ๋ชจ๋ impact ๊ฒฝ์def _node_impact(state: _GraphState) -> dict:return {"tier3": run_impact(state["alarm"], state["tier1"], state["tier2"])}def _node_fast_impact(state: _GraphState) -> dict:"""fast_track: LLM ํธ์ถ ์์ด ๊ฒฐ์ ๋ก ์ lightweight impactTier 4๊ฐ tier3์ ์๊ตฌํ๋ฏ๋ก ์ต์ ์ ๋ณด๋ง ์ฑ์ ์ ๋ฌ.yield_loss๋ tier1 score๋ฅผ ๋จ์ ๋ณํ, downstream์ current stage๋ง."""alarm = state["alarm"]tier1 = state["tier1"]return {"tier3": {"yield_loss": round(float(tier1["score"]) * 3.0, 1), # ๋จ์ ์ถ์ "dependencies": [{"stage": alarm["title"].split()[0],"delta": f"+{tier1['score']}","tag": "ํ์ฌ","kind": "current",}],"impact_lots": get_affected_wip(alarm["id"]),}}def _node_response(state: _GraphState) -> dict:tier4 = run_response(state["alarm"], state["tier1"], state["tier2"], state["tier3"])# supervisor๊ฐ escalate ๊ฒฐ์ ์ immediate ์ฒซ ํญ๋ชฉ์ human review ํ๋๊ทธ ์ถ๊ฐdecision = state.get("supervisor_decision", {})if decision.get("action") == "escalate" and tier4.get("immediate"):tier4["immediate"][0]["text"] = "๐จ [HUMAN REVIEW ์๊ตฌ] " + tier4["immediate"][0]["text"]return {"tier4": tier4}@lru_cache(maxsize=1)3 unmodified linesg.add_node("noise", _node_noise)g.add_node("cause", _node_cause)g.add_node("cause_retry", _node_cause_retry)g.add_node("supervisor", _node_supervisor)g.add_node("impact", _node_impact)g.add_node("fast_impact", _node_fast_impact)g.add_node("response", _node_response)g.add_edge(START, "detect")3 unmodified lines})g.add_edge("noise", END)g.add_conditional_edges("cause", _route_after_cause, {"supervisor": "supervisor","cause_retry": "cause_retry",})g.add_edge("cause_retry", "supervisor")g.add_conditional_edges("supervisor", _route_after_supervisor, {"impact": "impact","fast_impact": "fast_impact",})g.add_edge("impact", "response")g.add_edge("fast_impact", "response")g.add_edge("response", END)return g.compile()
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113"""Supervisor Agent - LLM-driven dynamic workflow routing
Tier 2 ์์ธ ๋ถ์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ณ ํ์ ํ๋์ LLM์ด ๋์ ์ผ๋ก ๊ฒฐ์ ํฉ๋๋ค.threshold ๊ธฐ๋ฐ conditional edge(orchestrator)์ ๋ค๋ฅธ ์ :- ์ฌ๋์ด ์ ์ํ ์๊ณ์น๊ฐ ์๋๋ผ LLM์ด ๋งฅ๋ฝ์ ๋ณด๊ณ ํ๋จ- ๊ธฐ์ฌ๋ยท์ฆ์ยท์ํฅ lotยท๊ณต์ critical ์ฌ๋ถ๋ฅผ ์ข
ํฉ ๊ณ ๋ ค
๊ฐ์ฉ action:- proceed_full: ํ์ค ๊ฒฝ๋ก (Tier 3 -> Tier 4)- fast_track: ์์ธ์ด ๋ช
ํํด ์ํฅ ํ๊ฐ skip, Tier 4 ์ง์ง (latencyยทcost ์ ๊ฐ)- escalate: ๊ณ ์ํ - ์ ์ ์งํ + human review ํ๋๊ทธ
severity ๋ถ๋ฅ: low / medium / high / critical
๋น์ฉ ์ ๊ฐ: ์์ฌ๊ฒฐ์ ๋ง ํ๋ ์์์
์ด๋ผ SUPERVISOR_MODEL = gpt-4o-miniํ๊ฒฝ๋ณ์ SUPERVISOR_ENABLED=false ๋ฉด ํญ์ proceed_full (Supervisor ํธ์ถ ์์ฒด skip)"""import jsonimport os
from langsmith import traceable
from agents.llm import clientfrom core.schema import Tier1, Tier2
SUPERVISOR_MODEL = "gpt-4o-mini"
ACTION_SCHEMA = { "type": "object", "properties": { "action": { "type": "string", "enum": ["proceed_full", "fast_track", "escalate"], }, "severity": { "type": "string", "enum": ["low", "medium", "high", "critical"], }, "reasoning": {"type": "string"}, }, "required": ["action", "severity", "reasoning"], "additionalProperties": False,}
SYSTEM_PROMPT = """๋น์ ์ ๋ฐ๋์ฒด fab์ incident response supervisor ์์ด์ ํธ์
๋๋ค.์์ธ ๋ถ์(Tier 2) ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ณ ํ์ ๋จ๊ณ ์งํ ๋ฐฉ์์ ๊ฒฐ์ ํฉ๋๋ค.
[๊ฐ์ฉ action]- proceed_full: ํ์ค - Tier 3(์ํฅํ๊ฐ) โ Tier 4(๋์๊ถ๊ณ ) ์์ฐจ ์งํ (๋๋ถ๋ถ์ ๊ฒฝ์ฐ)- fast_track: ์์ธ์ด 1~2๊ฐ๋ก ๋ช
ํํ๊ณ ์ฆ์ ์กฐ์น๋ง ํ์ - Tier 3 skip, Tier 4 ์ง์ง (latencyยทcost ์ ๊ฐ)- escalate: ๊ณ ์ํ - ์ ์ ์งํํ๋ human review ํ๋๊ทธ (์กฐ์น ์ ์ด์์ ์ถ๊ฐ ๊ฒํ ๊ฐ์ )
[severity ๋ถ๋ฅ]- low: ์ฌ์ํ ๋ณ๋, ์๋ ์ฒ๋ฆฌ ๊ฐ๋ฅ- medium: ํ์ค ๋์์ผ๋ก ์ถฉ๋ถ- high: ์ํฅ ํผ, ๋น ๋ฅธ ์กฐ์น ํ์- critical: ๋ผ์ธ ์ ์ง ๊ฒํ ์์ค
[ํ๋จ ๊ธฐ์ค]- ์์ธ 1๊ฐ๊ฐ ๊ธฐ์ฌ๋ 70%+ ๋จ๋
์ฐ์ธ + medium/low severity โ fast_track ํ๋ณด- ์์ธ ๋ค์(3๊ฐ+) ๋ถํฌ ๋๋ high/critical severity โ proceed_full- critical severity, downstream ์ํฅ์ด ํด ๊ฒ์ผ๋ก ์ถ์ โ escalate- ์ผ๋ฐ์ ์ผ๋ก proceed_full์ด ์์ ํ ๊ธฐ๋ณธ๊ฐ
[์ค์] reasoning์ ํ ๋ฌธ์ฅ์ผ๋ก ๊ฒฐ์ ๊ทผ๊ฑฐ๋ฅผ ๋ช
์ (์: "๋จ์ผ ์์ธ 80% ๋จ๋
์ฐ์ธ, medium severity โ fast_track")"""
def _build_user_prompt(alarm: dict, tier1: Tier1, tier2: Tier2) -> str: causes = "\n".join( f"- [{c['pct']}%] {c['name']}: {c['evidence'][:120]}" for c in tier2["causes"] ) return f"""## ์ด์ ์๋- ๊ณต์ : {alarm['title']}- lot: {alarm['lot_id']}
## Tier 1 ์ด์ ํ์ง- ์ด์ ์ ์: {tier1['score']}
## Tier 2 ์์ธ ๋ถ์ ๊ฒฐ๊ณผ{causes}
ํ์ actionยทseverityยทreasoning์ ๊ฒฐ์ ํด ์ฃผ์ธ์."""
def supervisor_enabled() -> bool: return os.getenv("SUPERVISOR_ENABLED", "true").lower() not in ("false", "0", "no")
@traceable(name="Supervisor_Agent", run_type="chain")def run_supervisor(alarm: dict, tier1: Tier1, tier2: Tier2) -> dict: """ํ์ workflow action์ LLM์ด ๊ฒฐ์
๋ฐํ: {"action": "proceed_full"|"fast_track"|"escalate", "severity": "low"|"medium"|"high"|"critical", "reasoning": str} """ if not supervisor_enabled(): return {"action": "proceed_full", "severity": "medium", "reasoning": "(Supervisor ๋นํ์ฑ - ํญ์ ํ์ค ๊ฒฝ๋ก)"}
resp = client().chat.completions.create( model=SUPERVISOR_MODEL, messages=[ {"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": _build_user_prompt(alarm, tier1, tier2)}, ], response_format={ "type": "json_schema", "json_schema": {"name": "supervisor", "schema": ACTION_SCHEMA, "strict": True}, }, ) return json.loads(resp.choices[0].message.content)8 unmodified lines91011121314151617181920212223248 unmodified linesnoise(noise)cause(cause)cause_retry(cause_retry)impact(impact)response(response)__end__([<p>__end__</p>]):::last__start__ --> detect;cause -.-> cause_retry;cause -.-> impact;cause_retry --> impact;detect -.-> cause;detect -.-> noise;impact --> response;noise --> __end__;response --> __end__;classDef default fill:#f2f0ff,line-height:1.28 unmodified lines910111213141516171819202122232425262728298 unmodified linesnoise(noise)cause(cause)cause_retry(cause_retry)supervisor(supervisor)impact(impact)fast_impact(fast_impact)response(response)__end__([<p>__end__</p>]):::last__start__ --> detect;cause -.-> cause_retry;cause -.-> supervisor;cause_retry --> supervisor;detect -.-> cause;detect -.-> noise;fast_impact --> response;impact --> response;supervisor -.-> fast_impact;supervisor -.-> impact;noise --> __end__;response --> __end__;classDef default fill:#f2f0ff,line-height:1.2
agents/orchestrator.py+71-19
agents/supervisor.pyAdded+113
docs/orchestrator_graph.mmd+7-2