OPEA guardrail microservice for GenAI application
6.0K
To fortify AI initiatives in production, this microservice introduces guardrails designed to encapsulate LLMs, ensuring the enforcement of responsible behavior. With this microservice, you can secure model inputs and outputs, hastening your journey to production and democratizing AI within your organization, building Trustworthy, Safe, and Secure LLM-based Applications.
These guardrails actively prevent the model from interacting with unsafe content, promptly signaling its inability to assist with such requests. With these protective measures in place, you can expedite production timelines and alleviate concerns about unpredictable model responses.
The Guardrails Microservice now offers two primary types of guardrails:
This microservice supports Meta's Llama Guard and Allen Institute for AI's WildGuard models.
Any content that is detected in the following categories is determined as unsafe:
allenai/wildguard was fine-tuned from mistralai/Mistral-7B-v0.3 on their own allenai/wildguardmix dataset. Any content that is detected in the following categories is determined as unsafe:
git clone https://github.com/opea-project/GenAIComps.git
export OPEA_GENAICOMPS_ROOT=$(pwd)/GenAIComps
export GUARDRAIL_PORT=9090
Before starting the guardrail service, we first need to start the TGI server that will be hosting the guardrail model.
Choose one of the following before starting your TGI server.
For LlamaGuard:
export SAFETY_GUARD_MODEL_ID="meta-llama/Meta-Llama-Guard-2-8B"
export GUARDRAILS_COMPONENT_NAME=OPEA_LLAMA_GUARD
Or
export SAFETY_GUARD_MODEL_ID="meta-llama/LlamaGuard-7b"
export GUARDRAILS_COMPONENT_NAME=OPEA_LLAMA_GUARD
Other variations of LlamaGuard are also an option to use but are not guaranteed to work OOB.
For Wild Guard:
export SAFETY_GUARD_MODEL_ID="allenai/wildguard"
export GUARDRAILS_COMPONENT_NAME=OPEA_WILD_GUARD
Note that both of these models are gated and you need to complete their form on their associated model pages first in order to use them with your HF token.
Follow the steps here to start the TGI server container where LLM_MODEL_ID is set to your SAFETY_GUARD_MODEL_ID like below:
export LLM_MODEL_ID=$SAFETY_GUARD_MODEL_ID
Once the container is starting up and loading the model, set the endpoint that you will use to make requests to the TGI server:
export SAFETY_GUARD_ENDPOINT="http://${host_ip}:${LLM_ENDPOINT_PORT}"
Verify that the TGI Server is ready for inference
First check that the TGI server successfully loaded the guardrail model. Loading the model could take up to 5-10 minutes. You can do this by running the following:
docker logs tgi-gaudi-server
If the last line of the log contains something like INFO text_generation_router::server: router/src/server.rs:2209: Connected then your TGI server is ready and the following curl should work:
curl localhost:${LLM_ENDPOINT_PORT}/generate \
-X POST \
-d '{"inputs":"How do you buy a tiger in the US?","parameters":{"max_new_tokens":32}}' \
-H 'Content-Type: application/json'
Check the logs again with the logs command to confirm that the curl request resulted in Success.
To start the Guardrails microservice, you need to install python packages first.
pip install $OPEA_GENAICOMPS_ROOT
cd $OPEA_GENAICOMPS_ROOT/comps/guardrails/src/guardrails
pip install -r requirements.txt
python opea_guardrails_microservice.py
With the TGI server already running, now we can start the guardrail service container.
cd $OPEA_GENAICOMPS_ROOT
docker build -t opea/guardrails:latest \
--build-arg https_proxy=$https_proxy \
--build-arg http_proxy=$http_proxy \
-f comps/guardrails/src/guardrails/Dockerfile .
To run with LLama Guard:
docker compose -f $OPEA_GENAICOMPS_ROOT/comps/guardrails/deployment/docker_compose/compose.yaml up -d llamaguard-guardrails-server
To run with WildGuard:
docker compose -f $OPEA_GENAICOMPS_ROOT/comps/guardrails/deployment/docker_compose/compose.yaml up -d wildguard-guardrails-server
To run with LLama Guard:
docker run -d \
--name="llamaguard-guardrails-server" \
-p ${GUARDRAIL_PORT}:${GUARDRAIL_PORT} \
--ipc=host \
-e http_proxy=$http_proxy \
-e https_proxy=$https_proxy \
-e no_proxy=$no_proxy \
-e SAFETY_GUARD_ENDPOINT=$SAFETY_GUARD_ENDPOINT \
-e HF_TOKEN=$HF_TOKEN \
opea/guardrails:latest
To run with WildGuard:
docker run -d \
--name="wildguard-guardrails-server" \
-p ${GUARDRAIL_PORT}:${GUARDRAIL_PORT} \
--ipc=host \
-e http_proxy=$http_proxy \
-e https_proxy=$https_proxy \
-e no_proxy=$no_proxy \
-e SAFETY_GUARD_ENDPOINT=$SAFETY_GUARD_ENDPOINT \
-e HF_TOKEN=$HF_TOKEN \
-e GUARDRAILS_COMPONENT_NAME="OPEA_WILD_GUARD" \
opea/guardrails:latest
curl http://localhost:${GUARDRAIL_PORT}/v1/health_check\
-X GET \
-H 'Content-Type: application/json'
curl http://localhost:${GUARDRAIL_PORT}/v1/guardrails\
-X POST \
-d '{"text":"How do you buy a tiger in the US?","parameters":{"max_new_tokens":32}}' \
-H 'Content-Type: application/json'
This request should return text containing:
"Violated policies: <category>, please check your input."
Where category is Violent Crimes or harmful for Llama-Guard-2-8B or wildguard, respectively.
Content type
Image
Digest
sha256:e164c2fa8…
Size
628.9 MB
Last updated
6 months ago
docker pull opea/guardrails