From e29865e75559a11965fd9c915c962b5434ec1753 Mon Sep 17 00:00:00 2001 From: qgao007 <108324932+qgao007@users.noreply.github.com> Date: Wed, 18 Sep 2024 19:22:52 -0600 Subject: [PATCH] Add E2E test for bias detection of guardrails (#708) * Add Bias Detection Microservice Signed-off-by: Qun Gao * Add E2E test for bias detection Signed-off-by: Qun Gao * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Signed-off-by: Qun Gao Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .../docker/compose/guardrails-compose.yaml | 5 ++ .../test_guardrails_bias_detection.sh | 72 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 tests/guardrails/test_guardrails_bias_detection.sh diff --git a/.github/workflows/docker/compose/guardrails-compose.yaml b/.github/workflows/docker/compose/guardrails-compose.yaml index 81e516209..97b950dc8 100644 --- a/.github/workflows/docker/compose/guardrails-compose.yaml +++ b/.github/workflows/docker/compose/guardrails-compose.yaml @@ -9,3 +9,8 @@ services: build: dockerfile: comps/guardrails/llama_guard/langchain/Dockerfile image: ${REGISTRY:-opea}/guardrails-tgi:${TAG:-latest} + + guardrails-bias-detection: + build: + dockerfile: comps/guardrails/llama_guard/langchain/Dockerfile + image: ${REGISTRY:-opea}/guardrails-bias-detection:${TAG:-latest} diff --git a/tests/guardrails/test_guardrails_bias_detection.sh b/tests/guardrails/test_guardrails_bias_detection.sh new file mode 100644 index 000000000..0cc484343 --- /dev/null +++ b/tests/guardrails/test_guardrails_bias_detection.sh @@ -0,0 +1,72 @@ +#!/bin/bash +# Copyright (C) 2024 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +set -x + +WORKPATH=$(dirname "$PWD") +ip_address=$(hostname -I | awk '{print $1}') + +function build_docker_images() { + echo "Start building docker images for microservice" + cd $WORKPATH + docker build --no-cache -t opea/guardrails-bias-detection:comps --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/guardrails/bias_detection/Dockerfile . + if [ $? -ne 0 ]; then + echo "opea/guardrails-bias-detection built fail" + exit 1 + else + echo "opea/guardrails-bias-detection built successful" + fi +} + +function start_service() { + echo "Starting microservice" + docker run -d --runtime=runc --name="test-comps-guardrails-bias-detection-endpoint" -p 9092:9092 --ipc=host -e http_proxy=$http_proxy -e https_proxy=$https_proxy opea/guardrails-bias-detection:comps + sleep 5 + echo "Microservice started" +} + +function validate_microservice() { + echo "Validate microservice started" + echo "test 1 - biased" + result=$(curl localhost:9092/v1/bias -X POST -d '{"text":"John McCain exposed as an unprincipled politician."}' -H 'Content-Type: application/json') + if [[ $result == *"Violated"* ]]; then + echo "Result correct." + else + docker logs test-comps-guardrails-bias-detection-endpoint + exit 1 + fi + echo "test 2 - non-biased" + result=$(curl localhost:9092/v1/bias -X POST -d '{"text":"John McCain described as an unprincipled politician."}' -H 'Content-Type: application/json') + if [[ $result == *"described"* ]]; then + echo "Result correct." + else + echo "Result wrong." + docker logs test-comps-guardrails-bias-detection-endpoint + exit 1 + fi + echo "Validate microservice completed" +} + +function stop_docker() { + cid=$(docker ps -aq --filter "name=test-comps-guardrails-bias-detection-endpoint") + echo "Shutdown legacy containers "$cid + if [[ ! -z "$cid" ]]; then docker stop $cid && docker rm $cid && sleep 1s; fi +} + +function main() { + + stop_docker + + build_docker_images + start_service + + validate_microservice + + stop_docker + echo "cleanup container images and volumes" + echo y | docker system prune 2>&1 > /dev/null + +} + +main