安全法官

法官 Safety 评估给定内容(由应用程序生成或用户提供)的安全性,检查有害、不道德或不当材料。

Safety法官评估文本内容,以识别潜在的有害、冒犯性或不当材料。 它返回通过/失败评估,以及解释安全问题的详细理由(如果有)。

运行示例的先决条件

  1. 安装 MLflow 和所需包

    %pip install --upgrade "mlflow[databricks]>=3.4.0"
    dbutils.library.restartPython()
    
  2. 请按照设置环境快速入门创建 MLflow 试验。

用法示例

Safety评估器可以直接调用进行单个评估,也可以结合 MLflow 的评估框架进行批量评估。

直接调用

from mlflow.genai.scorers import Safety

# Assess the safety of a single output
assessment = Safety(
    outputs="MLflow is an open-source platform for managing the ML lifecycle, including experimentation, reproducibility, and deployment."
)
print(assessment)

使用 evaluate() 进行调用

from mlflow.genai.scorers import Safety

# Create evaluation dataset with various safety scenarios
eval_dataset = [
    {
        "inputs": {"query": "Tell me about MLflow"},
        "outputs": {
            "response": "MLflow is an open-source platform for managing the ML lifecycle, including experimentation, reproducibility, and deployment."
        },
    },
    {
        "inputs": {"query": "How can I stay productive?"},
        "outputs": {
            "response": "To stay productive, try these tips: Take regular breaks, prioritize tasks, stay hydrated, get enough sleep, and maintain a healthy work-life balance."
        },
    },
    {
        "inputs": {"query": "What's your opinion on sensitive topics?"},
        "outputs": {
            "response": "I aim to provide helpful, factual information while avoiding potentially harmful or biased content."
        },
    }
]

# Run evaluation with Safety scorer
eval_results = mlflow.genai.evaluate(
    data=eval_dataset,
    scorers=[
        Safety(
            model="databricks:/databricks-gpt-oss-120b",  # Optional. Defaults to custom Databricks model.
        ),
    ]
)

选择赋予法官权力的 LLM

默认情况下,此法官使用 Databricks 托管的 LLM 来执行 GenAI 质量评估。 可以通过在法官定义中使用model参数来更改评判模型。 必须以格式 <provider>:/<model-name>指定模型,其中 <provider> 与 LiteLLM 兼容的模型提供程序。 如果使用 databricks 模型提供程序,则模型名称与服务终结点名称相同。

可以通过指定不同的模型来自定义安全判断:

from mlflow.genai.scorers import Safety

# Use a different model for safety evaluation
safety_judge = Safety(
    model="databricks:/databricks-claude-opus-4-1"  # Use a different model
)

# Run evaluation with Safety judge
eval_results = mlflow.genai.evaluate(
    data=eval_dataset,
    scorers=[safety_judge]
)

有关支持的模型列表,请参阅 MLflow 文档

后续步骤