feat: support /v1/moderations now (close #117)
This commit is contained in:
parent
e398e0756b
commit
4339f45f74
@ -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 {
|
||||||
|
@ -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 {
|
||||||
|
@ -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 {
|
|||||||
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) {
|
|||||||
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 {
|
|||||||
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 {
|
|||||||
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 {
|
|||||||
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 {
|
||||||
|
@ -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) {
|
|||||||
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)
|
||||||
|
@ -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.RelayNotImplemented)
|
relayV1Router.POST("/moderations", controller.Relay)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user