@@ -164,8 +164,22 @@ class LanguageModel extends EventEmitter {
164
164
} else {
165
165
if ( typeof optionsOrCb === 'object' ) {
166
166
this . options . maxTokens = ( typeof optionsOrCb . maxTokens === 'number' ) ? optionsOrCb . maxTokens : this . options . maxTokens ;
167
- this . options . temperature = ( typeof optionsOrCb . temperature === 'number' ) ? optionsOrCb . temperature : this . options . temperature ;
168
- this . options . topp = ( typeof optionsOrCb . topp === 'number' ) ? optionsOrCb . topp : this . options . topp ;
167
+ if ( typeof optionsOrCb . temperature === 'number' ) {
168
+ this . options . temperature = optionsOrCb . temperature ;
169
+ // disable top-p unless explicitly provided
170
+ if ( typeof optionsOrCb . topp !== 'number' && this . options . topp != 0 ) {
171
+ this . options . topp = 0 ;
172
+ console . log ( 'Disabling top-p sampling' ) ;
173
+ }
174
+ }
175
+ if ( typeof optionsOrCb . topp === 'number' ) {
176
+ this . options . topp = optionsOrCb . topp ;
177
+ // set temperature to 1.0 unless explicity provided
178
+ if ( typeof optionsOrCb . temperature !== 'number' && this . options . temperature != 1 ) {
179
+ this . options . temperature = 1 ;
180
+ console . log ( 'Using temperature 1.0 for top-p sampling' ) ;
181
+ }
182
+ }
169
183
this . options . stopOnBosOrEos = ( typeof optionsOrCb . stopOnBosOrEos == 'boolean' ) ? optionsOrCb . stopPropagation : this . options . stopOnBosOrEos ;
170
184
}
171
185
if ( typeof cb === 'function' ) {
@@ -175,6 +189,13 @@ class LanguageModel extends EventEmitter {
175
189
}
176
190
}
177
191
192
+ // https://peterchng.com/blog/2023/05/02/token-selection-strategies-top-k-top-p-and-temperature/
193
+ // https://docs.cohere.com/docs/controlling-generation-with-top-k-top-p
194
+ // https://huggingface.co/blog/how-to-generate
195
+ if ( this . options . temperature != 1 && this . options . topp != 0 ) {
196
+ console . log ( 'Generating with temperature ' + this . options . temperature + ' and top-p ' + this . options . topp + '. Note: it\'s recommended to only tweak one, and leave the other at it\'s default value.' ) ;
197
+ }
198
+
178
199
// if there are any outstanding requests, resolve them
179
200
// with the output received so far
180
201
if ( this . promiseResolve ) {
@@ -221,8 +242,22 @@ class LanguageModel extends EventEmitter {
221
242
} else {
222
243
if ( typeof optionsOrCb === 'object' ) {
223
244
this . options . maxTokens = ( typeof optionsOrCb . maxTokens === 'number' ) ? optionsOrCb . maxTokens : this . options . maxTokens ;
224
- this . options . temperature = ( typeof optionsOrCb . temperature === 'number' ) ? optionsOrCb . temperature : this . options . temperature ;
225
- this . options . topp = ( typeof optionsOrCb . topp === 'number' ) ? optionsOrCb . topp : this . options . topp ;
245
+ if ( typeof optionsOrCb . temperature === 'number' ) {
246
+ this . options . temperature = optionsOrCb . temperature ;
247
+ // disable top-p unless explicitly provided
248
+ if ( typeof optionsOrCb . topp !== 'number' ) {
249
+ this . options . topp = 0 ;
250
+ console . log ( 'Disabling top-p sampling' ) ;
251
+ }
252
+ }
253
+ if ( typeof optionsOrCb . topp === 'number' ) {
254
+ this . options . topp = optionsOrCb . topp ;
255
+ // set temperature to 1.0 unless explicity provided
256
+ if ( typeof optionsOrCb . temperature !== 'number' ) {
257
+ this . options . temperature = 1 ;
258
+ console . log ( 'Using temperature 1.0 for top-p sampling' ) ;
259
+ }
260
+ }
226
261
this . options . stopOnBosOrEos = ( typeof optionsOrCb . stopOnBosOrEos == 'boolean' ) ? optionsOrCb . stopPropagation : this . options . stopOnBosOrEos ;
227
262
}
228
263
if ( typeof cb === 'function' ) {
@@ -232,6 +267,13 @@ class LanguageModel extends EventEmitter {
232
267
}
233
268
}
234
269
270
+ // https://peterchng.com/blog/2023/05/02/token-selection-strategies-top-k-top-p-and-temperature/
271
+ // https://docs.cohere.com/docs/controlling-generation-with-top-k-top-p
272
+ // https://huggingface.co/blog/how-to-generate
273
+ if ( this . options . temperature != 1 && this . options . topp != 0 ) {
274
+ console . log ( 'Generating with temperature ' + this . options . temperature + ' and top-p ' + this . options . topp + '. Note: it\'s recommended to only tweak one, and leave the other at it\'s default value.' ) ;
275
+ }
276
+
235
277
// if there are any outstanding requests, resolve them
236
278
// with the output received so far
237
279
if ( this . promiseResolve ) {
0 commit comments