promptflow.evals.evaluators 模块#

class promptflow.evals.evaluators.BleuScoreEvaluator#

基类:object

计算两个字符串之间 BLEU 分数的评估器。

BLEU (Bilingual Evaluation Understudy) 分数常用于自然语言处理 (NLP) 和机器翻译。它广泛用于文本摘要和文本生成用例。它评估生成文本与参考文本的匹配程度。BLEU 分数范围从 0 到 1,分数越高表示质量越好。

用法

eval_fn = BleuScoreEvaluator()
result = eval_fn(
    answer="Tokyo is the capital of Japan.",
    ground_truth="The capital of Japan is Tokyo.")

输出格式

{
    "bleu_score": 0.22
}
__call__(*, answer: str, ground_truth: str, **kwargs)#

评估答案和真实值之间的 BLEU 分数。

参数:
  • answer (str) – 要评估的答案。

  • ground_truth (str) – 用于比较的真实值。

返回:

BLEU 分数。

返回类型:

dict

class promptflow.evals.evaluators.ChatEvaluator(model_config: Union[AzureOpenAIModelConfiguration, OpenAIModelConfiguration], eval_last_turn: bool = False, parallel: bool = True)#

基类:object

初始化针对特定 Azure OpenAI 模型配置的聊天评估器。

参数:
  • model_config (Union[AzureOpenAIModelConfiguration, OpenAIModelConfiguration]) – Azure OpenAI 模型的配置。

  • eval_last_turn (bool) – 设置为 True 以仅评估对话中的最新交互,重点关注最新的用户查询和助手对应的响应。默认为 False

  • parallel (bool) – 如果为 True,则评估器使用并行执行。否则,使用顺序执行。默认为 True。

返回:

一个评估并生成“聊天”场景指标的函数。

返回类型:

可调用

用法

chat_eval = ChatEvaluator(model_config)
conversation = [
    {"role": "user", "content": "What is the value of 2 + 2?"},
    {"role": "assistant", "content": "2 + 2 = 4", "context": {
        "citations": [
                {"id": "math_doc.md", "content": "Information about additions: 1 + 2 = 3, 2 + 2 = 4"}
                ]
        }
    }
]
result = chat_eval(conversation=conversation)

输出格式

{
    "evaluation_per_turn": {
        "gpt_retrieval": [1.0, 2.0],
        "gpt_groundedness": [5.0, 2.0],
        "gpt_relevance": [3.0, 5.0],
        "gpt_coherence": [1.0, 2.0],
        "gpt_fluency": [3.0, 5.0]
    }
    "gpt_retrieval": 1.5,
    "gpt_groundedness": 3.5,
    "gpt_relevance": 4.0,
    "gpt_coherence": 1.5,
    "gpt_fluency": 4.0
}
__call__(*, conversation, **kwargs)#

评估聊天场景。

参数:

conversation (List[Dict]) – 要评估的对话。每个回合应具有“role”和“content”键。“context”键对于助手的回合是可选的,并且应具有包含引用的列表的“citations”键。

返回:

聊天场景的分数。

返回类型:

dict

class promptflow.evals.evaluators.CoherenceEvaluator(model_config: Union[AzureOpenAIModelConfiguration, OpenAIModelConfiguration])#

基类:object

初始化针对特定 Azure OpenAI 模型配置的连贯性评估器。

参数:

model_config (Union[AzureOpenAIModelConfiguration, OpenAIModelConfiguration]) – Azure OpenAI 模型的配置。

用法

eval_fn = CoherenceEvaluator(model_config)
result = eval_fn(
    question="What is the capital of Japan?",
    answer="The capital of Japan is Tokyo.")

输出格式

{
    "gpt_coherence": 1.0
}
__call__(*, question: str, answer: str, **kwargs)#

评估连贯性。

参数:
  • question (str) – 要评估的问题。

  • answer (str) – 要评估的答案。

返回:

连贯性分数。

返回类型:

Dict[str, float]

class promptflow.evals.evaluators.ContentSafetyChatEvaluator(project_scope: dict, eval_last_turn: bool = False, parallel: bool = True, credential=None)#

基类:object

初始化内容安全聊天评估器,配置用于评估聊天场景的内容安全指标。

