预定义的评委和评分者

概述

MLflow 提供由研究支持的评分标准(作为预定义评分器),用于 SDK 中的常见质量检查。

重要

虽然评委可用作独立 API,但它们必须包装在 自定义评分器 中,供 评估工具生产监视服务使用。 MLflow 提供评分器的预定义实现,但你也可以创建自定义评分器,以使用法官的 API 进行更高级的用例。

法官 关键输入 需要真实数据 它评估什么? 在预定义的评分器中可用
is_context_relevant requestcontext context 是否与用户的 request 直接相关,而不会偏离无关主题? RelevanceToQuery
RetrievalRelevance
is_safe content content 是否包含有害、冒犯性或有毒物质? Safety
is_grounded requestresponsecontext responserequest 是否基于 context 中提供的信息(例如,应用程序没有产生幻觉的回答)? RetrievalGroundedness
is_correct requestresponseexpected_facts 是的 与提供的参考标准 response 相比,requestexpected_facts 是否正确? Correctness
is_context_sufficient requestcontextexpected_facts 是的 context 是否提供了所有必要的信息来生成一个响应,该响应包括给定 expected_facts 的基本事实 request RetrievalSufficiency

运行示例的先决条件

  1. 安装 MLflow 和所需包

    pip install --upgrade "mlflow[databricks]>=3.1.0"
    
  2. 请按照设置环境快速入门创建 MLflow 试验。

3 种使用预构建评判标准的方法

有 3 种方法可以使用预构建评判标准。

1.直接通过 SDK

通过 SDK 直接调用法官,允许您将法官直接集成到应用程序中。 例如,你可能希望在将响应返回给用户之前检查响应的扎实性。

下面是一个使用 is_grounded Judge SDK 的示例。 有关其他示例,请参阅每个法官的页面。

from mlflow.genai.judges import is_grounded

result = is_grounded(
    request="What is the capital of France?",
    response="Paris",
    context="Paris is the capital of France.",
)
# result is...
# mlflow.entities.Assessment.Feedback(
#     rationale="The response asks 'What is the capital of France?' and answers 'Paris'. The retrieved context states 'Paris is the capital of France.' This directly supports the answer given in the response.",
#     feedback=FeedbackValue(value=<CategoricalRating.YES: 'yes'>)
# )

result = is_grounded(
    request="What is the capital of France?",
    response="Paris",
    context="Paris is known for its Eiffel Tower.",
)

# result is...
# mlflow.entities.Assessment.Feedback(
#     rationale="The retrieved context states that 'Paris is known for its Eiffel Tower,' but it does not mention that Paris is the capital of France. Therefore, the response is not fully supported by the retrieved context.",
#     feedback=FeedbackValue(value=<CategoricalRating.NO: 'no'>)
# )

2. 通过预构建的评分器使用

对于更简单的应用程序,可以使用 MLflow 的预定义评分器开始评估。

下面是使用 Correctness 预定义的记分器的示例。 请参阅每个法官的页面以获取其他示例和所需的跟踪数据架构,以使用其预定义的记分器。

eval_dataset = [
    {
        "inputs": {"query": "What is the capital of France?"},
        "outputs": {
            "response": "Paris is the magnificent capital city of France, a stunning metropolis known worldwide for its iconic Eiffel Tower, rich cultural heritage, beautiful architecture, world-class museums like the Louvre, and its status as one of Europe's most important political and economic centers. As the capital city, Paris serves as the seat of France's government and is home to numerous important national institutions."
        },
        "expectations": {
            "expected_facts": ["Paris is the capital of France."],
        },
    },
]

from mlflow.genai.scorers import Correctness

eval_results = mlflow.genai.evaluate(data=eval_dataset, scorers=[Correctness])

3. 在自定义评分器中使用

当应用程序逻辑和评估条件变得更加复杂时,你需要对传递给法官的数据进行更多控制,或者应用程序的跟踪不符合预定义的记分器的要求,可以将法官的 SDK 包装在自定义评分器

下面是一个将 is_grounded 判断 SDK 封装在自定义评分器中的示例。

from mlflow.genai.judges import is_grounded
from mlflow.genai.scorers import scorer

eval_dataset = [
    {
        "inputs": {"query": "What is the capital of France?"},
        "outputs": {
            "response": "Paris",
            "retrieved_context": [
                {
                    "content": "Paris is the capital of France.",
                    "source": "wikipedia",
                }
            ],
        },
    },
]

@scorer
def is_grounded_scorer(inputs: Dict[Any, Any], outputs: Dict[Any, Any]):
    return is_grounded(
        request=inputs["query"],
        response=outputs["response"],
        context=outputs["retrieved_context"],
    )

eval_results = mlflow.genai.evaluate(data=eval_dataset, scorers=[is_grounded_scorer])

评估结果

后续步骤