mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-12-06 16:03:04 +00:00
fix(relay): request stream usage on every cross-protocol conversion to OpenAI chat (#7389)
This commit is contained in:
1 parent
81336fc69b
commit
610334dbd7
3 files changed
+164
-27
No files matched your search
@@ -77,15 +77,7 @@ func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayIn
|
||||
case relayconvert.ConverterNone:
|
||||
return a.claudeAdaptor.ConvertClaudeRequest(c, info, request)
|
||||
case relayconvert.ConverterClaudeMessagesToOpenAIChat:
|
||||
result, err := service.ConvertRequestByID(c, info, converter, request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
chatRequest, ok := result.Value.(*dto.GeneralOpenAIRequest)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("expected OpenAI chat completions request, got %T", result.Value)
|
||||
}
|
||||
return a.convertOpenAICompatibleRequest(c, info, chatRequest)
|
||||
return a.convertCrossProtocolChatRequest(c, info, converter, request)
|
||||
default:
|
||||
return nil, fmt.Errorf("converter %q does not support Anthropic Messages requests", converter)
|
||||
}
|
||||
@@ -101,15 +93,7 @@ func (a *Adaptor) ConvertGeminiRequest(c *gin.Context, info *relaycommon.RelayIn
|
||||
case relayconvert.ConverterNone:
|
||||
return a.geminiAdaptor.ConvertGeminiRequest(c, info, request)
|
||||
case relayconvert.ConverterGeminiContentToOpenAIChat:
|
||||
result, err := service.ConvertRequestByID(c, info, converter, request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
chatRequest, ok := result.Value.(*dto.GeneralOpenAIRequest)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("expected OpenAI chat completions request, got %T", result.Value)
|
||||
}
|
||||
return a.convertOpenAICompatibleRequest(c, info, chatRequest)
|
||||
return a.convertCrossProtocolChatRequest(c, info, converter, request)
|
||||
default:
|
||||
return nil, fmt.Errorf("converter %q does not support Gemini generateContent requests", converter)
|
||||
}
|
||||
@@ -124,15 +108,7 @@ func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommo
|
||||
case relayconvert.ConverterNone:
|
||||
return a.convertOpenAICompatibleResponsesRequest(c, info, request)
|
||||
case relayconvert.ConverterOpenAIResponsesToOpenAIChat:
|
||||
result, err := service.ConvertRequestByID(c, info, converter, request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
chatRequest, ok := result.Value.(*dto.GeneralOpenAIRequest)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("expected OpenAI chat completions request, got %T", result.Value)
|
||||
}
|
||||
return a.convertOpenAICompatibleRequest(c, info, chatRequest)
|
||||
return a.convertCrossProtocolChatRequest(c, info, converter, request)
|
||||
case relayconvert.ConverterOpenAIResponsesToGemini:
|
||||
result, err := service.ConvertRequestByID(c, info, converter, request)
|
||||
if err != nil {
|
||||
@@ -536,6 +512,25 @@ func isJSONRequest(c *gin.Context) bool {
|
||||
return strings.Contains(strings.ToLower(c.Request.Header.Get("Content-Type")), "application/json")
|
||||
}
|
||||
|
||||
// convertCrossProtocolChatRequest converts a Claude, Gemini, or Responses request into an
|
||||
// OpenAI chat completions request for an OpenAI-compatible upstream. Streaming requests
|
||||
// always ask the upstream for usage, because the downstream protocol reports usage in its
|
||||
// own format and never carries stream_options itself.
|
||||
func (a *Adaptor) convertCrossProtocolChatRequest(c *gin.Context, info *relaycommon.RelayInfo, converter string, request any) (any, error) {
|
||||
result, err := service.ConvertRequestByID(c, info, converter, request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
chatRequest, ok := result.Value.(*dto.GeneralOpenAIRequest)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("expected OpenAI chat completions request, got %T", result.Value)
|
||||
}
|
||||
if info.SupportStreamOptions && info.IsStream {
|
||||
chatRequest.StreamOptions = &dto.StreamOptions{IncludeUsage: true}
|
||||
}
|
||||
return a.convertOpenAICompatibleRequest(c, info, chatRequest)
|
||||
}
|
||||
|
||||
func (a *Adaptor) convertOpenAICompatibleRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
|
||||
old := info.ChannelType
|
||||
info.ChannelType = constant.ChannelTypeOpenAI
|
||||
|
||||
@@ -829,6 +829,143 @@ func TestAdaptorConvertsGeminiRequestToOpenAIChatUpstream(t *testing.T) {
|
||||
assert.Equal(t, "user", chatReq.Messages[0].Role)
|
||||
}
|
||||
|
||||
func TestAdaptorCrossProtocolChatUpstreamRequestsStreamUsage(t *testing.T) {
|
||||
claudeReq := &dto.ClaudeRequest{
|
||||
Model: "gpt-test",
|
||||
Messages: []dto.ClaudeMessage{{Role: "user", Content: "hello"}},
|
||||
}
|
||||
geminiReq := &dto.GeminiChatRequest{
|
||||
Contents: []dto.GeminiChatContent{{Role: "user", Parts: []dto.GeminiPart{{Text: "hello"}}}},
|
||||
}
|
||||
responsesReq := dto.OpenAIResponsesRequest{
|
||||
Model: "gpt-test",
|
||||
Input: mustAdvancedCustomRawMessage(t, "hello"),
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
route dto.AdvancedCustomRoute
|
||||
relayFormat types.RelayFormat
|
||||
relayMode int
|
||||
requestPath string
|
||||
convert func(*Adaptor, *gin.Context, *relaycommon.RelayInfo) (any, error)
|
||||
isStream bool
|
||||
supportUsage bool
|
||||
wantUsage bool
|
||||
}{
|
||||
{
|
||||
name: "claude stream requests usage",
|
||||
route: dto.AdvancedCustomRoute{
|
||||
IncomingPath: "/v1/messages",
|
||||
UpstreamPath: "/v1/chat/completions",
|
||||
Converter: relayconvert.ConverterClaudeMessagesToOpenAIChat,
|
||||
},
|
||||
relayFormat: types.RelayFormatClaude,
|
||||
relayMode: relayconstant.RelayModeChatCompletions,
|
||||
requestPath: "/v1/messages",
|
||||
convert: func(a *Adaptor, c *gin.Context, info *relaycommon.RelayInfo) (any, error) {
|
||||
return a.ConvertClaudeRequest(c, info, claudeReq)
|
||||
},
|
||||
isStream: true,
|
||||
supportUsage: true,
|
||||
wantUsage: true,
|
||||
},
|
||||
{
|
||||
name: "gemini stream requests usage",
|
||||
route: dto.AdvancedCustomRoute{
|
||||
IncomingPath: "/v1beta/models/{model}:generateContent",
|
||||
UpstreamPath: "/v1/chat/completions",
|
||||
Converter: relayconvert.ConverterGeminiContentToOpenAIChat,
|
||||
},
|
||||
relayFormat: types.RelayFormatGemini,
|
||||
relayMode: relayconstant.RelayModeGemini,
|
||||
requestPath: "/v1beta/models/gpt-test:generateContent",
|
||||
convert: func(a *Adaptor, c *gin.Context, info *relaycommon.RelayInfo) (any, error) {
|
||||
return a.ConvertGeminiRequest(c, info, geminiReq)
|
||||
},
|
||||
isStream: true,
|
||||
supportUsage: true,
|
||||
wantUsage: true,
|
||||
},
|
||||
{
|
||||
name: "responses stream requests usage",
|
||||
route: dto.AdvancedCustomRoute{
|
||||
IncomingPath: "/v1/responses",
|
||||
UpstreamPath: "/v1/chat/completions",
|
||||
Converter: relayconvert.ConverterOpenAIResponsesToOpenAIChat,
|
||||
},
|
||||
relayFormat: types.RelayFormatOpenAIResponses,
|
||||
relayMode: relayconstant.RelayModeResponses,
|
||||
requestPath: "/v1/responses",
|
||||
convert: func(a *Adaptor, c *gin.Context, info *relaycommon.RelayInfo) (any, error) {
|
||||
return a.ConvertOpenAIResponsesRequest(c, info, responsesReq)
|
||||
},
|
||||
isStream: true,
|
||||
supportUsage: true,
|
||||
wantUsage: true,
|
||||
},
|
||||
{
|
||||
name: "claude non-stream leaves stream options unset",
|
||||
route: dto.AdvancedCustomRoute{
|
||||
IncomingPath: "/v1/messages",
|
||||
UpstreamPath: "/v1/chat/completions",
|
||||
Converter: relayconvert.ConverterClaudeMessagesToOpenAIChat,
|
||||
},
|
||||
relayFormat: types.RelayFormatClaude,
|
||||
relayMode: relayconstant.RelayModeChatCompletions,
|
||||
requestPath: "/v1/messages",
|
||||
convert: func(a *Adaptor, c *gin.Context, info *relaycommon.RelayInfo) (any, error) {
|
||||
return a.ConvertClaudeRequest(c, info, claudeReq)
|
||||
},
|
||||
isStream: false,
|
||||
supportUsage: true,
|
||||
wantUsage: false,
|
||||
},
|
||||
{
|
||||
name: "claude stream without stream options support",
|
||||
route: dto.AdvancedCustomRoute{
|
||||
IncomingPath: "/v1/messages",
|
||||
UpstreamPath: "/v1/chat/completions",
|
||||
Converter: relayconvert.ConverterClaudeMessagesToOpenAIChat,
|
||||
},
|
||||
relayFormat: types.RelayFormatClaude,
|
||||
relayMode: relayconstant.RelayModeChatCompletions,
|
||||
requestPath: "/v1/messages",
|
||||
convert: func(a *Adaptor, c *gin.Context, info *relaycommon.RelayInfo) (any, error) {
|
||||
return a.ConvertClaudeRequest(c, info, claudeReq)
|
||||
},
|
||||
isStream: true,
|
||||
supportUsage: false,
|
||||
wantUsage: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
adaptor := &Adaptor{}
|
||||
info := advancedCustomRelayInfo(&dto.AdvancedCustomConfig{Routes: []dto.AdvancedCustomRoute{tt.route}})
|
||||
info.RelayFormat = tt.relayFormat
|
||||
info.RelayMode = tt.relayMode
|
||||
info.RequestURLPath = tt.requestPath
|
||||
info.IsStream = tt.isStream
|
||||
info.SupportStreamOptions = tt.supportUsage
|
||||
c := advancedCustomGinContext(tt.requestPath)
|
||||
|
||||
converted, err := tt.convert(adaptor, c, info)
|
||||
require.NoError(t, err)
|
||||
chatReq, ok := converted.(*dto.GeneralOpenAIRequest)
|
||||
require.True(t, ok)
|
||||
|
||||
if !tt.wantUsage {
|
||||
assert.Nil(t, chatReq.StreamOptions)
|
||||
return
|
||||
}
|
||||
require.NotNil(t, chatReq.StreamOptions)
|
||||
assert.True(t, chatReq.StreamOptions.IncludeUsage)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func advancedCustomRelayInfo(config *dto.AdvancedCustomConfig) *relaycommon.RelayInfo {
|
||||
return &relaycommon.RelayInfo{
|
||||
RelayFormat: types.RelayFormatOpenAI,
|
||||
|
||||
@@ -51,6 +51,11 @@ func (a *Adaptor) ConvertGeminiRequest(c *gin.Context, info *relaycommon.RelayIn
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("expected OpenAI chat completions request, got %T", result.Value)
|
||||
}
|
||||
if info.SupportStreamOptions && info.IsStream {
|
||||
openaiRequest.StreamOptions = &dto.StreamOptions{
|
||||
IncludeUsage: true,
|
||||
}
|
||||
}
|
||||
return a.ConvertOpenAIRequest(c, info, openaiRequest)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user