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
ab437eb6
authored
Jul 15, 2023
by
玩牛牛
Committed by
GitHub
Jul 15, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: add support for /v1/engines/text-embedding-ada-002/embeddings (#224, close #222)
parent
2ff65043
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
19 additions
and
4 deletions
+19
-4
controller/relay-text.go
+5
-1
controller/relay.go
+4
-1
middleware/distributor.go
+7
-1
router/relay-router.go
+3
-1
No files found.
controller/relay-text.go
View file @
ab437eb6
...
@@ -6,12 +6,13 @@ import (
...
@@ -6,12 +6,13 @@ import (
"encoding/json"
"encoding/json"
"errors"
"errors"
"fmt"
"fmt"
"github.com/gin-gonic/gin"
"io"
"io"
"net/http"
"net/http"
"one-api/common"
"one-api/common"
"one-api/model"
"one-api/model"
"strings"
"strings"
"github.com/gin-gonic/gin"
)
)
func
relayTextHelper
(
c
*
gin
.
Context
,
relayMode
int
)
*
OpenAIErrorWithStatusCode
{
func
relayTextHelper
(
c
*
gin
.
Context
,
relayMode
int
)
*
OpenAIErrorWithStatusCode
{
...
@@ -30,6 +31,9 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
...
@@ -30,6 +31,9 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
if
relayMode
==
RelayModeModerations
&&
textRequest
.
Model
==
""
{
if
relayMode
==
RelayModeModerations
&&
textRequest
.
Model
==
""
{
textRequest
.
Model
=
"text-moderation-latest"
textRequest
.
Model
=
"text-moderation-latest"
}
}
if
relayMode
==
RelayModeEmbeddings
&&
textRequest
.
Model
==
""
{
textRequest
.
Model
=
c
.
Param
(
"model"
)
}
// request validation
// request validation
if
textRequest
.
Model
==
""
{
if
textRequest
.
Model
==
""
{
return
errorWrapper
(
errors
.
New
(
"model is required"
),
"required_field_missing"
,
http
.
StatusBadRequest
)
return
errorWrapper
(
errors
.
New
(
"model is required"
),
"required_field_missing"
,
http
.
StatusBadRequest
)
...
...
controller/relay.go
View file @
ab437eb6
...
@@ -2,10 +2,11 @@ package controller
...
@@ -2,10 +2,11 @@ package controller
import
(
import
(
"fmt"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"net/http"
"one-api/common"
"one-api/common"
"strings"
"strings"
"github.com/gin-gonic/gin"
)
)
type
Message
struct
{
type
Message
struct
{
...
@@ -100,6 +101,8 @@ func Relay(c *gin.Context) {
...
@@ -100,6 +101,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
.
HasSuffix
(
c
.
Request
.
URL
.
Path
,
"embeddings"
)
{
relayMode
=
RelayModeEmbeddings
}
else
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/moderations"
)
{
}
else
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/moderations"
)
{
relayMode
=
RelayModeModerations
relayMode
=
RelayModeModerations
}
else
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/images/generations"
)
{
}
else
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/v1/images/generations"
)
{
...
...
middleware/distributor.go
View file @
ab437eb6
...
@@ -2,12 +2,13 @@ package middleware
...
@@ -2,12 +2,13 @@ package middleware
import
(
import
(
"fmt"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"net/http"
"one-api/common"
"one-api/common"
"one-api/model"
"one-api/model"
"strconv"
"strconv"
"strings"
"strings"
"github.com/gin-gonic/gin"
)
)
type
ModelRequest
struct
{
type
ModelRequest
struct
{
...
@@ -73,6 +74,11 @@ func Distribute() func(c *gin.Context) {
...
@@ -73,6 +74,11 @@ func Distribute() func(c *gin.Context) {
modelRequest
.
Model
=
"text-moderation-stable"
modelRequest
.
Model
=
"text-moderation-stable"
}
}
}
}
if
strings
.
HasSuffix
(
c
.
Request
.
URL
.
Path
,
"embeddings"
)
{
if
modelRequest
.
Model
==
""
{
modelRequest
.
Model
=
c
.
Param
(
"model"
)
}
}
channel
,
err
=
model
.
CacheGetRandomSatisfiedChannel
(
userGroup
,
modelRequest
.
Model
)
channel
,
err
=
model
.
CacheGetRandomSatisfiedChannel
(
userGroup
,
modelRequest
.
Model
)
if
err
!=
nil
{
if
err
!=
nil
{
message
:=
"无可用渠道"
message
:=
"无可用渠道"
...
...
router/relay-router.go
View file @
ab437eb6
package
router
package
router
import
(
import
(
"github.com/gin-gonic/gin"
"one-api/controller"
"one-api/controller"
"one-api/middleware"
"one-api/middleware"
"github.com/gin-gonic/gin"
)
)
func
SetRelayRouter
(
router
*
gin
.
Engine
)
{
func
SetRelayRouter
(
router
*
gin
.
Engine
)
{
...
@@ -24,6 +25,7 @@ func SetRelayRouter(router *gin.Engine) {
...
@@ -24,6 +25,7 @@ func SetRelayRouter(router *gin.Engine) {
relayV1Router
.
POST
(
"/images/edits"
,
controller
.
RelayNotImplemented
)
relayV1Router
.
POST
(
"/images/edits"
,
controller
.
RelayNotImplemented
)
relayV1Router
.
POST
(
"/images/variations"
,
controller
.
RelayNotImplemented
)
relayV1Router
.
POST
(
"/images/variations"
,
controller
.
RelayNotImplemented
)
relayV1Router
.
POST
(
"/embeddings"
,
controller
.
Relay
)
relayV1Router
.
POST
(
"/embeddings"
,
controller
.
Relay
)
relayV1Router
.
POST
(
"/engines/:model/embeddings"
,
controller
.
Relay
)
relayV1Router
.
POST
(
"/audio/transcriptions"
,
controller
.
RelayNotImplemented
)
relayV1Router
.
POST
(
"/audio/transcriptions"
,
controller
.
RelayNotImplemented
)
relayV1Router
.
POST
(
"/audio/translations"
,
controller
.
RelayNotImplemented
)
relayV1Router
.
POST
(
"/audio/translations"
,
controller
.
RelayNotImplemented
)
relayV1Router
.
GET
(
"/files"
,
controller
.
RelayNotImplemented
)
relayV1Router
.
GET
(
"/files"
,
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