参数:
  • project_scope (dict) – Azure AI 项目的范围。它包含订阅 ID、资源组和项目名称。

  • eval_last_turn (bool) – 设置为 True 以仅评估对话中的最新交互,重点关注最新的用户查询和助手对应的响应。默认为 False

  • parallel (bool) – 如果为 True,则评估器使用并行执行。否则,使用顺序执行。默认为 True。

  • credential (TokenCredential) – 用于连接到 Azure AI 项目的凭据。

返回:

一个评估并生成“聊天”场景指标的函数。

返回类型:

可调用

用法

eval_fn = ContentSafetyChatEvaluator(model_config)
conversation = [
    {"role": "user", "content": "What is the value of 2 + 2?"},
    {"role": "assistant", "content": "2 + 2 = 4"}
]
result = ContentSafetyChatEvaluator(conversation=conversation)

输出格式

{
    "evaluation_per_turn": {
        "violence": ["High", "Low"],
        "violence_score": [7.0, 3.0],
        "violence_reason": "Some reason",
        "sexual": ["High", "Low"],
        "sexual_score": [7.0, 3.0],
        "sexual_reason": "Some reason",
        "self_harm": ["High", "Low"],
        "self_harm_score": [7.0, 3.0],
        "self_harm_reason": "Some reason",
        "hate_unfairness": ["High", "Low"],
        "hate_unfairness_score": [7.0, 3.0],
        "hate_unfairness_reason": "Some reason"
    },
    "violence": "Medium",
    "violence_score": 5.0,
    "sexual": "Medium",
    "sexual_score": 5.0,
    "self_harm": "Medium",
    "self_harm_score": 5.0,
    "hate_unfairness": "Medium",
    "hate_unfairness_score": 5.0,
}
__call__(*, conversation, **kwargs)#

评估“聊天”场景的内容安全指标。

参数:

conversation (List[Dict]) – 要评估的对话。每个回合应具有“role”和“content”键。

返回:

聊天场景的分数。

返回类型:

dict

class promptflow.evals.evaluators.ContentSafetyEvaluator(project_scope: dict, parallel: bool = True, credential=None)#

基类:object

初始化内容安全评估器,配置用于评估 QA 场景的内容安全指标。

参数:
  • project_scope (dict) – Azure AI 项目的范围。它包含订阅 ID、资源组和项目名称。

  • parallel – 如果为 True,则评估器使用并行执行。否则,使用顺序执行。默认为 True。

  • credential (TokenCredential) – 用于连接到 Azure AI 项目的凭据。

返回:

一个评估并生成“问答”场景内容安全指标的函数。

返回类型:

可调用

用法

project_scope = {
    "subscription_id": "<subscription_id>",
    "resource_group_name": "<resource_group_name>",
    "project_name": "<project_name>",
}
eval_fn = ContentSafetyEvaluator(project_scope)
result = eval_fn(
    question="What is the capital of France?",
    answer="Paris.",
)

输出格式

{
    "violence": "Medium",
    "violence_score": 5.0,
    "violence_reason": "Some reason",
    "sexual": "Medium",
    "sexual_score": 5.0,
    "sexual_reason": "Some reason",
    "self_harm": "Medium",
    "self_harm_score": 5.0,
    "self_harm_reason": "Some reason",
    "hate_unfairness": "Medium",
    "hate_unfairness_score": 5.0,
    "hate_unfairness_reason": "Some reason"
}
__call__(*, question: str, answer: str, **kwargs)#

评估“问答”场景的内容安全指标。

参数:
  • question (str) – 要评估的问题。

  • answer (str) – 要评估的答案。

  • parallel (bool) – 是否并行评估。

返回:

内容安全的分数。

返回类型:

dict

class promptflow.evals.evaluators.F1ScoreEvaluator#

基类:object

初始化 F1 分数评估器以计算 F1 分数。

用法

eval_fn = F1ScoreEvaluator()
result = eval_fn(
    answer="The capital of Japan is Tokyo.",
    ground_truth="Tokyo is Japan's capital, known for its blend of traditional culture                 and technological advancements.")

输出格式

{
    "f1_score": 0.42
}
__call__(*, answer: str, ground_truth: str, **kwargs)#

评估 F1 分数。

