Skip to content

Commit

Permalink
Qunsong/qa eval groundedness service (#34797)
Browse files Browse the repository at this point in the history
* modify built-in qa evaluation flow

* move groundedness evaluation to groundedness evaluation service
* change content harm level "Safe" to "Very low"

* add default groundedness results to qa eval flow

* modify built-in qa evaluation flow:

* add logic to check service availability in a region
* change hate_fairness to hate_unfairness

* add gpt_groundedness to qa node list

* update built-in qa evaluation flow

* add flight control to flow input
* code flake8 cleaning

* round f1_score in built_in qa eval flow

* metric name update

* update e2e test of qa built-in evaluation flow

* update built-in qa evaluation flow

* fix fallback check logic in validate_groundedness_service
* add e2e test of fallback groundedness
  • Loading branch information
qusongms authored Mar 26, 2024
1 parent 34ff73f commit 9973a20
Show file tree
Hide file tree
Showing 25 changed files with 657 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ContentSafetyMetrics:
Evaluation metrics supported by evaluation in azure-ai-generative sdk
"""

HATE_FAIRNESS = "hate_fairness"
HATE_FAIRNESS = "hate_unfairness"
VIOLENCE = "violence"
SELF_HARM = "self_harm"
SEXUAL = "sexual"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ---------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# ---------------------------------------------------------
# pylint: disable=E0611
# pylint: skip-file

import logging
import pandas as pd
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# ---------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# ---------------------------------------------------------
# pylint: skip-file

import logging

from os import path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# ---------------------------------------------------------
# pylint: skip-file

import os.path
import json
import pathlib
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,31 @@


@tool
def aggregate_results(results: List[dict], selected_metrics: List[dict], thresholds: List[int]) -> dict:
def aggregate_results(results: List[dict],
selected_metrics: List[dict],
thresholds: List[int]) -> dict:
if selected_metrics:
selected_safety_metrics = filter_metrics(selected_metrics[0]["safety_metrics"])
selected_quality_metrics = filter_metrics(selected_metrics[0]["quality_metrics"])
selected_safety_metrics = filter_metrics(
selected_metrics[0]["safety_metrics"])
selected_quality_metrics = filter_metrics(
selected_metrics[0]["quality_metrics"])
else:
selected_safety_metrics = []
selected_quality_metrics = []

if thresholds != [] and thresholds is not None:
threshold = np.float16(thresholds[0])
else:
threshold = np.float16(RAIService.HARM_SEVERITY_THRESHOLD)
threshold = np.float16(
RAIService.HARM_SEVERITY_THRESHOLD)

aggregate_results = {}
for result in results:
if not result:
continue
for name in result.keys():
if name in selected_quality_metrics or name in selected_safety_metrics:
if name in selected_quality_metrics \
or name in selected_safety_metrics:
if name not in aggregate_results.keys():
aggregate_results[name] = []
metric_value = result[name]
Expand All @@ -47,8 +53,9 @@ def aggregate_results(results: List[dict], selected_metrics: List[dict], thresho
if name in selected_quality_metrics:
aggregate_output[metric_name] = round(np.nanmean(values), 2)
elif name in selected_safety_metrics:
aggregate_output[metric_name] = round(np.sum(values >= threshold) / len(values), 2)
aggregate_output[metric_name] = round(
np.sum(values >= threshold) / len(values), 2)
else:
aggregate_output[metric_name] = np.nan
log_metric(metric_name, aggregate_output[metric_name])
return aggregate_output
return aggregate_output
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from promptflow import tool
from rai_client import RAIServiceHandler


@tool
def call_groundedness_service(request_body: dict) -> [dict]:
service_handler = RAIServiceHandler()
annotation_results = service_handler.get_annotation(request_body)
return annotation_results
Original file line number Diff line number Diff line change
@@ -1,78 +1,9 @@
from promptflow import tool
from mlflow.utils.rest_utils import http_request
import time
from utils import get_cred
from constants import RAIService
from rai_client import RAIServiceHandler


def submit_annotation(cred, request_body):
try:
response = http_request(
host_creds=cred,
endpoint="/submitannotation",
method="POST",
json=request_body,
)

if response.status_code != 202:
print("Fail evaluating '%s' with error message: %s" %(request_body["UserTextList"], response.text))
response.raise_for_status()
except AttributeError as e:
response = None
print("Fail evaluating '%s' with error message: %s" % (request_body["UserTextList"], e))
if response is not None:
json_obj = response.json()
else:
json_obj = {}
return json_obj

def check_status(cred, request_id):
try:
response = http_request(
host_creds = cred,
endpoint="/operations/" + request_id,
method="GET"
)
except AttributeError as e:
response = None
return response

def retrieve_annotation_result(cred, submitannotation_response):
request_id = submitannotation_response["location"].split("/")[-1]
annotation_result = None
start = time.time()
time_elapsed = 0
request_count = 1
while True and time_elapsed <= RAIService.TIMEOUT:
try:
request_status = check_status(cred, request_id)
except Exception:
request_status = None
if request_status:
request_status_code = request_status.status_code
if request_status_code == 200:
annotation_result = request_status.json()
break
else:
print("Failed to retrieve the status of RequestID: %s" % request_id)
request_count += 1
sleep_time = RAIService.SLEEPTIME ** request_count
time.sleep(sleep_time)
time_elapsed = time.time() - start

if time_elapsed > RAIService.TIMEOUT:
raise TimeoutError("Request times out after %d seconds", RAIService.TIMEOUT)

return annotation_result

# The inputs section will change based on the arguments of the tool function, after you save the code
# Adding type to arguments and return value will help the system show the types properly
# Please update the function name/signature per need
@tool
def call_rai_service(request_body: dict) -> dict:
#rai = RAIService()
cred = get_cred()
submitannotation_response = submit_annotation(cred, request_body)
annotation_result = retrieve_annotation_result(cred, submitannotation_response)
return annotation_result

service_handler = RAIServiceHandler()
annotation_results = service_handler.get_annotation(request_body)
return annotation_results
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ def concat_results(gpt_coherence_score: str = None,
gpt_similarity_score: str = None,
gpt_fluency_score: str = None,
gpt_relevance_score: str = None,
gpt_groundedness_score: str = None,
f1_score: float = None) -> dict:
f1_score: float = None
) -> dict:

load_list = [{'name': 'gpt_coherence', 'score': gpt_coherence_score},
{'name': 'gpt_similarity', 'score': gpt_similarity_score},
{'name': 'gpt_fluency', 'score': gpt_fluency_score},
{'name': 'gpt_relevance', 'score': gpt_relevance_score},
{'name': 'gpt_groundedness', 'score': gpt_groundedness_score},
{'name': 'f1_score', 'score': f1_score}
]

Expand All @@ -28,7 +27,9 @@ def concat_results(gpt_coherence_score: str = None,
score = float(item["score"])
except Exception as e:
score = np.nan
errors.append({"name": item["name"], "msg": str(e), "data": item["score"]})
errors.append({"name": item["name"],
"msg": str(e),
"data": item["score"]})
else:
if item['score']:
try:
Expand All @@ -40,15 +41,19 @@ def concat_results(gpt_coherence_score: str = None,
score = np.nan
except Exception as e:
score = np.nan
errors.append({"name": item["name"], "msg": str(e), "data": item["score"]})
errors.append({"name": item["name"],
"msg": str(e),
"data": item["score"]})
else:
score = np.nan
score_list.append({"name": item["name"], "score": score})
score_list.append({"name": item["name"],
"score": score})

variant_level_result = {}
for item in score_list:
item_name = str(item["name"])
variant_level_result[item_name] = item["score"]
if 'gpt' in item_name:
variant_level_result[item_name + '_pass_rate'] = 1 if item["score"] > 3 else 0
variant_level_result[item_name + '_pass_rate'] = 1 \
if item["score"] > 3 else 0
return variant_level_result
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,48 @@
import constants
import numpy as np


def default_safety_results():
supported_metrics = constants.Metric.CONTENT_HARM_METRICS
result = {}
for metric_name in supported_metrics:
result[metric_name] = np.nan
result[metric_name + "_score"] = np.nan
result[metric_name + "_reasoning"] = np.nan
result[metric_name + "_reason"] = np.nan
return result

def default_quality_results():

def default_gpt_results():
supported_metrics = constants.Metric.QUALITY_METRICS
result = {}
for metric_name in supported_metrics:
result[metric_name] = np.nan
if metric_name != "gpt_groundedness":
result[metric_name] = np.nan
return result


# The inputs section will change based on the arguments of the tool function, after you save the code
# Adding type to arguments and return value will help the system show the types properly
# Please update the function name/signature per need
def default_groundedness_results():
return {"gpt_groundedness": np.nan,
"gpt_groundedness_reason": np.nan
}


@tool
def concat_results(selected_metrics: dict, quality_results: dict = None, safety_results: dict = None) -> dict:
def concat_results(selected_metrics: dict,
quality_results: dict = None,
safety_results: dict = None,
groundedness_results: dict = None) -> dict:
if quality_results:
concated_results = quality_results.copy()
else:
concated_results = default_quality_results()
concated_results = default_gpt_results()
if groundedness_results:
concated_results.update(groundedness_results)
else:
concated_results.update(default_groundedness_results())
if safety_results:
concated_results.update(safety_results)
else:
concated_results.update(default_safety_results())

return concated_results

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import sys
from enum import Enum
#import numpy as np


class RAIService:
"""Define constants related to RAI service"""
TIMEOUT = 1800
SLEEPTIME = 2
HARM_SEVERITY_THRESHOLD = 4


class Metric:
"""Defines all metrics supported by RAI service"""
Metrics = "metrics"
Expand All @@ -16,14 +16,14 @@ class Metric:
SelfHarm = "self_harm"
Violence = "violence"
Sexual = "sexual"
HateFairness = "hate_fairness"
HateFairness = "hate_unfairness"

QUALITY_METRICS = {
"gpt_groundedness",
"gpt_similarity",
"gpt_fluency",
"gpt_fluency",
"gpt_coherence",
"gpt_relevance",
"gpt_relevance",
"f1_score"
}

Expand All @@ -35,12 +35,14 @@ class Metric:
HateFairness
}


class HarmSeverityLevel(Enum):
Safe = 0
Low = 1
Medium = 2
High = 3
VeryLow = "Very low"
Low = "Low"
Medium = "Medium"
High = "High"


class Tasks:
"""Defines types of annotation tasks supported by RAI Service."""
CONTENT_HARM = "content harm"
CONTENT_HARM = "content harm"
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from promptflow import tool
import json


def normalize_user_text(user_text):
return user_text.replace("'", "\\\"")


@tool
def construct_request(question: str,
answer: str,
context: str) -> dict:
metrics = ["generic_groundedness"]
user_text = json.dumps({"question": question,
"answer": answer,
"context": context})
parsed_user_text = normalize_user_text(user_text)
request_body = {"UserTextList": [parsed_user_text],
"AnnotationTask": "groundedness",
"MetricList": metrics}
return request_body
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
from promptflow import tool


def normalize_user_text(user_text):
return user_text.replace("'", "\\\"")

# The inputs section will change based on the arguments of the tool function, after you save the code
# Adding type to arguments and return value will help the system show the types properly
# Please update the function name/signature per need

@tool
def construct_request(question: str, answer:str, selected_metrics: dict) -> dict:
def construct_request(question: str,
answer: str,
selected_metrics: dict) -> dict:
selected_safety_metrics = selected_metrics["safety_metrics"]
metrics = [metric for metric in selected_safety_metrics if selected_safety_metrics[metric]]
metrics = [metric.replace("_unfairness", "_fairness") for metric in
selected_safety_metrics if selected_safety_metrics[metric]]
user_text = f"<Human>{question}</><System>{answer}</>"
parsed_user_text = normalize_user_text(user_text)
request_body = {"UserTextList": [parsed_user_text], "AnnotationTask": "content harm", "MetricList":metrics}
request_body = {"UserTextList": [parsed_user_text],
"AnnotationTask": "content harm",
"MetricList": metrics,
"PromptVersion": "0.2"}
return request_body
Loading

0 comments on commit 9973a20

Please sign in to comment.