| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- 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"
- "strings"
- )
- 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 "ERNIE-4.0-8K":
- fullRequestURL = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro"
- case "ERNIE-3.5-8K":
- fullRequestURL = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions"
- case "ERNIE-Speed-8K":
- fullRequestURL = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie_speed"
- case "ERNIE-Character-8K":
- fullRequestURL = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-char-8k"
- case "ERNIE-Functions-8K":
- fullRequestURL = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-func-8k"
- case "ERNIE-Lite-8K-0922":
- fullRequestURL = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant"
- case "Yi-34B-Chat":
- fullRequestURL = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/yi_34b_chat"
- case "Embedding-V1":
- fullRequestURL = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/embeddings/embedding-v1"
- default:
- fullRequestURL = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/" + strings.ToLower(info.UpstreamModelName)
- }
- 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) {
- 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
- }
|