参数:
  • answer (str) – 要评估的答案。

  • ground_truth (str) – 要评估的真实值。

返回:

F1 分数。

返回类型:

dict

class promptflow.evals.evaluators.FluencyEvaluator(model_config: Union[AzureOpenAIModelConfiguration, OpenAIModelConfiguration])#

基类:object

初始化针对特定 Azure OpenAI 模型配置的流利度评估器。

参数:

model_config (Union[AzureOpenAIModelConfiguration, OpenAIModelConfiguration]) – Azure OpenAI 模型的配置。

用法

eval_fn = FluencyEvaluator(model_config)
result = eval_fn(
    question="What is the capital of Japan?",
    answer="The capital of Japan is Tokyo.")

输出格式

{
    "gpt_fluency": 4.0
}
__call__(*, question: str, answer: str, **kwargs)#

评估流利度。

参数:
  • question (str) – 要评估的问题。

  • answer (str) – 要评估的答案。

返回:

流利度分数。

返回类型:

dict

class promptflow.evals.evaluators.GleuScoreEvaluator#

基类:object

计算两个字符串之间 BLEU 分数的评估器。

GLEU (Google-BLEU) 分数评估器通过评估 n-gram 重叠来衡量生成文本与参考文本之间的相似性,同时考虑精确度和召回率。这种为句子级评估设计的平衡评估使其成为翻译质量详细分析的理想选择。GLEU 适用于机器翻译、文本摘要和文本生成等用例。

用法

eval_fn = GleuScoreEvaluator()
result = eval_fn(
    answer="Tokyo is the capital of Japan.",
    ground_truth="The capital of Japan is Tokyo.")

输出格式

{
    "gleu_score": 0.41
}
__call__(*, ground_truth: str, answer: str, **kwargs)#

评估答案和真实值之间的 GLEU 分数。

参数:
  • answer (str) – 要评估的答案。

  • ground_truth (str) – 用于比较的真实值。

返回:

GLEU 分数。

返回类型:

dict

class promptflow.evals.evaluators.GroundednessEvaluator(model_config: Union[AzureOpenAIModelConfiguration, OpenAIModelConfiguration])#

基类:object

初始化针对特定 Azure OpenAI 模型配置的接地性评估器。

参数:

model_config (Union[AzureOpenAIModelConfiguration, OpenAIModelConfiguration]) – Azure OpenAI 模型的配置。

用法

eval_fn = GroundednessEvaluator(model_config)
result = eval_fn(
    answer="The capital of Japan is Tokyo.",
    context="Tokyo is Japan's capital, known for its blend of traditional culture                 and technological advancements.")

输出格式

{
    "gpt_groundedness": 5
}
__call__(*, answer: str, context: str, **kwargs)#

评估答案在上下文中的接地性。

参数:
  • answer (str) – 要评估的答案。

  • context (str) – 评估答案的上下文。

返回:

接地性分数。

返回类型:

dict

class promptflow.evals.evaluators.HateUnfairnessEvaluator(project_scope: dict, credential=None)#

基类:object

初始化仇恨不公平评估器以评估仇恨不公平分数。

参数:
  • project_scope (dict) – Azure AI 项目的范围。它包含订阅 ID、资源组和项目名称。

  • credential (TokenCredential) – 用于连接到 Azure AI 项目的凭据。

用法

project_scope = {
    "subscription_id": "<subscription_id>",
    "resource_group_name": "<resource_group_name>",
    "project_name": "<project_name>",
}
eval_fn = HateUnfairnessEvaluator(project_scope)
result = eval_fn(question="What is the capital of France?", answer="Paris.")

输出格式

{
    "hate_unfairness": "High",
    "hate_unfairness_score": 6.5,
    "hate_unfairness_reason": "Some reason"
}
__call__(*, question: str, answer: str, **kwargs)#

评估仇恨不公平内容。

参数:
  • question (str) – 要评估的问题。

  • answer (str) – 要评估的答案。

返回:

仇恨不公平分数。

返回类型:

dict

class promptflow.evals.evaluators.IndirectAttackEvaluator(project_scope: dict, eval_last_turn: bool = False, credential=None)#

基类:object

初始化 XPIA(跨域提示注入攻击)越狱评估器,用于检测 AI 系统的响应中是否存在跨域注入攻击。

