| 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 enum import Enum |
| 8 | from typing import Any, Dict, List, Protocol |
| 9 | |
| 10 | from llama_models.schema_utils import json_schema_type, webmethod |
| 11 | from pydantic import BaseModel |
| 12 | |
| 13 | from llama_models.llama3.api.datatypes import * # noqa: F403 |
| 14 | |
| 15 | |
| 16 | @json_schema_type |
| 17 | class ViolationLevel(Enum): |
| 18 | INFO = "info" |
| 19 | WARN = "warn" |
| 20 | ERROR = "error" |
| 21 | |
| 22 | |
| 23 | @json_schema_type |
| 24 | class SafetyViolation(BaseModel): |
| 25 | violation_level: ViolationLevel |
| 26 | |
| 27 | # what message should you convey to the user |
| 28 | user_message: Optional[str] = None |
| 29 | |
| 30 | # additional metadata (including specific violation codes) more for |
| 31 | # debugging, telemetry |
| 32 | metadata: Dict[str, Any] = Field(default_factory=dict) |
| 33 | |
| 34 | |
| 35 | @json_schema_type |
| 36 | class RunShieldResponse(BaseModel): |
| 37 | violation: Optional[SafetyViolation] = None |
| 38 | |
| 39 | |
| 40 | class Safety(Protocol): |
| 41 | @webmethod(route="/safety/run_shield") |
| 42 | async def run_shield( |
| 43 | self, shield_type: str, messages: List[Message], params: Dict[str, Any] = None |
| 44 | ) -> RunShieldResponse: ... |