Skip to content

Commit 1fd2c83

Browse files
misbernermalte-prophet
authored andcommitted
add more test cases
1 parent 69b1536 commit 1fd2c83

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

chat.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,23 @@ func (r *ChatCompletionRequest) UnmarshalJSON(data []byte) error {
288288
return err
289289
}
290290
if r.TemperatureOpt != nil {
291-
r.Temperature = *r.TemperatureOpt
292-
// Link TemperatureOpt to temperature. This ensures that code modifying temperature
293-
// after unmarshaling (i.e., when TemperatureOpt might be set) will continue to
294-
// work correctly.
295-
r.TemperatureOpt = &r.Temperature
296-
} else if r.Temperature != 0 {
297-
r.TemperatureOpt = &r.Temperature
291+
if *r.TemperatureOpt == 0 {
292+
// Explicit zero. This can only be represented in the TemperatureOpt field, so
293+
// we need to preserve it.
294+
// We still link r.TemperatureOpt to r.Temperature, such that legacy code modifying
295+
// temperature after unmarshaling will continue to work correctly.
296+
r.Temperature = 0
297+
r.TemperatureOpt = &r.Temperature
298+
} else {
299+
// Non-zero temperature. This can be represented in the legacy field, and in order
300+
// to minimize incompatibilities, we use the legacy field exclusively.
301+
// New code should use `GetTemperature()` to retrieve the temperature, and explicitly
302+
// setting TemperatureOpt will still be respected.
303+
r.Temperature = *r.TemperatureOpt
304+
r.TemperatureOpt = nil
305+
}
306+
} else {
307+
r.Temperature = 0
298308
}
299309
return nil
300310
}

chat_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,13 @@ func TestTemperature(t *testing.T) {
997997
},
998998
expectedTemperature: &[]float32{0.5}[0],
999999
},
1000+
{
1001+
name: "set_new_explicit_zero",
1002+
in: openai.ChatCompletionRequest{
1003+
TemperatureOpt: &[]float32{0}[0],
1004+
},
1005+
expectedTemperature: &[]float32{0}[0],
1006+
},
10001007
}
10011008

10021009
for _, tt := range tests {

0 commit comments

Comments
 (0)