dspy.Predict

dspy.Predict

dspy.Predict(signature: str | type[Signature], callbacks: list[BaseCallback] | None = None, **config)

  • 기반 클래스(base): Module, Parameter

언어 모델로 입력을 출력에 매핑하는 가장 기본적인 DSPy 모듈이에요. 서명 하나와 옵션 설정(config)을 받아, LM 호출을 만들고 결과를 Prediction으로 돌려줘요.

파라미터

이름 타입 설명 기본값
signature str | type[Signature] 작업을 설명하는 입력/출력 서명 필수
callbacks list[BaseCallback] | None 계측(instrumentation)을 위한 선택적 콜백 목록 None
**config 내부 언어 모델로 전달되는 기본 키워드 인자. 호출 시점에 config 사전을 넘기면 단일 호출에 대해 이 값을 덮어쓸 수 있어요 {}

**config의 예:

predict = dspy.Predict("q -> a", rollout_id=1, temperature=1.0)
predict(q="What is 1 + 52?", config={"rollout_id": 2, "temperature": 1.0})

동작 방식

Predict는 호출되면 _forward_preprocess로 LM·설정·서명·데모·입력을 정리하고, 어댑터(settings.adapter 또는 ChatAdapter())를 통해 LM을 호출한 뒤 _forward_postprocess로 결과를 정리해요. 이때 기본적으로 어댑터를 ChatAdapter로 쓰고, 스트리밍 여부에 따라 처리 흐름이 달라져요. 결과로는 Prediction 객체가 돌아오고, 서명의 출력 필드에 접근할 수 있어요(예: result.answer).

주요 메서드

  • __call__(*args, **kwargs) — 모듈 호출. 위치 인자(positional args)를 넘기면 ValueError
  • acall(*args, **kwargs) (async) — 비동기 호출
  • forward(**kwargs) — 동기 실행 본체
  • get_config() / update_config(**kwargs) — 실행 설정 조회·갱신
  • set_lm(lm) / get_lm() — 모듈의 predictor에 LM 재귀 설정/조회. 서로 다른 LM이 여러 개면 ValueError
  • inspect_history(n=1, file=None) — 최근 LM 호출 히스토리 출력
  • batch(examples, ...)dspy.Example 목록을 Parallel 모듈로 병렬 처리
  • load(path, ...) / save(...) / deepcopy() / dump_state(json_mode=True) — 직렬화·상태 관리

더 알아보기 (Learn more)