2023-04-05 21:21:02 +08:00
|
|
|
const buildDefaultConversation = ({
|
|
|
|
|
conversation,
|
|
|
|
|
endpoint,
|
2023-04-10 00:41:34 +08:00
|
|
|
endpointsConfig = {},
|
2023-04-05 21:21:02 +08:00
|
|
|
lastConversationSetup = {}
|
|
|
|
|
}) => {
|
2023-04-11 21:33:14 -04:00
|
|
|
const lastSelectedModel = JSON.parse(localStorage.getItem('lastSelectedModel')) || {};
|
|
|
|
|
|
2023-03-31 00:20:45 +08:00
|
|
|
if (endpoint === 'azureOpenAI' || endpoint === 'openAI') {
|
|
|
|
|
conversation = {
|
|
|
|
|
...conversation,
|
|
|
|
|
endpoint,
|
2023-04-05 21:21:02 +08:00
|
|
|
model:
|
2023-04-11 21:33:14 -04:00
|
|
|
lastConversationSetup?.model ??
|
|
|
|
|
lastSelectedModel[endpoint] ??
|
|
|
|
|
endpointsConfig[endpoint]?.availableModels?.[0] ??
|
|
|
|
|
'gpt-3.5-turbo',
|
2023-04-05 17:25:35 +08:00
|
|
|
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
|
2023-03-31 00:20:45 +08:00
|
|
|
};
|
|
|
|
|
} else if (endpoint === 'bingAI') {
|
|
|
|
|
conversation = {
|
|
|
|
|
...conversation,
|
|
|
|
|
endpoint,
|
2023-04-05 17:25:35 +08:00
|
|
|
jailbreak: lastConversationSetup?.jailbreak ?? false,
|
|
|
|
|
context: lastConversationSetup?.context ?? null,
|
|
|
|
|
systemMessage: lastConversationSetup?.systemMessage ?? null,
|
|
|
|
|
toneStyle: lastConversationSetup?.toneStyle ?? 'fast',
|
|
|
|
|
jailbreakConversationId: lastConversationSetup?.jailbreakConversationId ?? null,
|
2023-03-31 04:22:16 +08:00
|
|
|
conversationSignature: null,
|
|
|
|
|
clientId: null,
|
2023-04-05 02:46:22 +08:00
|
|
|
invocationId: 1
|
2023-03-31 00:20:45 +08:00
|
|
|
};
|
|
|
|
|
} else if (endpoint === 'chatGPTBrowser') {
|
|
|
|
|
conversation = {
|
|
|
|
|
...conversation,
|
|
|
|
|
endpoint,
|
2023-04-05 21:21:02 +08:00
|
|
|
model:
|
|
|
|
|
lastConversationSetup?.model ??
|
2023-04-11 21:33:14 -04:00
|
|
|
lastSelectedModel[endpoint] ??
|
2023-04-10 00:41:34 +08:00
|
|
|
endpointsConfig[endpoint]?.availableModels?.[0] ??
|
2023-04-05 21:21:02 +08:00
|
|
|
'text-davinci-002-render-sha'
|
2023-03-31 00:20:45 +08:00
|
|
|
};
|
|
|
|
|
} 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-04-10 00:41:34 +08:00
|
|
|
const getDefaultConversation = ({ conversation, prevConversation, endpointsConfig, preset }) => {
|
2023-04-02 04:15:07 +08:00
|
|
|
const { endpoint: targetEndpoint } = preset || {};
|
|
|
|
|
|
2023-03-31 04:22:16 +08:00
|
|
|
if (targetEndpoint) {
|
2023-04-05 03:49:54 +08:00
|
|
|
// try to use preset
|
2023-03-31 04:22:16 +08:00
|
|
|
const endpoint = targetEndpoint;
|
2023-04-10 00:41:34 +08:00
|
|
|
if (endpointsConfig?.[endpoint]) {
|
2023-03-31 04:22:16 +08:00
|
|
|
conversation = buildDefaultConversation({
|
|
|
|
|
conversation,
|
|
|
|
|
endpoint,
|
2023-04-05 21:21:02 +08:00
|
|
|
lastConversationSetup: preset,
|
2023-04-10 00:41:34 +08:00
|
|
|
endpointsConfig
|
2023-03-31 04:22:16 +08:00
|
|
|
});
|
|
|
|
|
return conversation;
|
|
|
|
|
} else {
|
|
|
|
|
console.log(endpoint);
|
2023-04-10 00:41:34 +08:00
|
|
|
console.warn(`Illegal target endpoint ${targetEndpoint} ${endpointsConfig}`);
|
2023-03-31 04:22:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 03:49:54 +08:00
|
|
|
// try {
|
|
|
|
|
// // try to use current model
|
|
|
|
|
// const { endpoint = null } = prevConversation || {};
|
2023-04-10 00:41:34 +08:00
|
|
|
// if (endpointsConfig?.[endpoint]) {
|
2023-04-05 03:49:54 +08:00
|
|
|
// conversation = buildDefaultConversation({
|
|
|
|
|
// conversation,
|
|
|
|
|
// endpoint,
|
2023-04-05 21:21:02 +08:00
|
|
|
// lastConversationSetup: prevConversation,
|
2023-04-10 00:41:34 +08:00
|
|
|
// endpointsConfig
|
2023-04-05 03:49:54 +08:00
|
|
|
// });
|
|
|
|
|
// return conversation;
|
|
|
|
|
// }
|
|
|
|
|
// } catch (error) {}
|
2023-03-31 00:20:45 +08:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// try to read latest selected model from local storage
|
|
|
|
|
const lastConversationSetup = JSON.parse(localStorage.getItem('lastConversationSetup'));
|
|
|
|
|
const { endpoint = null } = lastConversationSetup;
|
|
|
|
|
|
2023-04-10 00:41:34 +08:00
|
|
|
if (endpointsConfig?.[endpoint]) {
|
|
|
|
|
conversation = buildDefaultConversation({ conversation, endpoint, endpointsConfig });
|
2023-03-31 00:20:45 +08:00
|
|
|
return conversation;
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {}
|
|
|
|
|
|
|
|
|
|
// if anything happens, reset to default model
|
2023-03-31 04:22:16 +08:00
|
|
|
|
2023-04-10 00:41:34 +08:00
|
|
|
const endpoint = ['openAI', 'azureOpenAI', 'bingAI', 'chatGPTBrowser'].find(e => endpointsConfig?.[e]);
|
2023-03-31 00:20:45 +08:00
|
|
|
if (endpoint) {
|
2023-04-10 00:41:34 +08:00
|
|
|
conversation = buildDefaultConversation({ conversation, endpoint, endpointsConfig });
|
2023-03-31 00:20:45 +08:00
|
|
|
return conversation;
|
|
|
|
|
} else {
|
2023-04-10 00:41:34 +08:00
|
|
|
conversation = buildDefaultConversation({ conversation, endpoint: null, endpointsConfig });
|
2023-03-31 00:20:45 +08:00
|
|
|
return conversation;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default getDefaultConversation;
|