Skip to content

Commit 87dc4f1

Browse files
committed
add polite_guard to tests, workflows, and service
Signed-off-by: Ankur-singh <[email protected]>
1 parent 19e8d0b commit 87dc4f1

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ services:
1919
build:
2020
dockerfile: comps/guardrails/src/pii_detection/Dockerfile
2121
image: ${REGISTRY:-opea}/guardrails-pii-predictionguard:${TAG:-latest}
22+
guardrails-polite-guard:
23+
build:
24+
dockerfile: comps/guardrails/src/polite_guard/Dockerfile
25+
image: ${REGISTRY:-opea}/guardrails-polite-guard:${TAG:-latest}
2226
guardrails-toxicity-predictionguard:
2327
build:
2428
dockerfile: comps/guardrails/src/toxicity_detection/Dockerfile

comps/guardrails/deployment/docker_compose/compose.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ services:
117117
PREDICTIONGUARD_API_KEY: ${PREDICTIONGUARD_API_KEY}
118118
restart: unless-stopped
119119

120+
# polite guard service
121+
guardrails-polite-guard-server:
122+
image: ${REGISTRY:-opea}/guardrails-polite-guard:${TAG:-latest}
123+
container_name: guardrails-polite-guard-server
124+
ports:
125+
- "${POLITE_GUARD_PORT:-9092}:9092"
126+
ipc: host
127+
environment:
128+
no_proxy: ${no_proxy}
129+
http_proxy: ${http_proxy}
130+
https_proxy: ${https_proxy}
131+
HUGGINGFACEHUB_API_TOKEN: ${HF_TOKEN}
132+
restart: unless-stopped
133+
120134
# predictionguard injection service
121135
injection-predictionguard-server:
122136
image: ${REGISTRY:-opea}/injection-predictionguard:${TAG:-latest}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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-polite-guard:comps --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/guardrails/src/polite_guard/Dockerfile .
14+
if [ $? -ne 0 ]; then
15+
echo "opea/guardrails-polite-guard built fail"
16+
exit 1
17+
else
18+
echo "opea/guardrails-polite-guard built successful"
19+
fi
20+
}
21+
22+
function start_service() {
23+
echo "Starting microservice"
24+
export POLITE_GUARD_PORT=11301
25+
export TAG=comps
26+
service_name="guardrails-polite-guard-server"
27+
cd $WORKPATH
28+
cd comps/guardrails/deployment/docker_compose/
29+
docker compose up ${service_name} -d
30+
sleep 15
31+
until docker logs ${service_name} 2>&1 | grep -q "Application startup complete"; do
32+
echo "Waiting for application startup to complete..."
33+
sleep 2 # Wait for 2 seconds before checking again
34+
done
35+
echo "Microservice started"
36+
}
37+
38+
function validate_microservice() {
39+
echo "Validate microservice started"
40+
echo "test 1 - Impolite"
41+
result=$(curl localhost:11301/v1/polite -X POST -d '{"text":"He is stupid"}' -H 'Content-Type: application/json')
42+
if [[ $result == *"Violated"* ]]; then
43+
echo "Result correct."
44+
else
45+
docker logs guardrails-polite-guard-server
46+
exit 1
47+
fi
48+
echo "test 2 - Polite"
49+
result=$(curl localhost:11301/v1/polite -X POST -d '{"text":"He is kind"}' -H 'Content-Type: application/json')
50+
if [[ $result == *"kind"* ]]; then
51+
echo "Result correct."
52+
else
53+
echo "Result wrong."
54+
docker logs guardrails-polite-guard-server
55+
exit 1
56+
fi
57+
echo "Validate microservice completed"
58+
}
59+
60+
function stop_docker() {
61+
cid=$(docker ps -aq --filter "name=guardrails-polite-guard-server")
62+
echo "Shutdown legacy containers "$cid
63+
if [[ ! -z "$cid" ]]; then docker stop $cid && docker rm $cid && sleep 1s; fi
64+
}
65+
66+
function main() {
67+
68+
stop_docker
69+
70+
build_docker_images
71+
start_service
72+
73+
validate_microservice
74+
75+
stop_docker
76+
echo "cleanup container images and volumes"
77+
echo y | docker system prune 2>&1 > /dev/null
78+
79+
}
80+
81+
main

0 commit comments

Comments
 (0)