모듈을 바꿔 추론 전략 바꾸기

모듈을 바꿔 추론 전략 바꾸기 (Change inference strategies by changing the module)

시그니처가 "무엇을" 할지 정한다면, 모듈은 "어떻게" 할지를 정해요. 같은 시그니처를 다른 모듈에 넘기기만 해도 추론 전략이 통째로 바뀝니다. 이번 강의에서는 그걸 직접 체험해 볼게요.

출처: 문서

본문

지금까지의 예시에서는 시그니처를 실행하는 데 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 를 사용했어요. 이 모듈은 최종 답을 내놓기 전에 LM이 먼저 추론(reason) 하도록 프롬프트를 구성합니다. 시그니처인 클래스 기반 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) 등 다른 전략들을 구현합니다.

다음: ReAct로 도구 사용하기 →

더 알아보기 (Learn more)