/moderations 엔드포인트

/moderations 엔드포인트

콘텐츠 모더레이션 분류를 수행하는 /moderations 엔드포인트 사용법을 알려드릴게요. LiteLLM은 모든 지원 프로바이더에서 OpenAI Moderation 파라미터를 받아 변환해 줘요.

출처: 문서

본문

사용법 (Usage)

  • LiteLLM Python SDK / LiteLLM Proxy Server
from litellm import moderation

response = moderation(
    input="hello from litellm",
    model="text-moderation-stable"
)

/moderations 엔드포인트에는 요청이나 litellm config.yaml에 모델을 지정할 필요가 없어요.

config.yaml 설정:

model_list:
  - model_name: text-moderation-stable
    litellm_params:
      model: openai/omni-moderation-latest

LiteLLM proxy server 시작:

litellm --config /path/to/config.yaml

OpenAI Python SDK:

from openai import OpenAI

# set base_url to your proxy server
# set api_key to send to proxy server
client = OpenAI(
    api_key="<proxy-api-key>",
    base_url="http://0.0.0.0:4000"
)

response = client.moderations.create(
    input="hello from litellm",
    model="text-moderation-stable"
)
print(response)

Curl 요청:

curl --location 'http://0.0.0.0:4000/moderations' \
  --header 'Content-Type: application/json' \
  --header "Authorization: Bearer ***" \
  --data '{"input": "Sample text goes here", "model": "text-moderation-stable"}'

입력 파라미터 (Input Params)

LiteLLM은 OpenAI Moderation 파라미터를 받아 모든 지원 프로바이더에 맞게 변환해요.

필수 필드

  • input : string 또는 array — 분류할 입력(들). 단일 문자열, 문자열 배열, 또는 다른 모델과 유사한 멀티모달 입력 객체 배열일 수 있어요.
    • 문자열: 분류할 텍스트 문자열
    • 문자열 배열: 분류할 문자열 여러 개
    • 객체 배열: 모더레이션 모델에 대한 멀티모달 입력. 각 객체는 다음 중 하나일 수 있어요:
      • 이미지 분류 객체: type: string, 필수 — 항상 image_url, image_url: object, 필수 — 이미지 URL 또는 base64 인코딩 이미지의 data URL 포함
      • 텍스트 분류 객체: type: string, 필수 — 항상 text, text: string, 필수 — 분류할 텍스트 문자열

선택적 필드

  • model : string (선택) — 사용할 모더레이션 모델. 기본값은 omni-moderation-latest.

출력 형식 (Output Format)

모든 모더레이션 호출에서 기대할 수 있는 정확한 json 출력과 타입이에요. LiteLLM은 OpenAI의 출력 형식을 따릅니다.

{
  "id": "modr-AB8CjOTu2jiq12hp1AQPfeqFWaORR",
  "model": "text-moderation-007",
  "results": [
    {
      "flagged": true,
      "categories": {
        "sexual": false,
        "hate": false,
        "harassment": true,
        "self-harm": false,
        "sexual/minors": false,
        "hate/threatening": false,
        "violence/graphic": false,
        "self-harm/intent": false,
        "self-harm/instructions": false,
        "harassment/threatening": true,
        "violence": true
      },
      "category_scores": {
        "sexual": 0.000011726012417057063,
        "hate": 0.22706663608551025,
        "harassment": 0.5215635299682617,
        "self-harm": 2.227119921371923e-6,
        "sexual/minors": 7.107352217872176e-8,
        "hate/threatening": 0.023547329008579254,
        "violence/graphic": 0.00003391829886822961,
        "self-harm/intent": 1.646940972932498e-6,
        "self-harm/instructions": 1.1198755256458526e-9,
        "harassment/threatening": 0.5694745779037476,
        "violence": 0.9971134662628174
      }
    }
  ]
}

지원 프로바이더 (Supported Providers)

⚡️ 지원되는 모든 모델과 프로바이더는 models.litellm.ai에서 확인할 수 있어요.

  • OpenAI

더 알아보기 (Learn more)