feats:replace GLM-4v authentication headers to support customize api key
This commit is contained in:
@@ -54,8 +54,7 @@ func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
|
|||||||
|
|
||||||
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
|
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
|
||||||
channel.SetupApiRequestHeader(info, c, req)
|
channel.SetupApiRequestHeader(info, c, req)
|
||||||
token := getZhipuToken(info.ApiKey)
|
req.Set("Authorization", "Bearer "+info.ApiKey)
|
||||||
req.Set("Authorization", token)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,69 +1,10 @@
|
|||||||
package zhipu_4v
|
package zhipu_4v
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/golang-jwt/jwt"
|
|
||||||
"one-api/common"
|
|
||||||
"one-api/dto"
|
"one-api/dto"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// https://open.bigmodel.cn/doc/api#chatglm_std
|
|
||||||
// chatglm_std, chatglm_lite
|
|
||||||
// https://open.bigmodel.cn/api/paas/v3/model-api/chatglm_std/invoke
|
|
||||||
// https://open.bigmodel.cn/api/paas/v3/model-api/chatglm_std/sse-invoke
|
|
||||||
|
|
||||||
var zhipuTokens sync.Map
|
|
||||||
var expSeconds int64 = 24 * 3600
|
|
||||||
|
|
||||||
func getZhipuToken(apikey string) string {
|
|
||||||
data, ok := zhipuTokens.Load(apikey)
|
|
||||||
if ok {
|
|
||||||
tokenData := data.(tokenData)
|
|
||||||
if time.Now().Before(tokenData.ExpiryTime) {
|
|
||||||
return tokenData.Token
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
split := strings.Split(apikey, ".")
|
|
||||||
if len(split) != 2 {
|
|
||||||
common.SysError("invalid zhipu key: " + apikey)
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
id := split[0]
|
|
||||||
secret := split[1]
|
|
||||||
|
|
||||||
expMillis := time.Now().Add(time.Duration(expSeconds)*time.Second).UnixNano() / 1e6
|
|
||||||
expiryTime := time.Now().Add(time.Duration(expSeconds) * time.Second)
|
|
||||||
|
|
||||||
timestamp := time.Now().UnixNano() / 1e6
|
|
||||||
|
|
||||||
payload := jwt.MapClaims{
|
|
||||||
"api_key": id,
|
|
||||||
"exp": expMillis,
|
|
||||||
"timestamp": timestamp,
|
|
||||||
}
|
|
||||||
|
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, payload)
|
|
||||||
|
|
||||||
token.Header["alg"] = "HS256"
|
|
||||||
token.Header["sign_type"] = "SIGN"
|
|
||||||
|
|
||||||
tokenString, err := token.SignedString([]byte(secret))
|
|
||||||
if err != nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
zhipuTokens.Store(apikey, tokenData{
|
|
||||||
Token: tokenString,
|
|
||||||
ExpiryTime: expiryTime,
|
|
||||||
})
|
|
||||||
|
|
||||||
return tokenString
|
|
||||||
}
|
|
||||||
|
|
||||||
func requestOpenAI2Zhipu(request dto.GeneralOpenAIRequest) *dto.GeneralOpenAIRequest {
|
func requestOpenAI2Zhipu(request dto.GeneralOpenAIRequest) *dto.GeneralOpenAIRequest {
|
||||||
messages := make([]dto.Message, 0, len(request.Messages))
|
messages := make([]dto.Message, 0, len(request.Messages))
|
||||||
for _, message := range request.Messages {
|
for _, message := range request.Messages {
|
||||||
|
|||||||
Reference in New Issue
Block a user