Merge pull request #2842 from SillyTavern/o1 OpenAI O1

5dd1d26350b3a6d98f5f53e52150d2e8c5090820

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
7 files changed, +55 -46Showing whitespace changes
package-lock.json+5 -4
@@ -46,7 +46,7 @@
46 "sanitize-filename": "^1.6.3",46 "sanitize-filename": "^1.6.3",
47 "sillytavern-transformers": "2.14.6",47 "sillytavern-transformers": "2.14.6",
48 "simple-git": "^3.19.1",48 "simple-git": "^3.19.1",
49 "tiktoken": "^1.0.15",49 "tiktoken": "^1.0.16",
50 "vectra": "^0.2.2",50 "vectra": "^0.2.2",
51 "wavefile": "^11.0.0",51 "wavefile": "^11.0.0",
52 "write-file-atomic": "^5.0.1",52 "write-file-atomic": "^5.0.1",
@@ -5751,9 +5751,10 @@
5751 "license": "MIT"5751 "license": "MIT"
5752 },5752 },
5753 "node_modules/tiktoken": {5753 "node_modules/tiktoken": {
5754 "version": "1.0.15",5754 "version": "1.0.16",
5755 "resolved": "https://registry.npmjs.org/tiktoken/-/tiktoken-1.0.15.tgz",5755 "resolved": "https://registry.npmjs.org/tiktoken/-/tiktoken-1.0.16.tgz",
5756 "integrity": "sha512-sCsrq/vMWUSEW29CJLNmPvWxlVp7yh2tlkAjpJltIKqp5CKf98ZNpdeHRmAlPVFlGEbswDc6SmI8vz64W/qErw=="5756 "integrity": "sha512-hRcORIGF2YlAgWx3nzrGJOrKSJwLoc81HpXmMQk89632XAgURc7IeV2FgQ2iXo9z/J96fCvpsHg2kWoHcbj9fg==",
5757 "license": "MIT"
5757 },5758 },
5758 "node_modules/timm": {5759 "node_modules/timm": {
5759 "version": "1.7.1",5760 "version": "1.7.1",
package.json+1 -1
@@ -36,7 +36,7 @@
36 "sanitize-filename": "^1.6.3",36 "sanitize-filename": "^1.6.3",
37 "sillytavern-transformers": "2.14.6",37 "sillytavern-transformers": "2.14.6",
38 "simple-git": "^3.19.1",38 "simple-git": "^3.19.1",
39 "tiktoken": "^1.0.15",39 "tiktoken": "^1.0.16",
40 "vectra": "^0.2.2",40 "vectra": "^0.2.2",
41 "wavefile": "^11.0.0",41 "wavefile": "^11.0.0",
42 "write-file-atomic": "^5.0.1",42 "write-file-atomic": "^5.0.1",
public/index.html+5 -1
@@ -383,7 +383,7 @@
383 Max Response Length (tokens)383 Max Response Length (tokens)
384 </div>384 </div>
385 <div class="wide100p">385 <div class="wide100p">
386 <input type="number" id="openai_max_tokens" name="openai_max_tokens" class="text_pole" min="1" max="16384">386 <input type="number" id="openai_max_tokens" name="openai_max_tokens" class="text_pole" min="1" max="65536">
387 </div>387 </div>
388 </div>388 </div>
389 <div class="range-block" data-source="openai,custom">389 <div class="range-block" data-source="openai,custom">
@@ -2611,6 +2611,10 @@
2611 <option value="gpt-4-0125-preview">gpt-4-0125-preview (2024)</option>2611 <option value="gpt-4-0125-preview">gpt-4-0125-preview (2024)</option>
2612 <option value="gpt-4-1106-preview">gpt-4-1106-preview (2023)</option>2612 <option value="gpt-4-1106-preview">gpt-4-1106-preview (2023)</option>
2613 </optgroup>2613 </optgroup>
2614 <optgroup label="o1">
2615 <option value="o1-preview">o1-preview</option>
2616 <option value="o1-mini">o1-mini</option>
2617 </optgroup>
2614 <optgroup label="Other">2618 <optgroup label="Other">
2615 <option value="text-davinci-003">text-davinci-003</option>2619 <option value="text-davinci-003">text-davinci-003</option>
2616 <option value="text-davinci-002">text-davinci-002</option>2620 <option value="text-davinci-002">text-davinci-002</option>
public/script.js+10 -34
@@ -881,7 +881,6 @@ let abortController;
881881
882//css882//css
883var css_send_form_display = $('<div id=send_form></div>').css('display');883var css_send_form_display = $('<div id=send_form></div>').css('display');
884const MAX_GENERATION_LOOPS = 5;
885884
886var kobold_horde_model = '';885var kobold_horde_model = '';
887886
@@ -2862,7 +2861,12 @@ export function getCharacterCardFields() {
28622861
2863export function isStreamingEnabled() {2862export function isStreamingEnabled() {
2864 const noStreamSources = [chat_completion_sources.SCALE];2863 const noStreamSources = [chat_completion_sources.SCALE];
2865 return ((main_api == 'openai' && oai_settings.stream_openai && !noStreamSources.includes(oai_settings.chat_completion_source) && !(oai_settings.chat_completion_source == chat_completion_sources.MAKERSUITE && oai_settings.google_model.includes('bison')))2864 return (
2865 (main_api == 'openai' &&
2866 oai_settings.stream_openai &&
2867 !noStreamSources.includes(oai_settings.chat_completion_source) &&
2868 !(oai_settings.chat_completion_source == chat_completion_sources.OPENAI && oai_settings.openai_model.startsWith('o1-')) &&
2869 !(oai_settings.chat_completion_source == chat_completion_sources.MAKERSUITE && oai_settings.google_model.includes('bison')))
2866 || (main_api == 'kobold' && kai_settings.streaming_kobold && kai_flags.can_use_streaming)2870 || (main_api == 'kobold' && kai_settings.streaming_kobold && kai_flags.can_use_streaming)
2867 || (main_api == 'novel' && nai_settings.streaming_novel)2871 || (main_api == 'novel' && nai_settings.streaming_novel)
2868 || (main_api == 'textgenerationwebui' && textgen_settings.streaming));2872 || (main_api == 'textgenerationwebui' && textgen_settings.streaming));
@@ -3337,11 +3341,11 @@ function removeLastMessage() {
3337 * @param {GenerateOptions} options Generation options3341 * @param {GenerateOptions} options Generation options
3338 * @param {boolean} dryRun Whether to actually generate a message or just assemble the prompt3342 * @param {boolean} dryRun Whether to actually generate a message or just assemble the prompt
3339 * @returns {Promise<any>} Returns a promise that resolves when the text is done generating.3343 * @returns {Promise<any>} Returns a promise that resolves when the text is done generating.
3340 * @typedef {{automatic_trigger?: boolean, force_name2?: boolean, quiet_prompt?: string, quietToLoud?: boolean, skipWIAN?: boolean, force_chid?: number, signal?: AbortSignal, quietImage?: string, maxLoops?: number, quietName?: string }} GenerateOptions3344 * @typedef {{automatic_trigger?: boolean, force_name2?: boolean, quiet_prompt?: string, quietToLoud?: boolean, skipWIAN?: boolean, force_chid?: number, signal?: AbortSignal, quietImage?: string, quietName?: string }} GenerateOptions
3341 */3345 */
3342export async function Generate(type, { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, maxLoops, quietName } = {}, dryRun = false) {3346export async function Generate(type, { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, quietName } = {}, dryRun = false) {
3343 console.log('Generate entered');3347 console.log('Generate entered');
3344 await eventSource.emit(event_types.GENERATION_STARTED, type, { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, maxLoops }, dryRun);3348 await eventSource.emit(event_types.GENERATION_STARTED, type, { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage }, dryRun);
3345 setGenerationProgress(0);3349 setGenerationProgress(0);
3346 generation_started = new Date();3350 generation_started = new Date();
33473351
@@ -3403,7 +3407,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
3403 if (selected_group && !is_group_generating) {3407 if (selected_group && !is_group_generating) {
3404 if (!dryRun) {3408 if (!dryRun) {
3405 // Returns the promise that generateGroupWrapper returns; resolves when generation is done3409 // Returns the promise that generateGroupWrapper returns; resolves when generation is done
3406 return generateGroupWrapper(false, type, { quiet_prompt, force_chid, signal: abortController.signal, quietImage, maxLoops });3410 return generateGroupWrapper(false, type, { quiet_prompt, force_chid, signal: abortController.signal, quietImage });
3407 }3411 }
34083412
3409 const characterIndexMap = new Map(characters.map((char, index) => [char.avatar, index]));3413 const characterIndexMap = new Map(characters.map((char, index) => [char.avatar, index]));
@@ -4435,7 +4439,6 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
4435 const displayIncomplete = type === 'quiet' && !quietToLoud;4439 const displayIncomplete = type === 'quiet' && !quietToLoud;
4436 getMessage = cleanUpMessage(getMessage, isImpersonate, isContinue, displayIncomplete);4440 getMessage = cleanUpMessage(getMessage, isImpersonate, isContinue, displayIncomplete);
44374441
4438 if (getMessage.length > 0 || data.allowEmptyResponse) {
4439 if (isImpersonate) {4442 if (isImpersonate) {
4440 $('#send_textarea').val(getMessage)[0].dispatchEvent(new Event('input', { bubbles: true }));4443 $('#send_textarea').val(getMessage)[0].dispatchEvent(new Event('input', { bubbles: true }));
4441 generatedPromptCache = '';4444 generatedPromptCache = '';
@@ -4461,28 +4464,6 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
4461 if (type !== 'quiet') {4464 if (type !== 'quiet') {
4462 playMessageSound();4465 playMessageSound();
4463 }4466 }
4464 } else {
4465 // If maxLoops is not passed in (e.g. first time generating), set it to MAX_GENERATION_LOOPS
4466 maxLoops ??= MAX_GENERATION_LOOPS;
4467
4468 if (maxLoops === 0) {
4469 if (type !== 'quiet') {
4470 throwCircuitBreakerError();
4471 }
4472 throw new Error('Generate circuit breaker interruption');
4473 }
4474
4475 // regenerate with character speech reenforced
4476 // to make sure we leave on swipe type while also adding the name2 appendage
4477 await delay(1000);
4478 // A message was already deleted on regeneration, so instead treat is as a normal gen
4479 if (type === 'regenerate') {
4480 type = 'normal';
4481 }
4482 // The first await is for waiting for the generate to start. The second one is waiting for it to finish
4483 const result = await await Generate(type, { automatic_trigger, force_name2: true, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, quietName, maxLoops: maxLoops - 1 });
4484 return result;
4485 }
44864467
4487 if (power_user.auto_swipe) {4468 if (power_user.auto_swipe) {
4488 console.debug('checking for autoswipeblacklist on non-streaming message');4469 console.debug('checking for autoswipeblacklist on non-streaming message');
@@ -5254,11 +5235,6 @@ function getGenerateUrl(api) {
5254 }5235 }
5255}5236}
52565237
5257function throwCircuitBreakerError() {
5258 callPopup(`Could not extract reply in ${MAX_GENERATION_LOOPS} attempts. Try generating again`, 'text');
5259 unblockGeneration();
5260}
5261
5262function extractTitleFromData(data) {5238function extractTitleFromData(data) {
5263 if (main_api == 'koboldhorde') {5239 if (main_api == 'koboldhorde') {
5264 return data.workerName;5240 return data.workerName;
public/scripts/openai.js+29 -6
@@ -1797,7 +1797,7 @@ async function sendOpenAIRequest(type, messages, signal) {
1797 const isQuiet = type === 'quiet';1797 const isQuiet = type === 'quiet';
1798 const isImpersonate = type === 'impersonate';1798 const isImpersonate = type === 'impersonate';
1799 const isContinue = type === 'continue';1799 const isContinue = type === 'continue';
1800 const stream = oai_settings.stream_openai && !isQuiet && !isScale && !(isGoogle && oai_settings.google_model.includes('bison'));1800 const stream = oai_settings.stream_openai && !isQuiet && !isScale && !(isGoogle && oai_settings.google_model.includes('bison')) && !(isOAI && oai_settings.openai_model.startsWith('o1-'));
1801 const useLogprobs = !!power_user.request_token_probabilities;1801 const useLogprobs = !!power_user.request_token_probabilities;
1802 const canMultiSwipe = oai_settings.n > 1 && !isContinue && !isImpersonate && !isQuiet && (isOAI || isCustom);1802 const canMultiSwipe = oai_settings.n > 1 && !isContinue && !isImpersonate && !isQuiet && (isOAI || isCustom);
18031803
@@ -1960,12 +1960,35 @@ async function sendOpenAIRequest(type, messages, signal) {
1960 generate_data['seed'] = oai_settings.seed;1960 generate_data['seed'] = oai_settings.seed;
1961 }1961 }
19621962
1963 await eventSource.emit(event_types.CHAT_COMPLETION_SETTINGS_READY, generate_data);
1964
1965 if (isFunctionCallingSupported() && !stream) {1963 if (isFunctionCallingSupported() && !stream) {
1966 await registerFunctionTools(type, generate_data);1964 await registerFunctionTools(type, generate_data);
1967 }1965 }
19681966
1967 if (isOAI && oai_settings.openai_model.startsWith('o1-')) {
1968 generate_data.messages.forEach((msg) => {
1969 if (msg.role === 'system') {
1970 msg.role = 'user';
1971 }
1972 });
1973 generate_data.max_completion_tokens = generate_data.max_tokens;
1974 delete generate_data.max_tokens;
1975 delete generate_data.stream;
1976 delete generate_data.logprobs;
1977 delete generate_data.top_logprobs;
1978 delete generate_data.n;
1979 delete generate_data.temperature;
1980 delete generate_data.top_p;
1981 delete generate_data.frequency_penalty;
1982 delete generate_data.presence_penalty;
1983 delete generate_data.tools;
1984 delete generate_data.tool_choice;
1985 delete generate_data.stop;
1986 // It does support logit_bias, but the tokenizer used and its effect is yet unknown.
1987 // delete generate_data.logit_bias;
1988 }
1989
1990 await eventSource.emit(event_types.CHAT_COMPLETION_SETTINGS_READY, generate_data);
1991
1969 const generate_url = '/api/backends/chat-completions/generate';1992 const generate_url = '/api/backends/chat-completions/generate';
1970 const response = await fetch(generate_url, {1993 const response = await fetch(generate_url, {
1971 method: 'POST',1994 method: 'POST',
@@ -2111,7 +2134,6 @@ async function checkFunctionToolCalls(data) {
2111 const args = toolCall.function;2134 const args = toolCall.function;
2112 console.log('Function tool call:', toolCall);2135 console.log('Function tool call:', toolCall);
2113 await eventSource.emit(event_types.LLM_FUNCTION_TOOL_CALL, args);2136 await eventSource.emit(event_types.LLM_FUNCTION_TOOL_CALL, args);
2114 data.allowEmptyResponse = true;
2115 }2137 }
2116 }2138 }
21172139
@@ -2125,7 +2147,6 @@ async function checkFunctionToolCalls(data) {
2125 /** @type {FunctionToolCall} */2147 /** @type {FunctionToolCall} */
2126 const args = { name: content.name, arguments: JSON.stringify(content.input) };2148 const args = { name: content.name, arguments: JSON.stringify(content.input) };
2127 await eventSource.emit(event_types.LLM_FUNCTION_TOOL_CALL, args);2149 await eventSource.emit(event_types.LLM_FUNCTION_TOOL_CALL, args);
2128 data.allowEmptyResponse = true;
2129 }2150 }
2130 }2151 }
2131 }2152 }
@@ -2140,7 +2161,6 @@ async function checkFunctionToolCalls(data) {
2140 const args = { name: toolCall.name, arguments: JSON.stringify(toolCall.parameters) };2161 const args = { name: toolCall.name, arguments: JSON.stringify(toolCall.parameters) };
2141 console.log('Function tool call:', toolCall);2162 console.log('Function tool call:', toolCall);
2142 await eventSource.emit(event_types.LLM_FUNCTION_TOOL_CALL, args);2163 await eventSource.emit(event_types.LLM_FUNCTION_TOOL_CALL, args);
2143 data.allowEmptyResponse = true;
2144 }2164 }
2145 }2165 }
2146}2166}
@@ -3905,6 +3925,9 @@ function getMaxContextOpenAI(value) {
3905 if (oai_settings.max_context_unlocked) {3925 if (oai_settings.max_context_unlocked) {
3906 return unlocked_max;3926 return unlocked_max;
3907 }3927 }
3928 else if (value.startsWith('o1-')) {
3929 return max_128k;
3930 }
3908 else if (value.includes('chatgpt-4o-latest') || value.includes('gpt-4-turbo') || value.includes('gpt-4o') || value.includes('gpt-4-1106') || value.includes('gpt-4-0125') || value.includes('gpt-4-vision')) {3931 else if (value.includes('chatgpt-4o-latest') || value.includes('gpt-4-turbo') || value.includes('gpt-4o') || value.includes('gpt-4-1106') || value.includes('gpt-4-0125') || value.includes('gpt-4-vision')) {
3909 return max_128k;3932 return max_128k;
3910 }3933 }
src/endpoints/backends/chat-completions.js+1 -0
@@ -965,6 +965,7 @@ router.post('/generate', jsonParser, function (request, response) {
965 'model': request.body.model,965 'model': request.body.model,
966 'temperature': request.body.temperature,966 'temperature': request.body.temperature,
967 'max_tokens': request.body.max_tokens,967 'max_tokens': request.body.max_tokens,
968 'max_completion_tokens': request.body.max_completion_tokens,
968 'stream': request.body.stream,969 'stream': request.body.stream,
969 'presence_penalty': request.body.presence_penalty,970 'presence_penalty': request.body.presence_penalty,
970 'frequency_penalty': request.body.frequency_penalty,971 'frequency_penalty': request.body.frequency_penalty,
src/endpoints/tokenizers.js+4 -0
@@ -350,6 +350,10 @@ function getWebTokenizersChunks(tokenizer, ids) {
350 * @returns {string} Tokenizer model to use350 * @returns {string} Tokenizer model to use
351 */351 */
352function getTokenizerModel(requestModel) {352function getTokenizerModel(requestModel) {
353 if (requestModel.includes('o1-preview') || requestModel.includes('o1-mini')) {
354 return 'gpt-4o';
355 }
356
353 if (requestModel.includes('gpt-4o')) {357 if (requestModel.includes('gpt-4o')) {
354 return 'gpt-4o';358 return 'gpt-4o';
355 }359 }