본문 바로가기
C.W.K.
Stream
Lesson 02 of 05 · published

대화 요약 기억

~22 min · memory, summarization

Level 0Scout
0 XP0/41 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

요약이 원문 대화보다 유리한 이유

대화가 50번 오가면 모델의 문맥 창에 모두 넣기 어려워져. 마지막 N개만 남기는 단순한 방법은 그보다 앞서 일어난 일을 전부 버려. 요약 기억은 원본 로그보다 훨씬 천천히 자라는 누적 요약을 모델이 계속 갱신하게 해.

요약 갱신 순환

  1. N번 대화할 때마다 또는 누적 문맥이 토큰 예산을 넘을 때 LLM에 이전 요약과 새 대화를 함께 넘겨.
  2. 누가 무엇을 말했는지, 핵심 결정, 남은 질문, 해결되지 않은 이견을 보존해 요약하도록 요청해.
  3. 이전 요약을 새 요약으로 바꾸고, 자연스러운 대화 흐름을 위해 가장 최근 K개 대화는 원문 그대로 유지해.

임베딩한 대화에서 세부 내용 복구하기

요약은 설계상 세부 내용을 잃어. 사용자가 '지난 화요일 RRF에 대해 정확히 뭐라고 했어?'라고 묻는다면 원문 대화가 필요해. 모든 대화를 일화 기억 저장소에 임베딩해 두고, 요약이 너무 거칠 때 원본 로그에서 검색해.

Code

누적 요약 갱신기·python
SUMMARY_PROMPT = '''You maintain a running summary of a conversation between Dad and Pippa.

Previous summary:
{summary}

New turns:
{turns}

Produce an updated summary that preserves: who said what, key decisions, outstanding questions, and unresolved disagreements. Keep it under 400 words.
'''

def update_summary(prev_summary: str, new_turns: list[dict]) -> str:
    turn_text = '\n'.join(f"{t['role']}: {t['content']}" for t in new_turns)
    return llm.complete(SUMMARY_PROMPT.format(summary=prev_summary, turns=turn_text))

# 매 10 turn 마다 호출, 또는 토큰 카운트가 threshold 넘을 때.
여러 기억을 결합한 프롬프트·python
def build_prompt(question: str, summary: str, recent_turns: list[dict]):
    episodic = episodic_collection.query(query_embeddings=[embed(question)], n_results=4)
    return [
        {'role': 'system', 'content': f'Conversation summary so far:\n{summary}'},
        {'role': 'system', 'content': 'Relevant past turns:\n' + '\n'.join(episodic['documents'][0])},
        *recent_turns,
        {'role': 'user', 'content': question},
    ]

External links

Exercise

30번 오간 대화 기록으로 누적 요약 갱신기를 구현해. 요약, 최근 다섯 대화, 검색한 일화 기억 네 개를 포함한 프롬프트를 만들어. N개보다 앞선 문맥을 묻는 질문 다섯 개에 답하고, 마지막 N개만 남긴 단순한 방식과 품질을 비교해.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.