添加对智谱V4 API的支持

This commit is contained in:
hongsheng
2024-01-25 04:21:22 +08:00
parent 4f214c48c6
commit 31b85ded54
12 changed files with 380 additions and 4 deletions

View File

@@ -19,6 +19,7 @@ import (
"one-api/relay/channel/tencent"
"one-api/relay/channel/xunfei"
"one-api/relay/channel/zhipu"
"one-api/relay/channel/zhipu_v4"
"one-api/relay/constant"
"one-api/relay/util"
"strings"
@@ -79,6 +80,8 @@ func GetRequestURL(requestURL string, apiType int, relayMode int, meta *util.Rel
method = "sse-invoke"
}
fullRequestURL = fmt.Sprintf("https://open.bigmodel.cn/api/paas/v3/model-api/%s/%s", textRequest.Model, method)
case constant.APITypeZhipu_v4:
fullRequestURL = "https://open.bigmodel.cn/api/paas/v4/chat/completions"
case constant.APITypeAli:
fullRequestURL = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation"
if relayMode == constant.RelayModeEmbeddings {
@@ -147,6 +150,13 @@ func GetRequestBody(c *gin.Context, textRequest openai.GeneralOpenAIRequest, isM
return nil, err
}
requestBody = bytes.NewBuffer(jsonStr)
case constant.APITypeZhipu_v4:
zhipuRequest := zhipu_v4.ConvertRequest(textRequest)
jsonStr, err := json.Marshal(zhipuRequest)
if err != nil {
return nil, err
}
requestBody = bytes.NewBuffer(jsonStr)
case constant.APITypeAli:
var jsonStr []byte
var err error
@@ -223,6 +233,9 @@ func SetupAuthHeaders(c *gin.Context, req *http.Request, apiType int, meta *util
case constant.APITypeZhipu:
token := zhipu.GetToken(apiKey)
req.Header.Set("Authorization", token)
case constant.APITypeZhipu_v4:
token := zhipu_v4.GetToken(apiKey)
req.Header.Set("Authorization", token)
case constant.APITypeAli:
req.Header.Set("Authorization", "Bearer "+apiKey)
if isStream {
@@ -286,6 +299,12 @@ func DoResponse(c *gin.Context, textRequest *openai.GeneralOpenAIRequest, resp *
} else {
err, usage = zhipu.Handler(c, resp)
}
case constant.APITypeZhipu_v4:
if isStream {
err, usage = zhipu_v4.StreamHandler(c, resp)
} else {
err, usage = zhipu_v4.Handler(c, resp)
}
case constant.APITypeAli:
if isStream {
err, usage = ali.StreamHandler(c, resp)