Skip to content

Commit 1920026

Browse files
committed
LanguageModel: Better default options when user passes temperature or topp
This automatically picks reasonable default value for the other parameter, if not explicity specified, and prints a message if both are.
1 parent 8743705 commit 1920026

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

src/LanguageModel/index.js

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,22 @@ class LanguageModel extends EventEmitter {
164164
} else {
165165
if (typeof optionsOrCb === 'object') {
166166
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+
}
169183
this.options.stopOnBosOrEos = (typeof optionsOrCb.stopOnBosOrEos == 'boolean') ? optionsOrCb.stopPropagation : this.options.stopOnBosOrEos;
170184
}
171185
if (typeof cb === 'function') {
@@ -175,6 +189,13 @@ class LanguageModel extends EventEmitter {
175189
}
176190
}
177191

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+
178199
// if there are any outstanding requests, resolve them
179200
// with the output received so far
180201
if (this.promiseResolve) {
@@ -221,8 +242,22 @@ class LanguageModel extends EventEmitter {
221242
} else {
222243
if (typeof optionsOrCb === 'object') {
223244
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+
}
226261
this.options.stopOnBosOrEos = (typeof optionsOrCb.stopOnBosOrEos == 'boolean') ? optionsOrCb.stopPropagation : this.options.stopOnBosOrEos;
227262
}
228263
if (typeof cb === 'function') {
@@ -232,6 +267,13 @@ class LanguageModel extends EventEmitter {
232267
}
233268
}
234269

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+
235277
// if there are any outstanding requests, resolve them
236278
// with the output received so far
237279
if (this.promiseResolve) {

0 commit comments

Comments
 (0)