|
| 1 | +package openai |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/vxcontrol/langchaingo/llms" |
| 12 | +) |
| 13 | + |
| 14 | +// Guards the GetEffort clamp removal at the wire level: xhigh/max must reach the |
| 15 | +// request body, not be downgraded to high (legacy reasoning_effort and modern reasoning.effort). |
| 16 | +func TestReasoningEffortPassthroughToWire(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + const completion = `{"id":"x","object":"chat.completion","created":1,"model":"m",` + |
| 20 | + `"choices":[{"index":0,"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],` + |
| 21 | + `"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}` |
| 22 | + |
| 23 | + cases := []struct { |
| 24 | + name string |
| 25 | + effort llms.ReasoningEffort |
| 26 | + modern bool |
| 27 | + want string |
| 28 | + }{ |
| 29 | + {"legacy xhigh", llms.ReasoningXHigh, false, `"reasoning_effort":"xhigh"`}, |
| 30 | + {"legacy max", llms.ReasoningMax, false, `"reasoning_effort":"max"`}, |
| 31 | + {"modern xhigh", llms.ReasoningXHigh, true, `"effort":"xhigh"`}, |
| 32 | + {"modern max", llms.ReasoningMax, true, `"effort":"max"`}, |
| 33 | + } |
| 34 | + |
| 35 | + for _, tc := range cases { |
| 36 | + t.Run(tc.name, func(t *testing.T) { |
| 37 | + var body string |
| 38 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 39 | + b, _ := io.ReadAll(r.Body) |
| 40 | + body = string(b) |
| 41 | + w.Header().Set("Content-Type", "application/json") |
| 42 | + _, _ = io.WriteString(w, completion) |
| 43 | + })) |
| 44 | + defer srv.Close() |
| 45 | + |
| 46 | + opts := []Option{WithBaseURL(srv.URL), WithToken("test"), WithModel("reasoning-model")} |
| 47 | + if tc.modern { |
| 48 | + opts = append(opts, WithModernReasoningFormat()) |
| 49 | + } |
| 50 | + llm, err := New(opts...) |
| 51 | + if err != nil { |
| 52 | + t.Fatalf("New() error: %v", err) |
| 53 | + } |
| 54 | + |
| 55 | + if _, err := llm.GenerateContent( |
| 56 | + context.Background(), |
| 57 | + []llms.MessageContent{llms.TextParts(llms.ChatMessageTypeHuman, "hi")}, |
| 58 | + llms.WithReasoning(tc.effort, 0), |
| 59 | + ); err != nil { |
| 60 | + t.Fatalf("GenerateContent() error: %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + if !strings.Contains(body, tc.want) { |
| 64 | + t.Fatalf("wire body missing %s\nbody: %s", tc.want, body) |
| 65 | + } |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
0 commit comments