Aporia Guardrails와 LiteLLM Gateway

Aporia Guardrails와 LiteLLM Gateway

이 튜토리얼에서는 LiteLLM AI Gateway를 Aporia와 함께 사용해 요청에서 PII를 감지하고 응답에서 욕설(profanity)을 감지하는 방법을 알아봐요.

출처: 문서

본문

1. Aporia에 guardrails 설정

Aporia 프로젝트 생성

Aporia에 두 개의 프로젝트를 만드세요:

  • Pre LLM API Call - pre LLM API call에서 실행하려는 모든 정책을 설정
  • Post LLM API Call - post LLM API call에서 실행하려는 모든 정책을 설정

Pre-Call: PII 감지

PII - Prompt를 Pre LLM API Call 프로젝트에 추가하세요.

Post-Call: 응답에서 욕설 감지

Toxicity - Response를 Post LLM API Call 프로젝트에 추가하세요.

2. LiteLLM config.yaml에 guardrails 정의

  • guardrails 섹션 아래에 guardrails를 정의하고 pre_call_guardrailspost_call_guardrails를 설정하세요.
model_list:
  - model_name: gpt-5.6-luna
    litellm_params:
      model: openai/gpt-5.6-luna
      api_key: os.environ/OPENAI_API_KEYguardrails:
  - guardrail_name: "aporia-pre-guard"
    litellm_params:
      guardrail: aporia  # supported values: "aporia", "lakera"
      mode: "during_call"
      api_key: os.environ/APORIA_API_KEY_1
      api_base: os.environ/APORIA_API_BASE_1
  - guardrail_name: "aporia-post-guard"
    litellm_params:
      guardrail: aporia  # supported values: "aporia", "lakera"
      mode: "post_call"
      api_key: os.environ/APORIA_API_KEY_2
      api_base: os.environ/APORIA_API_BASE_2

mode에 지원되는 값

  • pre_call LLM 호출 전에 입력에 대해 실행
  • post_call LLM 호출 후에 입력과 출력에 대해 실행
  • during_call LLM 호출 중에 입력에 대해 실행. pre_call과 같지만 LLM 호출과 병렬로 실행됨. guardrail 검사가 완료될 때까지 응답이 반환되지 않음

3. LiteLLM Gateway 시작

litellm --config config.yaml --detailed_debug

4. 테스트 요청

Langchain, OpenAI SDK 사용 예시

  • 실패하는 호출
  • 성공하는 호출

요청의 [email protected]가 PII이므로 실패할 것으로 예상하세요.

curl -i http://localhost:4000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ***" \
  -d '{
    "model": "gpt-5.6-luna",
    "messages": [
      {"role": "user", "content": "hi my email is [email protected]"}
    ],
    "guardrails": ["aporia-pre-guard", "aporia-post-guard"]
  }'

실패 시 기대되는 응답

{
  "error": {
    "message": {
      "error": "Violated guardrail policy",
      "aporia_ai_response": {
        "action": "block",
        "revised_prompt": null,
        "revised_response": "Aporia detected and blocked PII",
        "explain_log": null
      }
    },
    "type": "None",
    "param": "None",
    "code": "400"
  }
}
curl -i http://localhost:4000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ***" \
  -d '{
    "model": "gpt-5.6-luna",
    "messages": [
      {"role": "user", "content": "hi what is the weather"}
    ],
    "guardrails": ["aporia-pre-guard", "aporia-post-guard"]
  }'

5. 프로젝트(API Key)별 Guardrails 제어

프로젝트마다 어떤 guardrails를 실행할지 제어하려면 이 기능을 사용하세요. 이 튜토리얼에서는 1개 프로젝트(API Key)에 대해 다음 guardrails만 실행하려고 해요:

  • guardrails: ["aporia-pre-guard", "aporia-post-guard"]

1단계 guardrail 설정으로 키 생성하기

  • /key/generate
  • /key/update
curl -X POST 'http://0.0.0.0:4000/key/generate' \
    -H "Authorization: Bearer ***" \
    -H 'Content-Type: application/json' \
    -d '{
            "guardrails": ["aporia-pre-guard", "aporia-post-guard"]
        }
    }'
curl --location 'http://0.0.0.0:4000/key/update' \
    --header "Authorization: Bearer ***" \
    --header 'Content-Type: application/json' \
    --data '{
        "key": "sk-jNm...1kSQ",
        "guardrails": ["aporia-pre-guard", "aporia-post-guard"]
        }}'

2단계 새 키로 테스트

curl --location 'http://0.0.0.0:4000/chat/completions' \
    --header 'Authorization: Bearer ***' \
    --header 'Content-Type: application/json' \
    --data '{
    "model": "gpt-5.6-luna",
    "messages": [
        {
        "role": "user",
        "content": "my email is [email protected]"
        }
    ]}'

더 알아보기 (Learn more)