feat: support user-provided token to bingAI and chatgptBrowser

This commit is contained in:
Wentao Lyu 2023-04-10 00:41:34 +08:00
parent a953fc9f2b
commit bbf2f8a6ca
22 changed files with 309 additions and 86 deletions

View file

@ -1,4 +1,4 @@
const cleanupPreset = ({ preset: _preset, endpointsFilter = {} }) => {
const cleanupPreset = ({ preset: _preset, endpointsConfig = {} }) => {
const { endpoint } = _preset;
let preset = {};
@ -6,7 +6,7 @@ const cleanupPreset = ({ preset: _preset, endpointsFilter = {} }) => {
preset = {
endpoint,
presetId: _preset?.presetId ?? null,
model: _preset?.model ?? endpointsFilter[endpoint]?.availableModels?.[0] ?? 'gpt-3.5-turbo',
model: _preset?.model ?? endpointsConfig[endpoint]?.availableModels?.[0] ?? 'gpt-3.5-turbo',
chatGptLabel: _preset?.chatGptLabel ?? null,
promptPrefix: _preset?.promptPrefix ?? null,
temperature: _preset?.temperature ?? 1,
@ -30,7 +30,7 @@ const cleanupPreset = ({ preset: _preset, endpointsFilter = {} }) => {
endpoint,
presetId: _preset?.presetId ?? null,
model:
_preset?.model ?? endpointsFilter[endpoint]?.availableModels?.[0] ?? 'text-davinci-002-render-sha',
_preset?.model ?? endpointsConfig[endpoint]?.availableModels?.[0] ?? 'text-davinci-002-render-sha',
title: _preset?.title ?? 'New Preset'
};
} else if (endpoint === null) {

View file

@ -1,7 +1,7 @@
const buildDefaultConversation = ({
conversation,
endpoint,
endpointsFilter = {},
endpointsConfig = {},
lastConversationSetup = {}
}) => {
if (endpoint === 'azureOpenAI' || endpoint === 'openAI') {
@ -9,7 +9,7 @@ const buildDefaultConversation = ({
...conversation,
endpoint,
model:
lastConversationSetup?.model ?? endpointsFilter[endpoint]?.availableModels?.[0] ?? 'gpt-3.5-turbo',
lastConversationSetup?.model ?? endpointsConfig[endpoint]?.availableModels?.[0] ?? 'gpt-3.5-turbo',
chatGptLabel: lastConversationSetup?.chatGptLabel ?? null,
promptPrefix: lastConversationSetup?.promptPrefix ?? null,
temperature: lastConversationSetup?.temperature ?? 1,
@ -36,7 +36,7 @@ const buildDefaultConversation = ({
endpoint,
model:
lastConversationSetup?.model ??
endpointsFilter[endpoint]?.availableModels?.[0] ??
endpointsConfig[endpoint]?.availableModels?.[0] ??
'text-davinci-002-render-sha'
};
} else if (endpoint === null) {
@ -55,35 +55,35 @@ const buildDefaultConversation = ({
return conversation;
};
const getDefaultConversation = ({ conversation, prevConversation, endpointsFilter, preset }) => {
const getDefaultConversation = ({ conversation, prevConversation, endpointsConfig, preset }) => {
const { endpoint: targetEndpoint } = preset || {};
if (targetEndpoint) {
// try to use preset
const endpoint = targetEndpoint;
if (endpointsFilter?.[endpoint]) {
if (endpointsConfig?.[endpoint]) {
conversation = buildDefaultConversation({
conversation,
endpoint,
lastConversationSetup: preset,
endpointsFilter
endpointsConfig
});
return conversation;
} else {
console.log(endpoint);
console.warn(`Illegal target endpoint ${targetEndpoint} ${endpointsFilter}`);
console.warn(`Illegal target endpoint ${targetEndpoint} ${endpointsConfig}`);
}
}
// try {
// // try to use current model
// const { endpoint = null } = prevConversation || {};
// if (endpointsFilter?.[endpoint]) {
// if (endpointsConfig?.[endpoint]) {
// conversation = buildDefaultConversation({
// conversation,
// endpoint,
// lastConversationSetup: prevConversation,
// endpointsFilter
// endpointsConfig
// });
// return conversation;
// }
@ -94,20 +94,20 @@ const getDefaultConversation = ({ conversation, prevConversation, endpointsFilte
const lastConversationSetup = JSON.parse(localStorage.getItem('lastConversationSetup'));
const { endpoint = null } = lastConversationSetup;
if (endpointsFilter?.[endpoint]) {
conversation = buildDefaultConversation({ conversation, endpoint, endpointsFilter });
if (endpointsConfig?.[endpoint]) {
conversation = buildDefaultConversation({ conversation, endpoint, endpointsConfig });
return conversation;
}
} catch (error) {}
// if anything happens, reset to default model
const endpoint = ['openAI', 'azureOpenAI', 'bingAI', 'chatGPTBrowser'].find(e => endpointsFilter?.[e]);
const endpoint = ['openAI', 'azureOpenAI', 'bingAI', 'chatGPTBrowser'].find(e => endpointsConfig?.[e]);
if (endpoint) {
conversation = buildDefaultConversation({ conversation, endpoint, endpointsFilter });
conversation = buildDefaultConversation({ conversation, endpoint, endpointsConfig });
return conversation;
} else {
conversation = buildDefaultConversation({ conversation, endpoint: null, endpointsFilter });
conversation = buildDefaultConversation({ conversation, endpoint: null, endpointsConfig });
return conversation;
}
};

View file

@ -7,7 +7,9 @@ const useMessageHandler = () => {
const currentConversation = useRecoilValue(store.conversation) || {};
const setSubmission = useSetRecoilState(store.submission);
const isSubmitting = useRecoilValue(store.isSubmitting);
const endpointsFilter = useRecoilValue(store.endpointsFilter);
const endpointsConfig = useRecoilValue(store.endpointsConfig);
const { getToken } = store.useToken(currentConversation?.endpoint);
const latestMessage = useRecoilValue(store.latestMessage);
@ -29,7 +31,7 @@ const useMessageHandler = () => {
endpointOption = {
endpoint,
model:
currentConversation?.model ?? endpointsFilter[endpoint]?.availableModels?.[0] ?? 'gpt-3.5-turbo',
currentConversation?.model ?? endpointsConfig[endpoint]?.availableModels?.[0] ?? 'gpt-3.5-turbo',
chatGptLabel: currentConversation?.chatGptLabel ?? null,
promptPrefix: currentConversation?.promptPrefix ?? null,
temperature: currentConversation?.temperature ?? 1,
@ -48,7 +50,8 @@ const useMessageHandler = () => {
jailbreakConversationId: currentConversation?.jailbreakConversationId ?? null,
conversationSignature: currentConversation?.conversationSignature ?? null,
clientId: currentConversation?.clientId ?? null,
invocationId: currentConversation?.invocationId ?? 1
invocationId: currentConversation?.invocationId ?? 1,
token: endpointsConfig[endpoint]?.userProvide ? getToken() : null
};
responseSender = endpointOption.jailbreak ? 'Sydney' : 'BingAI';
} else if (endpoint === 'chatGPTBrowser') {
@ -56,8 +59,9 @@ const useMessageHandler = () => {
endpoint,
model:
currentConversation?.model ??
endpointsFilter[endpoint]?.availableModels?.[0] ??
'text-davinci-002-render-sha'
endpointsConfig[endpoint]?.availableModels?.[0] ??
'text-davinci-002-render-sha',
token: endpointsConfig[endpoint]?.userProvide ? getToken() : null
};
responseSender = 'ChatGPT';
} else if (endpoint === null) {