가드레일 공급자: 커스텀 가드레일

가드레일 공급자: 커스텀 가드레일 (Custom Guardrail / CustomGuardrail)

CustomGuardrail 클래스를 사용해 LiteLLM에서 직접 정의한 커스텀 가드레일을 만들어요. 어떤 공급자가 아닌 자체 로직을 프롬프트·응답 검사에 사용할 때 쓰는 범용 기반 클래스로, 이 문서는 그 사용법과 가드레일 파라미터 전달·모드별 동작을 다뤄요.

출처: 문서

본문

개요 (Overview)

CustomGuardrail은 LiteLLM의 내장 커스텀 가드레일 기반 클래스예요. 이를 서브클래싱하고 async_pre_call_hook, async_post_call_success_hook 같은 훅 메서드를 구현해 사용자 정의 검사를 프록시 요청 파이프라인에 끼워넣을 수 있어요.

빠른 시작 (Quick Start)

1. 커스텀 가드레일 파일 작성

from litellm.integrations.custom_guardrail import CustomGuardrail
from litellm.proxy._types import UserAPIKeyAuth
from fastapi import HTTPException

class MyGuardrail(CustomGuardrail):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    async def async_pre_call_hook(self, data, user_api_key_dict, call_type):
        # 입력 검사 로직
        return data

2. config.yaml에서 등록

guardrails:
  - guardrail_name: "custom-guard"
    litellm_params:
      guardrail: custom_guardrail.MyGuardrail
      mode: "pre_call"
      default_on: true

파라미터 (Parameters)

  • guardrail: <파일명>.<클래스명> 형식의 커스텀 클래스 경로
  • mode: pre_call, post_call, during_call
  • default_on: 기본으로 실행할지 여부

참고 사항 (Notes)

가드레일에 전달할 커스텀 파라미터, CustomGuardrail의 전체 훅 시그니처, 그리고 프로젝트/API 키별 가드레일 제어 등 자세한 내용은 원문 문서를 참고하세요.