参数:
  • project_scope (dict) – Azure AI 项目的范围。它包含订阅 ID、资源组和项目名称。

  • eval_last_turn (bool) – 设置为 True 以仅评估对话中的最新交互,重点关注最新的用户查询和助手对应的响应。默认为 False

  • credential (TokenCredential) – 用于连接到 Azure AI 项目的凭据。

返回:

一个评估并生成 XPIA 聊天场景指标的函数。指标包括 Q/A 对的整体评估标签和原因,以及操纵内容、入侵和信息子标签。

返回类型:

可调用

用法

eval_fn = IndirectAttackEvaluator(model_config)
result = eval_fn(question="What is the capital of France?", answer="Paris.")

问答对的输出格式

{
    'xpia_label': False,
    'xpia_reason': 'The conversation does not contain any manipulated content, intrusion or
                  information gathering.'
    'xpia_information_gathering': False,
    'xpia_intrusion': False
    'xpia_manipulated_content': False
}
__call__(*, question: Optional[str], answer: Optional[str], **kwargs)#

根据对话上下文中是否存在注入攻击来评估内容,这些攻击旨在通过引发操纵内容、入侵和尝试收集超出 AI 系统范围的信息来中断正常的预期功能。

参数:
  • question (Optional[str]) – 要评估的问题。与“conversation”互斥。

  • answer (Optional[str]) – 要评估的答案。与“conversation”互斥。

返回:

评估分数和原因。

返回类型:

dict

class promptflow.evals.evaluators.MeteorScoreEvaluator(alpha: float = 0.9, beta: float = 3.0, gamma: float = 0.5)#

基类:object

计算两个字符串之间 METEOR 分数的评估器。

METEOR (Metric for Evaluation of Translation with Explicit Ordering) 分数评估器通过将生成文本与参考文本进行比较来评估生成文本,重点关注精确度、召回率和内容对齐。它通过考虑同义词、词干提取和释义来解决 BLEU 等其他指标的局限性。METEOR 分数考虑同义词和词干以更准确地捕捉含义和语言变异。除了机器翻译和文本摘要,释义检测是 METEOR 分数的最佳用例。

参数:
  • alpha (float) – METEOR 分数 alpha 参数。默认为 0.9。

  • beta (float) – METEOR 分数 beta 参数。默认为 3.0。

  • gamma (float) – METEOR 分数 gamma 参数。默认为 0.5。

用法

eval_fn = MeteorScoreEvaluator(
    alpha=0.9,
    beta=3.0,
    gamma=0.5
)
result = eval_fn(
    answer="Tokyo is the capital of Japan.",
    ground_truth="The capital of Japan is Tokyo.")

输出格式

{
    "meteor_score": 0.62
}
__call__(*, ground_truth: str, answer: str, **kwargs)#

评估答案和真实值之间的 METEOR 分数。

参数:
  • answer (str) – 要评估的答案。

  • ground_truth (str) – 用于比较的真实值。

返回:

METEOR 分数。

返回类型:

dict

class promptflow.evals.evaluators.ProtectedMaterialEvaluator(project_scope: dict, credential=None)#

基类:object

初始化受保护材料评估器,用于检测 AI 系统的响应中是否存在受保护材料。输出 True 或 False 以及 AI 生成的原因。

参数:
  • project_scope (dict) – Azure AI 项目的范围。它包含订阅 ID、资源组和项目名称。

  • credential (TokenCredential) – 用于连接到 Azure AI 项目的凭据。

返回:

响应中是否找到受保护材料,以及 AI 生成的原因。

返回类型:

Dict[str, str]

用法

project_scope = {
    "subscription_id": "<subscription_id>",
    "resource_group_name": "<resource_group_name>",
    "project_name": "<project_name>",
}
eval_fn = ProtectedMaterialEvaluator(project_scope)
result = eval_fn(question="What is the capital of France?", answer="Paris.")

输出格式

{
    "protected_material_label": "False",
    "protected_material_reason": "This question does not contain any protected material."
}
__call__(*, question: str, answer: str, **kwargs)#

评估受保护材料内容。

参数:
  • question (str) – 要评估的问题。

  • answer (str) – 要评估的答案。

返回:

包含布尔标签和原因的字典。

返回类型:

dict

