Files
new-api/relay/channel/baidu/adaptor.go

110 lines
3.8 KiB
Go
Raw Normal View History

2024-02-29 01:08:18 +08:00
package baidu
import (
"errors"
"github.com/gin-gonic/gin"
"io"
"net/http"
"one-api/dto"
2024-02-29 16:21:25 +08:00
"one-api/relay/channel"
2024-02-29 01:08:18 +08:00
relaycommon "one-api/relay/common"
"one-api/relay/constant"
Squashed commit of the following: commit 5a6a0df45dee3dfbf2f65591a79fe5f2b74a49e6 Author: Akarin <jimmyshjj@gmail.com> Date: Thu May 16 14:05:28 2024 +0800 Revert "Update docker-image-amd64.yml" This reverts commit 581343a78783bbd779e65b476e125af0e2b64ce5. commit a0aec1bd030da2c6b25d9541199d598f16813a60 Merge: 5b46c7d 58abb38 Author: Jiayun Shen <jimmyshjj@gmail.com> Date: Thu May 16 06:46:51 2024 +0800 Merge branch 'main' of https://github.com/jimmyshjj/new-api commit 58abb3864a89294d82f812cda9fe49ccf7e2dd91 Merge: 7d2c026 93858c3 Author: Akarin <jimmyshjj@gmail.com> Date: Thu May 16 06:46:00 2024 +0800 Merge branch 'Calcium-Ion:main' into main commit 5b46c7dd8e6132d2be3b59c7b2ed6a4b84b93cef Author: Jiayun Shen <jimmyshjj@gmail.com> Date: Thu May 16 06:45:00 2024 +0800 Update constants.go Remove replaced Baidu models commit 7d2c02679cd90b8b53f4145f83969b980a8c2095 Author: Akarin <jimmyshjj@gmail.com> Date: Wed May 15 23:40:50 2024 +0800 Update adaptor.go - Normalize model name to lowercase Baidu's official model names may include mixed case letters, but their model APIs are case-sensitive and accept only lowercase. To ensure compatibility, the default behavior has been updated to convert model names to lowercase before constructing API requests. commit 6bc168a39d9a6194d66f2f32b175e56de9295b2e Merge: bb9fecd 910e76a Author: Jiayun Shen <jimmyshjj@gmail.com> Date: Wed May 15 21:51:52 2024 +0800 Merge branch 'main' of https://github.com/jimmyshjj/new-api commit 910e76ac94d7f5dca6254abb4d0669cbb762e724 Merge: 581343a ff044de Author: Akarin <jimmyshjj@gmail.com> Date: Wed May 15 21:51:13 2024 +0800 Merge branch 'Calcium-Ion:main' into main commit bb9fecd5bf2bd9f1859a4017e7e68f80bdb6685a Author: Jiayun Shen <jimmyshjj@gmail.com> Date: Wed May 15 21:50:08 2024 +0800 update Baidu and 360 models Add Baidu and 360 new models. Add Baidu completion ratio commit 581343a78783bbd779e65b476e125af0e2b64ce5 Author: Akarin <jimmyshjj@gmail.com> Date: Wed May 15 19:41:34 2024 +0800 Update docker-image-amd64.yml commit de17e2d95eec80f1eeae66e82dec4e9601cdee43 Merge: 046f653 a3b3e6c Author: Akarin <jimmyshjj@gmail.com> Date: Wed May 15 19:22:09 2024 +0800 Merge branch 'Calcium-Ion:main' into main commit 046f6537913ae8ad8ecf21019b64c0379331b3fd Merge: 4164d51 7b58305 Author: Akarin <jimmyshjj@gmail.com> Date: Wed May 15 15:32:38 2024 +0800 Merge branch 'Calcium-Ion:main' into main commit 4164d51207808283a18ca2728241fd5cddc4855f Merge: ef35b07 c222bc8 Author: Akarin <jimmyshjj@gmail.com> Date: Wed May 15 11:19:13 2024 +0800 Merge branch 'Calcium-Ion:main' into main commit ef35b072824b5095ecd2d1ed7ca9fa11673da2c4 Author: Jiayun Shen <jimmyshjj@gmail.com> Date: Tue May 14 19:17:32 2024 +0800 Update adaptor.go Update frequently used model names from Baidu official docs and support custom models
2024-05-16 14:05:44 +08:00
"strings"
2024-02-29 01:08:18 +08:00
)
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"
Squashed commit of the following: commit 5a6a0df45dee3dfbf2f65591a79fe5f2b74a49e6 Author: Akarin <jimmyshjj@gmail.com> Date: Thu May 16 14:05:28 2024 +0800 Revert "Update docker-image-amd64.yml" This reverts commit 581343a78783bbd779e65b476e125af0e2b64ce5. commit a0aec1bd030da2c6b25d9541199d598f16813a60 Merge: 5b46c7d 58abb38 Author: Jiayun Shen <jimmyshjj@gmail.com> Date: Thu May 16 06:46:51 2024 +0800 Merge branch 'main' of https://github.com/jimmyshjj/new-api commit 58abb3864a89294d82f812cda9fe49ccf7e2dd91 Merge: 7d2c026 93858c3 Author: Akarin <jimmyshjj@gmail.com> Date: Thu May 16 06:46:00 2024 +0800 Merge branch 'Calcium-Ion:main' into main commit 5b46c7dd8e6132d2be3b59c7b2ed6a4b84b93cef Author: Jiayun Shen <jimmyshjj@gmail.com> Date: Thu May 16 06:45:00 2024 +0800 Update constants.go Remove replaced Baidu models commit 7d2c02679cd90b8b53f4145f83969b980a8c2095 Author: Akarin <jimmyshjj@gmail.com> Date: Wed May 15 23:40:50 2024 +0800 Update adaptor.go - Normalize model name to lowercase Baidu's official model names may include mixed case letters, but their model APIs are case-sensitive and accept only lowercase. To ensure compatibility, the default behavior has been updated to convert model names to lowercase before constructing API requests. commit 6bc168a39d9a6194d66f2f32b175e56de9295b2e Merge: bb9fecd 910e76a Author: Jiayun Shen <jimmyshjj@gmail.com> Date: Wed May 15 21:51:52 2024 +0800 Merge branch 'main' of https://github.com/jimmyshjj/new-api commit 910e76ac94d7f5dca6254abb4d0669cbb762e724 Merge: 581343a ff044de Author: Akarin <jimmyshjj@gmail.com> Date: Wed May 15 21:51:13 2024 +0800 Merge branch 'Calcium-Ion:main' into main commit bb9fecd5bf2bd9f1859a4017e7e68f80bdb6685a Author: Jiayun Shen <jimmyshjj@gmail.com> Date: Wed May 15 21:50:08 2024 +0800 update Baidu and 360 models Add Baidu and 360 new models. Add Baidu completion ratio commit 581343a78783bbd779e65b476e125af0e2b64ce5 Author: Akarin <jimmyshjj@gmail.com> Date: Wed May 15 19:41:34 2024 +0800 Update docker-image-amd64.yml commit de17e2d95eec80f1eeae66e82dec4e9601cdee43 Merge: 046f653 a3b3e6c Author: Akarin <jimmyshjj@gmail.com> Date: Wed May 15 19:22:09 2024 +0800 Merge branch 'Calcium-Ion:main' into main commit 046f6537913ae8ad8ecf21019b64c0379331b3fd Merge: 4164d51 7b58305 Author: Akarin <jimmyshjj@gmail.com> Date: Wed May 15 15:32:38 2024 +0800 Merge branch 'Calcium-Ion:main' into main commit 4164d51207808283a18ca2728241fd5cddc4855f Merge: ef35b07 c222bc8 Author: Akarin <jimmyshjj@gmail.com> Date: Wed May 15 11:19:13 2024 +0800 Merge branch 'Calcium-Ion:main' into main commit ef35b072824b5095ecd2d1ed7ca9fa11673da2c4 Author: Jiayun Shen <jimmyshjj@gmail.com> Date: Tue May 14 19:17:32 2024 +0800 Update adaptor.go Update frequently used model names from Baidu official docs and support custom models
2024-05-16 14:05:44 +08:00
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"
2024-02-29 01:08:18 +08:00
case "Embedding-V1":
fullRequestURL = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/embeddings/embedding-v1"
Squashed commit of the following: commit 5a6a0df45dee3dfbf2f65591a79fe5f2b74a49e6 Author: Akarin <jimmyshjj@gmail.com> Date: Thu May 16 14:05:28 2024 +0800 Revert "Update docker-image-amd64.yml" This reverts commit 581343a78783bbd779e65b476e125af0e2b64ce5. commit a0aec1bd030da2c6b25d9541199d598f16813a60 Merge: 5b46c7d 58abb38 Author: Jiayun Shen <jimmyshjj@gmail.com> Date: Thu May 16 06:46:51 2024 +0800 Merge branch 'main' of https://github.com/jimmyshjj/new-api commit 58abb3864a89294d82f812cda9fe49ccf7e2dd91 Merge: 7d2c026 93858c3 Author: Akarin <jimmyshjj@gmail.com> Date: Thu May 16 06:46:00 2024 +0800 Merge branch 'Calcium-Ion:main' into main commit 5b46c7dd8e6132d2be3b59c7b2ed6a4b84b93cef Author: Jiayun Shen <jimmyshjj@gmail.com> Date: Thu May 16 06:45:00 2024 +0800 Update constants.go Remove replaced Baidu models commit 7d2c02679cd90b8b53f4145f83969b980a8c2095 Author: Akarin <jimmyshjj@gmail.com> Date: Wed May 15 23:40:50 2024 +0800 Update adaptor.go - Normalize model name to lowercase Baidu's official model names may include mixed case letters, but their model APIs are case-sensitive and accept only lowercase. To ensure compatibility, the default behavior has been updated to convert model names to lowercase before constructing API requests. commit 6bc168a39d9a6194d66f2f32b175e56de9295b2e Merge: bb9fecd 910e76a Author: Jiayun Shen <jimmyshjj@gmail.com> Date: Wed May 15 21:51:52 2024 +0800 Merge branch 'main' of https://github.com/jimmyshjj/new-api commit 910e76ac94d7f5dca6254abb4d0669cbb762e724 Merge: 581343a ff044de Author: Akarin <jimmyshjj@gmail.com> Date: Wed May 15 21:51:13 2024 +0800 Merge branch 'Calcium-Ion:main' into main commit bb9fecd5bf2bd9f1859a4017e7e68f80bdb6685a Author: Jiayun Shen <jimmyshjj@gmail.com> Date: Wed May 15 21:50:08 2024 +0800 update Baidu and 360 models Add Baidu and 360 new models. Add Baidu completion ratio commit 581343a78783bbd779e65b476e125af0e2b64ce5 Author: Akarin <jimmyshjj@gmail.com> Date: Wed May 15 19:41:34 2024 +0800 Update docker-image-amd64.yml commit de17e2d95eec80f1eeae66e82dec4e9601cdee43 Merge: 046f653 a3b3e6c Author: Akarin <jimmyshjj@gmail.com> Date: Wed May 15 19:22:09 2024 +0800 Merge branch 'Calcium-Ion:main' into main commit 046f6537913ae8ad8ecf21019b64c0379331b3fd Merge: 4164d51 7b58305 Author: Akarin <jimmyshjj@gmail.com> Date: Wed May 15 15:32:38 2024 +0800 Merge branch 'Calcium-Ion:main' into main commit 4164d51207808283a18ca2728241fd5cddc4855f Merge: ef35b07 c222bc8 Author: Akarin <jimmyshjj@gmail.com> Date: Wed May 15 11:19:13 2024 +0800 Merge branch 'Calcium-Ion:main' into main commit ef35b072824b5095ecd2d1ed7ca9fa11673da2c4 Author: Jiayun Shen <jimmyshjj@gmail.com> Date: Tue May 14 19:17:32 2024 +0800 Update adaptor.go Update frequently used model names from Baidu official docs and support custom models
2024-05-16 14:05:44 +08:00
default:
fullRequestURL = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/" + strings.ToLower(info.UpstreamModelName)
2024-02-29 01:08:18 +08:00
}
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 {
2024-02-29 16:21:25 +08:00
channel.SetupApiRequestHeader(info, c, req)
2024-02-29 01:08:18 +08:00
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) {
2024-02-29 16:21:25 +08:00
return channel.DoApiRequest(a, c, info, requestBody)
2024-02-29 01:08:18 +08:00
}
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage *dto.Usage, err *dto.OpenAIErrorWithStatusCode) {
2024-02-29 01:08:18 +08:00
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
}