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-02-17 16:15:31 +00:00
|
|
|
"github.com/songquanpeng/one-api/relay/channel"
|
2024-01-28 11:38:58 +00:00
|
|
|
"github.com/songquanpeng/one-api/relay/channel/openai"
|
2024-02-17 16:15:31 +00:00
|
|
|
"github.com/songquanpeng/one-api/relay/model"
|
|
|
|
"github.com/songquanpeng/one-api/relay/util"
|
|
|
|
"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-01-21 15:21:42 +00:00
|
|
|
}
|
|
|
|
|
2024-02-17 16:15:31 +00:00
|
|
|
func (a *Adaptor) GetRequestURL(meta *util.RelayMeta) (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *util.RelayMeta) error {
|
|
|
|
channel.SetupCommonRequestHeader(c, req, meta)
|
|
|
|
// 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-02-17 16:15:31 +00:00
|
|
|
func (a *Adaptor) DoRequest(c *gin.Context, meta *util.RelayMeta, requestBody io.Reader) (*http.Response, error) {
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *util.RelayMeta) (usage *model.Usage, err *model.ErrorWithStatusCode) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if meta.IsStream {
|
|
|
|
err, usage = StreamHandler(c, *a.request, splits[0], splits[1], splits[2])
|
|
|
|
} else {
|
|
|
|
err, usage = Handler(c, *a.request, splits[0], splits[1], splits[2])
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) GetModelList() []string {
|
|
|
|
return ModelList
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) GetChannelName() string {
|
|
|
|
return "xunfei"
|
2024-01-21 15:21:42 +00:00
|
|
|
}
|