2023-03-31 00:20:45 +08:00
|
|
|
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,
|
2023-04-01 23:27:44 +08:00
|
|
|
temperature: lastConversationSetup?.temperature || 1,
|
2023-03-31 00:20:45 +08:00
|
|
|
top_p: lastConversationSetup?.top_p || 1,
|
2023-04-01 23:27:44 +08:00
|
|
|
presence_penalty: lastConversationSetup?.presence_penalty || 0,
|
|
|
|
|
frequency_penalty: lastConversationSetup?.frequency_penalty || 0
|
2023-03-31 00:20:45 +08:00
|
|
|
};
|
|
|
|
|
} else if (endpoint === 'bingAI') {
|
|
|
|
|
conversation = {
|
|
|
|
|
...conversation,
|
|
|
|
|
endpoint,
|
|
|
|
|
jailbreak: lastConversationSetup?.jailbreak || false,
|
|
|
|
|
jailbreakConversationId: lastConversationSetup?.jailbreakConversationId || null,
|
2023-03-31 04:22:16 +08:00
|
|
|
conversationSignature: null,
|
|
|
|
|
clientId: null,
|
|
|
|
|
invocationId: 1,
|
2023-03-31 04:39:11 +08:00
|
|
|
toneStyle: lastConversationSetup?.toneStyle || 'fast'
|
2023-03-31 00:20:45 +08:00
|
|
|
};
|
|
|
|
|
} else if (endpoint === 'chatGPTBrowser') {
|
|
|
|
|
conversation = {
|
|
|
|
|
...conversation,
|
|
|
|
|
endpoint,
|
|
|
|
|
model: lastConversationSetup?.model || '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
|
|
|
|
|
};
|
2023-03-31 00:20:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return conversation;
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-31 04:22:16 +08:00
|
|
|
const getDefaultConversation = ({ conversation, prevConversation, endpointsFilter, targetEndpoint }) => {
|
|
|
|
|
if (targetEndpoint) {
|
|
|
|
|
// try to use current model
|
|
|
|
|
const endpoint = targetEndpoint;
|
|
|
|
|
if (endpointsFilter?.[endpoint]) {
|
|
|
|
|
conversation = buildDefaultConversation({
|
|
|
|
|
conversation,
|
|
|
|
|
endpoint,
|
|
|
|
|
lastConversationSetup: {}
|
|
|
|
|
});
|
|
|
|
|
return conversation;
|
|
|
|
|
} else {
|
|
|
|
|
console.log(endpoint);
|
|
|
|
|
console.warn(`Illegal target endpoint ${targetEndpoint} ${endpointsFilter}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-31 00:20:45 +08:00
|
|
|
try {
|
|
|
|
|
// try to use current model
|
|
|
|
|
const { endpoint = null } = prevConversation || {};
|
2023-03-31 04:22:16 +08:00
|
|
|
if (endpointsFilter?.[endpoint]) {
|
2023-03-31 00:20:45 +08:00
|
|
|
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;
|
|
|
|
|
|
2023-03-31 04:22:16 +08:00
|
|
|
if (endpointsFilter?.[endpoint]) {
|
2023-03-31 00:20:45 +08:00
|
|
|
conversation = buildDefaultConversation({ conversation, endpoint, lastConversationSetup });
|
|
|
|
|
return conversation;
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {}
|
|
|
|
|
|
|
|
|
|
// if anything happens, reset to default model
|
2023-03-31 04:22:16 +08:00
|
|
|
|
|
|
|
|
const endpoint = ['openAI', 'azureOpenAI', 'bingAI', 'chatGPTBrowser'].find(e => endpointsFilter?.[e]);
|
2023-03-31 00:20:45 +08:00
|
|
|
if (endpoint) {
|
|
|
|
|
conversation = buildDefaultConversation({ conversation, endpoint });
|
|
|
|
|
return conversation;
|
|
|
|
|
} else {
|
|
|
|
|
conversation = buildDefaultConversation({ conversation, endpoint: null });
|
|
|
|
|
return conversation;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default getDefaultConversation;
|