99 lines
4.2 KiB
Python
99 lines
4.2 KiB
Python
"""갱신된 맥락과 페르소나 정보로 다음 발화의 가이드를 만든다."""
|
|
|
|
import asyncio
|
|
|
|
from models.context import AnalysisInput, ContextExtractionInput, render_claims
|
|
from models.persona import PersonaInfo
|
|
from models.utterance_guide import StyleGuideResult, UtteranceGuide, UtteranceGuideInput
|
|
from services.context_extraction import extract_context
|
|
from services.persona_analysis import prepare_persona, select_strategy
|
|
from utils.common_llm import create_llm
|
|
from utils.session_store import load_context, load_persona_info, save_context, save_persona_info
|
|
|
|
SYSTEM_PROMPT = """너는 독서토론에 참여하는 AI의 다음 발화를 설계한다.
|
|
주어진 참여자로서 페르소나대로 토론에 참여한다고 보고, 발화문은 쓰지 말고 어떻게 발화할지만 정한다.
|
|
주장 목록에서 참여자 자신의 주장은 입장의 일관성을 지키는 데 참고한다.
|
|
|
|
- target_claim: 다른 화자의 주장 중 반응할 주장 하나를 고르고 한 문장으로 옮긴다. 신규/갱신 주장을 우선하고, 과거 주장은 참고만 한다.
|
|
- intent: 그 주장에 대해 무엇을 할지 쓴다. 동의, 반박, 질문, 확장 등.
|
|
- key_message: 핵심으로 전할 내용을 한 문장으로 쓴다.
|
|
- tone: 발화 전략과 페르소나에 맞는 태도를 쓴다."""
|
|
|
|
guide_llm = create_llm()
|
|
|
|
|
|
def _render_input(guide_input: UtteranceGuideInput) -> str:
|
|
persona = guide_input.persona
|
|
context = guide_input.context
|
|
lines = [
|
|
f"책: {guide_input.book.title} ({guide_input.book.author})",
|
|
f"\n[참여자]\n{guide_input.next_speaker}",
|
|
"\n[페르소나]",
|
|
f"키워드: {', '.join(persona.keywords)}",
|
|
f"요약: {persona.summary}",
|
|
f"토론 관점: {persona.topic_view_point}",
|
|
f"발화 전략: {persona.current_strategy}",
|
|
f"\n[현재 맥락]\n{context.summary}",
|
|
]
|
|
|
|
if context.current_claims:
|
|
lines.append(f"\n[신규/갱신 주장]\n{render_claims(context.current_claims)}")
|
|
if context.past_claims:
|
|
lines.append(f"\n[과거 주장]\n{render_claims(context.past_claims)}")
|
|
|
|
return "\n".join(lines)
|
|
|
|
|
|
async def build_utterance_guide(guide_input: UtteranceGuideInput) -> UtteranceGuide:
|
|
return await guide_llm.ask(
|
|
UtteranceGuide,
|
|
_render_input(guide_input),
|
|
system=SYSTEM_PROMPT,
|
|
)
|
|
|
|
|
|
async def generate_style_guide(analysis_input: AnalysisInput) -> StyleGuideResult:
|
|
"""지식 기반 스타일 가이드를 생성한다."""
|
|
speaker = analysis_input.next_speaker
|
|
discussion_id = analysis_input.discussion_id
|
|
topic = analysis_input.topic
|
|
previous_context, previous_persona_info = await asyncio.gather(
|
|
load_context(discussion_id),
|
|
load_persona_info(discussion_id, speaker.persona, topic),
|
|
)
|
|
new_turns, previous_turns = analysis_input.new_turns, analysis_input.previous_turns
|
|
if previous_context is None and previous_turns:
|
|
# 저장된 맥락이 만료됐으면 이전 발화까지 처음부터 다시 추출한다
|
|
new_turns, previous_turns = previous_turns + new_turns, []
|
|
extraction_input = ContextExtractionInput(
|
|
topic=topic,
|
|
book=analysis_input.book,
|
|
new_turns=new_turns,
|
|
previous_turns=previous_turns,
|
|
previous_context=previous_context,
|
|
)
|
|
context, (summary, view_point) = await asyncio.gather(
|
|
extract_context(extraction_input),
|
|
prepare_persona(speaker.persona, topic, previous_persona_info),
|
|
)
|
|
strategy = await select_strategy(speaker.name, summary, view_point, context)
|
|
persona_info = PersonaInfo(
|
|
keywords=speaker.persona.keywords,
|
|
summary=summary,
|
|
current_strategy=strategy,
|
|
topic_view_point=view_point,
|
|
)
|
|
guide = await build_utterance_guide(
|
|
UtteranceGuideInput(
|
|
book=analysis_input.book,
|
|
next_speaker=speaker.name,
|
|
persona=persona_info,
|
|
context=context,
|
|
)
|
|
)
|
|
await asyncio.gather(
|
|
save_context(discussion_id, context),
|
|
save_persona_info(discussion_id, speaker.persona, topic, persona_info),
|
|
)
|
|
return StyleGuideResult(context=context, persona_info=persona_info, guide=guide)
|