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
9ba6abed
authored
Apr 07, 2024
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: select channel
parent
107bd8d6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
73 additions
and
68 deletions
+73
-68
middleware/distributor.go
+73
-68
No files found.
middleware/distributor.go
View file @
9ba6abed
...
...
@@ -24,6 +24,9 @@ func Distribute() func(c *gin.Context) {
userId
:=
c
.
GetInt
(
"id"
)
var
channel
*
model
.
Channel
channelId
,
ok
:=
c
.
Get
(
"specific_channel_id"
)
modelRequest
,
shouldSelectChannel
,
err
:=
getModelRequest
(
c
)
userGroup
,
_
:=
model
.
CacheGetUserGroup
(
userId
)
c
.
Set
(
"group"
,
userGroup
)
if
ok
{
id
,
err
:=
strconv
.
Atoi
(
channelId
.
(
string
))
if
err
!=
nil
{
...
...
@@ -40,72 +43,7 @@ func Distribute() func(c *gin.Context) {
return
}
}
else
{
shouldSelectChannel
:=
true
// Select a channel for the user
var
modelRequest
ModelRequest
var
err
error
if
strings
.
Contains
(
c
.
Request
.
URL
.
Path
,
"/mj/"
)
{
relayMode
:=
relayconstant
.
Path2RelayModeMidjourney
(
c
.
Request
.
URL
.
Path
)
if
relayMode
==
relayconstant
.
RelayModeMidjourneyTaskFetch
||
relayMode
==
relayconstant
.
RelayModeMidjourneyTaskFetchByCondition
||
relayMode
==
relayconstant
.
RelayModeMidjourneyNotify
||
relayMode
==
relayconstant
.
RelayModeMidjourneyTaskImageSeed
{
shouldSelectChannel
=
false
}
else
{
midjourneyRequest
:=
dto
.
MidjourneyRequest
{}
err
=
common
.
UnmarshalBodyReusable
(
c
,
&
midjourneyRequest
)
if
err
!=
nil
{
abortWithMidjourneyMessage
(
c
,
http
.
StatusBadRequest
,
constant
.
MjErrorUnknown
,
"无效的请求, "
+
err
.
Error
())
return
}
midjourneyModel
,
mjErr
,
success
:=
service
.
GetMjRequestModel
(
relayMode
,
&
midjourneyRequest
)
if
mjErr
!=
nil
{
abortWithMidjourneyMessage
(
c
,
http
.
StatusBadRequest
,
mjErr
.
Code
,
mjErr
.
Description
)
return
}
if
midjourneyModel
==
""
{
if
!
success
{
abortWithMidjourneyMessage
(
c
,
http
.
StatusBadRequest
,
constant
.
MjErrorUnknown
,
"无效的请求, 无法解析模型"
)
return
}
else
{
// task fetch, task fetch by condition, notify
shouldSelectChannel
=
false
}
}
modelRequest
.
Model
=
midjourneyModel
}
c
.
Set
(
"relay_mode"
,
relayMode
)
}
else
if
!
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/audio/transcriptions"
)
{
err
=
common
.
UnmarshalBodyReusable
(
c
,
&
modelRequest
)
}
if
err
!=
nil
{
abortWithOpenAiMessage
(
c
,
http
.
StatusBadRequest
,
"无效的请求, "
+
err
.
Error
())
return
}
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/moderations"
)
{
if
modelRequest
.
Model
==
""
{
modelRequest
.
Model
=
"text-moderation-stable"
}
}
if
strings
.
HasSuffix
(
c
.
Request
.
URL
.
Path
,
"embeddings"
)
{
if
modelRequest
.
Model
==
""
{
modelRequest
.
Model
=
c
.
Param
(
"model"
)
}
}
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/images/generations"
)
{
if
modelRequest
.
Model
==
""
{
modelRequest
.
Model
=
"dall-e"
}
}
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/audio"
)
{
if
modelRequest
.
Model
==
""
{
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/audio/speech"
)
{
modelRequest
.
Model
=
"tts-1"
}
else
{
modelRequest
.
Model
=
"whisper-1"
}
}
}
// check token model mapping
modelLimitEnable
:=
c
.
GetBool
(
"token_model_limit_enabled"
)
if
modelLimitEnable
{
...
...
@@ -128,8 +66,6 @@ func Distribute() func(c *gin.Context) {
}
}
userGroup
,
_
:=
model
.
CacheGetUserGroup
(
userId
)
c
.
Set
(
"group"
,
userGroup
)
if
shouldSelectChannel
{
channel
,
err
=
model
.
CacheGetRandomSatisfiedChannel
(
userGroup
,
modelRequest
.
Model
,
0
)
if
err
!=
nil
{
...
...
@@ -147,13 +83,82 @@ func Distribute() func(c *gin.Context) {
abortWithOpenAiMessage
(
c
,
http
.
StatusServiceUnavailable
,
fmt
.
Sprintf
(
"当前分组 %s 下对于模型 %s 无可用渠道(数据库一致性已被破坏)"
,
userGroup
,
modelRequest
.
Model
))
return
}
SetupContextForSelectedChannel
(
c
,
channel
,
modelRequest
.
Model
)
}
}
SetupContextForSelectedChannel
(
c
,
channel
,
modelRequest
.
Model
)
c
.
Next
()
}
}
func
getModelRequest
(
c
*
gin
.
Context
)
(
*
ModelRequest
,
bool
,
error
)
{
var
modelRequest
ModelRequest
shouldSelectChannel
:=
true
var
err
error
if
strings
.
Contains
(
c
.
Request
.
URL
.
Path
,
"/mj/"
)
{
relayMode
:=
relayconstant
.
Path2RelayModeMidjourney
(
c
.
Request
.
URL
.
Path
)
if
relayMode
==
relayconstant
.
RelayModeMidjourneyTaskFetch
||
relayMode
==
relayconstant
.
RelayModeMidjourneyTaskFetchByCondition
||
relayMode
==
relayconstant
.
RelayModeMidjourneyNotify
||
relayMode
==
relayconstant
.
RelayModeMidjourneyTaskImageSeed
{
shouldSelectChannel
=
false
}
else
{
midjourneyRequest
:=
dto
.
MidjourneyRequest
{}
err
=
common
.
UnmarshalBodyReusable
(
c
,
&
midjourneyRequest
)
if
err
!=
nil
{
abortWithMidjourneyMessage
(
c
,
http
.
StatusBadRequest
,
constant
.
MjErrorUnknown
,
"无效的请求, "
+
err
.
Error
())
return
nil
,
false
,
err
}
midjourneyModel
,
mjErr
,
success
:=
service
.
GetMjRequestModel
(
relayMode
,
&
midjourneyRequest
)
if
mjErr
!=
nil
{
abortWithMidjourneyMessage
(
c
,
http
.
StatusBadRequest
,
mjErr
.
Code
,
mjErr
.
Description
)
return
nil
,
false
,
fmt
.
Errorf
(
mjErr
.
Description
)
}
if
midjourneyModel
==
""
{
if
!
success
{
abortWithMidjourneyMessage
(
c
,
http
.
StatusBadRequest
,
constant
.
MjErrorUnknown
,
"无效的请求, 无法解析模型"
)
return
nil
,
false
,
fmt
.
Errorf
(
"无效的请求, 无法解析模型"
)
}
else
{
// task fetch, task fetch by condition, notify
shouldSelectChannel
=
false
}
}
modelRequest
.
Model
=
midjourneyModel
}
c
.
Set
(
"relay_mode"
,
relayMode
)
}
else
if
!
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/audio/transcriptions"
)
{
err
=
common
.
UnmarshalBodyReusable
(
c
,
&
modelRequest
)
}
if
err
!=
nil
{
abortWithOpenAiMessage
(
c
,
http
.
StatusBadRequest
,
"无效的请求, "
+
err
.
Error
())
return
nil
,
false
,
err
}
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/moderations"
)
{
if
modelRequest
.
Model
==
""
{
modelRequest
.
Model
=
"text-moderation-stable"
}
}
if
strings
.
HasSuffix
(
c
.
Request
.
URL
.
Path
,
"embeddings"
)
{
if
modelRequest
.
Model
==
""
{
modelRequest
.
Model
=
c
.
Param
(
"model"
)
}
}
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/images/generations"
)
{
if
modelRequest
.
Model
==
""
{
modelRequest
.
Model
=
"dall-e"
}
}
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/audio"
)
{
if
modelRequest
.
Model
==
""
{
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/audio/speech"
)
{
modelRequest
.
Model
=
"tts-1"
}
else
{
modelRequest
.
Model
=
"whisper-1"
}
}
}
return
&
modelRequest
,
shouldSelectChannel
,
nil
}
func
SetupContextForSelectedChannel
(
c
*
gin
.
Context
,
channel
*
model
.
Channel
,
modelName
string
)
{
c
.
Set
(
"channel"
,
channel
.
Type
)
c
.
Set
(
"channel_id"
,
channel
.
Id
)
...
...
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