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:
@@ -2,6 +2,7 @@ package provider
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
@@ -51,3 +52,35 @@ func (p *OpenAIProvider) ValidateConfig(config *Config) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsOpenAIReasoningOrGPT5Model 判断模型是否为 OpenAI / Azure OpenAI 的
|
||||
// 推理类(o-series)或 GPT-5 系列模型。
|
||||
//
|
||||
// 这些模型在 OpenAI Chat Completions API 中:
|
||||
// - 不再支持 `max_tokens`,必须使用 `max_completion_tokens`;
|
||||
// - 仅支持默认的 `temperature=1`、`top_p=1`,且不支持 `frequency_penalty` /
|
||||
// `presence_penalty` 等采样参数(传非默认值会被拒绝)。
|
||||
//
|
||||
// 参考:
|
||||
// - https://platform.openai.com/docs/api-reference/chat
|
||||
// - https://learn.microsoft.com/azure/ai-services/openai/how-to/reasoning
|
||||
//
|
||||
// 仅基于模型名做启发式匹配;对于 Azure OpenAI,因为模型名实际上是 deployment 名,
|
||||
// 用户若用了自定义部署名我们无法识别,此时仍会按普通模型处理(保持原行为)。
|
||||
func IsOpenAIReasoningOrGPT5Model(modelName string) bool {
|
||||
name := strings.ToLower(strings.TrimSpace(modelName))
|
||||
if name == "" {
|
||||
return false
|
||||
}
|
||||
if strings.HasPrefix(name, "gpt-5") {
|
||||
return true
|
||||
}
|
||||
// o1 / o1-mini / o1-preview / o3 / o3-mini / o4-mini ...
|
||||
// 必须精确匹配,避免误命中 "openai-..." 之类。
|
||||
for _, prefix := range []string{"o1", "o3", "o4"} {
|
||||
if name == prefix || strings.HasPrefix(name, prefix+"-") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user