class promptflow.evals.evaluators.QAEvaluator(model_config: Union[AzureOpenAIModelConfiguration, OpenAIModelConfiguration], parallel: bool = True)#

基类:object

初始化针对特定 Azure OpenAI 模型配置的问答评估器。

参数:

model_config (Union[AzureOpenAIModelConfiguration, OpenAIModelConfiguration]) – Azure OpenAI 模型的配置。

返回:

一个评估并生成“问答”场景指标的函数。

返回类型:

可调用

用法

eval_fn = QAEvaluator(model_config)
result = qa_eval(
    question="Tokyo is the capital of which country?",
    answer="Japan",
    context="Tokyo is the capital of Japan.",
    ground_truth="Japan"
)

输出格式

{
    "gpt_groundedness": 3.5,
    "gpt_relevance": 4.0,
    "gpt_coherence": 1.5,
    "gpt_fluency": 4.0,
    "gpt_similarity": 3.0,
    "f1_score": 0.42
}
__call__(*, question: str, answer: str, context: str, ground_truth: str, **kwargs)#

评估问答场景。

参数:
  • question (str) – 要评估的问题。

  • answer (str) – 要评估的答案。

  • context (str) – 要评估的上下文。

  • ground_truth (str) – 要评估的真实值。

  • parallel (bool) – 是否并行评估。默认为 True。

返回:

QA 场景的分数。

返回类型:

dict

class promptflow.evals.evaluators.RelevanceEvaluator(model_config: Union[AzureOpenAIModelConfiguration, OpenAIModelConfiguration])#

基类:object

初始化针对特定 Azure OpenAI 模型配置的相关性评估器。

参数:

model_config (Union[AzureOpenAIModelConfiguration, OpenAIModelConfiguration]) – Azure OpenAI 模型的配置。

用法

eval_fn = RelevanceEvaluator(model_config)
result = eval_fn(
    question="What is the capital of Japan?",
    answer="The capital of Japan is Tokyo.",
    context="Tokyo is Japan's capital, known for its blend of traditional culture                 and technological advancements.")

输出格式

{
    "gpt_relevance": 3.0
}
__call__(*, question: str, answer: str, context: str, **kwargs)#

评估相关性。

参数:
  • question (str) – 要评估的问题。

  • answer (str) – 要评估的答案。

  • context (str) – 要评估的上下文。

返回:

相关性分数。

返回类型:

dict

class promptflow.evals.evaluators.RougeScoreEvaluator(rouge_type: RougeType)#

基类:object

用于计算两个字符串之间 ROUGE 分数的评估器。

ROUGE (Recall-Oriented Understudy for Gisting Evaluation) 是一组用于评估自动摘要和机器翻译的指标。它衡量生成文本与参考摘要之间的重叠。ROUGE 侧重于以召回率为导向的度量,以评估生成文本覆盖参考文本的程度。文本摘要和文档比较是 ROUGE 的最佳用例,尤其是在文本连贯性和相关性至关重要的场景中。

用法

eval_fn = RougeScoreEvaluator(rouge_type=RougeType.ROUGE_1)
result = eval_fn(
    answer="Tokyo is the capital of Japan.",
    ground_truth="The capital of Japan is Tokyo.")

输出格式

{
    "rouge_precision": 1.0,
    "rouge_recall": 1.0,
    "rouge_f1_score": 1.0
}
__call__(*, ground_truth: str, answer: str, **kwargs)#

评估答案和真实值之间的 ROUGE 分数。

参数:
  • answer (str) – 要评估的答案。

  • ground_truth (str) – 用于比较的真实值。

返回:

ROUGE 分数。

返回类型:

dict

class promptflow.evals.evaluators.RougeType(value)#

基类:str, Enum

ROUGE (Recall-Oriented Understudy for Gisting Evaluation) 类型的枚举。

ROUGE_1 = 'rouge1'#

生成文本和参考文本之间 unigram(单个词)的重叠。

ROUGE_2 = 'rouge2'#

生成文本和参考文本之间 bigram(两个连续的词)的重叠。

ROUGE_3 = 'rouge3'#

生成文本和参考文本之间 trigram(三个连续的词)的重叠。

ROUGE_4 = 'rouge4'#

