101 lines
2.7 KiB
Python
101 lines
2.7 KiB
Python
"""맥락 추출과 심층 분석의 입출력 모델."""
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
from models.persona import PersonaInput
|
|
|
|
|
|
class BookMetadata(BaseModel):
|
|
title: str
|
|
author: str
|
|
|
|
|
|
class DiscussionTurn(BaseModel):
|
|
speaker: str
|
|
text: str
|
|
|
|
|
|
class Reason(BaseModel):
|
|
reason: str
|
|
utterances: list[str] # 이 근거가 나온 발화 원문
|
|
book_excerpts: list[str] | None = None # 발화에서 책 원문을 인용한 부분
|
|
|
|
|
|
class Claim(BaseModel):
|
|
claim_id: str
|
|
speaker: str
|
|
claim: str
|
|
utterance: str # 이 주장이 처음 나온 발화 원문
|
|
reasons: list[Reason] = []
|
|
|
|
|
|
def render_claims(claims: list[Claim]) -> str:
|
|
lines = []
|
|
for claim in claims:
|
|
lines.append(f"[{claim.claim_id}] {claim.speaker}: {claim.claim}")
|
|
for reason in claim.reasons:
|
|
lines.append(f" 근거: {reason.reason}")
|
|
for utterance in reason.utterances:
|
|
lines.append(f" 발화: {utterance}")
|
|
for book_excerpt in reason.book_excerpts or []:
|
|
lines.append(f" 책 인용: {book_excerpt}")
|
|
return "\n".join(lines)
|
|
|
|
|
|
class DiscussionContext(BaseModel):
|
|
summary: str
|
|
current_claims: list[Claim] = [] # 이번 발화로 새로 생기거나 갱신된 주장
|
|
past_claims: list[Claim] = [] # 그 이전까지 쌓인 주장
|
|
|
|
|
|
class DiscussionInput(BaseModel):
|
|
topic: str
|
|
book: BookMetadata
|
|
new_turns: list[DiscussionTurn] = Field(min_length=1) # 구 맥락에 아직 반영되지 않은 발화 전부
|
|
previous_turns: list[DiscussionTurn] = [] # 구 맥락에 이미 반영된 발화
|
|
|
|
|
|
class ContextExtractionInput(DiscussionInput):
|
|
previous_context: DiscussionContext | None = None # 첫 턴에는 없음
|
|
|
|
|
|
class NextSpeaker(BaseModel):
|
|
name: str
|
|
persona: PersonaInput
|
|
|
|
|
|
class AnalysisInput(DiscussionInput):
|
|
discussion_id: str # 저장해 둔 맥락·페르소나 정보를 찾는 토론 ID
|
|
next_speaker: NextSpeaker
|
|
|
|
|
|
# 맥락 추출 단계별 LLM 출력. structured output 스키마로 쓰이므로 기본값을 두지 않는다.
|
|
|
|
class NewClaim(BaseModel):
|
|
claim: str
|
|
turn_id: str # 주장이 나온 최신 발화의 번호. 코드가 화자와 원문으로 바꾼다
|
|
|
|
|
|
class ClaimClassification(BaseModel):
|
|
new_claims: list[NewClaim]
|
|
updated_claim_ids: list[str]
|
|
|
|
|
|
class ReasonDraft(BaseModel):
|
|
reason: str
|
|
turn_ids: list[str] # 프롬프트에 번호 붙여 보여준 발화 중 고른 것. 코드가 원문으로 바꾼다
|
|
book_excerpts: list[str]
|
|
|
|
|
|
class ClaimReasons(BaseModel):
|
|
claim_id: str
|
|
reasons: list[ReasonDraft]
|
|
|
|
|
|
class ReasonAnalysis(BaseModel):
|
|
claims: list[ClaimReasons]
|
|
|
|
|
|
class ContextSummary(BaseModel):
|
|
summary: str
|