mirror of
https://github.com/linux-do/new-api.git
synced 2025-09-17 07:56:38 +08:00
93 lines
2.8 KiB
Go
93 lines
2.8 KiB
Go
package baidu
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/gin-gonic/gin"
|
|
"io"
|
|
"net/http"
|
|
"one-api/dto"
|
|
"one-api/relay/channel"
|
|
relaycommon "one-api/relay/common"
|
|
"one-api/relay/constant"
|
|
)
|
|
|
|
type Adaptor struct {
|
|
}
|
|
|
|
func (a *Adaptor) Init(info *relaycommon.RelayInfo, request dto.GeneralOpenAIRequest) {
|
|
|
|
}
|
|
|
|
func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
|
|
var fullRequestURL string
|
|
switch info.UpstreamModelName {
|
|
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 = getBaiduAccessToken(info.ApiKey); err != nil {
|
|
return "", err
|
|
}
|
|
fullRequestURL += "?access_token=" + accessToken
|
|
return fullRequestURL, nil
|
|
}
|
|
|
|
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, info *relaycommon.RelayInfo) error {
|
|
channel.SetupApiRequestHeader(info, c, req)
|
|
req.Header.Set("Authorization", "Bearer "+info.ApiKey)
|
|
return nil
|
|
}
|
|
|
|
func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *dto.GeneralOpenAIRequest) (any, error) {
|
|
if request == nil {
|
|
return nil, errors.New("request is nil")
|
|
}
|
|
switch relayMode {
|
|
case constant.RelayModeEmbeddings:
|
|
baiduEmbeddingRequest := embeddingRequestOpenAI2Baidu(*request)
|
|
return baiduEmbeddingRequest, nil
|
|
default:
|
|
baiduRequest := requestOpenAI2Baidu(*request)
|
|
return baiduRequest, nil
|
|
}
|
|
}
|
|
|
|
func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (*http.Response, error) {
|
|
return channel.DoApiRequest(a, c, info, requestBody)
|
|
}
|
|
|
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage *dto.Usage, err *dto.OpenAIErrorWithStatusCode, sensitiveResp *dto.SensitiveResponse) {
|
|
if info.IsStream {
|
|
err, usage = baiduStreamHandler(c, resp)
|
|
} else {
|
|
switch info.RelayMode {
|
|
case constant.RelayModeEmbeddings:
|
|
err, usage = baiduEmbeddingHandler(c, resp)
|
|
default:
|
|
err, usage = baiduHandler(c, resp)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (a *Adaptor) GetModelList() []string {
|
|
return ModelList
|
|
}
|
|
|
|
func (a *Adaptor) GetChannelName() string {
|
|
return ChannelName
|
|
}
|