-
Notifications
You must be signed in to change notification settings - Fork 218
Add WildGuard Guardrail Microservice #710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ashahba
merged 24 commits into
opea-project:main
from
daniel-de-leon-user293:daniel/wildguard
Oct 11, 2024
Merged
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
2dce95d
add wildguard microservice
daniel-de-leon-user293 fddb264
fix paths for wildguard dir
daniel-de-leon-user293 cb7c2ea
add README
daniel-de-leon-user293 c942808
add wildguard to guardrail README table
daniel-de-leon-user293 226541f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9109517
Merge branch 'main' into daniel/wildguard
letonghan 1e31d86
Merge branch 'opea-project:main' into daniel/wildguard
daniel-de-leon-user293 de85d42
updates per code review
daniel-de-leon-user293 a2896d6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1ea83bf
Merge branch 'opea-project:main' into daniel/wildguard
daniel-de-leon-user293 7ed2b87
update tokens
daniel-de-leon-user293 d125353
Merge branch 'opea-project:main' into daniel/wildguard
daniel-de-leon-user293 7387ee6
fix endpoint for testing
daniel-de-leon-user293 caeb916
Merge branch 'opea-project:main' into daniel/wildguard
daniel-de-leon-user293 5f2e489
Merge branch 'opea-project:main' into daniel/wildguard
daniel-de-leon-user293 3b33905
fixed formating
daniel-de-leon-user293 f923e6c
Merge branch 'opea-project:main' into daniel/wildguard
daniel-de-leon-user293 3b326c0
Merge branch 'opea-project:main' into daniel/wildguard
daniel-de-leon-user293 9a980d8
add more description of wildguard to distinguish from llamaguard
daniel-de-leon-user293 6fb4041
Merge branch 'opea-project:main' into daniel/wildguard
daniel-de-leon-user293 e765828
Merge branch 'opea-project:main' into daniel/wildguard
daniel-de-leon-user293 0d066b0
Merge branch 'main' into daniel/wildguard
ashahba 711101e
Merge branch 'main' into daniel/wildguard
ZePan110 ff119e9
Merge branch 'main' into daniel/wildguard
ashahba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # Copyright (C) 2024 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| FROM python:3.11-slim | ||
|
|
||
| ENV LANG=C.UTF-8 | ||
|
|
||
| ARG ARCH="cpu" | ||
|
|
||
| RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \ | ||
| libgl1-mesa-glx \ | ||
| libjemalloc-dev | ||
|
|
||
| RUN useradd -m -s /bin/bash user && \ | ||
| mkdir -p /home/user && \ | ||
| chown -R user /home/user/ | ||
|
|
||
| USER user | ||
|
|
||
| COPY comps /home/user/comps | ||
|
|
||
| RUN pip install --no-cache-dir --upgrade pip && \ | ||
| if [ ${ARCH} = "cpu" ]; then pip install --no-cache-dir torch --index-url https://download.pytorch.org/whl/cpu; fi && \ | ||
| pip install --no-cache-dir -r /home/user/comps/guardrails/wildguard/langchain/requirements.txt | ||
|
|
||
| ENV PYTHONPATH=$PYTHONPATH:/home/user | ||
|
|
||
| WORKDIR /home/user/comps/guardrails/wildguard/langchain/ | ||
|
|
||
| ENTRYPOINT ["python", "guardrails_tgi.py"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| # Guardrails Microservice | ||
|
|
||
| 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: | ||
|
|
||
| - Input Guardrails: These are applied to user inputs. An input guardrail can either reject the input, halting further processing. | ||
| - Output Guardrails: These are applied to outputs generated by the LLM. An output guardrail can reject the output, preventing it from being returned to the user. | ||
|
|
||
| We offer content moderation support utilizing Allen Institute for AI's [WildGuard](https://huggingface.co/allenai/wildguard) model. | ||
|
|
||
| Any content that is detected in the following categories is determined as unsafe: | ||
|
|
||
| - Privacy | ||
| - Misinformation | ||
| - Harmful Language | ||
| - Malicious Uses | ||
|
|
||
| ## 🚀1. Start Microservice with Python (Option 1) | ||
|
|
||
| To start the Guardrails microservice, you need to install python packages first. | ||
|
|
||
| ### 1.1 Install Requirements | ||
|
|
||
| ```bash | ||
| pip install -r requirements.txt | ||
| ``` | ||
|
|
||
| ### 1.2 Start TGI Gaudi Service | ||
|
|
||
| ```bash | ||
| export HF_TOKEN=${your_hf_api_token} | ||
| volume=$PWD/data | ||
| model_id="allenai/wildguard" | ||
| docker pull ghcr.io/huggingface/tgi-gaudi:2.0.1 | ||
| docker run -p 8088:80 -v $volume:/data --runtime=habana -e HABANA_VISIBLE_DEVICES=all -e OMPI_MCA_btl_vader_single_copy_mechanism=none --cap-add=sys_nice --ipc=host -e HTTPS_PROXY=$https_proxy -e HTTP_PROXY=$https_proxy -e HF_TOKEN=$HF_TOKEN ghcr.io/huggingface/tgi-gaudi:2.0.1 --model-id $model_id --max-input-length 1024 --max-total-tokens 2048 | ||
| ``` | ||
|
|
||
| ### 1.3 Verify the TGI Gaudi Service | ||
|
|
||
| ```bash | ||
| curl 127.0.0.1:8088/generate \ | ||
| -X POST \ | ||
| -d '{"inputs":"How do you buy a tiger in the US?","parameters":{"max_new_tokens":32}}' \ | ||
| -H 'Content-Type: application/json' | ||
| ``` | ||
|
|
||
| ### 1.4 Start Guardrails Service | ||
|
|
||
| ```bash | ||
| export SAFETY_GUARD_ENDPOINT="http://${your_ip}:8088" | ||
| python guardrails_tgi.py | ||
| ``` | ||
|
|
||
| ## 🚀2. Start Microservice with Docker (Option 2) | ||
|
|
||
| If you start an Guardrails microservice with docker, the `docker_compose_guardrails.yaml` file will automatically start a TGI gaudi service with docker. | ||
|
|
||
| ### 2.1 Setup Environment Variables | ||
|
|
||
| In order to start TGI and LLM services, you need to setup the following environment variables first. | ||
|
|
||
| ```bash | ||
| export HUGGINGFACEHUB_API_TOKEN=${your_hf_api_token} | ||
| export SAFETY_GUARD_ENDPOINT="http://${your_ip}:8088" | ||
| export LLM_MODEL_ID=${your_hf_llm_model} | ||
| ``` | ||
|
|
||
| ### 2.2 Build Docker Image | ||
|
|
||
| ```bash | ||
| cd ../../../../ | ||
| docker build -t opea/guardrails-tgi:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/guardrails/wildguard/langchain/Dockerfile . | ||
| ``` | ||
|
|
||
| ### 2.3 Run Docker with CLI | ||
|
|
||
| ```bash | ||
| docker run -d --name="guardrails-tgi-server" -p 9090:9090 --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 HUGGINGFACEHUB_API_TOKEN=$HUGGINGFACEHUB_API_TOKEN opea/guardrails-tgi:latest | ||
| ``` | ||
|
|
||
| ### 2.4 Run Docker with Docker Compose | ||
|
|
||
| ```bash | ||
| docker compose -f docker_compose_guardrails.yaml up -d | ||
| ``` | ||
|
|
||
| ## 🚀3. Consume Guardrails Service | ||
|
|
||
| ### 3.1 Check Service Status | ||
|
|
||
| ```bash | ||
| curl http://localhost:9090/v1/health_check\ | ||
daniel-de-leon-user293 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| -X GET \ | ||
| -H 'Content-Type: application/json' | ||
| ``` | ||
|
|
||
| ### 3.2 Consume Guardrails Service | ||
|
|
||
| ```bash | ||
| curl http://localhost:9090/v1/guardrails\ | ||
daniel-de-leon-user293 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| -X POST \ | ||
| -d '{"text":"How do you buy a tiger in the US?","parameters":{"max_new_tokens":32}}' \ | ||
| -H 'Content-Type: application/json' | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Copyright (C) 2024 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 |
35 changes: 35 additions & 0 deletions
35
comps/guardrails/wildguard/langchain/docker_compose_guardrails.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Copyright (C) 2024 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| version: "3.8" | ||
|
|
||
| services: | ||
| tgi_gaudi_service: | ||
| image: ghcr.io/huggingface/tgi-gaudi:2.0.1 | ||
| container_name: tgi-service | ||
| ports: | ||
| - "8088:80" | ||
| volumes: | ||
| - "./data:/data" | ||
| environment: | ||
| HF_TOKEN: ${HF_TOKEN} | ||
| shm_size: 1g | ||
| command: --model-id ${LLM_MODEL_ID} --max-input-tokens 1024 --max-total-tokens 2048 | ||
| guardrails: | ||
| image: opea/guardrails-tgi:latest | ||
| container_name: guardrails-tgi-gaudi-server | ||
| ports: | ||
| - "9090:9090" | ||
| ipc: host | ||
| environment: | ||
| no_proxy: ${no_proxy} | ||
| http_proxy: ${http_proxy} | ||
| https_proxy: ${https_proxy} | ||
| SAFETY_GUARD_ENDPOINT: ${SAFETY_GUARD_ENDPOINT} | ||
| LANGCHAIN_API_KEY: ${LANGCHAIN_API_KEY} | ||
| HUGGINGFACEHUB_API_TOKEN: ${HF_TOKEN} | ||
| restart: unless-stopped | ||
|
|
||
| networks: | ||
| default: | ||
| driver: bridge |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # Copyright (C) 2024 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import os | ||
| from typing import List, Union | ||
|
|
||
| from langchain_huggingface.llms import HuggingFaceEndpoint | ||
|
|
||
| from comps import CustomLogger, GeneratedDoc, ServiceType, TextDoc, opea_microservices, register_microservice | ||
|
|
||
| logger = CustomLogger("guardrails_tgi") | ||
| logflag = os.getenv("LOGFLAG", False) | ||
|
|
||
| DEFAULT_MODEL = "allenai/wildguard" | ||
| INSTRUCTION_FORMAT = """<s><|user|> | ||
| [INST] You are a classifier for analyzing interactions between humans and LLMs. Given a request from a human user and a response from an LLM assistant, determine 1) whether the human user's request is harmful, 2) whether the AI assistant's response is a refusal, and 3) whether the AI assistant's response is harmful. | ||
|
|
||
| Human user: | ||
| {prompt} | ||
|
|
||
| AI assistant: | ||
| {response} | ||
|
|
||
| --- | ||
|
|
||
| Answers: [/INST] | ||
| <|assistant|> | ||
| """ | ||
|
|
||
|
|
||
| @register_microservice( | ||
| name="opea_service@wildguard_tgi", | ||
| service_type=ServiceType.GUARDRAIL, | ||
| endpoint="/v1/guardrails", | ||
| host="0.0.0.0", | ||
| port=9090, | ||
| input_datatype=Union[GeneratedDoc, TextDoc], | ||
| output_datatype=TextDoc, | ||
| ) | ||
| def safety_guard(input: Union[GeneratedDoc, TextDoc]) -> TextDoc: | ||
| if logflag: | ||
| logger.info(input) | ||
|
|
||
| if isinstance(input, GeneratedDoc): | ||
| messages = INSTRUCTION_FORMAT.format(prompt=input.prompt, response=input.text) | ||
| else: | ||
| messages = INSTRUCTION_FORMAT.format(prompt=input.text, response="") | ||
| response_input_guard = llm_guard.invoke(messages) | ||
|
|
||
| if "Harmful request: yes" in response_input_guard or "Harmful response: yes" in response_input_guard: | ||
|
|
||
| if logflag: | ||
| logger.info("Violated policies: harmful") | ||
| res = TextDoc(text="Violated policies: harmful, please check your input.", downstream_black_list=[".*"]) | ||
| else: | ||
| res = TextDoc(text=input.text) | ||
| if logflag: | ||
| logger.info(res) | ||
| return res | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| safety_guard_endpoint = os.getenv("SAFETY_GUARD_ENDPOINT", "http://localhost:8080") | ||
| safety_guard_model = os.getenv("SAFETY_GUARD_MODEL_ID", DEFAULT_MODEL) | ||
| llm_guard = HuggingFaceEndpoint( | ||
| endpoint_url=safety_guard_endpoint, | ||
| max_new_tokens=100, | ||
| top_k=1, | ||
| top_p=0.95, | ||
| typical_p=0.95, | ||
| temperature=0.01, | ||
| repetition_penalty=1.03, | ||
| ) | ||
| # chat engine for server-side prompt templating | ||
| logger.info("guardrails - router] LLM initialized.") | ||
| opea_microservices["opea_service@wildguard_tgi"].start() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| docarray[full] | ||
| fastapi | ||
| huggingface-hub | ||
| langchain-community | ||
| langchain-huggingface | ||
| opentelemetry-api | ||
| opentelemetry-exporter-otlp | ||
| opentelemetry-sdk | ||
| prometheus-fastapi-instrumentator | ||
| shortuuid | ||
| uvicorn |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.