test(llm): a temperature option; zero is sent as zero, not 'unset' (red)
The reconciler was measured at temperature 0 but the client hardcodes 0.2. Zero must be sent explicitly, and the default must not change for existing callers. Red: WithTemperature is undefined. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// capturedTemperature runs one Complete against a fake gateway and returns the
|
||||
// "temperature" field the request carried, and whether it was present at all.
|
||||
func capturedTemperature(t *testing.T, opts ...Option) (float64, bool) {
|
||||
t.Helper()
|
||||
var body map[string]any
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
raw, _ := io.ReadAll(r.Body)
|
||||
_ = json.Unmarshal(raw, &body)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"choices": []map[string]any{{"message": map[string]any{"role": "assistant", "content": "ok"}}}})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
if _, err := New(srv.URL, "", "m", 5*time.Second, opts...).Complete(context.Background(), "s", "u"); err != nil {
|
||||
t.Fatalf("Complete: %v", err)
|
||||
}
|
||||
v, ok := body["temperature"].(float64)
|
||||
return v, ok
|
||||
}
|
||||
|
||||
func TestClient_DefaultTemperatureIsUnchanged(t *testing.T) {
|
||||
got, ok := capturedTemperature(t)
|
||||
|
||||
if !ok || got != 0.2 {
|
||||
t.Fatalf("default temperature = %v (present %v), want 0.2: existing callers must not change behaviour", got, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_WithTemperatureZeroSendsExactlyZero(t *testing.T) {
|
||||
// The reconciler's accuracy (92%/97%) was measured at temperature 0. Zero must be
|
||||
// SENT, not treated as "unset" and replaced by the default.
|
||||
got, ok := capturedTemperature(t, WithTemperature(0))
|
||||
|
||||
if !ok || got != 0 {
|
||||
t.Fatalf("temperature = %v (present %v), want an explicit 0", got, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_WithTemperatureSendsTheGivenValue(t *testing.T) {
|
||||
got, ok := capturedTemperature(t, WithTemperature(0.7))
|
||||
|
||||
if !ok || got != 0.7 {
|
||||
t.Fatalf("temperature = %v (present %v), want 0.7", got, ok)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user