"""ScriptModifier — LLM으로 협상 스크립트의 톤을 상황·상대 성향에 맞게 재작성한다. 평문 입출력. {변수} 플레이스홀더는 보존해야 하며, 검증은 verifier가 담당한다. """ import logging from typing import Optional from openai import OpenAI logger = logging.getLogger(__name__) _SYSTEM_PROMPT = """You are an expert Negotiation Script Editor. Rewrite the provided Korean negotiation script to match the requested tone/context. Rules: 1. **Preserve Variables**: Do NOT change, remove, or add any {placeholder} variables. \ Only use variables that exist in the input. 2. **Preserve Meaning**: Keep every factual statement and number intact. \ Only adjust tone, phrasing, and flow. 3. **Language**: The output MUST be in Korean (Hangul). 4. **Plain Text Only**: Output ONLY the rewritten script text. \ No markdown, no code fences, no explanations. 5. Keep the line structure similar to the input (one thought per line).""" _USER_TEMPLATE = """Original Script: {script} Context/Instructions: {context} Rewritten script:""" class ScriptModifier: def __init__(self, api_key: str, model: str = "gpt-4o"): self.client = OpenAI(api_key=api_key) self.model = model def modify_script(self, script: str, context: dict = None) -> Optional[str]: """스크립트 톤 재작성. 실패 시 None을 반환한다 (호출측에서 원문 유지).""" if not script or not script.strip(): return None try: response = self.client.chat.completions.create( model=self.model, temperature=0, messages=[ {"role": "system", "content": _SYSTEM_PROMPT}, {"role": "user", "content": _USER_TEMPLATE.format( script=script, context=str(context or {}))}, ], ) text = (response.choices[0].message.content or "").strip() # 혹시 모를 코드펜스 제거 if text.startswith("```"): text = text.strip("`").strip() return text or None except Exception as e: logger.error(f"[ScriptModifier] LLM 호출 실패: {e}") return None