2024-01-21 15:21:42 +00:00
|
|
|
package xunfei
|
|
|
|
|
|
|
|
import (
|
2024-02-17 16:15:31 +00:00
|
|
|
"errors"
|
2024-01-21 15:21:42 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2024-04-05 17:36:48 +00:00
|
|
|
"github.com/songquanpeng/one-api/relay/adaptor"
|
|
|
|
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
2024-04-05 17:31:44 +00:00
|
|
|
"github.com/songquanpeng/one-api/relay/meta"
|
2024-02-17 16:15:31 +00:00
|
|
|
"github.com/songquanpeng/one-api/relay/model"
|
|
|
|
"io"
|
2024-01-21 15:21:42 +00:00
|
|
|
"net/http"
|
2024-02-17 16:15:31 +00:00
|
|
|
"strings"
|
2024-01-21 15:21:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Adaptor struct {
|
2024-02-17 16:15:31 +00:00
|
|
|
request *model.GeneralOpenAIRequest
|
2024-04-26 15:05:48 +00:00
|
|
|
meta *meta.Meta
|
2024-01-21 15:21:42 +00:00
|
|
|
}
|
|
|
|
|
2024-04-05 17:31:44 +00:00
|
|
|
func (a *Adaptor) Init(meta *meta.Meta) {
|
2024-04-26 15:05:48 +00:00
|
|
|
a.meta = meta
|
2024-02-18 08:17:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-05 17:31:44 +00:00
|
|
|
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
2024-02-17 16:15:31 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2024-04-05 17:31:44 +00:00
|
|
|
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) error {
|
2024-04-05 17:36:48 +00:00
|
|
|
adaptor.SetupCommonRequestHeader(c, req, meta)
|
2024-02-17 16:15:31 +00:00
|
|
|
// check DoResponse for auth part
|
2024-01-21 15:21:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-17 16:15:31 +00:00
|
|
|
func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.GeneralOpenAIRequest) (any, error) {
|
|
|
|
if request == nil {
|
|
|
|
return nil, errors.New("request is nil")
|
|
|
|
}
|
|
|
|
a.request = request
|
2024-01-21 15:21:42 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-04-05 10:09:54 +00:00
|
|
|
func (a *Adaptor) ConvertImageRequest(request *model.ImageRequest) (any, error) {
|
|
|
|
if request == nil {
|
|
|
|
return nil, errors.New("request is nil")
|
|
|
|
}
|
|
|
|
return request, nil
|
|
|
|
}
|
|
|
|
|
2024-04-05 17:31:44 +00:00
|
|
|
func (a *Adaptor) DoRequest(c *gin.Context, meta *meta.Meta, requestBody io.Reader) (*http.Response, error) {
|
2024-02-17 16:15:31 +00:00
|
|
|
// xunfei's request is not http request, so we don't need to do anything here
|
|
|
|
dummyResp := &http.Response{}
|
|
|
|
dummyResp.StatusCode = http.StatusOK
|
|
|
|
return dummyResp, nil
|
|
|
|
}
|
|
|
|
|
2024-04-05 17:31:44 +00:00
|
|
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *meta.Meta) (usage *model.Usage, err *model.ErrorWithStatusCode) {
|
2024-02-17 16:15:31 +00:00
|
|
|
splits := strings.Split(meta.APIKey, "|")
|
|
|
|
if len(splits) != 3 {
|
|
|
|
return nil, openai.ErrorWrapper(errors.New("invalid auth"), "invalid_auth", http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
if a.request == nil {
|
|
|
|
return nil, openai.ErrorWrapper(errors.New("request is nil"), "request_is_nil", http.StatusBadRequest)
|
|
|
|
}
|
2024-06-12 16:07:26 +00:00
|
|
|
version := parseAPIVersionByModelName(meta.ActualModelName)
|
|
|
|
if version == "" {
|
|
|
|
version = a.meta.Config.APIVersion
|
|
|
|
}
|
|
|
|
if version == "" {
|
|
|
|
version = "v1.1"
|
|
|
|
}
|
|
|
|
a.meta.Config.APIVersion = version
|
2024-02-17 16:15:31 +00:00
|
|
|
if meta.IsStream {
|
2024-04-26 15:05:48 +00:00
|
|
|
err, usage = StreamHandler(c, meta, *a.request, splits[0], splits[1], splits[2])
|
2024-02-17 16:15:31 +00:00
|
|
|
} else {
|
2024-04-26 15:05:48 +00:00
|
|
|
err, usage = Handler(c, meta, *a.request, splits[0], splits[1], splits[2])
|
2024-02-17 16:15:31 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) GetModelList() []string {
|
|
|
|
return ModelList
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) GetChannelName() string {
|
|
|
|
return "xunfei"
|
2024-01-21 15:21:42 +00:00
|
|
|
}
|