Skip to content

Commit 451c594

Browse files
authored
Merge pull request songquanpeng#2334 from seefs001/feature/glm-coding
feat: glm coding plan && kimi coding plan
1 parent 46a18c4 commit 451c594

6 files changed

Lines changed: 82 additions & 22 deletions

File tree

constant/channel.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,27 @@ func GetChannelTypeName(channelType int) string {
180180
}
181181
return "Unknown"
182182
}
183+
184+
type ChannelSpecialBase struct {
185+
ClaudeBaseURL string
186+
OpenAIBaseURL string
187+
}
188+
189+
var ChannelSpecialBases = map[string]ChannelSpecialBase{
190+
"glm-coding-plan": {
191+
ClaudeBaseURL: "https://open.bigmodel.cn/api/anthropic",
192+
OpenAIBaseURL: "https://open.bigmodel.cn/api/coding/paas/v4",
193+
},
194+
"glm-coding-plan-international": {
195+
ClaudeBaseURL: "https://api.z.ai/api/anthropic",
196+
OpenAIBaseURL: "https://api.z.ai/api/coding/paas/v4",
197+
},
198+
"kimi-coding-plan": {
199+
ClaudeBaseURL: "https://api.kimi.com/coding",
200+
OpenAIBaseURL: "https://api.kimi.com/coding/v1",
201+
},
202+
"doubao-coding-plan": {
203+
ClaudeBaseURL: "https://ark.cn-beijing.volces.com/api/coding",
204+
OpenAIBaseURL: "https://ark.cn-beijing.volces.com/api/coding/v3",
205+
},
206+
}

controller/channel.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/QuantumNous/new-api/constant"
1212
"github.com/QuantumNous/new-api/dto"
1313
"github.com/QuantumNous/new-api/model"
14-
"github.com/QuantumNous/new-api/relay/channel/volcengine"
1514
"github.com/QuantumNous/new-api/service"
1615

