openai: don't override model sampler defaults (#19432)
The OpenAI-compatible endpoints filled in sampler options the client left out: temperature and top_p fell back to 1.0, and /v1/completions always sent frequency_penalty and presence_penalty as 0. Request options are applied last, so these values replaced the model's packaged defaults. /api/chat honored those defaults; /v1 did not. Omitted options are now left unset on /v1/chat/completions, /v1/completions and /v1/responses, so the model's parameters apply, the same as on /api/chat. Values the client sends still win, including an explicit 0. On /v1/completions, top_p and the penalties are now pointers so "omitted" and "0" can be told apart. Clients that send no sampler options now get the model's defaults instead of 1.0/1.0. A model with no sampler parameters gets Ollama's defaults (temperature 0.8, top_p 0.9), as on /api/chat. Fixes #17744 Fixes #18690
This commit is contained in:
1 parent
f9323416f0
commit
c1db5f9ef2
5 files changed
+266
-109
No files matched your search
+88
-60
@@ -14,6 +14,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
"github.com/klauspost/compress/zstd"
|
||||
|
||||
"github.com/ollama/ollama/api"
|
||||
@@ -854,11 +855,8 @@ func TestChatMiddleware(t *testing.T) {
|
||||
Content: "Hello",
|
||||
},
|
||||
},
|
||||
Options: map[string]any{
|
||||
"temperature": 1.0,
|
||||
"top_p": 1.0,
|
||||
},
|
||||
Stream: &False,
|
||||
Options: map[string]any{},
|
||||
Stream: &False,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -977,11 +975,8 @@ func TestChatMiddleware(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Options: map[string]any{
|
||||
"temperature": 1.0,
|
||||
"top_p": 1.0,
|
||||
},
|
||||
Stream: &False,
|
||||
Options: map[string]any{},
|
||||
Stream: &False,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -1016,11 +1011,8 @@ func TestChatMiddleware(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Options: map[string]any{
|
||||
"temperature": 1.0,
|
||||
"top_p": 1.0,
|
||||
},
|
||||
Stream: &False,
|
||||
Options: map[string]any{},
|
||||
Stream: &False,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -1056,11 +1048,8 @@ func TestChatMiddleware(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Options: map[string]any{
|
||||
"temperature": 1.0,
|
||||
"top_p": 1.0,
|
||||
},
|
||||
Stream: &False,
|
||||
Options: map[string]any{},
|
||||
Stream: &False,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -1095,11 +1084,8 @@ func TestChatMiddleware(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Options: map[string]any{
|
||||
"temperature": 1.0,
|
||||
"top_p": 1.0,
|
||||
},
|
||||
Stream: &False,
|
||||
Options: map[string]any{},
|
||||
Stream: &False,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -1135,11 +1121,8 @@ func TestChatMiddleware(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Options: map[string]any{
|
||||
"temperature": 1.0,
|
||||
"top_p": 1.0,
|
||||
},
|
||||
Stream: &False,
|
||||
Options: map[string]any{},
|
||||
Stream: &False,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -1181,11 +1164,8 @@ func TestChatMiddleware(t *testing.T) {
|
||||
ToolCallID: "id_abc",
|
||||
},
|
||||
},
|
||||
Options: map[string]any{
|
||||
"temperature": 1.0,
|
||||
"top_p": 1.0,
|
||||
},
|
||||
Stream: &False,
|
||||
Options: map[string]any{},
|
||||
Stream: &False,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -1226,11 +1206,8 @@ func TestChatMiddleware(t *testing.T) {
|
||||
ToolName: "get_current_weather",
|
||||
},
|
||||
},
|
||||
Options: map[string]any{
|
||||
"temperature": 1.0,
|
||||
"top_p": 1.0,
|
||||
},
|
||||
Stream: &False,
|
||||
Options: map[string]any{},
|
||||
Stream: &False,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -1294,11 +1271,8 @@ func TestChatMiddleware(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Options: map[string]any{
|
||||
"temperature": 1.0,
|
||||
"top_p": 1.0,
|
||||
},
|
||||
Stream: &True,
|
||||
Options: map[string]any{},
|
||||
Stream: &True,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -1378,11 +1352,8 @@ func TestCompletionsMiddleware(t *testing.T) {
|
||||
Model: "test-model",
|
||||
Prompt: "Hello",
|
||||
Options: map[string]any{
|
||||
"frequency_penalty": 0.0,
|
||||
"presence_penalty": 0.0,
|
||||
"temperature": 0.8,
|
||||
"top_p": 1.0,
|
||||
"stop": []any{"\n", "stop"},
|
||||
"temperature": 0.8,
|
||||
"stop": []any{"\n", "stop"},
|
||||
},
|
||||
Suffix: "suffix",
|
||||
Stream: &False,
|
||||
@@ -1402,11 +1373,8 @@ func TestCompletionsMiddleware(t *testing.T) {
|
||||
Model: "test-model",
|
||||
Prompt: "Hello",
|
||||
Options: map[string]any{
|
||||
"frequency_penalty": 0.0,
|
||||
"presence_penalty": 0.0,
|
||||
"temperature": 0.8,
|
||||
"top_p": 1.0,
|
||||
"stop": []any{"\n", "stop"},
|
||||
"temperature": 0.8,
|
||||
"stop": []any{"\n", "stop"},
|
||||
},
|
||||
Suffix: "suffix",
|
||||
Stream: &True,
|
||||
@@ -1427,11 +1395,8 @@ func TestCompletionsMiddleware(t *testing.T) {
|
||||
Model: "test-model",
|
||||
Prompt: "Hello",
|
||||
Options: map[string]any{
|
||||
"frequency_penalty": 0.0,
|
||||
"presence_penalty": 0.0,
|
||||
"temperature": 0.8,
|
||||
"top_p": 1.0,
|
||||
"stop": []any{"\n", "stop"},
|
||||
"temperature": 0.8,
|
||||
"stop": []any{"\n", "stop"},
|
||||
},
|
||||
Suffix: "suffix",
|
||||
Stream: &True,
|
||||
@@ -1492,6 +1457,69 @@ func TestCompletionsMiddleware(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSamplerOptionsPassThrough(t *testing.T) {
|
||||
endpoints := []struct {
|
||||
name string
|
||||
middleware gin.HandlerFunc
|
||||
base string
|
||||
penalties bool
|
||||
}{
|
||||
{"chat", ChatMiddleware(), `"model":"m","messages":[{"role":"user","content":"hi"}]`, true},
|
||||
{"completions", CompletionsMiddleware(), `"model":"m","prompt":"hi"`, true},
|
||||
{"responses", ResponsesMiddleware(), `"model":"m","input":"hi"`, false},
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
body string
|
||||
penalties bool
|
||||
want map[string]any
|
||||
}{
|
||||
{name: "omitted", want: map[string]any{}},
|
||||
{name: "explicit", body: `,"temperature":0.3,"top_p":0.7`, want: map[string]any{"temperature": 0.3, "top_p": 0.7}},
|
||||
{name: "explicit zero", body: `,"temperature":0,"top_p":0`, want: map[string]any{"temperature": 0.0, "top_p": 0.0}},
|
||||
{name: "explicit zero penalties", body: `,"frequency_penalty":0,"presence_penalty":0`, penalties: true, want: map[string]any{"frequency_penalty": 0.0, "presence_penalty": 0.0}},
|
||||
}
|
||||
|
||||
for _, ep := range endpoints {
|
||||
for _, tc := range cases {
|
||||
if tc.penalties && !ep.penalties {
|
||||
continue
|
||||
}
|
||||
t.Run(ep.name+"/"+tc.name, func(t *testing.T) {
|
||||
var captured struct {
|
||||
Options map[string]any `json:"options"`
|
||||
}
|
||||
|
||||
gin.SetMode(gin.TestMode)
|
||||
router := gin.New()
|
||||
router.Use(ep.middleware, captureRequestMiddleware(&captured))
|
||||
router.Handle(http.MethodPost, "/", func(c *gin.Context) {
|
||||
c.Status(http.StatusOK)
|
||||
})
|
||||
|
||||
req, _ := http.NewRequest(http.MethodPost, "/", strings.NewReader("{"+ep.base+tc.body+"}"))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp := httptest.NewRecorder()
|
||||
router.ServeHTTP(resp, req)
|
||||
if resp.Code != http.StatusOK {
|
||||
t.Fatalf("status %d: %s", resp.Code, resp.Body.String())
|
||||
}
|
||||
|
||||
got := map[string]any{}
|
||||
for _, key := range []string{"temperature", "top_p", "frequency_penalty", "presence_penalty"} {
|
||||
if v, ok := captured.Options[key]; ok {
|
||||
got[key] = v
|
||||
}
|
||||
}
|
||||
if diff := cmp.Diff(tc.want, got, cmpopts.EquateApprox(0, 1e-6)); diff != "" {
|
||||
t.Errorf("sampler options mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmbeddingsMiddleware(t *testing.T) {
|
||||
type testCase struct {
|
||||
name string
|
||||
|
||||
+11
-15
@@ -170,15 +170,15 @@ type ChatCompletionChunk struct {
|
||||
type CompletionRequest struct {
|
||||
Model string `json:"model"`
|
||||
Prompt string `json:"prompt"`
|
||||
FrequencyPenalty float32 `json:"frequency_penalty"`
|
||||
FrequencyPenalty *float32 `json:"frequency_penalty"`
|
||||
MaxTokens *int `json:"max_tokens"`
|
||||
PresencePenalty float32 `json:"presence_penalty"`
|
||||
PresencePenalty *float32 `json:"presence_penalty"`
|
||||
Seed *int `json:"seed"`
|
||||
Stop any `json:"stop"`
|
||||
Stream bool `json:"stream"`
|
||||
StreamOptions *StreamOptions `json:"stream_options"`
|
||||
Temperature *float32 `json:"temperature"`
|
||||
TopP float32 `json:"top_p"`
|
||||
TopP *float32 `json:"top_p"`
|
||||
Suffix string `json:"suffix"`
|
||||
Logprobs *int `json:"logprobs"`
|
||||
DebugRenderOnly bool `json:"_debug_render_only"`
|
||||
@@ -754,8 +754,6 @@ func FromChatRequest(r ChatCompletionRequest, thinking ...*model.Thinking) (*api
|
||||
|
||||
if r.Temperature != nil {
|
||||
options["temperature"] = *r.Temperature
|
||||
} else {
|
||||
options["temperature"] = 1.0
|
||||
}
|
||||
|
||||
if r.Seed != nil {
|
||||
@@ -772,8 +770,6 @@ func FromChatRequest(r ChatCompletionRequest, thinking ...*model.Thinking) (*api
|
||||
|
||||
if r.TopP != nil {
|
||||
options["top_p"] = *r.TopP
|
||||
} else {
|
||||
options["top_p"] = 1.0
|
||||
}
|
||||
|
||||
var format json.RawMessage
|
||||
@@ -904,22 +900,22 @@ func FromCompleteRequest(r CompletionRequest) (api.GenerateRequest, error) {
|
||||
|
||||
if r.Temperature != nil {
|
||||
options["temperature"] = *r.Temperature
|
||||
} else {
|
||||
options["temperature"] = 1.0
|
||||
}
|
||||
|
||||
if r.Seed != nil {
|
||||
options["seed"] = *r.Seed
|
||||
}
|
||||
|
||||
options["frequency_penalty"] = r.FrequencyPenalty
|
||||
if r.FrequencyPenalty != nil {
|
||||
options["frequency_penalty"] = *r.FrequencyPenalty
|
||||
}
|
||||
|
||||
options["presence_penalty"] = r.PresencePenalty
|
||||
if r.PresencePenalty != nil {
|
||||
options["presence_penalty"] = *r.PresencePenalty
|
||||
}
|
||||
|
||||
if r.TopP != 0.0 {
|
||||
options["top_p"] = r.TopP
|
||||
} else {
|
||||
options["top_p"] = 1.0
|
||||
if r.TopP != nil {
|
||||
options["top_p"] = *r.TopP
|
||||
}
|
||||
|
||||
var logprobs bool
|
||||
|
||||
@@ -322,6 +322,120 @@ func TestFromCompleteRequest_Basic(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Omitted sampler fields must stay unset so model defaults apply.
|
||||
func TestSamplerOptionsOmittedVsExplicit(t *testing.T) {
|
||||
samplerKeys := []string{"temperature", "top_p", "frequency_penalty", "presence_penalty"}
|
||||
|
||||
convert := map[string]func(t *testing.T, body string) map[string]any{
|
||||
"chat": func(t *testing.T, body string) map[string]any {
|
||||
var r ChatCompletionRequest
|
||||
if err := json.Unmarshal([]byte(`{"model":"m","messages":[{"role":"user","content":"hi"}]`+body+`}`), &r); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out, err := FromChatRequest(r)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return out.Options
|
||||
},
|
||||
"completions": func(t *testing.T, body string) map[string]any {
|
||||
var r CompletionRequest
|
||||
if err := json.Unmarshal([]byte(`{"model":"m","prompt":"hi"`+body+`}`), &r); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out, err := FromCompleteRequest(r)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return out.Options
|
||||
},
|
||||
"responses": func(t *testing.T, body string) map[string]any {
|
||||
var r ResponsesRequest
|
||||
if err := json.Unmarshal([]byte(`{"model":"m","input":"hi"`+body+`}`), &r); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out, err := FromResponsesRequest(r)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return out.Options
|
||||
},
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
endpoints []string
|
||||
body string
|
||||
want map[string]float64
|
||||
}{
|
||||
{
|
||||
name: "omitted",
|
||||
endpoints: []string{"chat", "completions", "responses"},
|
||||
want: map[string]float64{},
|
||||
},
|
||||
{
|
||||
name: "null",
|
||||
endpoints: []string{"chat", "completions", "responses"},
|
||||
body: `,"temperature":null,"top_p":null`,
|
||||
want: map[string]float64{},
|
||||
},
|
||||
{
|
||||
name: "explicit temperature and top_p",
|
||||
endpoints: []string{"chat", "completions", "responses"},
|
||||
body: `,"temperature":0.3,"top_p":0.7`,
|
||||
want: map[string]float64{"temperature": 0.3, "top_p": 0.7},
|
||||
},
|
||||
{
|
||||
name: "explicit zero temperature and top_p",
|
||||
endpoints: []string{"chat", "completions", "responses"},
|
||||
body: `,"temperature":0,"top_p":0`,
|
||||
want: map[string]float64{"temperature": 0, "top_p": 0},
|
||||
},
|
||||
{
|
||||
name: "explicit penalties",
|
||||
endpoints: []string{"chat", "completions"},
|
||||
body: `,"frequency_penalty":0.25,"presence_penalty":0.5`,
|
||||
want: map[string]float64{"frequency_penalty": 0.25, "presence_penalty": 0.5},
|
||||
},
|
||||
{
|
||||
name: "explicit zero penalties",
|
||||
endpoints: []string{"chat", "completions"},
|
||||
body: `,"frequency_penalty":0,"presence_penalty":0`,
|
||||
want: map[string]float64{"frequency_penalty": 0, "presence_penalty": 0},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
for _, ep := range tc.endpoints {
|
||||
t.Run(ep+"/"+tc.name, func(t *testing.T) {
|
||||
opts := convert[ep](t, tc.body)
|
||||
for _, key := range samplerKeys {
|
||||
got, present := opts[key]
|
||||
want, wantPresent := tc.want[key]
|
||||
if present != wantPresent {
|
||||
t.Fatalf("%s present = %v, want %v (options %v)", key, present, wantPresent, opts)
|
||||
}
|
||||
if !present {
|
||||
continue
|
||||
}
|
||||
var gotF float64
|
||||
switch v := got.(type) {
|
||||
case float32:
|
||||
gotF = float64(v)
|
||||
case float64:
|
||||
gotF = v
|
||||
default:
|
||||
t.Fatalf("%s has type %T", key, got)
|
||||
}
|
||||
if math.Abs(gotF-want) > 1e-6 {
|
||||
t.Errorf("%s = %v, want %v", key, gotF, want)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestToUsage(t *testing.T) {
|
||||
resp := api.ChatResponse{
|
||||
Metrics: api.Metrics{
|
||||
|
||||
+14
-34
@@ -851,18 +851,10 @@ func FromResponsesRequest(r ResponsesRequest, thinking ...*model.Thinking) (*api
|
||||
|
||||
if r.Temperature != nil {
|
||||
options["temperature"] = *r.Temperature
|
||||
} else {
|
||||
options["temperature"] = 1.0
|
||||
}
|
||||
|
||||
if r.TopP != nil {
|
||||
options["top_p"] = *r.TopP
|
||||
} else { //nolint:staticcheck // SA9003: empty branch
|
||||
// TODO(drifkin): OpenAI defaults to 1.0 here, but we don't follow that here
|
||||
// in case the model has a different default. It would be best if we
|
||||
// understood whether there was a model-specific default and if not, we
|
||||
// should also default to 1.0, but that will require some additional
|
||||
// plumbing
|
||||
}
|
||||
|
||||
if r.MaxOutputTokens != nil {
|
||||
@@ -1236,6 +1228,8 @@ type ResponsesIncompleteDetails struct {
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
// ResponsesResponse is a Responses API response. TopP and Temperature are nil
|
||||
// (null) when the request left them to the model's defaults.
|
||||
type ResponsesResponse struct {
|
||||
ID string `json:"id"`
|
||||
Object string `json:"object"`
|
||||
@@ -1253,11 +1247,11 @@ type ResponsesResponse struct {
|
||||
Truncation string `json:"truncation"`
|
||||
ParallelToolCalls bool `json:"parallel_tool_calls"`
|
||||
Text ResponsesTextField `json:"text"`
|
||||
TopP float64 `json:"top_p"`
|
||||
TopP *float64 `json:"top_p"`
|
||||
PresencePenalty float64 `json:"presence_penalty"`
|
||||
FrequencyPenalty float64 `json:"frequency_penalty"`
|
||||
TopLogprobs int `json:"top_logprobs"`
|
||||
Temperature float64 `json:"temperature"`
|
||||
Temperature *float64 `json:"temperature"`
|
||||
Reasoning *ResponsesReasoningOutput `json:"reasoning"`
|
||||
Usage *ResponsesUsage `json:"usage"`
|
||||
MaxOutputTokens *int `json:"max_output_tokens"`
|
||||
@@ -1351,16 +1345,10 @@ func intValue(v *int) int {
|
||||
return *v
|
||||
}
|
||||
|
||||
// derefFloat64 returns the value of a float64 pointer, or a default if nil.
|
||||
func derefFloat64(p *float64, def float64) float64 {
|
||||
if p != nil {
|
||||
return *p
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
// ToResponse converts an api.ChatResponse to a Responses API response.
|
||||
// The request is used to echo back request parameters in the response.
|
||||
// The request is used to echo back request parameters in the response; a
|
||||
// caller that knows the model's resolved sampling defaults sets them on the
|
||||
// request so the response reports what ran.
|
||||
func ToResponse(model, responseID, itemID string, chatResponse api.ChatResponse, request ResponsesRequest) ResponsesResponse {
|
||||
var output []ResponsesOutputItem
|
||||
|
||||
@@ -1474,11 +1462,11 @@ func ToResponse(model, responseID, itemID string, chatResponse api.ChatResponse,
|
||||
Truncation: truncation,
|
||||
ParallelToolCalls: true, // Default value
|
||||
Text: text,
|
||||
TopP: derefFloat64(request.TopP, 1.0),
|
||||
TopP: request.TopP,
|
||||
PresencePenalty: 0, // Default value
|
||||
FrequencyPenalty: 0, // Default value
|
||||
TopLogprobs: 0, // Default value
|
||||
Temperature: derefFloat64(request.Temperature, 1.0),
|
||||
Temperature: request.Temperature,
|
||||
Reasoning: reasoning,
|
||||
Usage: &ResponsesUsage{
|
||||
InputTokens: chatResponse.PromptEvalCount,
|
||||
@@ -1558,7 +1546,9 @@ func (c *ResponsesStreamConverter) newEvent(eventType string, data map[string]an
|
||||
}
|
||||
}
|
||||
|
||||
// NewResponsesStreamConverter creates a new converter with the given configuration.
|
||||
// NewResponsesStreamConverter creates a new converter with the given
|
||||
// configuration. As with ToResponse, the request's sampling settings are
|
||||
// reported, null when left to the model's defaults.
|
||||
func NewResponsesStreamConverter(responseID, itemID, model string, request ResponsesRequest) *ResponsesStreamConverter {
|
||||
return &ResponsesStreamConverter{
|
||||
responseID: responseID,
|
||||
@@ -1662,16 +1652,6 @@ func (c *ResponsesStreamConverter) buildResponseObject(status string, output []a
|
||||
reasoning = r
|
||||
}
|
||||
|
||||
// Build top_p and temperature with defaults
|
||||
topP := 1.0
|
||||
if c.request.TopP != nil {
|
||||
topP = *c.request.TopP
|
||||
}
|
||||
temperature := 1.0
|
||||
if c.request.Temperature != nil {
|
||||
temperature = *c.request.Temperature
|
||||
}
|
||||
|
||||
return map[string]any{
|
||||
"id": c.responseID,
|
||||
"object": "response",
|
||||
@@ -1689,11 +1669,11 @@ func (c *ResponsesStreamConverter) buildResponseObject(status string, output []a
|
||||
"truncation": truncation,
|
||||
"parallel_tool_calls": true,
|
||||
"text": map[string]any{"format": textFormat},
|
||||
"top_p": topP,
|
||||
"top_p": c.request.TopP,
|
||||
"presence_penalty": 0,
|
||||
"frequency_penalty": 0,
|
||||
"top_logprobs": 0,
|
||||
"temperature": temperature,
|
||||
"temperature": c.request.Temperature,
|
||||
"reasoning": reasoning,
|
||||
"usage": usage,
|
||||
"max_output_tokens": c.request.MaxOutputTokens,
|
||||
|
||||
@@ -3343,3 +3343,42 @@ func TestFromResponsesRequestAcceptsEncryptedContentPart(t *testing.T) {
|
||||
t.Fatalf("messages = %#v", chat.Messages)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponsesReportRequestedSampling(t *testing.T) {
|
||||
value := func(v float64) *float64 { return &v }
|
||||
tests := []struct {
|
||||
name string
|
||||
temperature, topP *float64
|
||||
want map[string]any
|
||||
}{
|
||||
// The model's defaults applied; the response must not claim others.
|
||||
{"omitted", nil, nil, map[string]any{"temperature": nil, "top_p": nil}},
|
||||
{"explicit", value(0.3), value(0.7), map[string]any{"temperature": 0.3, "top_p": 0.7}},
|
||||
{"explicit zero", value(0), value(0), map[string]any{"temperature": 0.0, "top_p": 0.0}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
request := ResponsesRequest{Model: "m", Temperature: tt.temperature, TopP: tt.topP}
|
||||
chat := api.ChatResponse{Message: api.Message{Content: "hi"}, Done: true}
|
||||
created := NewResponsesStreamConverter("resp_1", "item_1", "m", request).Process(chat)[0]
|
||||
for builder, response := range map[string]any{
|
||||
"ToResponse": ToResponse("m", "resp_1", "item_1", chat, request),
|
||||
"stream": created.Data.(map[string]any)["response"],
|
||||
} {
|
||||
data, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var fields map[string]any
|
||||
if err := json.Unmarshal(data, &fields); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for key, want := range tt.want {
|
||||
if got, ok := fields[key]; !ok || got != want {
|
||||
t.Errorf("%s %s = %v (present %v), want %v", builder, key, got, ok, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user