如何使OpenAI停止在其答案前加上”A:”或”Answer:”?

huangapple go评论120阅读模式
英文:

How to make OpenAI stop prepending "A:" or "Answer:" to its answers?

问题

有时,我的OpenAI API调用如下所示:

const completion = await openai.createChatCompletion({
  model: 'gpt-3.5-turbo',
  messages: [
    {
      role: 'system',
      content:  '您是ChatbotAssistant,一个自动化服务,用于回答网站访客的问题。' +
        '您以简短、非常对话友好的方式回应。如果找不到答案,请不要回答并道歉。'
    },
    {role: 'user', content: userQuestion}
  ]
})
const responseText = completion.data.choices[0].message.content

给出的答案以"A:"或"Answer:"为前缀。由于我不需要这样做,我试图明确指示它不要这样做,通过更改系统消息如下:

'您是ChatbotAssistant,一个自动化服务,用于回答网站访客的问题。' +
'请不要在您的答案前添加"A:"或"Answer:"' +
'您以简短、非常对话友好的方式回应。如果找不到答案,请不要回答并道歉。'

但没有效果。

我知道我可以在JavaScript中处理这个问题,例如:

let cleanedText = responseText

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

但是否有OpenAI的解决方案?谢谢。

英文:

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.

答案1

得分: 2

修复此问题的方法是设置对数偏差,在这里进行额外的步骤,同时使用n

基本上,您可以使用对数偏差来设置特定令牌的可能性,从-100(禁止)到100(独占选择)。这可以通过获取令牌ID并赋予它正确的值来实现。

您首先要做的是使用分词器工具对您想要禁止的令牌/令牌进行处理。这需要一些思考工作,因为并非所有字符组合都适合一个令牌。在您的情况下,“A:”是令牌[32, 25],“Answer:”是[33706, 25],进一步说,“Answer”的令牌是[33706],“:”是[25],“A”是32。

因此,您需要考虑一下这里的组合,因为虽然您想禁止“Answer:'和“A:”,但您可能不想禁止单词“Answer”或字母“A”。一个潜在的选择是使用负100的值禁止“:”,并可能在“Answer”上添加一些偏差和轻微的“A”偏差。这可能需要一些实验,以确定正确的比例,并且可能还会遇到其他应该禁止的事物,比如“a:”、“answer:”、“A-”等。

现在,这将帮助您在实验后禁止单词。如果您希望有一些额外的缓冲/保护,您还可以将n的值设置为大于1。n允许您返回多于1个答案,因此您可以要求它发送最佳的10个答案,并按顺序查看哪一个匹配。

因此,基本上,您要通过实验来调整对数偏差,以确保“A:”和“Answer”不会显示出来,并且当它显示出来时,您可以添加一些附加代码来帮助您过滤它。以下是示例:

 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 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

上面的代码禁止“:”,针对“Answer”设置了一个偏差,并对“A”设置了一个小的偏差,以获得前5个结果,允许您在出现问题时备份。如前所述,您需要通过实验来调整对数偏差,并且可能需要添加更多的禁止令牌,以应对“A:”(如“a:”)的新变种。

英文:

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.

huangapple
  • 本文由 发表于 2023年8月9日 15:03:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865340-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定