LibreChat/client/src/utils/getDefaultConversation.js

105 lines
3.3 KiB
JavaScript
Raw Normal View History

const buildDefaultConversation = ({ conversation, endpoint, lastConversationSetup = {} }) => {
if (endpoint === 'azureOpenAI' || endpoint === 'openAI') {
conversation = {
...conversation,
endpoint,
model: lastConversationSetup?.model || '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
};
} else if (endpoint === 'bingAI') {
conversation = {
...conversation,
endpoint,
jailbreak: lastConversationSetup?.jailbreak || false,
systemMessage: lastConversationSetup?.systemMessage || null,
context: lastConversationSetup?.context || null,
jailbreakConversationId: lastConversationSetup?.jailbreakConversationId || null,
conversationSignature: null,
clientId: null,
invocationId: 1,
toneStyle: lastConversationSetup?.toneStyle || 'fast'
};
} else if (endpoint === 'chatGPTBrowser') {
conversation = {
...conversation,
endpoint,
model: lastConversationSetup?.model || 'Default (GPT-3.5)'
};
} 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;
};
2023-04-02 04:15:07 +08:00
const getDefaultConversation = ({ conversation, prevConversation, endpointsFilter, preset }) => {
const { endpoint: targetEndpoint } = preset || {};
if (targetEndpoint) {
// try to use current model
const endpoint = targetEndpoint;
if (endpointsFilter?.[endpoint]) {
conversation = buildDefaultConversation({
conversation,
endpoint,
2023-04-02 04:15:07 +08:00
lastConversationSetup: preset
});
return conversation;
} else {
console.log(endpoint);
console.warn(`Illegal target endpoint ${targetEndpoint} ${endpointsFilter}`);
}
}
try {
// try to use current model
const { endpoint = null } = prevConversation || {};
if (endpointsFilter?.[endpoint]) {
conversation = buildDefaultConversation({
conversation,
endpoint,
lastConversationSetup: prevConversation
});
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 (endpointsFilter?.[endpoint]) {
conversation = buildDefaultConversation({ conversation, endpoint, lastConversationSetup });
return conversation;
}
} catch (error) {}
// if anything happens, reset to default model
const endpoint = ['openAI', 'azureOpenAI', 'bingAI', 'chatGPTBrowser'].find(e => endpointsFilter?.[e]);
if (endpoint) {
conversation = buildDefaultConversation({ conversation, endpoint });
return conversation;
} else {
conversation = buildDefaultConversation({ conversation, endpoint: null });
return conversation;
}
};
export default getDefaultConversation;