모델 선택

모델 선택 (Select Model)

SelectModel은 런 의존성, 메시지 기록, 사용량, 또는 현재 스텝에서 모델을 선택하는 캐퍼빌리티예요. 선택기는 런 설정 중에 먼저 평가되므로, 에이전트에 생성자 모델이 필요하지 않습니다.

출처: 문서

본문

from dataclasses import dataclass
from typing import Literal

from pydantic_ai import Agent, ModelSelectionContext
from pydantic_ai.capabilities import SelectModel


@dataclass
class Deps:
    """Dependencies that influence model selection."""

    task_complexity: Literal['standard', 'complex']


def select_model(ctx: ModelSelectionContext[Deps]) -> str:
    """Use the larger model for complex tasks."""
    return 'openai:gpt-5.6-sol' if ctx.deps.task_complexity == 'complex' else 'openai:gpt-5.6-luna'


agent = Agent(deps_type=Deps, capabilities=[SelectModel(select_model)])

SelectModel은 항상 callable을 받으며, 그것은 각각의 새로운 논리 모델 요청 스텝 전에 평가됩니다. callable은 동기 또는 비동기일 수 있어요. 여러 스텝에서 같은 모델 ID를 반환하면, 해석된 모델/공급자 인스턴스는 그 런의 나머지 동안 재사용됩니다. 같은 스텝 안의 공급자 측 연속 폴링(continuation polling)은 선택된 모델에 고정됩니다. 커스텀 캐퍼빌리티에서 훅을 구현하고 우선순위·라이프사이클 세부 사항을 보려면 Selecting the model를 참고하세요.

더 알아보기 (Learn more)