C.W.K.
Stream
Lesson 02 of 05 · published

Prompt assembly: Stuff, Map-Reduce, Refine

~22 min · rag, prompts

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

세 패턴, 각각 이기는 세 context

1. Stuff (default)

모든 retrieved 청크를 하나의 prompt 에 넣고 LLM 답하게. 단순, 싸고, top-k 청크가 context window 에 편안히 들어갈 때 옳은 답. 200K-token Claude context 면 대부분 앱 cover.

2. Map-reduce

각 청크마다 intermediate answer 생성 (map). 그 다음 intermediate answer 들을 final response 로 요약 (reduce). 코퍼스가 stuff 하기 너무 클 때 또는 답이 많은 source 종합해야 할 때 사용.

3. Refine

첫 청크에서 initial answer 생성, 후속 각 청크로 iteratively update. 순서 있는 자료 (transcript, time-series) 에서 새 context 가 override 아니라 refine 해야 할 때 유용.

고르는 법

Default 가 stuff. context overflow 또는 eval 에서 모델이 position 5 이후 청크 underuse 보일 때 map-reduce 로 전환. 순서 있는 콘텐츠에 refine. 거의 어떤 앱도 셋 다 동시에 안 필요.

Code

Map-reduce sketch·python
def rag_map_reduce(question: str, k: int = 20):
    chunks = retrieve(question, k=k)
    intermediates = [
        llm.complete(f'Answer this question using ONLY the chunk below.\n\nQuestion: {question}\n\nChunk: {c["text"]}')
        for c in chunks
    ]
    combined = '\n\n'.join(intermediates)
    final = llm.complete(
        f'Combine these answers into a single coherent response. Discard contradictory or irrelevant pieces.\n\n{combined}'
    )
    return final

External links

Exercise

5+ 청크 종합 필요한 질문 10개에 'stuff' vs 'map-reduce' 비교. 답 품질 + 총 토큰 비용 트래킹. 어느 질문 모양에서 비용이 가치 있는지 결정 + retriever 에 그 결정 코멘트로 작성.

Progress

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

댓글 0

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

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