Skip to content

Commit

Permalink
feat: add new SparkDesk models
Browse files Browse the repository at this point in the history
  • Loading branch information
songquanpeng committed Feb 18, 2024
1 parent 532c50d commit f598b9d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 13 deletions.
4 changes: 4 additions & 0 deletions common/model-ratio.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ var ModelRatio = map[string]float64{
"qwen-max-longcontext": 1.4286, // ¥0.02 / 1k tokens
"text-embedding-v1": 0.05, // ¥0.0007 / 1k tokens
"SparkDesk": 1.2858, // ¥0.018 / 1k tokens
"SparkDesk-v1.5": 1.2858, // ¥0.018 / 1k tokens
"SparkDesk-v2.0": 1.2858, // ¥0.018 / 1k tokens
"SparkDesk-v3.0": 1.2858, // ¥0.018 / 1k tokens
"SparkDesk-v3.5": 1.2858, // ¥0.018 / 1k tokens
"360GPT_S2_V9": 0.8572, // ¥0.012 / 1k tokens
"embedding-bert-512-v1": 0.0715, // ¥0.001 / 1k tokens
"embedding_s1_v1": 0.0715, // ¥0.001 / 1k tokens
Expand Down
4 changes: 4 additions & 0 deletions relay/channel/xunfei/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ package xunfei

var ModelList = []string{
"SparkDesk",
"SparkDesk-v1.5",
"SparkDesk-v2.0",
"SparkDesk-v3.0",
"SparkDesk-v3.5",
}
47 changes: 36 additions & 11 deletions relay/channel/xunfei/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func buildXunfeiAuthUrl(hostUrl string, apiKey, apiSecret string) string {
}

func StreamHandler(c *gin.Context, textRequest model.GeneralOpenAIRequest, appId string, apiSecret string, apiKey string) (*model.ErrorWithStatusCode, *model.Usage) {
domain, authUrl := getXunfeiAuthUrl(c, apiKey, apiSecret)
domain, authUrl := getXunfeiAuthUrl(c, apiKey, apiSecret, textRequest.Model)
dataChan, stopChan, err := xunfeiMakeRequest(textRequest, domain, authUrl, appId)
if err != nil {
return openai.ErrorWrapper(err, "make xunfei request err", http.StatusInternalServerError), nil
Expand Down Expand Up @@ -157,7 +157,7 @@ func StreamHandler(c *gin.Context, textRequest model.GeneralOpenAIRequest, appId
}

func Handler(c *gin.Context, textRequest model.GeneralOpenAIRequest, appId string, apiSecret string, apiKey string) (*model.ErrorWithStatusCode, *model.Usage) {
domain, authUrl := getXunfeiAuthUrl(c, apiKey, apiSecret)
domain, authUrl := getXunfeiAuthUrl(c, apiKey, apiSecret, textRequest.Model)
dataChan, stopChan, err := xunfeiMakeRequest(textRequest, domain, authUrl, appId)
if err != nil {
return openai.ErrorWrapper(err, "make xunfei request err", http.StatusInternalServerError), nil
Expand Down Expand Up @@ -242,20 +242,45 @@ func xunfeiMakeRequest(textRequest model.GeneralOpenAIRequest, domain, authUrl,
return dataChan, stopChan, nil
}

func getXunfeiAuthUrl(c *gin.Context, apiKey string, apiSecret string) (string, string) {
func getAPIVersion(c *gin.Context, modelName string) string {
query := c.Request.URL.Query()
apiVersion := query.Get("api-version")
if apiVersion == "" {
apiVersion = c.GetString(common.ConfigKeyAPIVersion)
if apiVersion != "" {
return apiVersion
}
if apiVersion == "" {
apiVersion = "v1.1"
logger.SysLog("api_version not found, use default: " + apiVersion)
parts := strings.Split(modelName, "-")
if len(parts) == 2 {
apiVersion = parts[1]
return apiVersion

}
apiVersion = c.GetString(common.ConfigKeyAPIVersion)
if apiVersion != "" {
return apiVersion
}
domain := "general"
if apiVersion != "v1.1" {
domain += strings.Split(apiVersion, ".")[0]
apiVersion = "v1.1"
logger.SysLog("api_version not found, using default: " + apiVersion)
return apiVersion
}

// https://www.xfyun.cn/doc/spark/Web.html#_1-%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
func apiVersion2domain(apiVersion string) string {
switch apiVersion {
case "v1.1":
return "general"
case "v2.1":
return "generalv2"
case "v3.1":
return "generalv3"
case "v3.5":
return "generalv3.5"
}
return "general" + apiVersion
}

func getXunfeiAuthUrl(c *gin.Context, apiKey string, apiSecret string, modelName string) (string, string) {
apiVersion := getAPIVersion(c, modelName)
domain := apiVersion2domain(apiVersion)
authUrl := buildXunfeiAuthUrl(fmt.Sprintf("wss://spark-api.xf-yun.com/%s/chat", apiVersion), apiKey, apiSecret)
return domain, authUrl
}
8 changes: 7 additions & 1 deletion web/berry/src/views/Channel/type/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ const typeConfig = {
other: "版本号",
},
input: {
models: ["SparkDesk"],
models: [
"SparkDesk",
"SparkDesk-v1.5",
"SparkDesk-v2.0",
"SparkDesk-v3.0",
"SparkDesk-v3.5"
],
},
prompt: {
key: "按照如下格式输入:APPID|APISecret|APIKey",
Expand Down
8 changes: 7 additions & 1 deletion web/default/src/pages/Channel/EditChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ const EditChannel = () => {
localModels = ['chatglm_turbo', 'chatglm_pro', 'chatglm_std', 'chatglm_lite'];
break;
case 18:
localModels = ['SparkDesk'];
localModels = [
'SparkDesk',
'SparkDesk-v1.5',
'SparkDesk-v2.0',
'SparkDesk-v3.0',
'SparkDesk-v3.5'
];
break;
case 19:
localModels = ['360GPT_S2_V9', 'embedding-bert-512-v1', 'embedding_s1_v1', 'semantic_similarity_s1_v1'];
Expand Down

0 comments on commit f598b9d

Please sign in to comment.