adds variable filter sampling rate

This commit is contained in:
nai-degen 2023-04-08 21:57:51 -05:00 committed by nai-degen
parent d0ce8a4632
commit b889374cef
3 changed files with 5 additions and 1 deletions

View File

@ -23,3 +23,4 @@ OPENAI_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# LOG_PROMPTS=false
# REJECT_DISALLOWED=false
# REJECT_MESSAGE=This content violates /aicg/'s acceptable use policy.
# REJECT_SAMPLE_RATE=0.2

View File

@ -14,6 +14,8 @@ type Config = {
maxOutputTokens: number;
/** Whether requests containing disallowed characters should be rejected. */
rejectDisallowed?: boolean;
/** Rejection sample rate (0 - 1). Higher values are more strict but increase server load. */
rejectSampleRate?: number;
/** Message to return when rejecting requests. */
rejectMessage?: string;
/** Logging threshold. */
@ -29,6 +31,7 @@ export const config: Config = {
modelRateLimit: getEnvWithDefault("MODEL_RATE_LIMIT", 2),
maxOutputTokens: getEnvWithDefault("MAX_OUTPUT_TOKENS", 256),
rejectDisallowed: getEnvWithDefault("REJECT_DISALLOWED", false),
rejectSampleRate: getEnvWithDefault("REJECT_SAMPLE_RATE", 0.2),
rejectMessage: getEnvWithDefault(
"REJECT_MESSAGE",
"This content violates /aicg/'s acceptable use policy."

View File

@ -9,7 +9,7 @@ const DISALLOWED_REGEX =
// 15k character request ten times a second. So we'll just sample 20% of the
// characters and hope that's enough.
const containsDisallowedCharacters = (text: string) => {
const sampleSize = Math.floor(text.length * 0.2);
const sampleSize = Math.ceil(text.length * (config.rejectSampleRate || 0.2));
const sample = text
.split("")
.sort(() => 0.5 - Math.random())