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
e479f43c
authored
Jun 11, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: support /v1/moderations now (close #117)
parent
1e8e3879
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
39 additions
and
3 deletions
+39
-3
common/model-ratio.go
+2
-2
controller/model.go
+18
-0
controller/relay.go
+12
-0
middleware/distributor.go
+6
-0
router/relay-router.go
+1
-1
No files found.
common/model-ratio.go
View file @
e479f43c
...
@@ -26,8 +26,8 @@ var ModelRatio = map[string]float64{
...
@@ -26,8 +26,8 @@ var ModelRatio = map[string]float64{
"ada"
:
10
,
"ada"
:
10
,
"text-embedding-ada-002"
:
0.2
,
"text-embedding-ada-002"
:
0.2
,
"text-search-ada-doc-001"
:
10
,
"text-search-ada-doc-001"
:
10
,
"text-moderation-stable"
:
10
,
"text-moderation-stable"
:
0.1
,
"text-moderation-latest"
:
10
,
"text-moderation-latest"
:
0.1
,
}
}
func
ModelRatio2JSONString
()
string
{
func
ModelRatio2JSONString
()
string
{
...
...
controller/model.go
View file @
e479f43c
...
@@ -161,6 +161,24 @@ func init() {
...
@@ -161,6 +161,24 @@ func init() {
Root
:
"text-ada-001"
,
Root
:
"text-ada-001"
,
Parent
:
nil
,
Parent
:
nil
,
},
},
{
Id
:
"text-moderation-latest"
,
Object
:
"model"
,
Created
:
1677649963
,
OwnedBy
:
"openai"
,
Permission
:
permission
,
Root
:
"text-moderation-latest"
,
Parent
:
nil
,
},
{
Id
:
"text-moderation-stable"
,
Object
:
"model"
,
Created
:
1677649963
,
OwnedBy
:
"openai"
,
Permission
:
permission
,
Root
:
"text-moderation-stable"
,
Parent
:
nil
,
},
}
}
openAIModelsMap
=
make
(
map
[
string
]
OpenAIModels
)
openAIModelsMap
=
make
(
map
[
string
]
OpenAIModels
)
for
_
,
model
:=
range
openAIModels
{
for
_
,
model
:=
range
openAIModels
{
...
...
controller/relay.go
View file @
e479f43c
...
@@ -24,6 +24,7 @@ const (
...
@@ -24,6 +24,7 @@ const (
RelayModeChatCompletions
RelayModeChatCompletions
RelayModeCompletions
RelayModeCompletions
RelayModeEmbeddings
RelayModeEmbeddings
RelayModeModeration
)
)
// https://platform.openai.com/docs/api-reference/chat
// https://platform.openai.com/docs/api-reference/chat
...
@@ -37,6 +38,7 @@ type GeneralOpenAIRequest struct {
...
@@ -37,6 +38,7 @@ type GeneralOpenAIRequest struct {
Temperature
float64
`json:"temperature"`
Temperature
float64
`json:"temperature"`
TopP
float64
`json:"top_p"`
TopP
float64
`json:"top_p"`
N
int
`json:"n"`
N
int
`json:"n"`
Input
string
`json:"input"`
}
}
type
ChatRequest
struct
{
type
ChatRequest
struct
{
...
@@ -100,6 +102,8 @@ func Relay(c *gin.Context) {
...
@@ -100,6 +102,8 @@ func Relay(c *gin.Context) {
relayMode
=
RelayModeCompletions
relayMode
=
RelayModeCompletions
}
else
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/embeddings"
)
{
}
else
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/embeddings"
)
{
relayMode
=
RelayModeEmbeddings
relayMode
=
RelayModeEmbeddings
}
else
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/moderations"
)
{
relayMode
=
RelayModeModeration
}
}
err
:=
relayHelper
(
c
,
relayMode
)
err
:=
relayHelper
(
c
,
relayMode
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -143,6 +147,9 @@ func relayHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
...
@@ -143,6 +147,9 @@ func relayHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
return
errorWrapper
(
err
,
"bind_request_body_failed"
,
http
.
StatusBadRequest
)
return
errorWrapper
(
err
,
"bind_request_body_failed"
,
http
.
StatusBadRequest
)
}
}
}
}
if
relayMode
==
RelayModeModeration
&&
textRequest
.
Model
==
""
{
textRequest
.
Model
=
"text-moderation-latest"
}
baseURL
:=
common
.
ChannelBaseURLs
[
channelType
]
baseURL
:=
common
.
ChannelBaseURLs
[
channelType
]
requestURL
:=
c
.
Request
.
URL
.
String
()
requestURL
:=
c
.
Request
.
URL
.
String
()
if
channelType
==
common
.
ChannelTypeCustom
{
if
channelType
==
common
.
ChannelTypeCustom
{
...
@@ -180,6 +187,8 @@ func relayHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
...
@@ -180,6 +187,8 @@ func relayHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
promptTokens
=
countTokenMessages
(
textRequest
.
Messages
,
textRequest
.
Model
)
promptTokens
=
countTokenMessages
(
textRequest
.
Messages
,
textRequest
.
Model
)
case
RelayModeCompletions
:
case
RelayModeCompletions
:
promptTokens
=
countTokenText
(
textRequest
.
Prompt
,
textRequest
.
Model
)
promptTokens
=
countTokenText
(
textRequest
.
Prompt
,
textRequest
.
Model
)
case
RelayModeModeration
:
promptTokens
=
countTokenText
(
textRequest
.
Input
,
textRequest
.
Model
)
}
}
preConsumedTokens
:=
common
.
PreConsumedQuota
preConsumedTokens
:=
common
.
PreConsumedQuota
if
textRequest
.
MaxTokens
!=
0
{
if
textRequest
.
MaxTokens
!=
0
{
...
@@ -239,6 +248,9 @@ func relayHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
...
@@ -239,6 +248,9 @@ func relayHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
quota
=
textResponse
.
Usage
.
PromptTokens
+
textResponse
.
Usage
.
CompletionTokens
*
completionRatio
quota
=
textResponse
.
Usage
.
PromptTokens
+
textResponse
.
Usage
.
CompletionTokens
*
completionRatio
}
}
quota
=
int
(
float64
(
quota
)
*
ratio
)
quota
=
int
(
float64
(
quota
)
*
ratio
)
if
ratio
!=
0
&&
quota
<=
0
{
quota
=
1
}
quotaDelta
:=
quota
-
preConsumedQuota
quotaDelta
:=
quota
-
preConsumedQuota
err
:=
model
.
PostConsumeTokenQuota
(
tokenId
,
quotaDelta
)
err
:=
model
.
PostConsumeTokenQuota
(
tokenId
,
quotaDelta
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
middleware/distributor.go
View file @
e479f43c
...
@@ -7,6 +7,7 @@ import (
...
@@ -7,6 +7,7 @@ import (
"one-api/common"
"one-api/common"
"one-api/model"
"one-api/model"
"strconv"
"strconv"
"strings"
)
)
type
ModelRequest
struct
{
type
ModelRequest
struct
{
...
@@ -64,6 +65,11 @@ func Distribute() func(c *gin.Context) {
...
@@ -64,6 +65,11 @@ func Distribute() func(c *gin.Context) {
c
.
Abort
()
c
.
Abort
()
return
return
}
}
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/moderations"
)
{
if
modelRequest
.
Model
==
""
{
modelRequest
.
Model
=
"text-moderation-stable"
}
}
userId
:=
c
.
GetInt
(
"id"
)
userId
:=
c
.
GetInt
(
"id"
)
userGroup
,
_
:=
model
.
GetUserGroup
(
userId
)
userGroup
,
_
:=
model
.
GetUserGroup
(
userId
)
channel
,
err
=
model
.
GetRandomSatisfiedChannel
(
userGroup
,
modelRequest
.
Model
)
channel
,
err
=
model
.
GetRandomSatisfiedChannel
(
userGroup
,
modelRequest
.
Model
)
...
...
router/relay-router.go
View file @
e479f43c
...
@@ -37,6 +37,6 @@ func SetRelayRouter(router *gin.Engine) {
...
@@ -37,6 +37,6 @@ func SetRelayRouter(router *gin.Engine) {
relayV1Router
.
POST
(
"/fine-tunes/:id/cancel"
,
controller
.
RelayNotImplemented
)
relayV1Router
.
POST
(
"/fine-tunes/:id/cancel"
,
controller
.
RelayNotImplemented
)
relayV1Router
.
GET
(
"/fine-tunes/:id/events"
,
controller
.
RelayNotImplemented
)
relayV1Router
.
GET
(
"/fine-tunes/:id/events"
,
controller
.
RelayNotImplemented
)
relayV1Router
.
DELETE
(
"/models/:model"
,
controller
.
RelayNotImplemented
)
relayV1Router
.
DELETE
(
"/models/:model"
,
controller
.
RelayNotImplemented
)
relayV1Router
.
POST
(
"/moderations"
,
controller
.
Relay
NotImplemented
)
relayV1Router
.
POST
(
"/moderations"
,
controller
.
Relay
)
}
}
}
}
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