7.7.10. OPENAI-10 — Moderations

This tutorial covers the content-moderation endpoint — classifying text for policy violations. As with every dasOPENAI script, the consumer root needs options rtti.

7.7.10.1. Classifying Text

ModerationRequest takes a model and an array of input strings; moderations returns one verdict per input in response.results:

require openai/openai_moderations

let req = ModerationRequest(model = "text-moderation-latest",
    input <- ["some text to classify"])
let res = moderations(client, req)

7.7.10.2. Reading the Verdict

Each ModerationCategoryResult carries a flagged boolean plus two tables keyed by category name ("violence", "hate", "self-harm", …) — categories (booleans) and category_scores (confidence in [0, 1]). Because they are tables, use safe lookup rather than assuming a key exists:

for (verdict in res.response.results) {
    let violence = verdict.category_scores?["violence"] ?? 0.0lf
    print("flagged={verdict.flagged}, violence score={violence}\n")
}

Note

The bundled mock flags any input containing the sentinel "flagged-example" so both outcomes are demonstrable offline; a real backend decides per its model.