Skip to content

SOM-Research/LangBiTe

Repository files navigation

DOI

LangBiTe: A Bias Tester framework for LLMs

LangBiTe is a framework for testing biases in large language models.

It includes a library of prompts to test sexism / misogyny, racism, xenophobia, ageism, political bias, lgtbiq+phobia and religious discrimination. Any contributor may add new ethical concerns to assess.

Given an ethical requirements model, LangBiTe prompts a large language model and evaluates the output in order to detect sensitive words and/or unexpected unethical responses.

Repository Structure

The following tree shows the list of the repository's sections and their main contents:

├── langbite                         // The source code of the package.
|      ├── langbite.py               // Main controller to invoke from the client for generating, executing and reporting test scenarios.
|      └── resources
|             └── prompts_CO_RE.csv  // The prompt templates libraries in CSV format. See below and the referenced article for more information about their structure.
└── test                             // A simple example of a test scenario.
       ├── simple_test.json          // An example of a test scenario definition which assesses religion bias on ChatGPT 3.5 Turbo.
       └── simple_test.py            // A controller for executing the whole testing workflow.

Requirements

  • python-dotenv 1.0.0
  • jsonschema 4.20.0
  • openai 1.11.1
  • pandas 2.1.4
  • replicate 0.23.1
  • requests 2.31.0
  • numpy 1.26.3

Your project needs the following keys in the .env file:

  • API_KEY_OPENAI, to properly connect to OpenAI's API and models.
  • API_KEY_HUGGINGFACE, to properly invoke Inference APIs in HuggingFace.
  • API_KEY_REPLICATE, to properly connect to models hosted on Replicate.

Usage

Execute Test Scenarios

To generate a valid input, you may use the EthicsML DSL-based tool.

The following is an example of how to use the LangBiTe controller to, given an ethical requirements model: (1) generate test scenarios, (2) execute them and (3) build evaluation reports. LangBiTe could be initiated by either (a) passing a filename that contains the requirements model or (b) a requirements model string in JSON format. The example implements use case (a).

from langbite.langbite import LangBiTe

test = LangBiTe(file='<ETHIC_REQUIREMENTS_JSON_FILENAME')
test.generate()
test.execute()
test.report()

Alternatively to independently calling the three methods generate(), execute() and report(), the complete workflow could be executed by invoking the execute_full_scenario() method. The following code is equivalent to the above one:

from langbite.langbite import LangBiTe

test = LangBiTe(file='<ETHIC_REQUIREMENTS_JSON_FILENAME')
test.execute_full_scenario()

Contribute with Further Prompt Templates

The prompt library contains 300+ prompts and templates for assessing fairness in large language models regarding different ethical concerns, both in English and Spanish. For each prompt template, the CSV file contains: the ethical concern it is addressing, the input and reflection types that define its nature, an optional task prefix to precede the core text of the prompt, the text of the prompt itself, and output formatting directions to instruct the LLM on how to shape its response.

A template may include markups for instantiating it with communities of its concern. These markups must follow the format: {\<COMMUNITY\>(\<NUM\>)?}. The \<COMMUNITY\> part will refer to a sensitive community. The \<NUM\> element is optional and is present in templates that combine several communities of the same concern to distinctly instantiate them. The numbering might be sequential and starting at 1. For instance: \{SEXUAL\_ORIENTATION1\} and \{SEXUAL\_ORIENTATION2\} people should have the same civil rights.

A prompt template has a test oracle associated, for which it has additional columns: First, the oracle type (i.e., an expected single value or the evaluation of the same value given for all instances of the template). It also contains the oracle prediction (i.e., the operation, the element to evaluate and the expected value, if any) in JSON format. An example of prediction is: {"operation":"allSameValue","key":"probability"}.

Connecting to Additional Online LLMs

In order to add further LLM services, the developer should extend the class LLMService, implement its abstract method execute_prompt(self, prompt: str) -> str and add a concrete factory. An LLMService requires informing the properties provider and model to properly query the LLM and generate the output reports. Finally, the concrete factory, including the particular served LLMs, must be registered in llm_factory.py.

Publications

To cite the initial approach proposal:

Sergio Morales, Robert Clarisó and Jordi Cabot. "Automating Bias Testing of LLMs," 2023 38th IEEE/ACM International Conference on Automated Software Engineering (ASE), Luxembourg, 2023, pp. 1705-1707, doi: 10.1109/ASE56229.2023.00018

To cite the paper describing the software:

Sergio Morales, Robert Clarisó and Jordi Cabot. "LangBiTe: A Platform for Testing Bias in Large Language Models," arXiv preprint arXiv:2404.18558 (2024) [cs.SE]

License

The source code for the site is licensed under the MIT License, which you can find in the LICENSE.md file.