Spaces:
Sleeping
Sleeping
| 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() | |
| ''' |