fix: skip field filtering when request passthrough is enabled
This commit is contained in:
@@ -76,7 +76,7 @@ func chatCompletionsViaResponses(c *gin.Context, info *relaycommon.RelayInfo, ad
|
|||||||
return nil, types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
|
return nil, types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
|
||||||
}
|
}
|
||||||
|
|
||||||
chatJSON, err = relaycommon.RemoveDisabledFields(chatJSON, info.ChannelOtherSettings)
|
chatJSON, err = relaycommon.RemoveDisabledFields(chatJSON, info.ChannelOtherSettings, info.ChannelSetting.PassThroughBodyEnabled)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
|
return nil, types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
|
||||||
}
|
}
|
||||||
@@ -120,7 +120,7 @@ func chatCompletionsViaResponses(c *gin.Context, info *relaycommon.RelayInfo, ad
|
|||||||
return nil, types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
|
return nil, types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonData, err = relaycommon.RemoveDisabledFields(jsonData, info.ChannelOtherSettings)
|
jsonData, err = relaycommon.RemoveDisabledFields(jsonData, info.ChannelOtherSettings, info.ChannelSetting.PassThroughBodyEnabled)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
|
return nil, types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -146,7 +146,7 @@ func ClaudeHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *typ
|
|||||||
}
|
}
|
||||||
|
|
||||||
// remove disabled fields for Claude API
|
// remove disabled fields for Claude API
|
||||||
jsonData, err = relaycommon.RemoveDisabledFields(jsonData, info.ChannelOtherSettings)
|
jsonData, err = relaycommon.RemoveDisabledFields(jsonData, info.ChannelOtherSettings, info.ChannelSetting.PassThroughBodyEnabled)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
|
return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/QuantumNous/new-api/dto"
|
||||||
|
"github.com/QuantumNous/new-api/setting/model_setting"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestApplyParamOverrideTrimPrefix(t *testing.T) {
|
func TestApplyParamOverrideTrimPrefix(t *testing.T) {
|
||||||
@@ -772,6 +775,43 @@ func TestApplyParamOverrideToUpper(t *testing.T) {
|
|||||||
assertJSONEqual(t, `{"model":"GPT-4"}`, string(out))
|
assertJSONEqual(t, `{"model":"GPT-4"}`, string(out))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRemoveDisabledFieldsSkipWhenChannelPassThroughEnabled(t *testing.T) {
|
||||||
|
input := `{
|
||||||
|
"service_tier":"flex",
|
||||||
|
"safety_identifier":"user-123",
|
||||||
|
"store":true,
|
||||||
|
"stream_options":{"include_obfuscation":false}
|
||||||
|
}`
|
||||||
|
settings := dto.ChannelOtherSettings{}
|
||||||
|
|
||||||
|
out, err := RemoveDisabledFields([]byte(input), settings, true)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("RemoveDisabledFields returned error: %v", err)
|
||||||
|
}
|
||||||
|
assertJSONEqual(t, input, string(out))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRemoveDisabledFieldsSkipWhenGlobalPassThroughEnabled(t *testing.T) {
|
||||||
|
original := model_setting.GetGlobalSettings().PassThroughRequestEnabled
|
||||||
|
model_setting.GetGlobalSettings().PassThroughRequestEnabled = true
|
||||||
|
t.Cleanup(func() {
|
||||||
|
model_setting.GetGlobalSettings().PassThroughRequestEnabled = original
|
||||||
|
})
|
||||||
|
|
||||||
|
input := `{
|
||||||
|
"service_tier":"flex",
|
||||||
|
"safety_identifier":"user-123",
|
||||||
|
"stream_options":{"include_obfuscation":false}
|
||||||
|
}`
|
||||||
|
settings := dto.ChannelOtherSettings{}
|
||||||
|
|
||||||
|
out, err := RemoveDisabledFields([]byte(input), settings, false)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("RemoveDisabledFields returned error: %v", err)
|
||||||
|
}
|
||||||
|
assertJSONEqual(t, input, string(out))
|
||||||
|
}
|
||||||
|
|
||||||
func assertJSONEqual(t *testing.T, want, got string) {
|
func assertJSONEqual(t *testing.T, want, got string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
|
|||||||
@@ -703,7 +703,11 @@ func FailTaskInfo(reason string) *TaskInfo {
|
|||||||
// store: 数据存储授权字段,涉及用户隐私(仅 OpenAI、Responses API 支持,默认允许透传,禁用后可能导致 Codex 无法使用)
|
// store: 数据存储授权字段,涉及用户隐私(仅 OpenAI、Responses API 支持,默认允许透传,禁用后可能导致 Codex 无法使用)
|
||||||
// safety_identifier: 安全标识符,用于向 OpenAI 报告违规用户(仅 OpenAI 支持,涉及用户隐私)
|
// safety_identifier: 安全标识符,用于向 OpenAI 报告违规用户(仅 OpenAI 支持,涉及用户隐私)
|
||||||
// stream_options.include_obfuscation: 响应流混淆控制字段(仅 OpenAI Responses API 支持)
|
// stream_options.include_obfuscation: 响应流混淆控制字段(仅 OpenAI Responses API 支持)
|
||||||
func RemoveDisabledFields(jsonData []byte, channelOtherSettings dto.ChannelOtherSettings) ([]byte, error) {
|
func RemoveDisabledFields(jsonData []byte, channelOtherSettings dto.ChannelOtherSettings, channelPassThroughEnabled bool) ([]byte, error) {
|
||||||
|
if model_setting.GetGlobalSettings().PassThroughRequestEnabled || channelPassThroughEnabled {
|
||||||
|
return jsonData, nil
|
||||||
|
}
|
||||||
|
|
||||||
var data map[string]interface{}
|
var data map[string]interface{}
|
||||||
if err := common.Unmarshal(jsonData, &data); err != nil {
|
if err := common.Unmarshal(jsonData, &data); err != nil {
|
||||||
common.SysError("RemoveDisabledFields Unmarshal error :" + err.Error())
|
common.SysError("RemoveDisabledFields Unmarshal error :" + err.Error())
|
||||||
|
|||||||
@@ -165,7 +165,7 @@ func TextHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *types
|
|||||||
}
|
}
|
||||||
|
|
||||||
// remove disabled fields for OpenAI API
|
// remove disabled fields for OpenAI API
|
||||||
jsonData, err = relaycommon.RemoveDisabledFields(jsonData, info.ChannelOtherSettings)
|
jsonData, err = relaycommon.RemoveDisabledFields(jsonData, info.ChannelOtherSettings, info.ChannelSetting.PassThroughBodyEnabled)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
|
return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ func ResponsesHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *
|
|||||||
}
|
}
|
||||||
|
|
||||||
// remove disabled fields for OpenAI Responses API
|
// remove disabled fields for OpenAI Responses API
|
||||||
jsonData, err = relaycommon.RemoveDisabledFields(jsonData, info.ChannelOtherSettings)
|
jsonData, err = relaycommon.RemoveDisabledFields(jsonData, info.ChannelOtherSettings, info.ChannelSetting.PassThroughBodyEnabled)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
|
return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user