새 가드레일 통합 추가하기
새 가드레일 통합 추가하기
LLM에 들어가기 전이나 나온 후에 텍스트를 검사하는 클래스를 만들게 돼요. 규칙을 위반하면 차단하는 구조예요.
동작 방식
가드레일이 있는 요청:
curl --location 'http://localhost:4000/chat/completions' \
--header "Authorization: Bearer ***" \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-5.6-terra",
"messages": [{"role": "user", "content": "How do I hack a system?"}],
"guardrails": ["my-guardrail"]
}'
가드레일은 입력을 확인하고, 그다음 출력을 확인해요. 문제가 있으면 예외를 발생시킵니다.
가드레일 만들기
디렉터리 만들기
mkdir -p litellm/proxy/guardrails/guardrail_hooks/my_guardrail
cd litellm/proxy/guardrails/guardrail_hooks/my_guardrail
파일 두 개: my_guardrail.py(메인 클래스)와 __init__.py(초기화).
메인 클래스 작성
my_guardrail.py:
Custom Guardrail 튜토리얼을 따르세요.
Init 파일 만들기
__init__.py:
from typing import TYPE_CHECKING
from litellm.types.guardrails import SupportedGuardrailIntegrations
from .my_guardrail import MyGuardrail
if TYPE_CHECKING:
from litellm.types.guardrails import Guardrail, LitellmParams
def initialize_guardrail(litellm_params: "LitellmParams", guardrail: "Guardrail"):
import litellm
_my_guardrail_callback = MyGuardrail(
api_base=litellm_params.api_base,
api_key=litellm_params.api_key,
guardrail_name=guardrail.get("guardrail_name", ""),
event_hook=litellm_params.mode,
default_on=litellm_params.default_on,
)
litellm.logging_callback_manager.add_litellm_callback(_my_guardrail_callback)
return _my_guardrail_callback
guardrail_initializer_registry = {
SupportedGuardrailIntegrations.MY_GUARDRAIL.value: initialize_guardrail,
}
guardrail_class_registry = {
SupportedGuardrailIntegrations.MY_GUARDRAIL.value: MyGuardrail,
}
가드레일 타입 등록
litellm/types/guardrails.py 에 추가하세요:
class SupportedGuardrailIntegrations(str, Enum):
LAKERA = "lakera_prompt_injection"
APORIA = "aporia"
BEDROCK = "bedrock_guardrails"
PRESIDIO = "presidio"
ZSCALER_AI_GUARD = "zscaler_ai_guard"
MY_GUARDRAIL = "my_guardrail"
출처: 문서
본문
사용법
Config 파일
model_list:
- model_name: gpt-5.6-terra
litellm_params:
model: gpt-5.6-terra
api_key: os.environ/OPENAI_API_KEY
guardrails:
- guardrail_name: my_guardrail
litellm_params:
guardrail: my_guardrail
mode: during_call
api_key: os.environ/MY_GUARDRAIL_API_KEY
api_base: https://api.myguardrail.com
요청별 사용
curl --location 'http://localhost:4000/chat/completions' \
--header "Authorization: Bearer ***" \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-5.6-terra",
"messages": [{"role": "user", "content": "Test message"}],
"guardrails": ["my_guardrail"]
}'
테스트
test_litellm/ 폴더 안에 유닛 테스트를 추가하세요.