⚡ Quickstart Guide
(Optional) Create a virtual environment for using LangFair
We recommend creating a new virtual environment using venv before installing LangFair. To do so, please follow instructions here.Installing LangFair
The latest version can be installed from PyPI:Usage Examples
Below are code samples illustrating how to use LangFair to assess bias and fairness risks in text generation and summarization use cases. The below examples assume the user has already defined a list of prompts from their use case,prompts
.
Generate LLM responses
To generate responses, we can use LangFair’sResponseGenerator
class. First, we must create a langchain
LLM object. Below we use ChatVertexAI
, but any of LangChain’s LLM classes may be used instead. Note that InMemoryRateLimiter
is to used to avoid rate limit errors.
ResponseGenerator.generate_responses
to generate 25 responses for each prompt, as is convention for toxicity evaluation.
Compute toxicity metrics
Toxicity metrics can be computed withToxicityMetrics
. Note that use of torch.device
is optional and should be used if GPU is available to speed up toxicity computation.
Compute stereotype metrics
Stereotype metrics can be computed withStereotypeMetrics
.
Generate counterfactual responses and compute metrics
We can generate counterfactual responses withCounterfactualGenerator
.
CounterfactualMetrics
.
Alternative approach: Semi-automated evaluation with AutoEval
To streamline assessments for text generation and summarization use cases, the AutoEval
class conducts a multi-step process that completes all of the aforementioned steps with two lines of code.