mirror of
https://github.com/Ed1s0nZ/CyberStrikeAI.git
synced 2026-12-01 13:03:41 +01:00
fix(models): respect the DeepSeek Flash context window
This commit is contained in:
1 parent
501a70c3e9
commit
e56f88b44a
4 files changed
+84
-19
No files matched your search
@@ -7,6 +7,8 @@ const (
|
||||
DefaultMaxTotalTokens = 120000
|
||||
// DefaultLocalContextWindow 是 local 档位下未提供 context_window 时的保守窗口。
|
||||
DefaultLocalContextWindow = 32768
|
||||
// DeepSeek's official /models response reports this total input and output window.
|
||||
deepSeekFlashContextWindow = 1048576
|
||||
// LocalContextBudgetPercent 是 local 档位把原生窗口折算成提示词预算的比例。
|
||||
LocalContextBudgetPercent = 60
|
||||
// LocalContextBudgetCap 防止 256K/1M 窗口把预算放大到超出本地模型有效上下文。
|
||||
@@ -58,14 +60,22 @@ func (c OpenAIConfig) OutputLimitField() string {
|
||||
return "max_completion_tokens"
|
||||
}
|
||||
|
||||
// EffectiveContextWindow 返回用于预算计算的原生窗口;local 档位缺省时按保守值处理。
|
||||
// EffectiveContextWindow returns the window used to calculate the token budget.
|
||||
func (c OpenAIConfig) EffectiveContextWindow() int {
|
||||
if c.ContextWindow > 0 {
|
||||
return c.ContextWindow
|
||||
}
|
||||
return c.DefaultContextWindow()
|
||||
}
|
||||
|
||||
// DefaultContextWindow is the fallback when the channel has no explicit window.
|
||||
func (c OpenAIConfig) DefaultContextWindow() int {
|
||||
if c.IsLocalContextProfile() {
|
||||
return DefaultLocalContextWindow
|
||||
}
|
||||
if c.IsDeepSeekOfficialEndpoint() && strings.EqualFold(strings.TrimSpace(c.Model), "deepseek-flash") {
|
||||
return deepSeekFlashContextWindow
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
@@ -42,6 +42,24 @@ func TestEffectiveMaxTotalTokens(t *testing.T) {
|
||||
oa: OpenAIConfig{},
|
||||
want: 120000,
|
||||
},
|
||||
{
|
||||
name: "official DeepSeek Flash uses its default window",
|
||||
oa: OpenAIConfig{BaseURL: "https://api.deepseek.com/v1", Model: "deepseek-flash",
|
||||
MaxTotalTokens: 2000000, MaxCompletionTokens: 32768},
|
||||
want: 1048576 - 32768,
|
||||
},
|
||||
{
|
||||
name: "official DeepSeek Flash preserves a manual window",
|
||||
oa: OpenAIConfig{BaseURL: "https://api.deepseek.com", Model: "deepseek-flash", ContextWindow: 65536,
|
||||
MaxTotalTokens: 2000000, MaxCompletionTokens: 32768},
|
||||
want: 65536 - 32768,
|
||||
},
|
||||
{
|
||||
name: "same model name on a different host has no known window",
|
||||
oa: OpenAIConfig{BaseURL: "https://api.deepseek.com.example.test", Model: "deepseek-flash",
|
||||
MaxTotalTokens: 2000000, MaxCompletionTokens: 32768},
|
||||
want: 2000000,
|
||||
},
|
||||
{
|
||||
name: "remote window reserves output",
|
||||
oa: OpenAIConfig{ContextWindow: 131072, MaxTotalTokens: 120000},
|
||||
|
||||
+27
-18
@@ -281,7 +281,8 @@ type GetConfigResponse struct {
|
||||
MultiAgent config.MultiAgentPublic `json:"multi_agent,omitempty"`
|
||||
C2 config.C2Public `json:"c2"`
|
||||
|
||||
ImageInputDefaults map[string]bool `json:"image_input_defaults,omitempty"`
|
||||
ImageInputDefaults map[string]bool `json:"image_input_defaults,omitempty"`
|
||||
ContextWindowDefaults map[string]int `json:"context_window_defaults,omitempty"`
|
||||
}
|
||||
|
||||
// ToolConfigInfo 工具配置信息
|
||||
@@ -377,12 +378,16 @@ func (h *ConfigHandler) GetConfig(c *gin.Context) {
|
||||
}
|
||||
|
||||
imageDefaults := make(map[string]bool)
|
||||
windowDefaults := make(map[string]int)
|
||||
for id, channel := range h.config.AI.Channels {
|
||||
model := channel.ToOpenAIConfig()
|
||||
model.ImageInput = ""
|
||||
if capability := model.ImageInputCapability(); capability != nil {
|
||||
imageDefaults[id] = *capability
|
||||
}
|
||||
if window := model.DefaultContextWindow(); window > 0 {
|
||||
windowDefaults[id] = window
|
||||
}
|
||||
}
|
||||
if len(h.config.AI.Channels) == 0 {
|
||||
model := h.config.OpenAI
|
||||
@@ -390,26 +395,30 @@ func (h *ConfigHandler) GetConfig(c *gin.Context) {
|
||||
if capability := model.ImageInputCapability(); capability != nil {
|
||||
imageDefaults[""] = *capability
|
||||
}
|
||||
if window := model.DefaultContextWindow(); window > 0 {
|
||||
windowDefaults[""] = window
|
||||
}
|
||||
}
|
||||
|
||||
response, err := maskedConfigResponse(GetConfigResponse{
|
||||
AgentRuntime: h.config.AgentRuntime,
|
||||
AI: h.config.AI,
|
||||
OpenAI: h.config.OpenAI,
|
||||
Vision: h.config.Vision,
|
||||
FOFA: h.config.FOFA,
|
||||
ZoomEye: h.config.ZoomEye,
|
||||
Quake: h.config.Quake,
|
||||
Shodan: h.config.Shodan,
|
||||
MCP: h.config.MCP,
|
||||
Tools: tools,
|
||||
Agent: h.config.Agent,
|
||||
Hitl: h.config.Hitl,
|
||||
Knowledge: h.config.Knowledge,
|
||||
C2: h.config.C2.Public(),
|
||||
Robots: h.config.Robots,
|
||||
MultiAgent: multiPub,
|
||||
ImageInputDefaults: imageDefaults,
|
||||
AgentRuntime: h.config.AgentRuntime,
|
||||
AI: h.config.AI,
|
||||
OpenAI: h.config.OpenAI,
|
||||
Vision: h.config.Vision,
|
||||
FOFA: h.config.FOFA,
|
||||
ZoomEye: h.config.ZoomEye,
|
||||
Quake: h.config.Quake,
|
||||
Shodan: h.config.Shodan,
|
||||
MCP: h.config.MCP,
|
||||
Tools: tools,
|
||||
Agent: h.config.Agent,
|
||||
Hitl: h.config.Hitl,
|
||||
Knowledge: h.config.Knowledge,
|
||||
C2: h.config.C2.Public(),
|
||||
Robots: h.config.Robots,
|
||||
MultiAgent: multiPub,
|
||||
ImageInputDefaults: imageDefaults,
|
||||
ContextWindowDefaults: windowDefaults,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "配置序列化失败"})
|
||||
|
||||
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
@@ -10,9 +11,36 @@ import (
|
||||
"cyberstrike-ai/internal/modelmeta"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func TestGetConfigReportsContextWindowDefaultsSeparately(t *testing.T) {
|
||||
cfg := &config.Config{AI: config.AIConfig{Channels: map[string]config.AIChannelConfig{
|
||||
"flash": {BaseURL: "https://api.deepseek.com", Model: "deepseek-flash"},
|
||||
"local": {ContextProfile: "local", ContextWindow: 65536},
|
||||
"gateway": {BaseURL: "https://example.test", Model: "deepseek-flash"},
|
||||
}}}
|
||||
h := &ConfigHandler{config: cfg, logger: zap.NewNop()}
|
||||
router := gin.New()
|
||||
router.GET("/config", h.GetConfig)
|
||||
res := httptest.NewRecorder()
|
||||
router.ServeHTTP(res, httptest.NewRequest(http.MethodGet, "/config", nil))
|
||||
if res.Code != http.StatusOK {
|
||||
t.Fatalf("GET config: %d %s", res.Code, res.Body.String())
|
||||
}
|
||||
var result GetConfigResponse
|
||||
if err := json.Unmarshal(res.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if defaults := result.ContextWindowDefaults; defaults["flash"] != 1048576 || defaults["local"] != 32768 || len(defaults) != 2 {
|
||||
t.Fatalf("unexpected context window defaults: %v", defaults)
|
||||
}
|
||||
if result.AI.Channels["flash"].ContextWindow != 0 || result.AI.Channels["local"].ContextWindow != 65536 {
|
||||
t.Fatal("default windows changed the configured values")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectedContextSettingsRoundTripThroughSavedYAML(t *testing.T) {
|
||||
var doc yaml.Node
|
||||
if err := yaml.Unmarshal([]byte("ai:\n default_channel: local\n channels:\n local:\n model: fixture\n context_profile: custom\n context_window: 131072\n"), &doc); err != nil {
|
||||
|
||||
Reference in new issue
Block a user