feat: allow temperature 0 and distinguish unset

This commit is contained in:
cointem 2026-02-20 01:22:04 +08:00
parent 493bb52fff
commit 2f0b679e75
6 changed files with 46 additions and 14 deletions

View file

@ -83,9 +83,9 @@ func NewAgentInstance(
maxTokens = 8192 maxTokens = 8192
} }
temperature := defaults.Temperature temperature := 0.7
if temperature == 0 { if defaults.Temperature != nil {
temperature = 0.7 temperature = *defaults.Temperature
} }
// Resolve fallback candidates // Resolve fallback candidates

View file

@ -20,12 +20,14 @@ func TestNewAgentInstance_UsesDefaultsTemperatureAndMaxTokens(t *testing.T) {
Workspace: tmpDir, Workspace: tmpDir,
Model: "test-model", Model: "test-model",
MaxTokens: 1234, MaxTokens: 1234,
Temperature: 1.0,
MaxToolIterations: 5, MaxToolIterations: 5,
}, },
}, },
} }
configuredTemp := 1.0
cfg.Agents.Defaults.Temperature = &configuredTemp
provider := &mockProvider{} provider := &mockProvider{}
agent := NewAgentInstance(nil, &cfg.Agents.Defaults, cfg, provider) agent := NewAgentInstance(nil, &cfg.Agents.Defaults, cfg, provider)
@ -50,7 +52,35 @@ func TestNewAgentInstance_DefaultsTemperatureWhenZero(t *testing.T) {
Workspace: tmpDir, Workspace: tmpDir,
Model: "test-model", Model: "test-model",
MaxTokens: 1234, MaxTokens: 1234,
Temperature: 0, MaxToolIterations: 5,
},
},
}
configuredTemp := 0.0
cfg.Agents.Defaults.Temperature = &configuredTemp
provider := &mockProvider{}
agent := NewAgentInstance(nil, &cfg.Agents.Defaults, cfg, provider)
if agent.Temperature != 0.0 {
t.Fatalf("Temperature = %f, want %f", agent.Temperature, 0.0)
}
}
func TestNewAgentInstance_DefaultsTemperatureWhenUnset(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "agent-instance-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
cfg := &config.Config{
Agents: config.AgentsConfig{
Defaults: config.AgentDefaults{
Workspace: tmpDir,
Model: "test-model",
MaxTokens: 1234,
MaxToolIterations: 5, MaxToolIterations: 5,
}, },
}, },

View file

@ -147,7 +147,7 @@ type AgentDefaults struct {
ImageModel string `json:"image_model,omitempty" env:"PICOCLAW_AGENTS_DEFAULTS_IMAGE_MODEL"` ImageModel string `json:"image_model,omitempty" env:"PICOCLAW_AGENTS_DEFAULTS_IMAGE_MODEL"`
ImageModelFallbacks []string `json:"image_model_fallbacks,omitempty"` ImageModelFallbacks []string `json:"image_model_fallbacks,omitempty"`
MaxTokens int `json:"max_tokens" env:"PICOCLAW_AGENTS_DEFAULTS_MAX_TOKENS"` MaxTokens int `json:"max_tokens" env:"PICOCLAW_AGENTS_DEFAULTS_MAX_TOKENS"`
Temperature float64 `json:"temperature" env:"PICOCLAW_AGENTS_DEFAULTS_TEMPERATURE"` Temperature *float64 `json:"temperature,omitempty" env:"PICOCLAW_AGENTS_DEFAULTS_TEMPERATURE"`
MaxToolIterations int `json:"max_tool_iterations" env:"PICOCLAW_AGENTS_DEFAULTS_MAX_TOOL_ITERATIONS"` MaxToolIterations int `json:"max_tool_iterations" env:"PICOCLAW_AGENTS_DEFAULTS_MAX_TOOL_ITERATIONS"`
} }
@ -330,7 +330,6 @@ func DefaultConfig() *Config {
Provider: "", Provider: "",
Model: "glm-4.7", Model: "glm-4.7",
MaxTokens: 8192, MaxTokens: 8192,
Temperature: 0.7,
MaxToolIterations: 20, MaxToolIterations: 20,
}, },
}, },

View file

@ -237,8 +237,8 @@ func TestDefaultConfig_MaxToolIterations(t *testing.T) {
func TestDefaultConfig_Temperature(t *testing.T) { func TestDefaultConfig_Temperature(t *testing.T) {
cfg := DefaultConfig() cfg := DefaultConfig()
if cfg.Agents.Defaults.Temperature == 0 { if cfg.Agents.Defaults.Temperature != nil {
t.Error("Temperature should not be zero") t.Error("Temperature should be nil when not provided")
} }
} }
@ -334,8 +334,8 @@ func TestConfig_Complete(t *testing.T) {
if cfg.Agents.Defaults.Model == "" { if cfg.Agents.Defaults.Model == "" {
t.Error("Model should not be empty") t.Error("Model should not be empty")
} }
if cfg.Agents.Defaults.Temperature == 0 { if cfg.Agents.Defaults.Temperature != nil {
t.Error("Temperature should have default value") t.Error("Temperature should be nil when not provided")
} }
if cfg.Agents.Defaults.MaxTokens == 0 { if cfg.Agents.Defaults.MaxTokens == 0 {
t.Error("MaxTokens should not be zero") t.Error("MaxTokens should not be zero")

View file

@ -76,7 +76,7 @@ func ConvertConfig(data map[string]interface{}) (*config.Config, []string, error
cfg.Agents.Defaults.MaxTokens = int(v) cfg.Agents.Defaults.MaxTokens = int(v)
} }
if v, ok := getFloat(defaults, "temperature"); ok { if v, ok := getFloat(defaults, "temperature"); ok {
cfg.Agents.Defaults.Temperature = v cfg.Agents.Defaults.Temperature = &v
} }
if v, ok := getFloat(defaults, "max_tool_iterations"); ok { if v, ok := getFloat(defaults, "max_tool_iterations"); ok {
cfg.Agents.Defaults.MaxToolIterations = int(v) cfg.Agents.Defaults.MaxToolIterations = int(v)

View file

@ -275,8 +275,11 @@ func TestConvertConfig(t *testing.T) {
if cfg.Agents.Defaults.MaxTokens != 4096 { if cfg.Agents.Defaults.MaxTokens != 4096 {
t.Errorf("MaxTokens = %d, want %d", cfg.Agents.Defaults.MaxTokens, 4096) t.Errorf("MaxTokens = %d, want %d", cfg.Agents.Defaults.MaxTokens, 4096)
} }
if cfg.Agents.Defaults.Temperature != 0.5 { if cfg.Agents.Defaults.Temperature == nil {
t.Errorf("Temperature = %f, want %f", cfg.Agents.Defaults.Temperature, 0.5) t.Fatalf("Temperature is nil, want %f", 0.5)
}
if *cfg.Agents.Defaults.Temperature != 0.5 {
t.Errorf("Temperature = %f, want %f", *cfg.Agents.Defaults.Temperature, 0.5)
} }
if cfg.Agents.Defaults.Workspace != "~/.picoclaw/workspace" { if cfg.Agents.Defaults.Workspace != "~/.picoclaw/workspace" {
t.Errorf("Workspace = %q, want %q", cfg.Agents.Defaults.Workspace, "~/.picoclaw/workspace") t.Errorf("Workspace = %q, want %q", cfg.Agents.Defaults.Workspace, "~/.picoclaw/workspace")