2023-03-28 20:36:21 +08:00
|
|
|
import { v4 } from 'uuid';
|
|
|
|
|
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
|
|
|
|
|
|
|
|
|
|
import store from '~/store';
|
2023-03-17 01:49:09 +08:00
|
|
|
|
|
|
|
|
const useMessageHandler = () => {
|
2023-03-31 03:22:57 +08:00
|
|
|
const currentConversation = useRecoilValue(store.conversation) || {};
|
2023-03-28 20:36:21 +08:00
|
|
|
const setSubmission = useSetRecoilState(store.submission);
|
|
|
|
|
const isSubmitting = useRecoilValue(store.isSubmitting);
|
2023-04-05 21:21:02 +08:00
|
|
|
const endpointsFilter = useRecoilValue(store.endpointsFilter);
|
2023-03-28 20:36:21 +08:00
|
|
|
|
|
|
|
|
const latestMessage = useRecoilValue(store.latestMessage);
|
|
|
|
|
|
|
|
|
|
const [messages, setMessages] = useRecoilState(store.messages);
|
|
|
|
|
|
|
|
|
|
const ask = (
|
|
|
|
|
{ text, parentMessageId = null, conversationId = null, messageId = null },
|
|
|
|
|
{ isRegenerate = false } = {}
|
|
|
|
|
) => {
|
2023-03-17 01:49:09 +08:00
|
|
|
if (!!isSubmitting || text === '') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-03-28 20:36:21 +08:00
|
|
|
|
|
|
|
|
// determine the model to be used
|
2023-03-31 03:22:57 +08:00
|
|
|
const { endpoint } = currentConversation;
|
|
|
|
|
let endpointOption = {};
|
|
|
|
|
let responseSender = '';
|
|
|
|
|
if (endpoint === 'azureOpenAI' || endpoint === 'openAI') {
|
|
|
|
|
endpointOption = {
|
|
|
|
|
endpoint,
|
2023-04-05 21:21:02 +08:00
|
|
|
model:
|
|
|
|
|
currentConversation?.model ?? endpointsFilter[endpoint]?.availableModels?.[0] ?? 'gpt-3.5-turbo',
|
2023-04-05 17:25:35 +08:00
|
|
|
chatGptLabel: currentConversation?.chatGptLabel ?? null,
|
|
|
|
|
promptPrefix: currentConversation?.promptPrefix ?? null,
|
|
|
|
|
temperature: currentConversation?.temperature ?? 1,
|
|
|
|
|
top_p: currentConversation?.top_p ?? 1,
|
|
|
|
|
presence_penalty: currentConversation?.presence_penalty ?? 0,
|
|
|
|
|
frequency_penalty: currentConversation?.frequency_penalty ?? 0
|
2023-03-31 03:22:57 +08:00
|
|
|
};
|
2023-04-05 17:25:35 +08:00
|
|
|
responseSender = endpointOption.chatGptLabel ?? 'ChatGPT';
|
2023-03-31 03:22:57 +08:00
|
|
|
} else if (endpoint === 'bingAI') {
|
|
|
|
|
endpointOption = {
|
|
|
|
|
endpoint,
|
2023-04-05 17:25:35 +08:00
|
|
|
jailbreak: currentConversation?.jailbreak ?? false,
|
|
|
|
|
systemMessage: currentConversation?.systemMessage ?? null,
|
|
|
|
|
context: currentConversation?.context ?? null,
|
|
|
|
|
toneStyle: currentConversation?.toneStyle ?? 'fast',
|
|
|
|
|
jailbreakConversationId: currentConversation?.jailbreakConversationId ?? null,
|
|
|
|
|
conversationSignature: currentConversation?.conversationSignature ?? null,
|
|
|
|
|
clientId: currentConversation?.clientId ?? null,
|
|
|
|
|
invocationId: currentConversation?.invocationId ?? 1
|
2023-03-31 03:22:57 +08:00
|
|
|
};
|
|
|
|
|
responseSender = endpointOption.jailbreak ? 'Sydney' : 'BingAI';
|
|
|
|
|
} else if (endpoint === 'chatGPTBrowser') {
|
|
|
|
|
endpointOption = {
|
|
|
|
|
endpoint,
|
2023-04-05 21:21:02 +08:00
|
|
|
model:
|
|
|
|
|
currentConversation?.model ??
|
|
|
|
|
endpointsFilter[endpoint]?.availableModels?.[0] ??
|
|
|
|
|
'text-davinci-002-render-sha'
|
2023-03-31 03:22:57 +08:00
|
|
|
};
|
|
|
|
|
responseSender = 'ChatGPT';
|
|
|
|
|
} else if (endpoint === null) {
|
|
|
|
|
console.error('No endpoint available');
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
console.error(`Unknown endpoint ${endpoint}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let currentMessages = messages;
|
2023-03-28 20:36:21 +08:00
|
|
|
|
|
|
|
|
// construct the query message
|
2023-03-17 01:49:09 +08:00
|
|
|
// this is not a real messageId, it is used as placeholder before real messageId returned
|
|
|
|
|
text = text.trim();
|
2023-03-20 01:51:07 -04:00
|
|
|
const fakeMessageId = v4();
|
2023-03-17 01:49:09 +08:00
|
|
|
parentMessageId = parentMessageId || latestMessage?.messageId || '00000000-0000-0000-0000-000000000000';
|
2023-03-28 20:36:21 +08:00
|
|
|
conversationId = conversationId || currentConversation?.conversationId;
|
|
|
|
|
if (conversationId == 'search') {
|
|
|
|
|
console.error('cannot send any message under search view!');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (conversationId == 'new') {
|
2023-03-17 01:49:09 +08:00
|
|
|
parentMessageId = '00000000-0000-0000-0000-000000000000';
|
|
|
|
|
currentMessages = [];
|
2023-03-28 20:36:21 +08:00
|
|
|
conversationId = null;
|
2023-03-17 01:49:09 +08:00
|
|
|
}
|
2023-03-28 20:36:21 +08:00
|
|
|
const currentMsg = {
|
|
|
|
|
sender: 'User',
|
|
|
|
|
text,
|
|
|
|
|
current: true,
|
|
|
|
|
isCreatedByUser: true,
|
|
|
|
|
parentMessageId,
|
|
|
|
|
conversationId,
|
|
|
|
|
messageId: fakeMessageId
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// construct the placeholder response message
|
|
|
|
|
const initialResponse = {
|
2023-03-31 03:22:57 +08:00
|
|
|
sender: responseSender,
|
2023-03-28 15:32:22 -04:00
|
|
|
text: '<span className="result-streaming">█</span>',
|
2023-03-28 20:36:21 +08:00
|
|
|
parentMessageId: isRegenerate ? messageId : fakeMessageId,
|
|
|
|
|
messageId: (isRegenerate ? messageId : fakeMessageId) + '_',
|
|
|
|
|
conversationId,
|
|
|
|
|
submitting: true
|
|
|
|
|
};
|
2023-03-17 01:49:09 +08:00
|
|
|
|
|
|
|
|
const submission = {
|
2023-03-28 20:36:21 +08:00
|
|
|
conversation: {
|
|
|
|
|
...currentConversation,
|
2023-03-31 03:22:57 +08:00
|
|
|
conversationId
|
2023-03-28 20:36:21 +08:00
|
|
|
},
|
2023-03-31 03:22:57 +08:00
|
|
|
endpointOption,
|
2023-03-28 20:36:21 +08:00
|
|
|
message: {
|
2023-03-17 01:49:09 +08:00
|
|
|
...currentMsg,
|
2023-03-28 20:36:21 +08:00
|
|
|
overrideParentMessageId: isRegenerate ? messageId : null
|
2023-03-17 01:49:09 +08:00
|
|
|
},
|
|
|
|
|
messages: currentMessages,
|
|
|
|
|
isRegenerate,
|
2023-03-28 20:36:21 +08:00
|
|
|
initialResponse
|
2023-03-17 01:49:09 +08:00
|
|
|
};
|
2023-03-18 01:00:38 +08:00
|
|
|
|
2023-03-17 01:49:09 +08:00
|
|
|
console.log('User Input:', text);
|
2023-03-18 01:00:38 +08:00
|
|
|
|
|
|
|
|
if (isRegenerate) {
|
2023-03-28 20:36:21 +08:00
|
|
|
setMessages([...currentMessages, initialResponse]);
|
2023-03-18 01:00:38 +08:00
|
|
|
} else {
|
2023-03-28 20:36:21 +08:00
|
|
|
setMessages([...currentMessages, currentMsg, initialResponse]);
|
2023-03-18 01:00:38 +08:00
|
|
|
}
|
2023-03-28 20:36:21 +08:00
|
|
|
setSubmission(submission);
|
|
|
|
|
};
|
2023-03-17 01:49:09 +08:00
|
|
|
|
|
|
|
|
const regenerate = ({ parentMessageId }) => {
|
|
|
|
|
const parentMessage = messages?.find(element => element.messageId == parentMessageId);
|
|
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
if (parentMessage && parentMessage.isCreatedByUser) ask({ ...parentMessage }, { isRegenerate: true });
|
|
|
|
|
else console.error('Failed to regenerate the message: parentMessage not found or not created by user.');
|
|
|
|
|
};
|
2023-03-17 01:49:09 +08:00
|
|
|
|
2023-03-17 03:13:42 +08:00
|
|
|
const stopGenerating = () => {
|
2023-03-28 20:36:21 +08:00
|
|
|
setSubmission(null);
|
|
|
|
|
};
|
2023-03-17 01:49:09 +08:00
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
return { ask, regenerate, stopGenerating };
|
|
|
|
|
};
|
2023-03-17 01:49:09 +08:00
|
|
|
|
|
|
|
|
export { useMessageHandler };
|