Spaces:
Sleeping
Sleeping
File size: 5,457 Bytes
5fc69e4 |
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 |
from typing import Dict, Any
def build_main_prompt(pre: Dict[str, Any], session_id: str, npc_id: str) -> str:
tags = pre.get("tags", {})
ps = pre.get("player_state", {})
rag_docs = pre.get("rag_main_docs", [])
# RAG ๋ฌธ์ ๋ถ๋ฆฌ
lore_text = ""
desc_text = ""
for doc in rag_docs:
if "LORE:" in doc:
lore_text += doc + "\n"
elif "DESCRIPTION:" in doc:
desc_text += doc + "\n"
else:
# fallback: type ๊ธฐ๋ฐ ๋ถ๋ฆฌ ๊ฐ๋ฅ
if "lore" in doc.lower():
lore_text += doc + "\n"
elif "description" in doc.lower():
desc_text += doc + "\n"
prompt = [
"<SYS>",
f"NPC_ID={tags.get('npc_id','')}",
f"NPC_LOCATION={tags.get('location','')}",
"TAGS:",
f" quest_stage={tags.get('quest_stage','')}",
f" relationship={tags.get('relationship','')}",
f" trust={tags.get('trust','')}",
f" npc_mood={tags.get('npc_mood','')}",
f" player_reputation={tags.get('player_reputation','')}",
f" style={tags.get('style','')}",
"</SYS>",
"<RAG>",
f"LORE: {lore_text.strip() or '(์์)'}",
f"DESCRIPTION: {desc_text.strip() or '(์์)'}",
"</RAG>",
"<PLAYER_STATE>"
]
if ps.get("items"):
prompt.append(f"items={','.join(ps['items'])}")
if ps.get("actions"):
prompt.append(f"actions={','.join(ps['actions'])}")
if ps.get("position"):
prompt.append(f"position={ps['position']}")
prompt.append("</PLAYER_STATE>")
prompt.append("<CTX>")
for h in pre.get("context", []):
prompt.append(f"{h['role']}: {h['text']}")
prompt.append("</CTX>")
prompt.append(f"<PLAYER>{pre.get('player_utterance','').rstrip()}")
prompt.append("<STATE>")
prompt.append("<NPC>")
return "\n".join(prompt)
def build_fallback_prompt(pre: Dict[str, Any], session_id: str, npc_id: str) -> str:
"""
additional_trigger ๊ฐ์ ๋ฐ๋ผ ์ผ๋ฐ fallback / ํน์ fallback ํ๋กฌํํธ๋ฅผ ํ ํจ์์์ ์ฒ๋ฆฌ
"""
tags = pre.get("tags", {})
ps = pre.get("player_state", {})
gs = pre.get("game_state", {})
rag_text = "\n".join(f"- {doc}" for doc in pre.get("rag_fallback_docs", []))
fb_style = pre.get("fallback_style") or {}
trigger_meta = pre.get("trigger_meta", {}) or {}
items = ",".join(ps.get("items", []))
actions = ",".join(ps.get("actions", []))
location = gs.get("location") or ps.get("location", "unknown")
quest_stage = gs.get("quest_stage", "unknown")
# ๊ธฐ๋ณธ ์๋ด๋ฌธ
instr = (
"๋น์ ์ NPC persona๋ฅผ ๊ฐ์ง ์บ๋ฆญํฐ์
๋๋ค. "
"ํ๋ ์ด์ด ๋ฐํ์ ์์ฐ์ค๋ฝ๊ณ ๋งฅ๋ฝ์ ๋ง๋ ๋์ฌ๋ฅผ ์์ฑํ์ธ์. "
"์คํ ๋ฆฌ ์งํ ์กฐ๊ฑด์ ์ถฉ์กฑ๋์ง ์์์ต๋๋ค."
)
# additional_trigger=True โ ํน์ fallback
if pre.get("additional_trigger"):
# trigger_meta ๊ธฐ๋ฐ ๊ตฌ์ฒดํ
s = fb_style.get("style") or trigger_meta.get("npc_style")
a = fb_style.get("npc_action") or trigger_meta.get("npc_action")
e = fb_style.get("npc_emotion") or trigger_meta.get("npc_emotion")
more = []
if s: more.append(f"๋ํ ์คํ์ผ={s}")
if a: more.append(f"NPC ํ๋={a}")
if e: more.append(f"NPC ๊ฐ์ ={e}")
if more:
instr += " " + "; ".join(more) + "."
# ํน์ fallback์์ ๋ช
์
instr += " ์ด ๋ฐ์์ ํ๋ ์ด์ด์ ํน์ ๋ฐํ(๊ธ์ง ํธ๋ฆฌ๊ฑฐ)์ ์ํด ์ ๋ฐ๋ ๊ฒ์
๋๋ค."
return f"""
<FALLBACK>
NPC_ID={npc_id}
SESSION_ID={session_id}
LOCATION={location}
QUEST_STAGE={quest_stage}
MOOD={tags.get("npc_mood","neutral")}
STYLE={tags.get("style","neutral")}
ITEMS={items}
ACTIONS={actions}
EMOTION_SUMMARY={', '.join([f"{k}:{round(v,2)}" for k,v in pre.get('emotion',{}).items()])}
INPUT="{pre['player_utterance']}"
RAG_CONTEXT:
{rag_text or "(none)"}
INSTRUCTION:
{instr}
</FALLBACK>
""".strip()
'''
def build_fallback_prompt(pre: dict, session_id: str, npc_id: str) -> str:
tags = pre.get("tags", {})
ps = pre.get("player_state", {})
gs = pre.get("game_state", {})
rag_text = "\n".join(f"- {doc}" for doc in pre.get("rag_fallback_docs", []))
fb = pre.get("fallback_style") or {}
items = ",".join(ps.get("items", []))
actions = ",".join(ps.get("actions", []))
location = gs.get("location") or ps.get("location", "unknown")
quest_stage = gs.get("quest_stage", "unknown")
instr = "์กฐ๊ฑด ๋ถ์ถฉ์กฑ. ์คํ ๋ฆฌ ์งํ์ ํ์ง ์๊ณ , ์บ๋ฆญํฐ ์ผ๊ด์ฑ์ ์ ์งํ๋ฉฐ ์์ฐ์ค๋ฝ๊ฒ ์๋ตํ๋ผ."
if fb:
# ์ ํ์ ๊ตฌ์ฒดํ
s = fb.get("style"); a = fb.get("npc_action"); e = fb.get("npc_emotion")
more = []
if s: more.append(f"๋ํ ์คํ์ผ={s}")
if a: more.append(f"NPC ํ๋={a}")
if e: more.append(f"NPC ๊ฐ์ ={e}")
if more:
instr += " " + "; ".join(more) + "."
return f"""
<FALLBACK>
NPC_ID={npc_id}
SESSION_ID={session_id}
LOCATION={location}
QUEST_STAGE={quest_stage}
MOOD={tags.get("npc_mood","neutral")}
STYLE={tags.get("style","neutral")}
ITEMS={items}
ACTIONS={actions}
EMOTION_SUMMARY={', '.join([f"{k}:{round(v,2)}" for k,v in pre.get('emotion',{}).items()])}
INPUT="{pre['player_utterance']}"
RAG:
{rag_text or "(none)"}
INSTRUCTION:
{instr}
</FALLBACK>
""".strip()
''' |