fix: clamp max_tokens and inference parameters to valid Gradio slider ranges
This commit is contained in:
@@ -414,7 +414,7 @@ func ResolveMaxTokens(req ChatCompletionRequest) int {
|
||||
mt = req.MaxCompletionTokens
|
||||
}
|
||||
if mt <= 0 {
|
||||
mt = 8192
|
||||
mt = 4096
|
||||
}
|
||||
if mt > 32768 {
|
||||
mt = 32768
|
||||
@@ -1633,11 +1633,29 @@ func (s *QwenService) chatGradioRespond(endpointURL string, w http.ResponseWrite
|
||||
if req.Temperature != nil {
|
||||
tempVal = *req.Temperature
|
||||
}
|
||||
if tempVal < 0.0 {
|
||||
tempVal = 0.0
|
||||
} else if tempVal > 2.0 {
|
||||
tempVal = 2.0
|
||||
}
|
||||
|
||||
topPVal := 0.95
|
||||
if req.TopP != nil {
|
||||
topPVal = *req.TopP
|
||||
}
|
||||
if topPVal < 0.0 {
|
||||
topPVal = 0.0
|
||||
} else if topPVal > 1.0 {
|
||||
topPVal = 1.0
|
||||
}
|
||||
|
||||
// MicroHERO /respond endpoint has a slider with strict range [64, 4096]
|
||||
clampedTokens := maxTokens
|
||||
if clampedTokens > 4096 {
|
||||
clampedTokens = 4096
|
||||
} else if clampedTokens < 64 {
|
||||
clampedTokens = 64
|
||||
}
|
||||
|
||||
effUA := EffectiveUserAgent(r)
|
||||
effHFToken := EffectiveHFToken(r)
|
||||
@@ -1650,7 +1668,7 @@ func (s *QwenService) chatGradioRespond(endpointURL string, w http.ResponseWrite
|
||||
"text": promptText,
|
||||
"files": []interface{}{},
|
||||
},
|
||||
maxTokens,
|
||||
clampedTokens,
|
||||
tempVal,
|
||||
topPVal,
|
||||
}
|
||||
@@ -1887,6 +1905,11 @@ func (s *QwenService) chatGradioChatResponse(endpointURL string, w http.Response
|
||||
} else if !enableThinking {
|
||||
tempVal = 0.7
|
||||
}
|
||||
if tempVal < 0.0 {
|
||||
tempVal = 0.0
|
||||
} else if tempVal > 2.0 {
|
||||
tempVal = 2.0
|
||||
}
|
||||
|
||||
topPVal := 0.95
|
||||
if req.TopP != nil {
|
||||
@@ -1894,12 +1917,30 @@ func (s *QwenService) chatGradioChatResponse(endpointURL string, w http.Response
|
||||
} else if !enableThinking {
|
||||
topPVal = 0.80
|
||||
}
|
||||
if topPVal < 0.1 {
|
||||
topPVal = 0.1
|
||||
} else if topPVal > 1.0 {
|
||||
topPVal = 1.0
|
||||
}
|
||||
|
||||
topKVal := 20
|
||||
presenceVal := 0.0
|
||||
if !enableThinking {
|
||||
presenceVal = 1.5
|
||||
}
|
||||
if presenceVal < 0.0 {
|
||||
presenceVal = 0.0
|
||||
} else if presenceVal > 2.0 {
|
||||
presenceVal = 2.0
|
||||
}
|
||||
|
||||
// Halvo78 /chat_response slider range is [256, 32768]
|
||||
clampedTokens := maxTokens
|
||||
if clampedTokens > 32768 {
|
||||
clampedTokens = 32768
|
||||
} else if clampedTokens < 256 {
|
||||
clampedTokens = 256
|
||||
}
|
||||
|
||||
customAPIKey := EffectiveUpstreamKey(r)
|
||||
if customAPIKey == "" {
|
||||
@@ -1943,7 +1984,7 @@ func (s *QwenService) chatGradioChatResponse(endpointURL string, w http.Response
|
||||
topPVal,
|
||||
topKVal,
|
||||
presenceVal,
|
||||
maxTokens,
|
||||
clampedTokens,
|
||||
systemPromptStr,
|
||||
customBaseURL,
|
||||
customAPIKey,
|
||||
|
||||
@@ -626,4 +626,27 @@ func TestQwenServiceAutoFailover(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveMaxTokens(t *testing.T) {
|
||||
// Default when empty
|
||||
if v := ResolveMaxTokens(ChatCompletionRequest{}); v != 4096 {
|
||||
t.Fatalf("expected default 4096, got %d", v)
|
||||
}
|
||||
|
||||
// Explicit max_tokens
|
||||
if v := ResolveMaxTokens(ChatCompletionRequest{MaxTokens: 2048}); v != 2048 {
|
||||
t.Fatalf("expected 2048, got %d", v)
|
||||
}
|
||||
|
||||
// max_completion_tokens precedence when max_tokens is 0
|
||||
if v := ResolveMaxTokens(ChatCompletionRequest{MaxCompletionTokens: 1024}); v != 1024 {
|
||||
t.Fatalf("expected 1024, got %d", v)
|
||||
}
|
||||
|
||||
// Cap at 32768
|
||||
if v := ResolveMaxTokens(ChatCompletionRequest{MaxTokens: 65536}); v != 32768 {
|
||||
t.Fatalf("expected cap at 32768, got %d", v)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user