Skip to content

Commit 812c85c

Browse files
qgao007pre-commit-ci[bot]ashahba
authored
Add Bias Detection Microservice (#659)
* Add Bias Detection Microservice Signed-off-by: Qun Gao <qun.gao@intel.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Adding Bias Detection Container to CI (#695) Signed-off-by: Abolfazl Shahbazi <abolfazl.shahbazi@intel.com> --------- Signed-off-by: Qun Gao <qun.gao@intel.com> Signed-off-by: Abolfazl Shahbazi <abolfazl.shahbazi@intel.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Abolfazl Shahbazi <abolfazl.shahbazi@intel.com>
1 parent 29fe569 commit 812c85c

File tree

7 files changed

+185
-0
lines changed

7 files changed

+185
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ services:
66
build:
77
dockerfile: comps/guardrails/pii_detection/Dockerfile
88
image: ${REGISTRY:-opea}/guardrails-pii-detection:${TAG:-latest}
9+
guardrails-bias-detection:
10+
build:
11+
dockerfile: comps/guardrails/bias_detection/Dockerfile
12+
image: ${REGISTRY:-opea}/guardrails-bias-detection:${TAG:-latest}
913
guardrails-toxicity-detection:
1014
build:
1115
dockerfile: comps/guardrails/toxicity_detection/Dockerfile

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,13 @@ 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/bias_detection/Dockerfile
16+
image: ${REGISTRY:-opea}/guardrails-bias-detection:${TAG:-latest}
17+
18+
guardrails-toxicity-detection:
19+
build:
20+
dockerfile: comps/guardrails/toxicity_detection/Dockerfile
21+
image: ${REGISTRY:-opea}/guardrails-toxicity-detection:${TAG:-latest}

comps/guardrails/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ The Guardrails service enhances the security of LLM-based applications by offeri
77
| [Llama Guard](./llama_guard/langchain/README.md) | Provides guardrails for inputs and outputs to ensure safe interactions |
88
| [PII Detection](./pii_detection/README.md) | Detects Personally Identifiable Information (PII) and Business Sensitive Information (BSI) |
99
| [Toxicity Detection](./toxicity_detection/README.md) | Detects Toxic language (rude, disrespectful, or unreasonable language that is likely to make someone leave a discussion) |
10+
| [Bias Detection](./bias_detection/README.md) | Detects Biased language (framing bias, epistemological bias, and demographic bias) |
1011

1112
Additional safety-related microservices will be available soon.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
FROM langchain/langchain:latest
5+
6+
ENV LANG=C.UTF-8
7+
8+
ARG ARCH="cpu"
9+
10+
RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
11+
libgl1-mesa-glx \
12+
libjemalloc-dev
13+
14+
15+
RUN useradd -m -s /bin/bash user && \
16+
mkdir -p /home/user && \
17+
chown -R user /home/user/
18+
19+
USER user
20+
21+
COPY comps /home/user/comps
22+
23+
RUN pip install --no-cache-dir --upgrade pip && \
24+
if [ ${ARCH} = "cpu" ]; then pip install --no-cache-dir torch --index-url https://download.pytorch.org/whl/cpu; fi && \
25+
pip install --no-cache-dir -r /home/user/comps/guardrails/bias_detection/requirements.txt
26+
27+
ENV PYTHONPATH=$PYTHONPATH:/home/user
28+
29+
WORKDIR /home/user/comps/guardrails/bias_detection/
30+
31+
ENTRYPOINT ["python", "bias_detection.py"]
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Bias Detection Microservice
2+
3+
## Introduction
4+
5+
Bias Detection Microservice allows AI Application developers to safeguard user input and LLM output from biased language in a RAG environment. By leveraging a smaller fine-tuned Transformer model for bias classification (e.g. DistilledBERT, RoBERTa, etc.), we maintain a lightweight guardrails microservice without significantly sacrificing performance making it readily deployable on both Intel Gaudi and Xeon.
6+
7+
Bias erodes our collective trust and fuels social conflict. Bias can be defined as inappropriate subjectivity in the form of one of the following:
8+
9+
- Framing bias -- using subjective words or phrases linked with a particular point of view
10+
- Epistemological bias -- linguistic features that subtly modify the believability of a proposition
11+
- Demographic bias -- text with presuppositions about particular genders, races, or other demographic categories
12+
13+
## Future Development
14+
15+
- Add a "neutralizing bias" microservice to neutralizing any detected bias in the RAG serving, guarding the RAG usage.
16+
17+
## 🚀1. Start Microservice with Python(Option 1)
18+
19+
### 1.1 Install Requirements
20+
21+
```bash
22+
pip install -r requirements.txt
23+
```
24+
25+
### 1.2 Start Bias Detection Microservice with Python Script
26+
27+
```bash
28+
python bias_detection.py
29+
```
30+
31+
## 🚀2. Start Microservice with Docker (Option 2)
32+
33+
### 2.1 Prepare bias detection model
34+
35+
export HUGGINGFACEHUB_API_TOKEN=${HP_TOKEN}
36+
37+
### 2.2 Build Docker Image
38+
39+
```bash
40+
cd ../../../ # back to GenAIComps/ folder
41+
docker build -t opea/guardrails-bias-detection:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/guardrails/bias_detection/Dockerfile .
42+
```
43+
44+
### 2.3 Run Docker Container with Microservice
45+
46+
```bash
47+
docker run -d --rm --runtime=runc --name="guardrails-bias-detection" -p 9092:9092 --ipc=host -e http_proxy=$http_proxy -e https_proxy=$https_proxy -e HUGGINGFACEHUB_API_TOKEN=${HUGGINGFACEHUB_API_TOKEN} -e HF_TOKEN=${HUGGINGFACEHUB_API_TOKEN} opea/guardrails-bias-detection:latest
48+
```
49+
50+
## 🚀3. Get Status of Microservice
51+
52+
```bash
53+
docker container logs -f guardrails-bias-detection
54+
```
55+
56+
## 🚀4. Consume Microservice Pre-LLM/Post-LLM
57+
58+
Once microservice starts, users can use examples (bash or python) below to apply bias detection for both user's query (Pre-LLM) or LLM's response (Post-LLM)
59+
60+
**Bash:**
61+
62+
```bash
63+
curl localhost:9092/v1/bias
64+
-X POST
65+
-d '{"text":"John McCain exposed as an unprincipled politician"}'
66+
-H 'Content-Type: application/json'
67+
```
68+
69+
Example Output:
70+
71+
```bash
72+
"\nI'm sorry, but your query or LLM's response is BIASED with an score of 0.74 (0-1)!!!\n"
73+
```
74+
75+
**Python Script:**
76+
77+
```python
78+
import requests
79+
import json
80+
81+
proxies = {"http": ""}
82+
url = "http://localhost:9092/v1/bias"
83+
data = {"text": "John McCain exposed as an unprincipled politician"}
84+
85+
86+
try:
87+
resp = requests.post(url=url, data=data, proxies=proxies)
88+
print(resp.text)
89+
resp.raise_for_status() # Raise an exception for unsuccessful HTTP status codes
90+
print("Request successful!")
91+
except requests.exceptions.RequestException as e:
92+
print("An error occurred:", e)
93+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
from transformers import pipeline
5+
6+
from comps import ServiceType, TextDoc, opea_microservices, register_microservice
7+
8+
9+
@register_microservice(
10+
name="opea_service@bias_detection",
11+
service_type=ServiceType.GUARDRAIL,
12+
endpoint="/v1/bias",
13+
host="0.0.0.0",
14+
port=9092,
15+
input_datatype=TextDoc,
16+
output_datatype=TextDoc,
17+
)
18+
def llm_generate(input: TextDoc):
19+
input_text = input.text
20+
toxic = bias_pipeline(input_text)
21+
print("done")
22+
if toxic[0]["label"] == "BIASED":
23+
return TextDoc(text="Violated policies: bias, please check your input.", downstream_black_list=[".*"])
24+
else:
25+
return TextDoc(text=input_text)
26+
27+
28+
if __name__ == "__main__":
29+
model = "valurank/distilroberta-bias"
30+
bias_pipeline = pipeline("text-classification", model=model, tokenizer=model)
31+
opea_microservices["opea_service@bias_detection"].start()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
aiohttp
2+
docarray[full]
3+
fastapi
4+
httpx
5+
huggingface_hub
6+
langchain-community
7+
langchain-huggingface
8+
opentelemetry-api
9+
opentelemetry-exporter-otlp
10+
opentelemetry-sdk
11+
prometheus-fastapi-instrumentator
12+
pyyaml
13+
requests
14+
shortuuid
15+
uvicorn

0 commit comments

Comments
 (0)