| 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # This source code is licensed under the terms described in the LICENSE file in |
| 5 | # the root directory of this source tree. |
| 6 | |
| 7 | from typing import List, Optional |
| 8 | |
| 9 | from llama_models.sku_list import CoreModelId, safety_models |
| 10 | |
| 11 | from pydantic import BaseModel, validator |
| 12 | |
| 13 | |
| 14 | class LlamaGuardShieldConfig(BaseModel): |
| 15 | model: str = "Llama-Guard-3-8B" |
| 16 | excluded_categories: List[str] = [] |
| 17 | disable_input_check: bool = False |
| 18 | disable_output_check: bool = False |
| 19 | |
| 20 | @validator("model") |
| 21 | @classmethod |
| 22 | def validate_model(cls, model: str) -> str: |
| 23 | permitted_models = [ |
| 24 | m.descriptor() |
| 25 | for m in safety_models() |
| 26 | if m.core_model_id == CoreModelId.llama_guard_3_8b |
| 27 | ] |
| 28 | if model not in permitted_models: |
| 29 | raise ValueError( |
| 30 | f"Invalid model: {model}. Must be one of {permitted_models}" |
| 31 | ) |
| 32 | return model |
| 33 | |
| 34 | |
| 35 | class PromptGuardShieldConfig(BaseModel): |
| 36 | model: str = "Prompt-Guard-86M" |
| 37 | |
| 38 | @validator("model") |
| 39 | @classmethod |
| 40 | def validate_model(cls, model: str) -> str: |
| 41 | permitted_models = [ |
| 42 | m.descriptor() |
| 43 | for m in safety_models() |
| 44 | if m.core_model_id == CoreModelId.prompt_guard_86m |
| 45 | ] |
| 46 | if model not in permitted_models: |
| 47 | raise ValueError( |
| 48 | f"Invalid model: {model}. Must be one of {permitted_models}" |
| 49 | ) |
| 50 | return model |
| 51 | |
| 52 | |
| 53 | class SafetyConfig(BaseModel): |
| 54 | llama_guard_shield: Optional[LlamaGuardShieldConfig] = None |
| 55 | prompt_guard_shield: Optional[PromptGuardShieldConfig] = None |