mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
Minor fixes: tokenizer, default Bing toneStyle, SiblingSwitch (#348)
* fix: tokenizer will count completion tokens correctly, remove global var, will allow unofficial models for alternative endpoints * refactor(askBingAI.js, Settings.jsx, types.ts, cleanupPreset.js, getDefaultConversation.js, handleSubmit.js): change default toneStyle to 'creative' instead of 'fast' for Bing AI endpoint. * fix(SiblingSwitch): correctly appears now style(HoverButtons.jsx): add 'active' class to hover buttons
This commit is contained in:
parent
791b515937
commit
4beb06aa4b
10 changed files with 125 additions and 78 deletions
|
|
@ -2,6 +2,7 @@ require('dotenv').config();
|
||||||
const { KeyvFile } = require('keyv-file');
|
const { KeyvFile } = require('keyv-file');
|
||||||
const { genAzureEndpoint } = require('../../utils/genAzureEndpoints');
|
const { genAzureEndpoint } = require('../../utils/genAzureEndpoints');
|
||||||
const tiktoken = require('@dqbd/tiktoken');
|
const tiktoken = require('@dqbd/tiktoken');
|
||||||
|
const tiktokenModels = require('../../utils/tiktokenModels');
|
||||||
const encoding_for_model = tiktoken.encoding_for_model;
|
const encoding_for_model = tiktoken.encoding_for_model;
|
||||||
|
|
||||||
const askClient = async ({
|
const askClient = async ({
|
||||||
|
|
@ -26,9 +27,8 @@ const askClient = async ({
|
||||||
};
|
};
|
||||||
|
|
||||||
const azure = process.env.AZURE_OPENAI_API_KEY ? true : false;
|
const azure = process.env.AZURE_OPENAI_API_KEY ? true : false;
|
||||||
if (promptPrefix == null) {
|
let promptText = 'You are ChatGPT, a large language model trained by OpenAI.';
|
||||||
promptText = 'You are ChatGPT, a large language model trained by OpenAI.';
|
if (promptPrefix) {
|
||||||
} else {
|
|
||||||
promptText = promptPrefix;
|
promptText = promptPrefix;
|
||||||
}
|
}
|
||||||
const maxContextTokens = model === 'gpt-4' ? 8191 : model === 'gpt-4-32k' ? 32767 : 4095; // 1 less than maximum
|
const maxContextTokens = model === 'gpt-4' ? 8191 : model === 'gpt-4-32k' ? 32767 : 4095; // 1 less than maximum
|
||||||
|
|
@ -68,25 +68,18 @@ const askClient = async ({
|
||||||
...(parentMessageId && conversationId ? { parentMessageId, conversationId } : {})
|
...(parentMessageId && conversationId ? { parentMessageId, conversationId } : {})
|
||||||
};
|
};
|
||||||
|
|
||||||
const enc = encoding_for_model(model);
|
const enc = encoding_for_model(tiktokenModels.has(model) ? model : 'gpt-3.5-turbo');
|
||||||
const text_tokens = enc.encode(text);
|
const usage = {
|
||||||
const prompt_tokens = enc.encode(promptText);
|
prompt_tokens: (enc.encode(promptText)).length + (enc.encode(text)).length,
|
||||||
// console.log("Prompt tokens = ", prompt_tokens.length);
|
}
|
||||||
// console.log("Message Tokens = ", text_tokens.length);
|
|
||||||
|
|
||||||
const res = await client.sendMessage(text, { ...options, userId });
|
const res = await client.sendMessage(text, { ...options, userId });
|
||||||
// return res;
|
usage.completion_tokens = (enc.encode(res.response)).length;
|
||||||
// create a new response object that includes the token counts
|
usage.total_tokens = usage.prompt_tokens + usage.completion_tokens;
|
||||||
const newRes = {
|
return {
|
||||||
...res,
|
...res,
|
||||||
usage: {
|
usage,
|
||||||
prompt_tokens: prompt_tokens.length,
|
}
|
||||||
completion_tokens: text_tokens.length,
|
|
||||||
total_tokens: prompt_tokens.length + text_tokens.length
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return newRes;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = { askClient };
|
module.exports = { askClient };
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ router.post('/', requireJwtAuth, async (req, res) => {
|
||||||
jailbreakConversationId: req.body?.jailbreakConversationId ?? null,
|
jailbreakConversationId: req.body?.jailbreakConversationId ?? null,
|
||||||
systemMessage: req.body?.systemMessage ?? null,
|
systemMessage: req.body?.systemMessage ?? null,
|
||||||
context: req.body?.context ?? null,
|
context: req.body?.context ?? null,
|
||||||
toneStyle: req.body?.toneStyle ?? 'fast',
|
toneStyle: req.body?.toneStyle ?? 'creative',
|
||||||
token: req.body?.token ?? null
|
token: req.body?.token ?? null
|
||||||
};
|
};
|
||||||
else
|
else
|
||||||
|
|
@ -51,7 +51,7 @@ router.post('/', requireJwtAuth, async (req, res) => {
|
||||||
conversationSignature: req.body?.conversationSignature ?? null,
|
conversationSignature: req.body?.conversationSignature ?? null,
|
||||||
clientId: req.body?.clientId ?? null,
|
clientId: req.body?.clientId ?? null,
|
||||||
invocationId: req.body?.invocationId ?? null,
|
invocationId: req.body?.invocationId ?? null,
|
||||||
toneStyle: req.body?.toneStyle ?? 'fast',
|
toneStyle: req.body?.toneStyle ?? 'creative',
|
||||||
token: req.body?.token ?? null
|
token: req.body?.token ?? null
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -110,7 +110,7 @@ const ask = async ({
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let lastSavedTimestamp = 0;
|
let lastSavedTimestamp = 0;
|
||||||
const { onProgress: progressCallback, getPartialText } = createOnProgress({
|
const { onProgress: progressCallback } = createOnProgress({
|
||||||
onProgress: ({ text }) => {
|
onProgress: ({ text }) => {
|
||||||
const currentTimestamp = Date.now();
|
const currentTimestamp = Date.now();
|
||||||
if (currentTimestamp - lastSavedTimestamp > 500) {
|
if (currentTimestamp - lastSavedTimestamp > 500) {
|
||||||
|
|
|
||||||
40
api/utils/tiktokenModels.js
Normal file
40
api/utils/tiktokenModels.js
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
const models = [
|
||||||
|
'text-davinci-003',
|
||||||
|
'text-davinci-002',
|
||||||
|
'text-davinci-001',
|
||||||
|
'text-curie-001',
|
||||||
|
'text-babbage-001',
|
||||||
|
'text-ada-001',
|
||||||
|
'davinci',
|
||||||
|
'curie',
|
||||||
|
'babbage',
|
||||||
|
'ada',
|
||||||
|
'code-davinci-002',
|
||||||
|
'code-davinci-001',
|
||||||
|
'code-cushman-002',
|
||||||
|
'code-cushman-001',
|
||||||
|
'davinci-codex',
|
||||||
|
'cushman-codex',
|
||||||
|
'text-davinci-edit-001',
|
||||||
|
'code-davinci-edit-001',
|
||||||
|
'text-embedding-ada-002',
|
||||||
|
'text-similarity-davinci-001',
|
||||||
|
'text-similarity-curie-001',
|
||||||
|
'text-similarity-babbage-001',
|
||||||
|
'text-similarity-ada-001',
|
||||||
|
'text-search-davinci-doc-001',
|
||||||
|
'text-search-curie-doc-001',
|
||||||
|
'text-search-babbage-doc-001',
|
||||||
|
'text-search-ada-doc-001',
|
||||||
|
'code-search-babbage-code-001',
|
||||||
|
'code-search-ada-code-001',
|
||||||
|
'gpt2',
|
||||||
|
'gpt-4',
|
||||||
|
'gpt-4-0314',
|
||||||
|
'gpt-4-32k',
|
||||||
|
'gpt-4-32k-0314',
|
||||||
|
'gpt-3.5-turbo',
|
||||||
|
'gpt-3.5-turbo-0301'
|
||||||
|
];
|
||||||
|
|
||||||
|
module.exports = new Set(models);
|
||||||
|
|
@ -47,7 +47,7 @@ function Settings(props) {
|
||||||
<div className="col-span-1 flex flex-col items-center justify-start gap-6">
|
<div className="col-span-1 flex flex-col items-center justify-start gap-6">
|
||||||
<div className="grid w-full items-center gap-2">
|
<div className="grid w-full items-center gap-2">
|
||||||
<Label htmlFor="toneStyle-dropdown" className="text-left text-sm font-medium">
|
<Label htmlFor="toneStyle-dropdown" className="text-left text-sm font-medium">
|
||||||
Tone Style <small className="opacity-40">(default: fast)</small>
|
Tone Style <small className="opacity-40">(default: creative)</small>
|
||||||
</Label>
|
</Label>
|
||||||
<SelectDropDown
|
<SelectDropDown
|
||||||
id="toneStyle-dropdown"
|
id="toneStyle-dropdown"
|
||||||
|
|
@ -112,7 +112,7 @@ function Settings(props) {
|
||||||
<a
|
<a
|
||||||
href="https://github.com/danny-avila/chatgpt-clone/blob/main/client/defaultSystemMessage.md"
|
href="https://github.com/danny-avila/chatgpt-clone/blob/main/client/defaultSystemMessage.md"
|
||||||
target="_blank"
|
target="_blank"
|
||||||
className="text-blue-500 transition-colors duration-200 hover:text-blue-800 dark:text-blue-400 dark:hover:text-blue-500"
|
className="text-blue-500 transition-colors duration-200 hover:text-blue-800 dark:text-blue-400 dark:hover:text-blue-500" rel="noreferrer"
|
||||||
>
|
>
|
||||||
System Message
|
System Message
|
||||||
</a>{' '}
|
</a>{' '}
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ export default function HoverButtons({
|
||||||
) : null}
|
) : null}
|
||||||
{regenerateEnabled ? (
|
{regenerateEnabled ? (
|
||||||
<button
|
<button
|
||||||
className="hover-button rounded-md p-1 hover:bg-gray-100 hover:text-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-gray-200 disabled:dark:hover:text-gray-400 md:invisible md:group-hover:visible"
|
className="hover-button active rounded-md p-1 hover:bg-gray-100 hover:text-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-gray-200 disabled:dark:hover:text-gray-400 md:invisible md:group-hover:visible"
|
||||||
onClick={regenerate}
|
onClick={regenerate}
|
||||||
type="button"
|
type="button"
|
||||||
title="regenerate"
|
title="regenerate"
|
||||||
|
|
@ -64,7 +64,7 @@ export default function HoverButtons({
|
||||||
) : null}
|
) : null}
|
||||||
|
|
||||||
<button
|
<button
|
||||||
className="hover-button rounded-md p-1 hover:bg-gray-100 hover:text-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-gray-200 disabled:dark:hover:text-gray-400 md:invisible md:group-hover:visible"
|
className="hover-button active rounded-md p-1 hover:bg-gray-100 hover:text-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-gray-200 disabled:dark:hover:text-gray-400 md:invisible md:group-hover:visible"
|
||||||
onClick={() => copyToClipboard(setIsCopied)}
|
onClick={() => copyToClipboard(setIsCopied)}
|
||||||
type="button"
|
type="button"
|
||||||
title={isCopied ? 'Copied to clipboard' : 'Copy to clipboard'}
|
title={isCopied ? 'Copied to clipboard' : 'Copy to clipboard'}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,15 @@ export type TExample = {
|
||||||
output: string;
|
output: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export enum EModelEndpoint {
|
||||||
|
azureOpenAI = 'azureOpenAI',
|
||||||
|
openAI = 'openAI',
|
||||||
|
bingAI = 'bingAI',
|
||||||
|
chatGPT = 'chatGPT',
|
||||||
|
chatGPTBrowser = 'chatGPTBrowser',
|
||||||
|
google = 'google'
|
||||||
|
}
|
||||||
|
|
||||||
export type TSubmission = {
|
export type TSubmission = {
|
||||||
clientId?: string;
|
clientId?: string;
|
||||||
context?: string;
|
context?: string;
|
||||||
|
|
@ -42,15 +51,6 @@ export type TSubmission = {
|
||||||
frequence_penalty?: number;
|
frequence_penalty?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export enum EModelEndpoint {
|
|
||||||
azureOpenAI = 'azureOpenAI',
|
|
||||||
openAI = 'openAI',
|
|
||||||
bingAI = 'bingAI',
|
|
||||||
chatGPT = 'chatGPT',
|
|
||||||
chatGPTBrowser = 'chatGPTBrowser',
|
|
||||||
google = 'google'
|
|
||||||
}
|
|
||||||
|
|
||||||
export type TConversation = {
|
export type TConversation = {
|
||||||
conversationId: string;
|
conversationId: string;
|
||||||
title: string;
|
title: string;
|
||||||
|
|
@ -157,7 +157,7 @@ export type TSearchResults = {
|
||||||
pageNumber: string;
|
pageNumber: string;
|
||||||
pageSize: string | number;
|
pageSize: string | number;
|
||||||
pages: string | number;
|
pages: string | number;
|
||||||
filter: {};
|
filter: object;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TEndpoints = {
|
export type TEndpoints = {
|
||||||
|
|
@ -175,11 +175,11 @@ export type TUpdateTokenCountResponse = {
|
||||||
count: number;
|
count: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TMessageTreeNode = {};
|
export type TMessageTreeNode = object;
|
||||||
|
|
||||||
export type TSearchMessage = {};
|
export type TSearchMessage = object;
|
||||||
|
|
||||||
export type TSearchMessageTreeNode = {};
|
export type TSearchMessageTreeNode = object;
|
||||||
|
|
||||||
export type TRegisterUser = {
|
export type TRegisterUser = {
|
||||||
name: string;
|
name: string;
|
||||||
|
|
|
||||||
|
|
@ -1,40 +1,8 @@
|
||||||
.switch-container {
|
.hover-button.active {
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
.switch-result {
|
|
||||||
display: block !important;
|
|
||||||
visibility: visible;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* .sibling-switch {
|
|
||||||
left: 114px;
|
|
||||||
top: unset;
|
|
||||||
bottom: 4px;
|
|
||||||
visibility: visible;
|
|
||||||
z-index: 2;
|
|
||||||
} */
|
|
||||||
.sibling-switch {
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hover-button {
|
|
||||||
display: block;
|
display: block;
|
||||||
visibility: visible;
|
visibility: visible;
|
||||||
}
|
}
|
||||||
|
|
||||||
.input-panel-button {
|
|
||||||
border: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.input-panel-button svg {
|
|
||||||
width: 16px;
|
|
||||||
height: 16px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.input-panel {
|
|
||||||
}
|
|
||||||
|
|
||||||
.nav-close-button {
|
.nav-close-button {
|
||||||
display: block;
|
display: block;
|
||||||
position: absolute;
|
position: absolute;
|
||||||
|
|
@ -96,3 +64,47 @@
|
||||||
position: fixed;
|
position: fixed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@media (min-width: 1024px) {
|
||||||
|
.switch-container {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
.switch-result {
|
||||||
|
display: block !important;
|
||||||
|
visibility: visible;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 1024px) {
|
||||||
|
/* .sibling-switch {
|
||||||
|
left: 114px;
|
||||||
|
top: unset;
|
||||||
|
bottom: 4px;
|
||||||
|
visibility: visible;
|
||||||
|
z-index: 2;
|
||||||
|
} */
|
||||||
|
.sibling-switch {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hover-button {
|
||||||
|
display: block;
|
||||||
|
visibility: visible;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 767px) {
|
||||||
|
.input-panel-button {
|
||||||
|
border: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.input-panel-button svg {
|
||||||
|
width: 16px;
|
||||||
|
height: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.input-panel {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ const cleanupPreset = ({ preset: _preset, endpointsConfig = {} }) => {
|
||||||
jailbreak: _preset?.jailbreak ?? false,
|
jailbreak: _preset?.jailbreak ?? false,
|
||||||
context: _preset?.context ?? null,
|
context: _preset?.context ?? null,
|
||||||
systemMessage: _preset?.systemMessage ?? null,
|
systemMessage: _preset?.systemMessage ?? null,
|
||||||
toneStyle: _preset?.toneStyle ?? 'fast',
|
toneStyle: _preset?.toneStyle ?? 'creative',
|
||||||
title: _preset?.title ?? 'New Preset'
|
title: _preset?.title ?? 'New Preset'
|
||||||
};
|
};
|
||||||
} else if (endpoint === 'chatGPTBrowser') {
|
} else if (endpoint === 'chatGPTBrowser') {
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ const buildDefaultConversation = ({
|
||||||
jailbreak: lastConversationSetup?.jailbreak ?? false,
|
jailbreak: lastConversationSetup?.jailbreak ?? false,
|
||||||
context: lastConversationSetup?.context ?? null,
|
context: lastConversationSetup?.context ?? null,
|
||||||
systemMessage: lastConversationSetup?.systemMessage ?? null,
|
systemMessage: lastConversationSetup?.systemMessage ?? null,
|
||||||
toneStyle: lastConversationSetup?.toneStyle ?? 'fast',
|
toneStyle: lastConversationSetup?.toneStyle ?? 'creative',
|
||||||
jailbreakConversationId: lastConversationSetup?.jailbreakConversationId ?? null,
|
jailbreakConversationId: lastConversationSetup?.jailbreakConversationId ?? null,
|
||||||
conversationSignature: null,
|
conversationSignature: null,
|
||||||
clientId: null,
|
clientId: null,
|
||||||
|
|
@ -80,7 +80,7 @@ const buildDefaultConversation = ({
|
||||||
return conversation;
|
return conversation;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getDefaultConversation = ({ conversation, prevConversation, endpointsConfig, preset }) => {
|
const getDefaultConversation = ({ conversation, endpointsConfig, preset }) => {
|
||||||
const { endpoint: targetEndpoint } = preset || {};
|
const { endpoint: targetEndpoint } = preset || {};
|
||||||
|
|
||||||
if (targetEndpoint) {
|
if (targetEndpoint) {
|
||||||
|
|
@ -123,7 +123,9 @@ const getDefaultConversation = ({ conversation, prevConversation, endpointsConfi
|
||||||
conversation = buildDefaultConversation({ conversation, endpoint, endpointsConfig });
|
conversation = buildDefaultConversation({ conversation, endpoint, endpointsConfig });
|
||||||
return conversation;
|
return conversation;
|
||||||
}
|
}
|
||||||
} catch (error) {}
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
|
||||||
// if anything happens, reset to default model
|
// if anything happens, reset to default model
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,7 @@ const useMessageHandler = () => {
|
||||||
jailbreak: currentConversation?.jailbreak ?? false,
|
jailbreak: currentConversation?.jailbreak ?? false,
|
||||||
systemMessage: currentConversation?.systemMessage ?? null,
|
systemMessage: currentConversation?.systemMessage ?? null,
|
||||||
context: currentConversation?.context ?? null,
|
context: currentConversation?.context ?? null,
|
||||||
toneStyle: currentConversation?.toneStyle ?? 'fast',
|
toneStyle: currentConversation?.toneStyle ?? 'creative',
|
||||||
jailbreakConversationId: currentConversation?.jailbreakConversationId ?? null,
|
jailbreakConversationId: currentConversation?.jailbreakConversationId ?? null,
|
||||||
conversationSignature: currentConversation?.conversationSignature ?? null,
|
conversationSignature: currentConversation?.conversationSignature ?? null,
|
||||||
clientId: currentConversation?.clientId ?? null,
|
clientId: currentConversation?.clientId ?? null,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue