0

I'm trying to block GPT-4 from saying a multi-token word but can't seem to do it. How can I block the word? (It works for the single-token word "a" but not the multi-token word "truly")

I have tried blocking all prefixes of "Truly" and it won't even switch it to lowercase

See typescript code: npx ts-node myfile.ts

import { OpenAIApi } from "openai";
import { Configuration as OpenAiConfig } from "openai/dist/configuration";
import tokenizer from "gpt-3-encoder";

const openai = new OpenAIApi(
  new OpenAiConfig({
    apiKey: process.env.OPENAI_API_KEY,
  }),
);

async function request_gpt4<T>({
  messages,
  max_tokens,
  logit_bias,
}: {
  messages: any,
  max_tokens: number,
  logit_bias: { [x: string]: number },
}): Promise<any> {
  const model = "gpt-4";
  const response = await openai
    .createChatCompletion({
      model,
      messages,
      temperature: 0.0,
      max_tokens,
      stop: [" END"],
      logit_bias,
      n: 1,
    }).catch((e) => {
      console.error("OPENAI CATCH ERROR", { ...e.toJSON(), config: undefined });
      return null;
    });
  // console.log(response);
  if (response) {
    return (response as any).data.choices[0].message.content;
  }
  return null;
}

function defaultAbBiases(bias: number): { [x: string]: number } {
  const tokens: number[] = ['a'].flatMap(tokenizer.encode);
  return {
    ...Object.fromEntries(tokens.map((t) => [t, bias])),
  };
}

function defaultTrulyFalselyBiases(bias: number): { [x: string]: number } {
  const tokens: number[] = [
    " truly",
    " Truly",
    "r",
    "ul",
    "ruly",
    "uly",
    "T",
    "Tr",
    "Tru",
    "Trul",
    "Truly",
  ].flatMap(tokenizer.encode);
  return {
    ...Object.fromEntries(tokens.map((t) => [t, bias])),
  };
}

(async () => {
  for (var bias of [0, -1, -10, -100]) {
    const x = await request_gpt4({
      messages: [{
        role: "user",
        content: `Say either: "a" or "b"`,
      }],
      max_tokens: 1,
      logit_bias: defaultAbBiases(bias),
    });
    console.log(bias, x);
  }
  for (var bias of [0, -1, -10, -100]) {
    const x = await request_gpt4({
      messages: [{
        role: "user",
        content: `Say either: "truly" or "falsely"`,
      }],
      max_tokens: 5,
      logit_bias: defaultTrulyFalselyBiases(bias),
    });
    console.log(bias, x);
  }
  console.log("DONE");
})().catch(e => {
  console.log(e);
});

Output is:

0 a
-1 a
-10 b
-100 b
0 Truly
-1 Truly
-10 Truly
-100 Truly
DONE

But I would expect it to switch to Falsely at some point

hello
  • 5
  • 2

0 Answers0