Skip to content

Commit e29865e

Browse files
Add E2E test for bias detection of guardrails (#708)
* Add Bias Detection Microservice Signed-off-by: Qun Gao <[email protected]> * Add E2E test for bias detection Signed-off-by: Qun Gao <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Signed-off-by: Qun Gao <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4b5d85b commit e29865e

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

.github/workflows/docker/compose/guardrails-compose.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,8 @@ services:
99
build:
1010
dockerfile: comps/guardrails/llama_guard/langchain/Dockerfile
1111
image: ${REGISTRY:-opea}/guardrails-tgi:${TAG:-latest}
12+
13+
guardrails-bias-detection:
14+
build:
15+
dockerfile: comps/guardrails/llama_guard/langchain/Dockerfile
16+
image: ${REGISTRY:-opea}/guardrails-bias-detection:${TAG:-latest}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/bash
2+
# Copyright (C) 2024 Intel Corporation
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
set -x
6+
7+
WORKPATH=$(dirname "$PWD")
8+
ip_address=$(hostname -I | awk '{print $1}')
9+
10+
function build_docker_images() {
11+
echo "Start building docker images for microservice"
12+
cd $WORKPATH
13+
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 .
14+
if [ $? -ne 0 ]; then
15+
echo "opea/guardrails-bias-detection built fail"
16+
exit 1
17+
else
18+
echo "opea/guardrails-bias-detection built successful"
19+
fi
20+
}
21+
22+
function start_service() {
23+
echo "Starting microservice"
24+
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
25+
sleep 5
26+
echo "Microservice started"
27+
}
28+
29+
function validate_microservice() {
30+
echo "Validate microservice started"
31+
echo "test 1 - biased"
32+
result=$(curl localhost:9092/v1/bias -X POST -d '{"text":"John McCain exposed as an unprincipled politician."}' -H 'Content-Type: application/json')
33+
if [[ $result == *"Violated"* ]]; then
34+
echo "Result correct."
35+
else
36+
docker logs test-comps-guardrails-bias-detection-endpoint
37+
exit 1
38+
fi
39+
echo "test 2 - non-biased"
40+
result=$(curl localhost:9092/v1/bias -X POST -d '{"text":"John McCain described as an unprincipled politician."}' -H 'Content-Type: application/json')
41+
if [[ $result == *"described"* ]]; then
42+
echo "Result correct."
43+
else
44+
echo "Result wrong."
45+
docker logs test-comps-guardrails-bias-detection-endpoint
46+
exit 1
47+
fi
48+
echo "Validate microservice completed"
49+
}
50+
51+
function stop_docker() {
52+
cid=$(docker ps -aq --filter "name=test-comps-guardrails-bias-detection-endpoint")
53+
echo "Shutdown legacy containers "$cid
54+
if [[ ! -z "$cid" ]]; then docker stop $cid && docker rm $cid && sleep 1s; fi
55+
}
56+
57+
function main() {
58+
59+
stop_docker
60+
61+
build_docker_images
62+
start_service
63+
64+
validate_microservice
65+
66+
stop_docker
67+
echo "cleanup container images and volumes"
68+
echo y | docker system prune 2>&1 > /dev/null
69+
70+
}
71+
72+
main

0 commit comments

Comments
 (0)