@@ -138,8 +138,10 @@ export interface AnthropicInput {
138
138
* from 0 to 1. Use temp closer to 0 for analytical /
139
139
* multiple choice, and temp closer to 1 for creative
140
140
* and generative tasks.
141
+ * To not set this field, pass `null`. If `undefined` is passed,
142
+ * the default (1) will be used.
141
143
*/
142
- temperature ?: number ;
144
+ temperature ?: number | null ;
143
145
144
146
/** Only sample from the top K options for each subsequent
145
147
* token. Used to remove "long tail" low probability
@@ -154,8 +156,13 @@ export interface AnthropicInput {
154
156
* specified by top_p. Defaults to -1, which disables it.
155
157
* Note that you should either alter temperature or top_p,
156
158
* but not both.
159
+ *
160
+ * To not set this field, pass `null`. If `undefined` is passed,
161
+ * the default (-1) will be used.
162
+ *
163
+ * For Opus 4.1, this defaults to `null`.
157
164
*/
158
- topP ?: number ;
165
+ topP ?: number | null ;
159
166
160
167
/** A maximum number of tokens to generate before stopping. */
161
168
maxTokens ?: number ;
@@ -652,11 +659,11 @@ export class ChatAnthropicMessages<
652
659
653
660
apiUrl ?: string ;
654
661
655
- temperature = 1 ;
662
+ temperature : number | undefined = 1 ;
656
663
657
664
topK = - 1 ;
658
665
659
- topP = - 1 ;
666
+ topP : number | undefined = - 1 ;
660
667
661
668
maxTokens = 2048 ;
662
669
@@ -713,9 +720,20 @@ export class ChatAnthropicMessages<
713
720
714
721
this . invocationKwargs = fields ?. invocationKwargs ?? { } ;
715
722
716
- this . temperature = fields ?. temperature ?? this . temperature ;
723
+ if ( this . model . includes ( "opus-4-1" ) ) {
724
+ // Default to `undefined` for `topP` for Opus 4.1 models
725
+ this . topP = fields ?. topP === null ? undefined : fields ?. topP ;
726
+ } else {
727
+ this . topP = fields ?. topP ?? this . topP ;
728
+ }
729
+
730
+ // If the user passes `null`, set it to `undefined`. Otherwise, use their value or the default. We have to check for null, because
731
+ // there's no way for us to know if they explicitly set it to `undefined`, or never passed a value
732
+ this . temperature =
733
+ fields ?. temperature === null
734
+ ? undefined
735
+ : fields ?. temperature ?? this . temperature ;
717
736
this . topK = fields ?. topK ?? this . topK ;
718
- this . topP = fields ?. topP ?? this . topP ;
719
737
this . maxTokens =
720
738
fields ?. maxTokensToSample ?? fields ?. maxTokens ?? this . maxTokens ;
721
739
this . stopSequences = fields ?. stopSequences ?? this . stopSequences ;
0 commit comments