Sign inSign up

opea/guardrails-bias-detection

By opea

•Updated over 1 year ago

OPEA guardrail microservice to provide bias detection for GenAI application

Image
0

2.0K

opea/guardrails-bias-detection repository overview

⁠Bias Detection Microservice

⁠Table of Contents

⁠Introduction

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.

Bias erodes our collective trust and fuels social conflict. Bias can be defined as inappropriate subjectivity in the form of one of the following:

  • Framing bias -- using subjective words or phrases linked with a particular point of view
  • Epistemological bias -- linguistic features that subtly modify the believability of a proposition
  • Demographic bias -- text with presuppositions about particular genders, races, or other demographic categories

⁠Future Development

  • Add a "neutralizing bias" microservice to neutralizing any detected bias in the RAG serving, guarding the RAG usage.

⁠Start Microservice

⁠Start Microservice with Python(Option 1)
⁠Install Requirements
pip install -r requirements.txt
⁠Start Bias Detection Microservice with Python Script
python bias_detection.py
⁠Start Microservice with Docker (Option 2)
⁠Prepare bias detection model
export HF_TOKEN=${HF_TOKEN}
⁠Build Docker Image
cd ../../../ # back to GenAIComps/ folder
docker build -t opea/guardrails-bias-detection:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/guardrails/src/bias_detection/Dockerfile .
⁠Run Docker Container with Microservice
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 HF_TOKEN=${HF_TOKEN} opea/guardrails-bias-detection:latest
⁠Consume Microservice
⁠Get Status of Microservice
docker container logs -f guardrails-bias-detection
⁠Consume Microservice Pre-LLM/Post-LLM

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)

Bash:

curl localhost:9092/v1/bias \
    -X POST \
    -d '{"text":"John McCain exposed as an unprincipled politician"}' \
    -H 'Content-Type: application/json'

Example Output:

"Violated policies: bias, please check your input."

Python Script:

import requests
import json

proxies = {"http": ""}
url = "http://localhost:9092/v1/bias"
data = {"text": "John McCain exposed as an unprincipled politician"}


try:
    resp = requests.post(url=url, data=data, proxies=proxies)
    print(resp.text)
    resp.raise_for_status()  # Raise an exception for unsuccessful HTTP status codes
    print("Request successful!")
except requests.exceptions.RequestException as e:
    print("An error occurred:", e)

Tag summary

Content type

Image

Digest

sha256:b1feb1b89…

Size

1 GB

Last updated

over 1 year ago

docker pull opea/guardrails-bias-detection