fix(chat): map MaxTokens to MaxCompletionTokens for GPT-5/o-series

OpenAI's GPT-5 series and o-series reasoning models (o1/o3/o4-mini) no
longer accept `max_tokens` and reject non-default sampling params
(`temperature`, `top_p`, `frequency_penalty`, `presence_penalty`).
Azure OpenAI propagates the same constraint, returning HTTP 400 with
"this model is not supported MaxTokens, please use MaxCompletionTokens".

Lower the compatibility shim into `RemoteAPIChat.BuildChatCompletionRequest`
(the single OpenAI-protocol egress) so every internal caller keeps using
`MaxTokens` uniformly:

- Add `provider.IsOpenAIReasoningOrGPT5Model` with precise prefix matching
  (covers `gpt-5*`, `o1`/`o1-*`, `o3`/`o3-*`, `o4`/`o4-*`; rejects
  `olympus-1`, `openai-*`, `o3xtra`, etc.).
- When the provider is `openai`/`azure_openai` and the model matches, map
  `MaxTokens` to `MaxCompletionTokens` (explicit `MaxCompletionTokens`
  wins) and skip the unsupported sampling fields; `omitempty` keeps them
  off the wire.
- Behavior is unchanged for gpt-4o, gpt-4, and all other providers.

Add unit tests for the matcher and for the request-building path
(Azure gpt-5.2, OpenAI gpt-5/o1-mini/o3/o4-mini, plus negative cases).

Fixes #1283
This commit is contained in:
wizardchen
2026-05-13 00:24:22 +08:00
committed by lyingbug
parent 4d2b8707ff
commit 9e08ab7302
4 changed files with 193 additions and 14 deletions
+36 -14
View File
@@ -267,21 +267,43 @@ func (c *RemoteAPIChat) BuildChatCompletionRequest(messages []Message, opts *Cha
}
if opts != nil {
req.Temperature = float32(opts.Temperature)
if opts.TopP > 0 {
req.TopP = float32(opts.TopP)
// OpenAI / Azure OpenAI 的推理类模型(o-series)以及 GPT-5 系列
// 不再支持 max_tokens,必须使用 max_completion_tokens;且不支持
// temperature / top_p / frequency_penalty / presence_penalty 的非默认值。
// 这里做兼容处理,避免上层无差别地传 MaxTokens 导致 400 错误:
// "this model is not supported MaxTokens, please use MaxCompletionTokens"
// 参考 issue #1283。
isOpenAIReasoning := (c.provider == provider.ProviderOpenAI || c.provider == provider.ProviderAzureOpenAI) &&
provider.IsOpenAIReasoningOrGPT5Model(c.modelName)
if !isOpenAIReasoning {
req.Temperature = float32(opts.Temperature)
if opts.TopP > 0 {
req.TopP = float32(opts.TopP)
}
if opts.FrequencyPenalty > 0 {
req.FrequencyPenalty = float32(opts.FrequencyPenalty)
}
if opts.PresencePenalty > 0 {
req.PresencePenalty = float32(opts.PresencePenalty)
}
}
if opts.MaxTokens > 0 {
req.MaxTokens = opts.MaxTokens
}
if opts.MaxCompletionTokens > 0 {
req.MaxCompletionTokens = opts.MaxCompletionTokens
}
if opts.FrequencyPenalty > 0 {
req.FrequencyPenalty = float32(opts.FrequencyPenalty)
}
if opts.PresencePenalty > 0 {
req.PresencePenalty = float32(opts.PresencePenalty)
switch {
case isOpenAIReasoning:
// 强制使用 max_completion_tokens;MaxTokens 字段保持零值,依赖 omitempty 不发送。
if opts.MaxCompletionTokens > 0 {
req.MaxCompletionTokens = opts.MaxCompletionTokens
} else if opts.MaxTokens > 0 {
req.MaxCompletionTokens = opts.MaxTokens
}
default:
if opts.MaxTokens > 0 {
req.MaxTokens = opts.MaxTokens
}
if opts.MaxCompletionTokens > 0 {
req.MaxCompletionTokens = opts.MaxCompletionTokens
}
}
// 处理 Tools