mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-17 09:16:36 +08:00
90 lines
2.8 KiB
Go
90 lines
2.8 KiB
Go
package baidu
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/songquanpeng/one-api/relay/channel"
|
|
"github.com/songquanpeng/one-api/relay/constant"
|
|
"github.com/songquanpeng/one-api/relay/model"
|
|
"github.com/songquanpeng/one-api/relay/util"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
type Adaptor struct {
|
|
}
|
|
|
|
func (a *Adaptor) GetRequestURL(meta *util.RelayMeta) (string, error) {
|
|
// https://cloud.baidu.com/doc/WENXINWORKSHOP/s/clntwmv7t
|
|
var fullRequestURL string
|
|
switch meta.ActualModelName {
|
|
case "ERNIE-Bot-4":
|
|
fullRequestURL = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro"
|
|
case "ERNIE-Bot-8K":
|
|
fullRequestURL = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie_bot_8k"
|
|
case "ERNIE-Bot":
|
|
fullRequestURL = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions"
|
|
case "ERNIE-Speed":
|
|
fullRequestURL = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie_speed"
|
|
case "ERNIE-Bot-turbo":
|
|
fullRequestURL = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant"
|
|
case "BLOOMZ-7B":
|
|
fullRequestURL = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/bloomz_7b1"
|
|
case "Embedding-V1":
|
|
fullRequestURL = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/embeddings/embedding-v1"
|
|
}
|
|
var accessToken string
|
|
var err error
|
|
if accessToken, err = GetAccessToken(meta.APIKey); err != nil {
|
|
return "", err
|
|
}
|
|
fullRequestURL += "?access_token=" + accessToken
|
|
return fullRequestURL, nil
|
|
}
|
|
|
|
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *util.RelayMeta) error {
|
|
channel.SetupCommonRequestHeader(c, req, meta)
|
|
req.Header.Set("Authorization", "Bearer "+meta.APIKey)
|
|
return nil
|
|
}
|
|
|
|
func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.GeneralOpenAIRequest) (any, error) {
|
|
if request == nil {
|
|
return nil, errors.New("request is nil")
|
|
}
|
|
switch relayMode {
|
|
case constant.RelayModeEmbeddings:
|
|
baiduEmbeddingRequest := ConvertEmbeddingRequest(*request)
|
|
return baiduEmbeddingRequest, nil
|
|
default:
|
|
baiduRequest := ConvertRequest(*request)
|
|
return baiduRequest, nil
|
|
}
|
|
}
|
|
|
|
func (a *Adaptor) DoRequest(c *gin.Context, meta *util.RelayMeta, requestBody io.Reader) (*http.Response, error) {
|
|
return channel.DoRequestHelper(a, c, meta, requestBody)
|
|
}
|
|
|
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *util.RelayMeta) (usage *model.Usage, err *model.ErrorWithStatusCode) {
|
|
if meta.IsStream {
|
|
err, usage = StreamHandler(c, resp)
|
|
} else {
|
|
switch meta.Mode {
|
|
case constant.RelayModeEmbeddings:
|
|
err, usage = EmbeddingHandler(c, resp)
|
|
default:
|
|
err, usage = Handler(c, resp)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (a *Adaptor) GetModelList() []string {
|
|
return ModelList
|
|
}
|
|
|
|
func (a *Adaptor) GetChannelName() string {
|
|
return "baidu"
|
|
}
|