生成文本和参考文本之间 four-gram(四个连续的词)的重叠。

ROUGE_5 = 'rouge5'#

生成文本和参考文本之间 five-gram(五个连续的词)的重叠。

ROUGE_L = 'rougeL'#

生成文本和参考文本之间 L-gram(L 个连续的词)的重叠。

class promptflow.evals.evaluators.SelfHarmEvaluator(project_scope: dict, credential=None)#

基类:object

初始化自残评估器以评估自残分数。

参数:
  • project_scope (dict) – Azure AI 项目的范围。它包含订阅 ID、资源组和项目名称。

  • credential (TokenCredential) – 用于连接到 Azure AI 项目的凭据。

用法

project_scope = {
    "subscription_id": "<subscription_id>",
    "resource_group_name": "<resource_group_name>",
    "project_name": "<project_name>",
}
eval_fn = SelfHarmEvaluator(project_scope)
result = eval_fn(question="What is the capital of France?", answer="Paris.")

输出格式

{
    "self_harm": "High",
    "self_harm_score": 6.5,
    "self_harm_reason": "Some reason"
}
__call__(*, question: str, answer: str, **kwargs)#

评估自残内容。

参数:
  • question (str) – 要评估的问题。

  • answer (str) – 要评估的答案。

返回:

自残分数。

返回类型:

dict

class promptflow.evals.evaluators.SexualEvaluator(project_scope: dict, credential=None)#

基类:object

初始化性内容评估器以评估性内容分数。

参数:
  • project_scope (dict) – Azure AI 项目的范围。它包含订阅 ID、资源组和项目名称。

  • credential (TokenCredential) – 用于连接到 Azure AI 项目的凭据。

用法

project_scope = {
    "subscription_id": "<subscription_id>",
    "resource_group_name": "<resource_group_name>",
    "project_name": "<project_name>",
}
eval_fn = SexualEvaluator(project_scope)
result = eval_fn(question="What is the capital of France?", answer="Paris.")

输出格式

{
    "sexual": "High",
    "sexual_score": 6.5,
    "sexual_reason": "Some reason"
}
__call__(*, question: str, answer: str, **kwargs)#

评估性内容。

参数:
  • question (str) – 要评估的问题。

  • answer (str) – 要评估的答案。

返回:

性内容分数。

返回类型:

dict

class promptflow.evals.evaluators.SimilarityEvaluator(model_config: Union[AzureOpenAIModelConfiguration, OpenAIModelConfiguration])#

基类:object

初始化针对特定 Azure OpenAI 模型配置的相似性评估器。

参数:

model_config (Union[AzureOpenAIModelConfiguration, OpenAIModelConfiguration]) – Azure OpenAI 模型的配置。

用法

eval_fn = SimilarityEvaluator(model_config)
result = eval_fn(
    question="What is the capital of Japan?",
    answer="The capital of Japan is Tokyo.",
    ground_truth="Tokyo is Japan's capital.")

输出格式

{
    "gpt_similarity": 3.0
}
__call__(*, question: str, answer: str, ground_truth: str, **kwargs)#

评估相似性。

参数:
  • question (str) – 要评估的问题。

  • answer (str) – 要评估的答案。

  • ground_truth (str) – 要评估的真实值。

返回:

相似性分数。

返回类型:

dict

class promptflow.evals.evaluators.ViolenceEvaluator(project_scope: dict, credential=None)#

基类:object

初始化暴力评估器以评估暴力分数。

参数:
  • project_scope (dict) – Azure AI 项目的范围。它包含订阅 ID、资源组和项目名称。

  • credential (TokenCredential) – 用于连接到 Azure AI 项目的凭据。

用法

project_scope = {
    "subscription_id": "<subscription_id>",
    "resource_group_name": "<resource_group_name>",
    "project_name": "<project_name>",
}
eval_fn = ViolenceEvaluator(project_scope)
result = eval_fn(question="What is the capital of France?", answer="Paris.")

输出格式

{
    "violence": "High",
    "violence_score": 6.5,
    "violence_reason": "Some reason"
}
__call__(*, question: str, answer: str, **kwargs)#

评估暴力内容。

参数:
  • question (str) – 要评估的问题。

  • answer (str) – 要评估的答案。

返回:

暴力分数。

返回类型:

dict