LibreChat/client/src/utils/getDefaultConversation.js

139 lines
4.5 KiB
JavaScript
Raw Normal View History

const buildDefaultConversation = ({
conversation,
endpoint,
endpointsConfig = {},
lastConversationSetup = {}
}) => {
const lastSelectedModel = JSON.parse(localStorage.getItem('lastSelectedModel')) || {};
if (endpoint === 'azureOpenAI' || endpoint === 'openAI') {
conversation = {
...conversation,
endpoint,
model:
lastConversationSetup?.model ??
lastSelectedModel[endpoint] ??
endpointsConfig[endpoint]?.availableModels?.[0] ??
'gpt-3.5-turbo',
chatGptLabel: lastConversationSetup?.chatGptLabel ?? null,
promptPrefix: lastConversationSetup?.promptPrefix ?? null,
temperature: lastConversationSetup?.temperature ?? 1,
top_p: lastConversationSetup?.top_p ?? 1,
presence_penalty: lastConversationSetup?.presence_penalty ?? 0,
frequency_penalty: lastConversationSetup?.frequency_penalty ?? 0
};
Feat: PaLM 2 (#262) * feat(api): add googleapis package to package.json feat(api): add reqDemo.js file to make a request to Google Cloud AI Platform API to get a response from a chatbot model. * feat: add PaLM2 support * feat(conversationPreset.js): add support for topP and topK for google endpoint feat(askGoogle.js): add support for topP and topK for google endpoint feat(ask/index.js): add google endpoint feat(endpoints.js): add google endpoint feat(MessageHeader.jsx): add support for modelLabel for google endpoint feat(PresetItem.jsx): add support for modelLabel for google endpoint feat(HoverButtons.jsx): add support for google endpoint feat(createPayload.ts): add google endpoint feat(types.ts): add google endpoint feat(store/endpoints.js): add google endpoint feat(cleanupPreset.js): add support for topP and topK for google endpoint feat(getDefaultConversation.js): add support for topP and topK for google endpoint feat(handleSubmit.js): add support for topP and topK for google endpoint * fix: messages payload * refactor(GoogleClient.js): set maxContextTokens based on isTextModel value feat(GoogleClient.js): add delay option to TextStream constructor feat(getIcon.jsx): add support for google endpoint and PaLM2 model label * feat: palm frontend changes * feat(askGoogle.js): set default example to empty input and output feat(Examples.jsx): add ability to add and remove examples refactor(Settings.jsx): remove examples from props and setOption function style(GoogleOptions): remove unnecessary whitespace after Settings2 import feat(GoogleOptions): add addExample and removeExample functions to manage examples fix(cleanupPreset): set default example to [{ input: '', output: ''}] fix(getDefaultConversation): set default example to [{ input: '', output: ''}] fix(handleSubmit): set default example to [{ input: '', output: ''}] * style(client): adjust height of settings and examples components to 350px fix(client): fix path to palm.png image in getIcon.jsx file * style(EndpointOptionsPopover.jsx, Examples.jsx, Settings.jsx): improve button styles and update input placeholders * feat (palm): finalize examples on the frontend * feat(GoogleClient.js): filter out empty examples in options feat(GoogleClient.js): add support for promptPrefix in buildPayload method feat(GoogleClient.js): add support for examples in buildPayload method feat(conversationPreset.js): add maxOutputTokens field to conversation preset schema feat(presetSchema.js): add examples field to preset schema feat(askGoogle.js): add support for examples and promptPrefix in endpointOption feat(EditPresetDialog.jsx): add Examples component for Google endpoint feat(EditPresetDialog.jsx): add button to show/hide Examples component feat(EditPresetDialog.jsx): add functionality to add, remove, and edit examples in Examples component feat(EndpointOptionsDialog.jsx): change endpoint name to PaLM for Google endpoint feat(Settings.jsx): add maxHeight prop to limit height of Settings component in EditPresetDialog and EndpointOptionsDialog fix(Settings.jsx): add examples prop to ChatGPTBrowser component fix(EndpointItem.jsx): add alternate name for google endpoint fix(MessageHeader.jsx): change title for google endpoint to PaLM feat(endpoints.js): add google endpoint to endpointsConfig fix(cleanupPreset.js): add missing comma in examples array * chore: change endpoint order * feat(PaLM 2): complete for testing * fix(PaLM): handle blocked messages
2023-05-13 16:29:06 -04:00
} else if (endpoint === 'google') {
conversation = {
...conversation,
endpoint,
model:
lastConversationSetup?.model ??
lastSelectedModel[endpoint] ??
endpointsConfig[endpoint]?.availableModels?.[0] ??
'chat-bison',
modelLabel: lastConversationSetup?.modelLabel ?? null,
promptPrefix: lastConversationSetup?.promptPrefix ?? null,
examples: lastConversationSetup?.examples ?? [{ input: { content: '' }, output: { content: '' }}],
temperature: lastConversationSetup?.temperature ?? 0.2,
maxOutputTokens: lastConversationSetup?.maxOutputTokens ?? 1024,
topP: lastConversationSetup?.topP ?? 0.95,
topK: lastConversationSetup?.topK ?? 40,
};
} else if (endpoint === 'bingAI') {
conversation = {
...conversation,
endpoint,
jailbreak: lastConversationSetup?.jailbreak ?? false,
context: lastConversationSetup?.context ?? null,
systemMessage: lastConversationSetup?.systemMessage ?? null,
toneStyle: lastConversationSetup?.toneStyle ?? 'fast',
jailbreakConversationId: lastConversationSetup?.jailbreakConversationId ?? null,
conversationSignature: null,
clientId: null,
invocationId: 1
};
} else if (endpoint === 'chatGPTBrowser') {
conversation = {
...conversation,
endpoint,
model:
lastConversationSetup?.model ??
lastSelectedModel[endpoint] ??
endpointsConfig[endpoint]?.availableModels?.[0] ??
'text-davinci-002-render-sha'
};
} else if (endpoint === null) {
conversation = {
...conversation,
endpoint
};
2023-03-31 03:22:57 +08:00
} else {
console.error(`Unknown endpoint ${endpoint}`);
conversation = {
...conversation,
endpoint: null
};
}
return conversation;
};
const getDefaultConversation = ({ conversation, prevConversation, endpointsConfig, preset }) => {
2023-04-02 04:15:07 +08:00
const { endpoint: targetEndpoint } = preset || {};
if (targetEndpoint) {
// try to use preset
const endpoint = targetEndpoint;
if (endpointsConfig?.[endpoint]) {
conversation = buildDefaultConversation({
conversation,
endpoint,
lastConversationSetup: preset,
endpointsConfig
});
return conversation;
} else {
console.log(endpoint);
console.warn(`Illegal target endpoint ${targetEndpoint} ${endpointsConfig}`);
}
}
// try {
// // try to use current model
// const { endpoint = null } = prevConversation || {};
// if (endpointsConfig?.[endpoint]) {
// conversation = buildDefaultConversation({
// conversation,
// endpoint,
// lastConversationSetup: prevConversation,
// endpointsConfig
// });
// return conversation;
// }
// } catch (error) {}
try {
// try to read latest selected model from local storage
const lastConversationSetup = JSON.parse(localStorage.getItem('lastConversationSetup'));
const { endpoint = null } = lastConversationSetup;
if (endpointsConfig?.[endpoint]) {
conversation = buildDefaultConversation({ conversation, endpoint, endpointsConfig });
return conversation;
}
} catch (error) {}
// if anything happens, reset to default model
Feat: PaLM 2 (#262) * feat(api): add googleapis package to package.json feat(api): add reqDemo.js file to make a request to Google Cloud AI Platform API to get a response from a chatbot model. * feat: add PaLM2 support * feat(conversationPreset.js): add support for topP and topK for google endpoint feat(askGoogle.js): add support for topP and topK for google endpoint feat(ask/index.js): add google endpoint feat(endpoints.js): add google endpoint feat(MessageHeader.jsx): add support for modelLabel for google endpoint feat(PresetItem.jsx): add support for modelLabel for google endpoint feat(HoverButtons.jsx): add support for google endpoint feat(createPayload.ts): add google endpoint feat(types.ts): add google endpoint feat(store/endpoints.js): add google endpoint feat(cleanupPreset.js): add support for topP and topK for google endpoint feat(getDefaultConversation.js): add support for topP and topK for google endpoint feat(handleSubmit.js): add support for topP and topK for google endpoint * fix: messages payload * refactor(GoogleClient.js): set maxContextTokens based on isTextModel value feat(GoogleClient.js): add delay option to TextStream constructor feat(getIcon.jsx): add support for google endpoint and PaLM2 model label * feat: palm frontend changes * feat(askGoogle.js): set default example to empty input and output feat(Examples.jsx): add ability to add and remove examples refactor(Settings.jsx): remove examples from props and setOption function style(GoogleOptions): remove unnecessary whitespace after Settings2 import feat(GoogleOptions): add addExample and removeExample functions to manage examples fix(cleanupPreset): set default example to [{ input: '', output: ''}] fix(getDefaultConversation): set default example to [{ input: '', output: ''}] fix(handleSubmit): set default example to [{ input: '', output: ''}] * style(client): adjust height of settings and examples components to 350px fix(client): fix path to palm.png image in getIcon.jsx file * style(EndpointOptionsPopover.jsx, Examples.jsx, Settings.jsx): improve button styles and update input placeholders * feat (palm): finalize examples on the frontend * feat(GoogleClient.js): filter out empty examples in options feat(GoogleClient.js): add support for promptPrefix in buildPayload method feat(GoogleClient.js): add support for examples in buildPayload method feat(conversationPreset.js): add maxOutputTokens field to conversation preset schema feat(presetSchema.js): add examples field to preset schema feat(askGoogle.js): add support for examples and promptPrefix in endpointOption feat(EditPresetDialog.jsx): add Examples component for Google endpoint feat(EditPresetDialog.jsx): add button to show/hide Examples component feat(EditPresetDialog.jsx): add functionality to add, remove, and edit examples in Examples component feat(EndpointOptionsDialog.jsx): change endpoint name to PaLM for Google endpoint feat(Settings.jsx): add maxHeight prop to limit height of Settings component in EditPresetDialog and EndpointOptionsDialog fix(Settings.jsx): add examples prop to ChatGPTBrowser component fix(EndpointItem.jsx): add alternate name for google endpoint fix(MessageHeader.jsx): change title for google endpoint to PaLM feat(endpoints.js): add google endpoint to endpointsConfig fix(cleanupPreset.js): add missing comma in examples array * chore: change endpoint order * feat(PaLM 2): complete for testing * fix(PaLM): handle blocked messages
2023-05-13 16:29:06 -04:00
const endpoint = ['openAI', 'azureOpenAI', 'bingAI', 'chatGPTBrowser', 'google'].find(e => endpointsConfig?.[e]);
if (endpoint) {
conversation = buildDefaultConversation({ conversation, endpoint, endpointsConfig });
return conversation;
} else {
conversation = buildDefaultConversation({ conversation, endpoint: null, endpointsConfig });
return conversation;
}
};
export default getDefaultConversation;