File size: 8,283 Bytes
a28cff7 ce17628 a28cff7 4819352 ce17628 a28cff7 ce17628 a28cff7 58debbc a28cff7 ce17628 a28cff7 ce17628 4819352 58debbc a28cff7 ce17628 a28cff7 ce17628 a28cff7 ce17628 a28cff7 ce17628 a28cff7 ce17628 a28cff7 ce17628 a28cff7 ce17628 ebb5f89 ce17628 ebb5f89 ce17628 ebb5f89 a28cff7 ce17628 a28cff7 ce17628 51aeab3 ce17628 51aeab3 ce17628 51aeab3 ce17628 51aeab3 ce17628 51aeab3 ce17628 a28cff7 ce17628 a28cff7 ce17628 a28cff7 ce17628 58debbc a28cff7 ce17628 a28cff7 ce17628 58debbc a28cff7 ce17628 a28cff7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | from __future__ import annotations
"""
์๋๋ฆฌ์ค ์นดํ๋ก๊ทธ ์๋ (Intent / Action / Behavior)
๋ฑ๋ก๋ ๋ชจ๋ ์๋๋ฆฌ์ค(available_scenarios)๋ฅผ catalog_* ํ
์ด๋ธ์ scenario_id์ ํจ๊ป ์ ์ฌํ๋ค.
โ AdminPage๊ฐ ์๋๋ฆฌ์ค๋ณ ์นดํ๋ก๊ทธ๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ์กฐํํ ์ ์๊ฒ ํ๋ค.
"""
import json
import logging
from config import settings
from data.executor import get_executor, DuckDBExecutor
from core.engines import config, available_scenarios
logger = logging.getLogger(__name__)
# ์๋๋ฆฌ์ค ๋ฉํ(scenarios ํ
์ด๋ธ) ํ์์ฉ ์ด๋ฆ
_SCENARIO_NAMES = {
"cs-myk-v3": "๋ง์ดK CS ์๋ด ์์ฐ",
"bundle-v3": "๊ฒฐํฉ ์ํ ์ถ์ฒ ์์ฐ",
"worker-v3": "์ง์ฅ์ธ ๋ผ์ดํ์ผ์ด ์์ฐ",
}
def seed_catalogs() -> None:
"""๋ฑ๋ก๋ ๋ชจ๋ ์๋๋ฆฌ์ค์ ์นดํ๋ก๊ทธ๋ฅผ ์ ์ฌํ๋ค.
catalog_intentsยทcatalog_actionsยทcatalog_behaviors๋ฅผ ๋น์ด ๋ค
available_scenarios()์ ๋ชจ๋ ์๋๋ฆฌ์ค๋ฅผ ๋ค์ ์ ์ฌํ๋ค(์ ์ฒด ๊ต์ฒด).
"""
ex = get_executor()
for tbl in ("catalog_intents", "catalog_actions", "catalog_behaviors"):
ex.execute(f"DELETE FROM {tbl}")
for sid in available_scenarios():
_seed_one(ex, sid)
def _seed_one(ex: DuckDBExecutor, scenario_id: str) -> None:
"""ํ ์๋๋ฆฌ์ค์ ์นดํ๋ก๊ทธ๋ฅผ catalog_* ํ
์ด๋ธ์ ์ ์ฌํ๋ค.
์๋๋ฆฌ์ค ๋ฉํ(scenarios)์ IntentยทActionยทBehavior ์นดํ๋ก๊ทธ๋ฅผ ์ ์ฌํ๋ค.
Action์ Intent-ํค 3์ฑ๋ ๊ตฌ์กฐ(dict)์ ๊ตฌ๋ฒ์ ๋ฆฌ์คํธ๋ฅผ ๋ชจ๋ ์ฒ๋ฆฌํ๊ณ ,
Behavior๋ tree-2stepยทsingle-selectยท๊ตฌ๋ฒ์ steps ๊ตฌ์กฐ๋ฅผ ๋ชจ๋ ์ฒ๋ฆฌํ๋ค.
Args:
ex: ์นดํ๋ก๊ทธ๋ฅผ ์ ์ฌํ DuckDB executor.
scenario_id: ์ ์ฌํ ์๋๋ฆฌ์ค ID.
"""
# โโ ์๋๋ฆฌ์ค ๋ฉํ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
taxonomy = config.get_taxonomy(scenario_id)
description = config.load_layer(scenario_id, "input").get("description", "")
ex.execute(
"INSERT OR REPLACE INTO scenarios (id, name, version, description) VALUES (?, ?, ?, ?)",
[
scenario_id,
_SCENARIO_NAMES.get(scenario_id, scenario_id),
taxonomy.get("version", ""),
description,
],
)
# โโ Intent ์นดํ๋ก๊ทธ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
rows = [
[
scenario_id,
i["id"], i["name"],
i["L1_id"], i["L1_name"],
i["L2_id"], i["L2_name"],
i["inference_type"],
json.dumps(i.get("features", []), ensure_ascii=False),
]
for i in taxonomy["intents"]
]
ex.executemany(
"INSERT INTO catalog_intents "
"(scenario_id, intent_id, intent_name, L1_id, L1_name, L2_id, L2_name, inference_type, features_json) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
rows,
)
# โโ Action ์นดํ๋ก๊ทธ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
actions_data = config.get_actions(scenario_id)
a_rows: list[list] = []
raw_actions = actions_data["actions"]
if isinstance(raw_actions, dict):
# v0.2.0: Intent-ํค 3์ฑ๋ ๊ตฌ์กฐ โ intent ร channel ๋ก ํ๋ฉดํ
for intent_id, ch_map in raw_actions.items():
for channel, body in ch_map.items():
if isinstance(body, dict): # ๊ณ ๊ฐ์ผํฐ ์๋ด์ฌ ์ปจํ
์คํธ (์ํฉ+์๋ด)
message = f"์ํฉ: {body.get('situation', '')} / ์๋ด: {body.get('guidance', '')}"
else:
message = str(body)
a_rows.append([
scenario_id,
f"{intent_id}#{channel}", channel,
json.dumps([intent_id], ensure_ascii=False),
"", channel, message,
])
else:
# ๊ตฌ๋ฒ์ : action ๋ฆฌ์คํธ
for a in raw_actions:
a_rows.append([
scenario_id,
a["id"], a["name"],
json.dumps(a.get("intents", []), ensure_ascii=False),
a.get("condition", ""), a["channel"], a.get("message", ""),
])
ex.executemany(
"INSERT INTO catalog_actions "
"(scenario_id, action_id, action_name, intents_json, condition, channel, message) "
"VALUES (?, ?, ?, ?, ?, ?, ?)",
a_rows,
)
# โโ Behavior ์นดํ๋ก๊ทธ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
behaviors_data = config.get_behaviors(scenario_id)
b_rows: list[list] = []
structure = behaviors_data.get("structure")
if structure == "tree-2step":
# 2๋จ๊ณ ํธ๋ฆฌ: step1.behaviors + step2.by_parent + step2.common
for b in behaviors_data["step1"]["behaviors"]:
b_rows.append([scenario_id, b["id"], 1, b["name"], b["event_type"], b["entity"]])
for parent_id, items in behaviors_data["step2"]["by_parent"].items():
for b in items:
b_rows.append([scenario_id, b["id"], 2, b["name"], b["event_type"], b["entity"]])
for b in behaviors_data["step2"]["common"]:
b_rows.append([scenario_id, b["id"], 2, b["name"], b["event_type"], b["entity"]])
elif structure == "single-select":
# ๋จ์ผ ์ ํ: apps[] (1๋จ๊ณ, app_open ๋จ์ผํ)
for b in behaviors_data["apps"]:
b_rows.append([scenario_id, b["id"], 1, b["name"], b["event_type"], b["entity"]])
else:
# ์ ์์: steps[] ํ๋ฉด
for step_block in behaviors_data["steps"]:
step = step_block["step"]
for b in step_block["behaviors"]:
b_rows.append([scenario_id, b["id"], step, b["name"], b["event_type"], b["entity"]])
ex.executemany(
"INSERT INTO catalog_behaviors "
"(scenario_id, behavior_id, step, behavior_name, event_type, entity) "
"VALUES (?, ?, ?, ?, ?, ?)",
b_rows,
)
logger.info(
f"Seeded catalog [{scenario_id}]: intents={len(rows)} actions={len(a_rows)} behaviors={len(b_rows)}"
)
def load_intents_catalog(scenario_id: str = settings.SCENARIO_ID) -> list[dict]:
"""์๋๋ฆฌ์ค๋ณ Intent ์นดํ๋ก๊ทธ๋ฅผ ์กฐํํ๋ค.
Args:
scenario_id: ์กฐํํ ์๋๋ฆฌ์ค ID (๊ธฐ๋ณธ๊ฐ์ settings.SCENARIO_ID).
Returns:
Intent ์ ์ dict์ ๋ชฉ๋ก (idยทnameยท๊ณ์ธตยทinference_typeยทfeatures ํฌํจ).
"""
ex = get_executor()
df = ex.to_pandas(
"SELECT intent_id, intent_name, L1_id, L1_name, L2_id, L2_name, inference_type, features_json "
"FROM catalog_intents WHERE scenario_id = ?",
[scenario_id],
)
rows = []
for _, r in df.iterrows():
rows.append({
"id": r["intent_id"],
"name": r["intent_name"],
"L1_id": r["L1_id"],
"L1_name": r["L1_name"],
"L2_id": r["L2_id"],
"L2_name": r["L2_name"],
"inference_type": r["inference_type"],
"features": json.loads(r["features_json"]) if r["features_json"] else [],
})
return rows
def load_behaviors_catalog(scenario_id: str = settings.SCENARIO_ID) -> dict[str, dict]:
"""์๋๋ฆฌ์ค๋ณ Behavior ์นดํ๋ก๊ทธ๋ฅผ ์กฐํํ๋ค.
Args:
scenario_id: ์กฐํํ ์๋๋ฆฌ์ค ID (๊ธฐ๋ณธ๊ฐ์ settings.SCENARIO_ID).
Returns:
behavior_id๋ฅผ ํค๋ก ํ๊ณ stepยทnameยทevent_typeยทentity๋ฅผ ๋ด์ dict๋ฅผ
๊ฐ์ผ๋ก ๊ฐ์ง๋ ๋งคํ.
"""
ex = get_executor()
df = ex.to_pandas(
"SELECT behavior_id, step, behavior_name, event_type, entity "
"FROM catalog_behaviors WHERE scenario_id = ?",
[scenario_id],
)
result = {}
for _, r in df.iterrows():
result[r["behavior_id"]] = {
"step": int(r["step"]),
"name": r["behavior_name"],
"event_type": r["event_type"],
"entity": r["entity"],
}
return result
|