Skip to content

Bias Evaluation

Run scheduled bias fairness checks using Scheduler.add_bias_job().

The bias job automatically collects real queries from your application (buffered silently during evaluate() calls), fetches the RAIT-managed bias template, generates prompt variants across demographic combinations, and posts the results for scoring.

Basic Setup

from rait_connector import RAITClient, Scheduler

client = RAITClient()
scheduler = Scheduler(client)

scheduler.add_bias_job(
    model_name="gpt-4",
    model_version="1.0",
    environment="production",
    model_purpose="monitoring",
    invoke_model=lambda prompt: my_llm.generate(prompt),
    interval="weekly",
)

scheduler.start()

No extra setup is needed — queries are captured automatically from evaluate() calls.

How It Works

  1. Every time evaluate() is called, the query is silently added to an internal buffer (up to 100 unique queries).
  2. On each scheduled run, the buffer is drained and the RAIT-managed bias template is fetched from the backend.
  3. For each query, the template generates prompts across every combination of variant × race/ethnicity × gender, and invoke_model is called for each.
  4. Results are posted to the RAIT platform with log_type="bias" for scoring.

What invoke_model Receives

invoke_model receives a fully-formatted prompt string — the original query already embedded into a template variant with demographic context substituted in. It should return the model's response as a string.

def call_my_model(prompt: str) -> str:
    response = openai_client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content

scheduler.add_bias_job(
    model_name="gpt-4",
    model_version="1.0",
    environment="production",
    model_purpose="monitoring",
    invoke_model=call_my_model,
    interval="weekly",
)

scheduler.start()

Individual variants where invoke_model raises are skipped and logged; the rest of the job continues.

Running Immediately on Start

scheduler.add_bias_job(
    model_name="gpt-4",
    model_version="1.0",
    environment="production",
    model_purpose="monitoring",
    invoke_model=lambda prompt: my_llm.generate(prompt),
    interval="weekly",
    run_immediately=True,
)

scheduler.start()

Custom Interval

from datetime import timedelta

scheduler.add_bias_job(
    model_name="gpt-4",
    model_version="1.0",
    environment="production",
    model_purpose="monitoring",
    invoke_model=lambda prompt: my_llm.generate(prompt),
    interval=timedelta(days=3),
)

Note on the Query Buffer

  • Holds up to 100 unique queries (oldest evicted when full).
  • Duplicate queries (same text) are deduplicated automatically.
  • Queries that arrive during a running bias job are held for the next run.
  • If the buffer is empty when the job fires, the run is skipped.