Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
5d127557
authored
Oct 29, 2025
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor(relay): enhance error logging and improve multipart form handling in audio requests #2127
parent
602a9284
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
35 additions
and
34 deletions
+35
-34
controller/relay.go
+2
-1
relay/channel/openai/adaptor.go
+26
-7
relay/channel/siliconflow/adaptor.go
+7
-26
No files found.
controller/relay.go
View file @
5d127557
...
@@ -84,6 +84,7 @@ func Relay(c *gin.Context, relayFormat types.RelayFormat) {
...
@@ -84,6 +84,7 @@ func Relay(c *gin.Context, relayFormat types.RelayFormat) {
defer
func
()
{
defer
func
()
{
if
newAPIError
!=
nil
{
if
newAPIError
!=
nil
{
logger
.
LogError
(
c
,
fmt
.
Sprintf
(
"relay error: %s"
,
newAPIError
.
Error
()))
newAPIError
.
SetMessage
(
common
.
MessageWithRequestId
(
newAPIError
.
Error
(),
requestId
))
newAPIError
.
SetMessage
(
common
.
MessageWithRequestId
(
newAPIError
.
Error
(),
requestId
))
switch
relayFormat
{
switch
relayFormat
{
case
types
.
RelayFormatOpenAIRealtime
:
case
types
.
RelayFormatOpenAIRealtime
:
...
@@ -281,7 +282,7 @@ func shouldRetry(c *gin.Context, openaiErr *types.NewAPIError, retryTimes int) b
...
@@ -281,7 +282,7 @@ func shouldRetry(c *gin.Context, openaiErr *types.NewAPIError, retryTimes int) b
}
}
func
processChannelError
(
c
*
gin
.
Context
,
channelError
types
.
ChannelError
,
err
*
types
.
NewAPIError
)
{
func
processChannelError
(
c
*
gin
.
Context
,
channelError
types
.
ChannelError
,
err
*
types
.
NewAPIError
)
{
logger
.
LogError
(
c
,
fmt
.
Sprintf
(
"
relay
error (channel #%d, status code: %d): %s"
,
channelError
.
ChannelId
,
err
.
StatusCode
,
err
.
Error
()))
logger
.
LogError
(
c
,
fmt
.
Sprintf
(
"
channel
error (channel #%d, status code: %d): %s"
,
channelError
.
ChannelId
,
err
.
StatusCode
,
err
.
Error
()))
// 不要使用context获取渠道信息,异步处理时可能会出现渠道信息不一致的情况
// 不要使用context获取渠道信息,异步处理时可能会出现渠道信息不一致的情况
// do not use context to get channel info, there may be inconsistent channel info when processing asynchronously
// do not use context to get channel info, there may be inconsistent channel info when processing asynchronously
if
service
.
ShouldDisableChannel
(
channelError
.
ChannelId
,
err
)
&&
channelError
.
AutoBan
{
if
service
.
ShouldDisableChannel
(
channelError
.
ChannelId
,
err
)
&&
channelError
.
AutoBan
{
...
...
relay/channel/openai/adaptor.go
View file @
5d127557
...
@@ -15,9 +15,11 @@ import (
...
@@ -15,9 +15,11 @@ import (
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/constant"
"github.com/QuantumNous/new-api/constant"
"github.com/QuantumNous/new-api/dto"
"github.com/QuantumNous/new-api/dto"
"github.com/QuantumNous/new-api/logger"
"github.com/QuantumNous/new-api/relay/channel"
"github.com/QuantumNous/new-api/relay/channel"
"github.com/QuantumNous/new-api/relay/channel/ai360"
"github.com/QuantumNous/new-api/relay/channel/ai360"
"github.com/QuantumNous/new-api/relay/channel/lingyiwanwu"
"github.com/QuantumNous/new-api/relay/channel/lingyiwanwu"
//"github.com/QuantumNous/new-api/relay/channel/minimax"
//"github.com/QuantumNous/new-api/relay/channel/minimax"
"github.com/QuantumNous/new-api/relay/channel/openrouter"
"github.com/QuantumNous/new-api/relay/channel/openrouter"
"github.com/QuantumNous/new-api/relay/channel/xinference"
"github.com/QuantumNous/new-api/relay/channel/xinference"
...
@@ -352,27 +354,43 @@ func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInf
...
@@ -352,27 +354,43 @@ func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInf
writer
.
WriteField
(
"model"
,
request
.
Model
)
writer
.
WriteField
(
"model"
,
request
.
Model
)
// 获取所有表单字段
formData
,
err2
:=
common
.
ParseMultipartFormReusable
(
c
)
formData
:=
c
.
Request
.
PostForm
if
err2
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"error parsing multipart form: %w"
,
err2
)
}
// 打印类似 curl 命令格式的信息
logger
.
LogDebug
(
c
.
Request
.
Context
(),
fmt
.
Sprintf
(
"--form 'model=
\"
%s
\"
'"
,
request
.
Model
))
// 遍历表单字段并打印输出
// 遍历表单字段并打印输出
for
key
,
values
:=
range
formData
{
for
key
,
values
:=
range
formData
.
Value
{
if
key
==
"model"
{
if
key
==
"model"
{
continue
continue
}
}
for
_
,
value
:=
range
values
{
for
_
,
value
:=
range
values
{
writer
.
WriteField
(
key
,
value
)
writer
.
WriteField
(
key
,
value
)
logger
.
LogDebug
(
c
.
Request
.
Context
(),
fmt
.
Sprintf
(
"--form '%s=
\"
%s
\"
'"
,
key
,
value
))
}
}
}
}
//
添加文件字段
//
从 formData 中获取文件
file
,
header
,
err
:=
c
.
Request
.
FormFile
(
"file"
)
file
Headers
:=
formData
.
File
[
"file"
]
if
err
!=
nil
{
if
len
(
fileHeaders
)
==
0
{
return
nil
,
errors
.
New
(
"file is required"
)
return
nil
,
errors
.
New
(
"file is required"
)
}
}
// 使用 formData 中的第一个文件
fileHeader
:=
fileHeaders
[
0
]
logger
.
LogDebug
(
c
.
Request
.
Context
(),
fmt
.
Sprintf
(
"--form 'file=@
\"
%s
\"
' (size: %d bytes, content-type: %s)"
,
fileHeader
.
Filename
,
fileHeader
.
Size
,
fileHeader
.
Header
.
Get
(
"Content-Type"
)))
file
,
err
:=
fileHeader
.
Open
()
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"error opening audio file: %v"
,
err
)
}
defer
file
.
Close
()
defer
file
.
Close
()
part
,
err
:=
writer
.
CreateFormFile
(
"file"
,
h
eader
.
Filename
)
part
,
err
:=
writer
.
CreateFormFile
(
"file"
,
fileH
eader
.
Filename
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"create form file failed"
)
return
nil
,
errors
.
New
(
"create form file failed"
)
}
}
...
@@ -383,6 +401,7 @@ func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInf
...
@@ -383,6 +401,7 @@ func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInf
// 关闭 multipart 编写器以设置分界线
// 关闭 multipart 编写器以设置分界线
writer
.
Close
()
writer
.
Close
()
c
.
Request
.
Header
.
Set
(
"Content-Type"
,
writer
.
FormDataContentType
())
c
.
Request
.
Header
.
Set
(
"Content-Type"
,
writer
.
FormDataContentType
())
logger
.
LogDebug
(
c
.
Request
.
Context
(),
fmt
.
Sprintf
(
"--header 'Content-Type: %s'"
,
writer
.
FormDataContentType
()))
return
&
requestBody
,
nil
return
&
requestBody
,
nil
}
}
}
}
...
...
relay/channel/siliconflow/adaptor.go
View file @
5d127557
...
@@ -31,8 +31,8 @@ func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayIn
...
@@ -31,8 +31,8 @@ func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayIn
}
}
func
(
a
*
Adaptor
)
ConvertAudioRequest
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
,
request
dto
.
AudioRequest
)
(
io
.
Reader
,
error
)
{
func
(
a
*
Adaptor
)
ConvertAudioRequest
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
,
request
dto
.
AudioRequest
)
(
io
.
Reader
,
error
)
{
//TODO implement me
adaptor
:=
openai
.
Adaptor
{}
return
nil
,
errors
.
New
(
"not supported"
)
return
adaptor
.
ConvertAudioRequest
(
c
,
info
,
request
)
}
}
func
(
a
*
Adaptor
)
ConvertImageRequest
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
,
request
dto
.
ImageRequest
)
(
any
,
error
)
{
func
(
a
*
Adaptor
)
ConvertImageRequest
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
,
request
dto
.
ImageRequest
)
(
any
,
error
)
{
...
@@ -65,16 +65,8 @@ func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
...
@@ -65,16 +65,8 @@ func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
func
(
a
*
Adaptor
)
GetRequestURL
(
info
*
relaycommon
.
RelayInfo
)
(
string
,
error
)
{
func
(
a
*
Adaptor
)
GetRequestURL
(
info
*
relaycommon
.
RelayInfo
)
(
string
,
error
)
{
if
info
.
RelayMode
==
constant
.
RelayModeRerank
{
if
info
.
RelayMode
==
constant
.
RelayModeRerank
{
return
fmt
.
Sprintf
(
"%s/v1/rerank"
,
info
.
ChannelBaseUrl
),
nil
return
fmt
.
Sprintf
(
"%s/v1/rerank"
,
info
.
ChannelBaseUrl
),
nil
}
else
if
info
.
RelayMode
==
constant
.
RelayModeEmbeddings
{
return
fmt
.
Sprintf
(
"%s/v1/embeddings"
,
info
.
ChannelBaseUrl
),
nil
}
else
if
info
.
RelayMode
==
constant
.
RelayModeChatCompletions
{
return
fmt
.
Sprintf
(
"%s/v1/chat/completions"
,
info
.
ChannelBaseUrl
),
nil
}
else
if
info
.
RelayMode
==
constant
.
RelayModeCompletions
{
return
fmt
.
Sprintf
(
"%s/v1/completions"
,
info
.
ChannelBaseUrl
),
nil
}
else
if
info
.
RelayMode
==
constant
.
RelayModeImagesGenerations
{
return
fmt
.
Sprintf
(
"%s/v1/images/generations"
,
info
.
ChannelBaseUrl
),
nil
}
}
return
fmt
.
Sprintf
(
"%s/v1/chat/completions"
,
info
.
ChannelBaseUrl
),
nil
return
relaycommon
.
GetFullRequestURL
(
info
.
ChannelBaseUrl
,
info
.
RequestURLPath
,
info
.
ChannelType
),
nil
}
}
func
(
a
*
Adaptor
)
SetupRequestHeader
(
c
*
gin
.
Context
,
req
*
http
.
Header
,
info
*
relaycommon
.
RelayInfo
)
error
{
func
(
a
*
Adaptor
)
SetupRequestHeader
(
c
*
gin
.
Context
,
req
*
http
.
Header
,
info
*
relaycommon
.
RelayInfo
)
error
{
...
@@ -103,7 +95,8 @@ func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommo
...
@@ -103,7 +95,8 @@ func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommo
}
}
func
(
a
*
Adaptor
)
DoRequest
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
,
requestBody
io
.
Reader
)
(
any
,
error
)
{
func
(
a
*
Adaptor
)
DoRequest
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
,
requestBody
io
.
Reader
)
(
any
,
error
)
{
return
channel
.
DoApiRequest
(
a
,
c
,
info
,
requestBody
)
adaptor
:=
openai
.
Adaptor
{}
return
adaptor
.
DoRequest
(
c
,
info
,
requestBody
)
}
}
func
(
a
*
Adaptor
)
ConvertRerankRequest
(
c
*
gin
.
Context
,
relayMode
int
,
request
dto
.
RerankRequest
)
(
any
,
error
)
{
func
(
a
*
Adaptor
)
ConvertRerankRequest
(
c
*
gin
.
Context
,
relayMode
int
,
request
dto
.
RerankRequest
)
(
any
,
error
)
{
...
@@ -118,21 +111,9 @@ func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycom
...
@@ -118,21 +111,9 @@ func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycom
switch
info
.
RelayMode
{
switch
info
.
RelayMode
{
case
constant
.
RelayModeRerank
:
case
constant
.
RelayModeRerank
:
usage
,
err
=
siliconflowRerankHandler
(
c
,
info
,
resp
)
usage
,
err
=
siliconflowRerankHandler
(
c
,
info
,
resp
)
case
constant
.
RelayModeEmbeddings
:
usage
,
err
=
openai
.
OpenaiHandler
(
c
,
info
,
resp
)
case
constant
.
RelayModeCompletions
:
fallthrough
case
constant
.
RelayModeChatCompletions
:
fallthrough
case
constant
.
RelayModeImagesGenerations
:
fallthrough
default
:
default
:
if
info
.
IsStream
{
adaptor
:=
openai
.
Adaptor
{}
usage
,
err
=
openai
.
OaiStreamHandler
(
c
,
info
,
resp
)
usage
,
err
=
adaptor
.
DoResponse
(
c
,
resp
,
info
)
}
else
{
usage
,
err
=
openai
.
OpenaiHandler
(
c
,
info
,
resp
)
}
}
}
return
return
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment