모듈을 바꿔 추론 전략을 바꾸기
모듈을 바꿔 추론 전략을 바꾸기
같은 시그니처라도 어떤 모듈로 실행하느냐에 따라 추론 방식이 달라져요. Predict로 시그니처를 실행했던 코드를 ChainOfThought로 한 단어만 바꾸면, 모델이 답을 내기 전에 먼저 추론하도록 지시가 바뀝니다. DSPy의 모듈 레이어가 얼마나 유연한지 보여주는 좋은 예시예요.
출처: 공식문서
Predict → ChainOfThought 한 줄 변경
앞선 예시에서는 Predict 모듈로 시그니처를 실행했어요. 다른 모듈들은 같은 작업을 수행하는 다른 전략을 정의하며, 바꿔 끼우는 건 아주 간단합니다.
reasoning_haiku_bot = dspy.ChainOfThought(HaikuBot)
result = reasoning_haiku_bot(location="Bodega Bay", mood="mysterious", season="autumn")
print(result.haiku)
그러면 이런 결과가 나와요.
Fog curls over waves
Leaves whisper, gulls glide at night
Moon pierces deep mist
Predict 대신 ChainOfThought를 썼죠. 이 모듈은 모델이 최종 답을 내기 전에 먼저 추론하도록 프롬프트하는 모듈입니다. 클래스 기반 시그니처 HaikuBot은 그대로고, 함수 호출도 완전히 동일해요.
하지만 무대 뒤에서는 DSPy가 우리 시그니처를 수정해서, 모델이 최종 시를 쓰기 전에 추론을 하도록 했어요. 새로 추가된 출력 필드 reasoning에는 이제 모델의 근거(rationale)가 담깁니다.
result.reasoning을 출력하면 이를 확인할 수 있어요.
Bodega Bay in autumn evokes a cool, misty atmosphere, and the mysterious mood invites subtle, haunting imagery. I chose fog, waves, and gulls to ground the poem in the specific coastal setting, while autumnal leaves signal the season. The haiku’s 5‑7‑5 syllable structure preserves the traditional form, allowing each image to resonate briefly. The first line establishes the misty shoreline, the second line introduces quiet, nocturnal movement, and the third line offers a luminous, almost spectral image of the moon cutting through the fog—capturing mystery and the fleeting quality of autumn evenings.
ChainOfThought는 단순하지만 DSPy 모듈 레이어의 유연함을 잘 보여줘요. 다른 모듈들은 모델 앙상블, 코딩 샌드박스, 그리고 다음 섹션에서 다룰 도구 호출(tool-calling) 같은 다양한 전략을 구현합니다.
더 알아보기 (Learn more)
- 클래스 기반 시그니처 — 위에서 쓴
HaikuBot시그니처를 정의하는 법. - Built-in module variants — DSPy가 제공하는 다른 모듈 전략들.