@@ -232,16 +232,21 @@ type ChatCompletionRequest struct {
232
232
MaxTokens int `json:"max_tokens,omitempty"`
233
233
// MaxCompletionTokens An upper bound for the number of tokens that can be generated for a completion,
234
234
// including visible output tokens and reasoning tokens https://platform.openai.com/docs/guides/reasoning
235
- MaxCompletionTokens int `json:"max_completion_tokens,omitempty"`
236
- Temperature float32 `json:"temperature,omitempty"`
237
- TopP float32 `json:"top_p,omitempty"`
238
- N int `json:"n,omitempty"`
239
- Stream bool `json:"stream,omitempty"`
240
- Stop []string `json:"stop,omitempty"`
241
- PresencePenalty float32 `json:"presence_penalty,omitempty"`
242
- ResponseFormat * ChatCompletionResponseFormat `json:"response_format,omitempty"`
243
- Seed * int `json:"seed,omitempty"`
244
- FrequencyPenalty float32 `json:"frequency_penalty,omitempty"`
235
+ MaxCompletionTokens int `json:"max_completion_tokens,omitempty"`
236
+
237
+ // Deprecated: Use TemperatureOpt instead. When TemperatureOpt is set, Temperature is ignored
238
+ // regardless of its value. Otherwise (if TemperatureOpt is nil), Temperature is used when
239
+ // non-zero.
240
+ Temperature float32 `json:"-"`
241
+ TemperatureOpt * float32 `json:"temperature,omitempty"`
242
+ TopP float32 `json:"top_p,omitempty"`
243
+ N int `json:"n,omitempty"`
244
+ Stream bool `json:"stream,omitempty"`
245
+ Stop []string `json:"stop,omitempty"`
246
+ PresencePenalty float32 `json:"presence_penalty,omitempty"`
247
+ ResponseFormat * ChatCompletionResponseFormat `json:"response_format,omitempty"`
248
+ Seed * int `json:"seed,omitempty"`
249
+ FrequencyPenalty float32 `json:"frequency_penalty,omitempty"`
245
250
// LogitBias is must be a token id string (specified by their token ID in the tokenizer), not a word string.
246
251
// incorrect: `"logit_bias":{"You": 6}`, correct: `"logit_bias":{"1639": 6}`
247
252
// refs: https://platform.openai.com/docs/api-reference/chat/create#chat/create-logit_bias
@@ -277,6 +282,42 @@ type ChatCompletionRequest struct {
277
282
Prediction * Prediction `json:"prediction,omitempty"`
278
283
}
279
284
285
+ func (r * ChatCompletionRequest ) UnmarshalJSON (data []byte ) error {
286
+ type plainChatCompletionRequest ChatCompletionRequest
287
+ if err := json .Unmarshal (data , (* plainChatCompletionRequest )(r )); err != nil {
288
+ return err
289
+ }
290
+ 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
298
+ }
299
+ return nil
300
+ }
301
+
302
+ func (r ChatCompletionRequest ) MarshalJSON () ([]byte , error ) {
303
+ type plainChatCompletionRequest ChatCompletionRequest
304
+ plainR := plainChatCompletionRequest (r )
305
+ if plainR .TemperatureOpt == nil && plainR .Temperature != 0 {
306
+ plainR .TemperatureOpt = & plainR .Temperature
307
+ }
308
+ return json .Marshal (& plainR )
309
+ }
310
+
311
+ func (r * ChatCompletionRequest ) GetTemperature () * float32 {
312
+ if r .TemperatureOpt != nil {
313
+ return r .TemperatureOpt
314
+ }
315
+ if r .Temperature != 0 {
316
+ return & r .Temperature
317
+ }
318
+ return nil
319
+ }
320
+
280
321
type StreamOptions struct {
281
322
// If set, an additional chunk will be streamed before the data: [DONE] message.
282
323
// The usage field on this chunk shows the token usage statistics for the entire request,
0 commit comments