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
44a78a75
authored
Jun 25, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: support /v1/edits now (close #196)
parent
a75364d2
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
41 additions
and
12 deletions
+41
-12
controller/model.go
+18
-0
controller/relay-text.go
+16
-9
controller/relay.go
+6
-2
router/relay-router.go
+1
-1
No files found.
controller/model.go
View file @
44a78a75
...
@@ -224,6 +224,24 @@ func init() {
...
@@ -224,6 +224,24 @@ func init() {
Root
:
"text-moderation-stable"
,
Root
:
"text-moderation-stable"
,
Parent
:
nil
,
Parent
:
nil
,
},
},
{
Id
:
"text-davinci-edit-001"
,
Object
:
"model"
,
Created
:
1677649963
,
OwnedBy
:
"openai"
,
Permission
:
permission
,
Root
:
"text-davinci-edit-001"
,
Parent
:
nil
,
},
{
Id
:
"code-davinci-edit-001"
,
Object
:
"model"
,
Created
:
1677649963
,
OwnedBy
:
"openai"
,
Permission
:
permission
,
Root
:
"code-davinci-edit-001"
,
Parent
:
nil
,
},
}
}
openAIModelsMap
=
make
(
map
[
string
]
OpenAIModels
)
openAIModelsMap
=
make
(
map
[
string
]
OpenAIModels
)
for
_
,
model
:=
range
openAIModels
{
for
_
,
model
:=
range
openAIModels
{
...
...
controller/relay-text.go
View file @
44a78a75
...
@@ -27,7 +27,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
...
@@ -27,7 +27,7 @@ func relayTextHelper(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
==
""
{
if
relayMode
==
RelayModeModeration
s
&&
textRequest
.
Model
==
""
{
textRequest
.
Model
=
"text-moderation-latest"
textRequest
.
Model
=
"text-moderation-latest"
}
}
// request validation
// request validation
...
@@ -37,16 +37,20 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
...
@@ -37,16 +37,20 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
switch
relayMode
{
switch
relayMode
{
case
RelayModeCompletions
:
case
RelayModeCompletions
:
if
textRequest
.
Prompt
==
""
{
if
textRequest
.
Prompt
==
""
{
return
errorWrapper
(
errors
.
New
(
"prompt is required"
),
"required_field_missing"
,
http
.
StatusBadRequest
)
return
errorWrapper
(
errors
.
New
(
"
field
prompt is required"
),
"required_field_missing"
,
http
.
StatusBadRequest
)
}
}
case
RelayModeChatCompletions
:
case
RelayModeChatCompletions
:
if
len
(
textRequest
.
Messages
)
==
0
{
if
textRequest
.
Messages
==
nil
||
len
(
textRequest
.
Messages
)
==
0
{
return
errorWrapper
(
errors
.
New
(
"messages is required"
),
"required_field_missing"
,
http
.
StatusBadRequest
)
return
errorWrapper
(
errors
.
New
(
"
field
messages is required"
),
"required_field_missing"
,
http
.
StatusBadRequest
)
}
}
case
RelayModeEmbeddings
:
case
RelayModeEmbeddings
:
case
RelayModeModeration
:
case
RelayModeModeration
s
:
if
textRequest
.
Input
==
""
{
if
textRequest
.
Input
==
""
{
return
errorWrapper
(
errors
.
New
(
"input is required"
),
"required_field_missing"
,
http
.
StatusBadRequest
)
return
errorWrapper
(
errors
.
New
(
"field input is required"
),
"required_field_missing"
,
http
.
StatusBadRequest
)
}
case
RelayModeEdits
:
if
textRequest
.
Instruction
==
""
{
return
errorWrapper
(
errors
.
New
(
"field instruction is required"
),
"required_field_missing"
,
http
.
StatusBadRequest
)
}
}
}
}
baseURL
:=
common
.
ChannelBaseURLs
[
channelType
]
baseURL
:=
common
.
ChannelBaseURLs
[
channelType
]
...
@@ -84,7 +88,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
...
@@ -84,7 +88,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
promptTokens
=
countTokenMessages
(
textRequest
.
Messages
,
textRequest
.
Model
)
promptTokens
=
countTokenMessages
(
textRequest
.
Messages
,
textRequest
.
Model
)
case
RelayModeCompletions
:
case
RelayModeCompletions
:
promptTokens
=
countTokenInput
(
textRequest
.
Prompt
,
textRequest
.
Model
)
promptTokens
=
countTokenInput
(
textRequest
.
Prompt
,
textRequest
.
Model
)
case
RelayModeModeration
:
case
RelayModeModeration
s
:
promptTokens
=
countTokenInput
(
textRequest
.
Input
,
textRequest
.
Model
)
promptTokens
=
countTokenInput
(
textRequest
.
Input
,
textRequest
.
Model
)
}
}
preConsumedTokens
:=
common
.
PreConsumedQuota
preConsumedTokens
:=
common
.
PreConsumedQuota
...
@@ -144,7 +148,10 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
...
@@ -144,7 +148,10 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
defer
func
()
{
defer
func
()
{
if
consumeQuota
{
if
consumeQuota
{
quota
:=
0
quota
:=
0
completionRatio
:=
1.333333
// default for gpt-3
completionRatio
:=
1.0
if
strings
.
HasPrefix
(
textRequest
.
Model
,
"gpt-3.5"
)
{
completionRatio
=
1.333333
}
if
strings
.
HasPrefix
(
textRequest
.
Model
,
"gpt-4"
)
{
if
strings
.
HasPrefix
(
textRequest
.
Model
,
"gpt-4"
)
{
completionRatio
=
2
completionRatio
=
2
}
}
...
@@ -172,7 +179,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
...
@@ -172,7 +179,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
}
}
if
quota
!=
0
{
if
quota
!=
0
{
tokenName
:=
c
.
GetString
(
"token_name"
)
tokenName
:=
c
.
GetString
(
"token_name"
)
logContent
:=
fmt
.
Sprintf
(
"模型倍率 %.2f,分组倍率 %.2f
"
,
modelRatio
,
group
Ratio
)
logContent
:=
fmt
.
Sprintf
(
"模型倍率 %.2f,分组倍率 %.2f
,补全倍率 %.2f"
,
modelRatio
,
groupRatio
,
completion
Ratio
)
model
.
RecordConsumeLog
(
userId
,
promptTokens
,
completionTokens
,
textRequest
.
Model
,
tokenName
,
quota
,
logContent
)
model
.
RecordConsumeLog
(
userId
,
promptTokens
,
completionTokens
,
textRequest
.
Model
,
tokenName
,
quota
,
logContent
)
model
.
UpdateUserUsedQuotaAndRequestCount
(
userId
,
quota
)
model
.
UpdateUserUsedQuotaAndRequestCount
(
userId
,
quota
)
channelId
:=
c
.
GetInt
(
"channel_id"
)
channelId
:=
c
.
GetInt
(
"channel_id"
)
...
...
controller/relay.go
View file @
44a78a75
...
@@ -19,8 +19,9 @@ const (
...
@@ -19,8 +19,9 @@ const (
RelayModeChatCompletions
RelayModeChatCompletions
RelayModeCompletions
RelayModeCompletions
RelayModeEmbeddings
RelayModeEmbeddings
RelayModeModeration
RelayModeModeration
s
RelayModeImagesGenerations
RelayModeImagesGenerations
RelayModeEdits
)
)
// https://platform.openai.com/docs/api-reference/chat
// https://platform.openai.com/docs/api-reference/chat
...
@@ -35,6 +36,7 @@ type GeneralOpenAIRequest struct {
...
@@ -35,6 +36,7 @@ type GeneralOpenAIRequest struct {
TopP
float64
`json:"top_p"`
TopP
float64
`json:"top_p"`
N
int
`json:"n"`
N
int
`json:"n"`
Input
any
`json:"input"`
Input
any
`json:"input"`
Instruction
string
`json:"instruction"`
}
}
type
ChatRequest
struct
{
type
ChatRequest
struct
{
...
@@ -99,9 +101,11 @@ func Relay(c *gin.Context) {
...
@@ -99,9 +101,11 @@ func Relay(c *gin.Context) {
}
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"
)
{
}
else
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/moderations"
)
{
relayMode
=
RelayModeModeration
relayMode
=
RelayModeModeration
s
}
else
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/images/generations"
)
{
}
else
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/images/generations"
)
{
relayMode
=
RelayModeImagesGenerations
relayMode
=
RelayModeImagesGenerations
}
else
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/edits"
)
{
relayMode
=
RelayModeEdits
}
}
var
err
*
OpenAIErrorWithStatusCode
var
err
*
OpenAIErrorWithStatusCode
switch
relayMode
{
switch
relayMode
{
...
...
router/relay-router.go
View file @
44a78a75
...
@@ -19,7 +19,7 @@ func SetRelayRouter(router *gin.Engine) {
...
@@ -19,7 +19,7 @@ func SetRelayRouter(router *gin.Engine) {
{
{
relayV1Router
.
POST
(
"/completions"
,
controller
.
Relay
)
relayV1Router
.
POST
(
"/completions"
,
controller
.
Relay
)
relayV1Router
.
POST
(
"/chat/completions"
,
controller
.
Relay
)
relayV1Router
.
POST
(
"/chat/completions"
,
controller
.
Relay
)
relayV1Router
.
POST
(
"/edits"
,
controller
.
Relay
NotImplemented
)
relayV1Router
.
POST
(
"/edits"
,
controller
.
Relay
)
relayV1Router
.
POST
(
"/images/generations"
,
controller
.
RelayNotImplemented
)
relayV1Router
.
POST
(
"/images/generations"
,
controller
.
RelayNotImplemented
)
relayV1Router
.
POST
(
"/images/edits"
,
controller
.
RelayNotImplemented
)
relayV1Router
.
POST
(
"/images/edits"
,
controller
.
RelayNotImplemented
)
relayV1Router
.
POST
(
"/images/variations"
,
controller
.
RelayNotImplemented
)
relayV1Router
.
POST
(
"/images/variations"
,
controller
.
RelayNotImplemented
)
...
...
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