Merge pull request #4 from FabAgentGroup/feat/tier4-response
Browse files1234567891011"""Tier 4 ๋์ ๊ถ๊ณ ์์ด์ ํธ (M2์์ ๊ตฌํ)์ ๋ ฅ: ์๋ ์ปจํ ์คํธ + Tier 1ยท2ยท3 ๊ฒฐ๊ณผ์ถ๋ ฅ: core.schema.Tier4 (์ฆ์ ์กฐ์น + ์ค์ฅ๊ธฐ ์กฐ์น + ๊ทผ๊ฑฐ ์๋ฃ)๋ชจ๋ธ: GPT-5 (์ค์ผ์คํธ๋ ์ดํฐ๊ธ) + RAG, SOP/ํ์ค ์ ์ฐจ ๋ฌธ์ ๊ฒ์"""from core.schema import Tier1, Tier2, Tier3, Tier4def run_response(alarm: dict, tier1: Tier1, tier2: Tier2, tier3: Tier3) -> Tier4:raise NotImplementedError("M2: Tier 4 ๋์ ๊ถ๊ณ ์์ด์ ํธ ๊ตฌํ ์์ ")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127"""Tier 4 ๋์ ๊ถ๊ณ ์์ด์ ํธ์๋ + Tier 1/2/3 ๊ฒฐ๊ณผ์ RAG ์ง์์ ๋ฐํ์ผ๋ก- immediate: ์ฆ์ ์กฐ์น ๋ชฉ๋ก (LLM)- longterm: ์ค์ฅ๊ธฐ ์กฐ์น ๋ชฉ๋ก (LLM)- refs: ๊ทผ๊ฑฐ ์๋ฃ (RAG๋ก ๊ฒ์๋ ๋ฌธ์ ID์ ์ ๋ชฉ, ๊ฒฐ์ ๋ก ์ )๋ชจ๋ธ: GPT-5 mini (agents.llm.SUBAGENT_MODEL)"""import jsonfrom agents.llm import SUBAGENT_MODEL, clientfrom agents.rag.store import load_document, searchfrom core.schema import Tier1, Tier2, Tier3, Tier4TOP_K_DOCS = 4# LLM์ด ์ฑ์ธ ๋ถ๋ถ๋ง ์คํค๋ง๋ก, refs๋ ๊ฒ์ ๊ฒฐ๊ณผ์์ ๊ฒฐ์ ๋ก ์ ์ผ๋ก ๊ตฌ์ฑLLM_PART_SCHEMA = {"type": "object","properties": {"immediate": {"type": "array","items": {"type": "object","properties": {"text": {"type": "string"},"meta": {"type": ["string", "null"]},},"required": ["text", "meta"],"additionalProperties": False,},},"longterm": {"type": "array","items": {"type": "object","properties": {"text": {"type": "string"},"meta": {"type": ["string", "null"]},},"required": ["text", "meta"],"additionalProperties": False,},},},"required": ["immediate", "longterm"],"additionalProperties": False,}SYSTEM_PROMPT = """๋น์ ์ ๋ฐ๋์ฒด ๊ณต์ ๋์ ๊ถ๊ณ ์ ๋ฌธ๊ฐ์ ๋๋ค.์ด์ ์๋๊ณผ ๊ทธ๋์์ ๋ถ์(ํ์งยท์์ธยท์ํฅ)์ ์ข ํฉํ์ฌ ๊ตฌ์ฒด์ ์ธ ์กฐ์น๋ฅผ ๊ถ๊ณ ํฉ๋๋ค.์ฐ์ถ๋ฌผ:1. immediate: ์ฆ์ ์กฐ์น (์๊ฐ ๋จ์ ์์ ์ํ, ์: PM ํฌ์ , ํ๊ณต์ hold, ์ผ์ ์ฌ์กฐ์ )2. longterm: ์ค์ฅ๊ธฐ ์กฐ์น (์ฌ๋ฐ ๋ฐฉ์ง, PM ์ฃผ๊ธฐ ์กฐ์ , ๋ชจ๋ํฐ๋ง ๊ฐํ, ์ ์ฐจ ๊ฐ์ )๊ฐ ์กฐ์น๋ text(๊ถ๊ณ ๋ณธ๋ฌธ)์ meta(๋ถ๊ฐ ์ ๋ณด, ์: "์์ 2์๊ฐ", "Etch hold", "PPC ํ์กฐ")๋ก ๊ตฌ์ฑํฉ๋๋ค.meta๊ฐ ํ์ ์์ผ๋ฉด null๋ก ๋ก๋๋ค. ์ ๊ณต๋ ์ง์ ๋ฌธ์๋ฅผ ๊ทผ๊ฑฐ๋ก ์์ฑํ๊ณ , ๊ทผ๊ฑฐ๊ฐ ์ฝํ ๊ถ๊ณ ๋ ํฌํจํ์ง ์์ต๋๋ค."""def _doc_description(doc_id: str) -> str:"""๋ฌธ์ ์ฒซ ์ค(# ์ ๋ชฉ)์์ ID ๋ค์ ๋ถ๋ถ์ desc๋ก ์ถ์ถ"""text = load_document(doc_id)if not text:return doc_idfirst_line = text.split("\n", 1)[0].lstrip("# ").strip()for sep in (" โ ", " - "):if sep in first_line:return first_line.split(sep, 1)[1].strip()return first_linedef _build_query(alarm: dict, tier2: Tier2) -> str:causes = " ".join(c["name"] for c in tier2["causes"])return f"{alarm['title']} ๋์ PM ์กฐ์น ๋ณด๋ฅ ์ฌ์กฐ์ ๋ชจ๋ํฐ๋ง {causes}"def run_response(alarm: dict, tier1: Tier1, tier2: Tier2, tier3: Tier3) -> Tier4:doc_ids = search(_build_query(alarm, tier2), top_k=TOP_K_DOCS)knowledge = "\n\n".join(f"[{d}]\n{load_document(d)}" for d in doc_ids)cause_lines = "\n".join(f"- {c['name']} ({c['pct']}%)" for c in tier2["causes"])impact_lots_text = ", ".join(f"{l['label']} {l['lots']}lot/{l['wafers']}์ฅ" for l in tier3["impact_lots"])user_prompt = f"""## ์ด์ ์๋- ๊ณต์ : {alarm['title']}- lot: {alarm['lot_id']}## Tier 1 ์ด์ ํ์ง- ์ด์ ์ ์: {tier1['score']}## Tier 2 ์์ธ (๊ธฐ์ฌ๋ ์){cause_lines}## Tier 3 ์ํฅ- ์์ ์์จ ์์ค: {tier3['yield_loss']} %p- ์ํฅ WIP: {impact_lots_text}## ์ฌ๋ด ์ง์ ๋ฌธ์{knowledge}์ ๋ถ์์ ์ข ํฉํด immediate์ longterm ์กฐ์น๋ฅผ ๊ถ๊ณ ํด ์ฃผ์ธ์."""resp = client().chat.completions.create(model=SUBAGENT_MODEL,messages=[{"role": "system", "content": SYSTEM_PROMPT},{"role": "user", "content": user_prompt},],response_format={"type": "json_schema","json_schema": {"name": "tier4_part", "schema": LLM_PART_SCHEMA, "strict": True},},)llm_out = json.loads(resp.choices[0].message.content)refs = [{"id": d, "desc": _doc_description(d)} for d in doc_ids]return {"immediate": llm_out["immediate"],"longterm": llm_out["longterm"],"refs": refs,}
agents/response.py+121-5