1716
"github.com/gin-gonic/gin"
@@ -192,10 +191,20 @@ func FetchUpstreamModels(c *gin.Context) {
192191
case constant.ChannelTypeAli:
193192
url = fmt.Sprintf("%s/compatible-mode/v1/models", baseURL)
194193
case constant.ChannelTypeZhipu_v4:
195-
url = fmt.Sprintf("%s/api/paas/v4/models", baseURL)
194+
if plan, ok := constant.ChannelSpecialBases[baseURL]; ok && plan.OpenAIBaseURL != "" {
195+
url = fmt.Sprintf("%s/models", plan.OpenAIBaseURL)
196+
} else {
197+
url = fmt.Sprintf("%s/api/paas/v4/models", baseURL)
198+
}
196199
case constant.ChannelTypeVolcEngine:
197-
if baseURL == volcengine.DoubaoCodingPlan {
198-
url = fmt.Sprintf("%s/v1/models", volcengine.DoubaoCodingPlanOpenAIBaseURL)
200+
if plan, ok := constant.ChannelSpecialBases[baseURL]; ok && plan.OpenAIBaseURL != "" {
201+
url = fmt.Sprintf("%s/v1/models", plan.OpenAIBaseURL)
202+
} else {
203+
url = fmt.Sprintf("%s/v1/models", baseURL)
204+
}
205+
case constant.ChannelTypeMoonshot:
206+
if plan, ok := constant.ChannelSpecialBases[baseURL]; ok && plan.OpenAIBaseURL != "" {
207+
url = fmt.Sprintf("%s/models", plan.OpenAIBaseURL)
199208
} else {
200209
url = fmt.Sprintf("%s/v1/models", baseURL)
201210
}

relay/channel/moonshot/adaptor.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"net/http"
88

9+
channelconstant "github.com/QuantumNous/new-api/constant"
910
"github.com/QuantumNous/new-api/dto"
1011
"github.com/QuantumNous/new-api/relay/channel"
1112
"github.com/QuantumNous/new-api/relay/channel/claude"
@@ -44,6 +45,16 @@ func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
4445
}
4546

4647
func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
48+
baseURL := info.ChannelBaseUrl
49+
if specialPlan, ok := channelconstant.ChannelSpecialBases[baseURL]; ok {
50+
if info.RelayFormat == types.RelayFormatClaude {
51+
return fmt.Sprintf("%s/v1/messages", specialPlan.ClaudeBaseURL), nil
52+
}
53+
if info.RelayFormat == types.RelayFormatOpenAI {
54+
return fmt.Sprintf("%s/chat/completions", specialPlan.OpenAIBaseURL), nil
55+
}
56+
}
57+
4758
switch info.RelayFormat {
4859
case types.RelayFormatClaude:
4960
return fmt.Sprintf("%s/anthropic/v1/messages", info.ChannelBaseUrl), nil

relay/channel/volcengine/adaptor.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@ import (
2424
)
2525

2626
const (
27-
contextKeyTTSRequest = "volcengine_tts_request"
28-
contextKeyResponseFormat = "response_format"
29-
DoubaoCodingPlan = "doubao-coding-plan"
30-
DoubaoCodingPlanClaudeBaseURL = "https://ark.cn-beijing.volces.com/api/coding"
31-
DoubaoCodingPlanOpenAIBaseURL = "https://ark.cn-beijing.volces.com/api/coding/v3"
27+
contextKeyTTSRequest = "volcengine_tts_request"
28+
contextKeyResponseFormat = "response_format"
3229
)
3330

3431
type Adaptor struct {
@@ -40,7 +37,7 @@ func (a *Adaptor) ConvertGeminiRequest(*gin.Context, *relaycommon.RelayInfo, *dt
4037
}
4138

4239
func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, req *dto.ClaudeRequest) (any, error) {
43-
if info.ChannelBaseUrl == DoubaoCodingPlan {
40+
if _, ok := channelconstant.ChannelSpecialBases[info.ChannelBaseUrl]; ok {
4441
adaptor := claude.Adaptor{}
4542
return adaptor.ConvertClaudeRequest(c, info, req)
4643
}
@@ -243,11 +240,12 @@ func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
243240
if baseUrl == "" {
244241
baseUrl = channelconstant.ChannelBaseURLs[channelconstant.ChannelTypeVolcEngine]
245242
}
243+
specialPlan, hasSpecialPlan := channelconstant.ChannelSpecialBases[baseUrl]
246244

247245
switch info.RelayFormat {
248246
case types.RelayFormatClaude:
249-
if baseUrl == DoubaoCodingPlan {
250-
return fmt.Sprintf("%s/v1/messages", DoubaoCodingPlanClaudeBaseURL), nil
247+
if hasSpecialPlan && specialPlan.ClaudeBaseURL != "" {
248+
return fmt.Sprintf("%s/v1/messages", specialPlan.ClaudeBaseURL), nil
251249
}
252250
if strings.HasPrefix(info.UpstreamModelName, "bot") {
253251
return fmt.Sprintf("%s/api/v3/bots/chat/completions", baseUrl), nil
@@ -256,8 +254,8 @@ func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
256254
default:
257255
switch info.RelayMode {
258256
case constant.RelayModeChatCompletions:
259-
if baseUrl == DoubaoCodingPlan {
260-
return fmt.Sprintf("%s/chat/completions", DoubaoCodingPlanOpenAIBaseURL), nil
257+
if hasSpecialPlan && specialPlan.OpenAIBaseURL != "" {
258+
return fmt.Sprintf("%s/chat/completions", specialPlan.OpenAIBaseURL), nil
261259
}
262260
if strings.HasPrefix(info.UpstreamModelName, "bot") {
263261
return fmt.Sprintf("%s/api/v3/bots/chat/completions", baseUrl), nil
@@ -345,11 +343,13 @@ func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, request
345343
}
346344

347345
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
348-
if info.RelayFormat == types.RelayFormatClaude && info.ChannelBaseUrl == DoubaoCodingPlan {
349-
if info.IsStream {
350-
return claude.ClaudeStreamHandler(c, resp, info, claude.RequestModeMessage)
346+
if info.RelayFormat == types.RelayFormatClaude {
347+
if _, ok := channelconstant.ChannelSpecialBases[info.ChannelBaseUrl]; ok {
348+
if info.IsStream {
349+
return claude.ClaudeStreamHandler(c, resp, info, claude.RequestModeMessage)
350+
}
351+
return claude.ClaudeHandler(c, resp, info, claude.RequestModeMessage)
351352
}
352-
return claude.ClaudeHandler(c, resp, info, claude.RequestModeMessage)
353353
}
354354

355355
if info.RelayMode == constant.RelayModeAudioSpeech {

relay/channel/zhipu_4v/adaptor.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"net/http"
88

9+
channelconstant "github.com/QuantumNous/new-api/constant"
910
"github.com/QuantumNous/new-api/dto"
1011
"github.com/QuantumNous/new-api/relay/channel"
1112
"github.com/QuantumNous/new-api/relay/channel/claude"
@@ -43,15 +44,30 @@ func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
4344
}
4445

4546
func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
47+
baseURL := info.ChannelBaseUrl
48+
if baseURL == "" {
49+
baseURL = channelconstant.ChannelBaseURLs[channelconstant.ChannelTypeZhipu_v4]
50+
}
51+
specialPlan, hasSpecialPlan := channelconstant.ChannelSpecialBases[baseURL]
52+
4653
switch info.RelayFormat {
4754
case types.RelayFormatClaude:
48-
return fmt.Sprintf("%s/api/anthropic/v1/messages", info.ChannelBaseUrl), nil
55+
if hasSpecialPlan && specialPlan.ClaudeBaseURL != "" {
56+
return fmt.Sprintf("%s/v1/messages", specialPlan.ClaudeBaseURL), nil
57+
}
58+
return fmt.Sprintf("%s/api/anthropic/v1/messages", baseURL), nil
4959
default:
5060
switch info.RelayMode {
5161
case relayconstant.RelayModeEmbeddings:
52-
return fmt.Sprintf("%s/api/paas/v4/embeddings", info.ChannelBaseUrl), nil
62+
if hasSpecialPlan && specialPlan.OpenAIBaseURL != "" {
63+
return fmt.Sprintf("%s/embeddings", specialPlan.OpenAIBaseURL), nil
64+
}
65+
return fmt.Sprintf("%s/api/paas/v4/embeddings", baseURL), nil
5366
default:
54-
return fmt.Sprintf("%s/api/paas/v4/chat/completions", info.ChannelBaseUrl), nil
67+
if hasSpecialPlan && specialPlan.OpenAIBaseURL != "" {
68+
return fmt.Sprintf("%s/chat/completions", specialPlan.OpenAIBaseURL), nil
69+
}
70+
return fmt.Sprintf("%s/api/paas/v4/chat/completions", baseURL), nil
5571
}
5672
}
5773
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package zhipu_4v
22

33
var ModelList = []string{
4-
"glm-4", "glm-4v", "glm-3-turbo", "glm-4-alltools", "glm-4-plus", "glm-4-0520", "glm-4-air", "glm-4-airx", "glm-4-long", "glm-4-flash", "glm-4v-plus",
4+
"glm-4", "glm-4v", "glm-3-turbo", "glm-4-alltools", "glm-4-plus", "glm-4-0520", "glm-4-air", "glm-4-airx", "glm-4-long", "glm-4-flash", "glm-4v-plus", "glm-4.6",
55
}
66

77
var ChannelName = "zhipu_4v"

0 commit comments

Comments
 (0)