IGenerationWatermarker<T>
Interfaces · AiDotNet.Safety.Watermarking
A text watermarker that embeds its keyed watermark while a language model generates, by biasing the model’s next-token logits.
Implements: IKeyedWatermarker, ITextWatermarker<T>
Known implementations: SamplingWatermarker<T>
For Beginners
while the AI writes, this watermarker nudges it toward words from a secret, constantly changing list. The text reads normally, but someone with the key can count how often the AI picked listed words, which happens far more often than chance.
How It Works
This is the watermark of Kirchenbauer et al. (2023): at every step the preceding token(s) and the key select a “green list” holding a fraction gamma of the vocabulary, and delta is added to the logits of the green tokens before sampling. Detection counts the green tokens of a text and applies a one-proportion z-test.
Embedding needs a hook into generation: wrap the model with WatermarkModel (for example before
handing it to LocalEngineChatClient) or call BiasLogits on each step’s logits in your
own decode loop. Detection needs the token ids, either directly (DetectTokens) or by re-tokenizing the
text with the generator’s tokenizer.
Methods
| Method | Summary |
|---|---|
ICausalLanguageModel<T> WatermarkModel(ICausalLanguageModel<T> model) |
Wraps model so every next-token logits vector it returns carries the watermark bias. |
WatermarkResult DetectTokens(IReadOnlyList<int> tokenIds) |
Detects the watermark in a sequence of token ids. |
void BiasLogits(Vector<T> logits, IReadOnlyList<int> context) |
Adds the watermark bias to logits in place: delta is added to every token on the green list selected by the last tokens of context. |