Skip to content

Commit e7d91d0

Browse files
committed
feats:replace GLM-4v authentication headers to support customize api key
1 parent b3a1948 commit e7d91d0

2 files changed

Lines changed: 1 addition & 61 deletions

File tree

relay/channel/zhipu_4v/adaptor.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
5454

5555
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
5656
channel.SetupApiRequestHeader(info, c, req)
57-
token := getZhipuToken(info.ApiKey)
58-
req.Set("Authorization", token)
57+
req.Set("Authorization", "Bearer "+info.ApiKey)
5958
return nil
6059
}
6160

relay/channel/zhipu_4v/relay-zhipu_v4.go

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,10 @@
11
package zhipu_4v
22

33
import (
4-
"github.com/golang-jwt/jwt"
5-
"one-api/common"
64
"one-api/dto"
75
"strings"
8-
"sync"
9-
"time"
106
)
117

12-
// https://open.bigmodel.cn/doc/api#chatglm_std
13-
// chatglm_std, chatglm_lite
14-
// https://open.bigmodel.cn/api/paas/v3/model-api/chatglm_std/invoke
15-
// https://open.bigmodel.cn/api/paas/v3/model-api/chatglm_std/sse-invoke
16-
17-
var zhipuTokens sync.Map
18-
var expSeconds int64 = 24 * 3600
19-
20-
func getZhipuToken(apikey string) string {
21-
data, ok := zhipuTokens.Load(apikey)
22-
if ok {
23-
tokenData := data.(tokenData)
24-
if time.Now().Before(tokenData.ExpiryTime) {
25-
return tokenData.Token
26-
}
27-
}
28-
29-
split := strings.Split(apikey, ".")
30-
if len(split) != 2 {
31-
common.SysError("invalid zhipu key: " + apikey)
32-
return ""
33-
}
34-
35-
id := split[0]
36-
secret := split[1]
37-
38-
expMillis := time.Now().Add(time.Duration(expSeconds)*time.Second).UnixNano() / 1e6
39-
expiryTime := time.Now().Add(time.Duration(expSeconds) * time.Second)
40-
41-
timestamp := time.Now().UnixNano() / 1e6
42-
43-
payload := jwt.MapClaims{
44-
"api_key": id,
45-
"exp": expMillis,
46-
"timestamp": timestamp,
47-
}
48-
49-
token := jwt.NewWithClaims(jwt.SigningMethodHS256, payload)
50-
51-
token.Header["alg"] = "HS256"
52-
token.Header["sign_type"] = "SIGN"
53-
54-
tokenString, err := token.SignedString([]byte(secret))
55-
if err != nil {
56-
return ""
57-
}
58-
59-
zhipuTokens.Store(apikey, tokenData{
60-
Token: tokenString,
61-
ExpiryTime: expiryTime,
62-
})
63-
64-
return tokenString
65-
}
66-
678
func requestOpenAI2Zhipu(request dto.GeneralOpenAIRequest) *dto.GeneralOpenAIRequest {
689
messages := make([]dto.Message, 0, len(request.Messages))
6910
for _, message := range request.Messages {

0 commit comments

Comments
 (0)