2

Sometimes, my OpenAI API on call like

 const completion = await openai.createChatCompletion({
  model: 'gpt-3.5-turbo',
  messages: [
    {
      role: 'system',
      content:  `You are ChatbotAssistant, an automated service to answer questions of a website visitors.` +
`You respond in a short, very conversational friendly style. If you can't find an answer, provide no answer and apologize.`
    },
    {role: 'user', content: userQuestion}
  ]
})
 const responseText = completion.data.choices[0].message.content

gives answer with "A:" or "Answer:" prepended. Since I don't need that, I tried to instruct it explicitly not to do it, by changing system message like:

`You are ChatbotAssistant, an automated service to answer questions of a website visitors.` +
`Do not prepend "A:" or "Answer:" to your answers` +
`You respond in a short, very conversational friendly style. If you can't find an 
answer, provide no answer and apologize.`

but to no effect.

I know I can handle this in Javascript like e.g.

let cleanedText = responseText

if (responseText.startsWith('A:') || responseText.startsWith('Answer:')) {
  cleanedText = responseText.replace(/^(A:|Answer:)\s*/, '')
}

but is there OpenAI solution to this? Thanks.

Gishas
  • 380
  • 6
  • 21

1 Answers1

2

The way to fix this would be to set the logit bias, additional walk through here and also use n.

Basically you can use logit bias to set the likihood of a specific token from -100(Banned) to 100(exclusive selection). You do this by taking the token id and giving it the correct value

What you do is first use a tokenizer tool for the token/tokens you want banned. This will require some thought work, as not all character combinations fit within one token. In your Case "A:" are tokens [32, 25] and "Answer:" is [33706, 25], taking it further "Answer" has a token of [33706], ":" is [25], and "A" is 32.

So youll want think on your combination here, because while you want to ban "Answer:' and "A:" you likely dont want to ban the word Answer or the letter A. One potental option is to ban ":" with a negative 100 value, and potentially put some bias on Answer and slight bias A. This will likely require some experimentation as you figure out the right ratios, and additionally you may come across other things you should ban like "a:", "answer:", "A-", etc.

Now that will help you ban words once you experiment. If you want some additional buffer/protection, you can also then add a value to n higher then 1. n allows you to return more then 1 answer, to you can have it send over the best 10 answers, and go through sequentially till one matches.

So basically you experiment with your logit bias till youve applied the right bias to ensure the "A:" and "Answer" Dont show up, and you can add in some additional code to help you filter it out when it does. Example below


 const completion = await openai.createChatCompletion({
  model: 'gpt-3.5-turbo',
  logit_bias={25:-100, 33706:-10, 32,-1},
  n:5,
  messages: [
    {
      role: 'system',
      content:  `You are ChatbotAssistant, an automated service to answer questions of a website visitors.` +
`You respond in a short, very conversational friendly style. If you can't find an answer, provide no answer and apologize.`
    },
    {role: 'user', content: userQuestion}
  ]
})
 const responseText = completion.data.choices[0].message.content

The code above bans ":" sets a bias against "Answer" and sets a small bias against "A" and gets me the top 5 results allowing me to have backups if something goes wrong. As mentioned youll need to experiment with the logit bias and you will likely need to add in more banned tokens as new variants of "A:"(like "a:") pop up.

Lucas Hendren
  • 2,786
  • 2
  • 18
  • 33
  • Thanks. Very informative answer. Yet in my case I'd rather stick with just replacing beginning 'A:' or 'Answer:' i.e. I'll live with it, but your answer provided some very useful insights, albeit a bit too much to implement in my particular case. – Gishas Aug 09 '23 at 08:45
  • 1
    Yeah, using the api to do this is a bit complicated versus just filtering. Especially since you can just check the first x characters. If its useful I can add on that your existing filter method is probably the better more light ended approach vs trying to get the ai model to change its output – Lucas Hendren Aug 09 '23 at 08:55
  • Still very useful and exciting to find out about openai's advanced capabilities. – Gishas Aug 09 '23 at 09:00