fix(ai-proxy): restrict the stream_options parameter to be effective only in the openai/v1/chatcompletions (#2524)

Signed-off-by: Xijun Dai <daixijun1990@gmail.com>
This commit is contained in:
Xijun Dai
2025-07-01 21:44:34 +08:00
committed by GitHub
parent 9d68ccbf35
commit 41a1455874
2 changed files with 4 additions and 3 deletions

View File

@@ -161,7 +161,8 @@ func onHttpRequestBody(ctx wrapper.HttpContext, pluginConfig config.PluginConfig
if settingErr != nil { if settingErr != nil {
log.Errorf("failed to replace request body by custom settings: %v", settingErr) log.Errorf("failed to replace request body by custom settings: %v", settingErr)
} }
if providerConfig.IsOpenAIProtocol() { // 仅 /v1/chat/completions 和 /v1/completions 接口支持 stream_options 参数
if providerConfig.IsOpenAIProtocol() && (apiName == provider.ApiNameChatCompletion || apiName == provider.ApiNameCompletion) {
newBody = normalizeOpenAiRequestBody(newBody) newBody = normalizeOpenAiRequestBody(newBody)
} }
log.Debugf("[onHttpRequestBody] newBody=%s", newBody) log.Debugf("[onHttpRequestBody] newBody=%s", newBody)
@@ -315,7 +316,7 @@ func onHttpResponseBody(ctx wrapper.HttpContext, pluginConfig config.PluginConfi
func normalizeOpenAiRequestBody(body []byte) []byte { func normalizeOpenAiRequestBody(body []byte) []byte {
var err error var err error
// Default setting include_usage. // Default setting include_usage.
if gjson.GetBytes(body, "stream").Bool() { if gjson.GetBytes(body, "stream").Bool() && (!gjson.GetBytes(body, "stream_options").Exists() || !gjson.GetBytes(body, "stream_options.include_usage").Exists()) {
body, err = sjson.SetBytes(body, "stream_options.include_usage", true) body, err = sjson.SetBytes(body, "stream_options.include_usage", true)
if err != nil { if err != nil {
log.Errorf("set include_usage failed, err:%s", err) log.Errorf("set include_usage failed, err:%s", err)

View File

@@ -82,7 +82,7 @@ func (m *doubaoProvider) TransformRequestBody(ctx wrapper.HttpContext, apiName A
// 移除火山 responses 接口暂时不支持的参数 // 移除火山 responses 接口暂时不支持的参数
// 参考: https://www.volcengine.com/docs/82379/1569618 // 参考: https://www.volcengine.com/docs/82379/1569618
// TODO: 这里应该用 DTO 处理 // TODO: 这里应该用 DTO 处理
for _, param := range []string{"parallel_tool_calls", "tool_choice", "stream_options"} { for _, param := range []string{"parallel_tool_calls", "tool_choice"} {
body, err = sjson.DeleteBytes(body, param) body, err = sjson.DeleteBytes(body, param)
if err != nil { if err != nil {
log.Warnf("[doubao] failed to delete %s in request body, err: %v", param, err) log.Warnf("[doubao] failed to delete %s in request body, err: %v", param, err)