feat: feat: new endpoint-style submit

This commit is contained in:
Wentao Lyu 2023-03-31 03:22:57 +08:00
parent 089ca5f120
commit adcc021c9e
22 changed files with 566 additions and 478 deletions

View file

@ -1,55 +1,22 @@
export default function createPayload(submission) {
const { conversation, messages, message, initialResponse, isRegenerate = false } = submission;
const { conversation, message, endpointOption } = submission;
const { conversationId } = conversation;
const { endpoint } = endpointOption;
const endpoint = `/api/ask`;
const {
model,
chatGptLabel,
promptPrefix,
jailbreakConversationId,
conversationId,
conversationSignature,
clientId,
invocationId,
toneStyle
} = conversation;
const endpointUrlMap = {
azureOpenAI: '/api/ask/azureOpenAI',
openAI: '/api/ask/openAI',
bingAI: '/api/ask/bingAI',
chatGPTBrowser: '/api/ask/chatGPTBrowser'
};
const server = endpointUrlMap[endpoint];
let payload = {
...message,
...{
model,
chatGptLabel,
promptPrefix,
conversationId
}
...endpointOption,
conversationId
};
// if (!payload.conversationId)
// if (convo?.conversationId && convo?.parentMessageId) {
// payload = {
// ...payload,
// conversationId: convo.conversationId,
// parentMessageId: convo.parentMessageId || '00000000-0000-0000-0000-000000000000'
// };
// }
const isBing = model === 'bingai' || model === 'sydney';
if (isBing && !conversationId) {
payload.toneStyle = toneStyle || 'fast';
}
if (isBing && conversationId) {
payload = {
...payload,
jailbreakConversationId,
conversationSignature,
clientId,
invocationId
};
}
let server = endpoint;
server = model === 'bingai' ? server + '/bing' : server;
server = model === 'sydney' ? server + '/sydney' : server;
return { server, payload };
}

View file

@ -19,7 +19,7 @@ const buildDefaultConversation = ({ conversation, endpoint, lastConversationSetu
conversationSignature: lastConversationSetup?.conversationSignature || null,
clientId: lastConversationSetup?.clientId || null,
invocationId: lastConversationSetup?.invocationId || null,
toneStyle: lastConversationSetup?.toneStyle || null,
toneStyle: lastConversationSetup?.toneStyle || 'fast',
suggestions: lastConversationSetup?.suggestions || []
};
} else if (endpoint === 'chatGPTBrowser') {
@ -33,6 +33,12 @@ const buildDefaultConversation = ({ conversation, endpoint, lastConversationSetu
...conversation,
endpoint
};
} else {
console.error(`Unknown endpoint ${endpoint}`);
conversation = {
...conversation,
endpoint: null
};
}
return conversation;

View file

@ -27,10 +27,7 @@ const getIcon = props => {
else if (!isCreatedByUser) {
const { endpoint, error } = props;
let icon = <GPTIcon size={size * 0.7} />;
let bg = 'grey';
let name = 'UNKNOWN';
let icon, bg, name;
if (endpoint === 'azureOpenAI') {
const { chatGptLabel } = props;
@ -59,6 +56,10 @@ const getIcon = props => {
icon = <GPTIcon size={size * 0.7} />;
bg = `grey`;
name = 'N/A';
} else {
icon = <GPTIcon size={size * 0.7} />;
bg = `grey`;
name = 'UNKNOWN';
}
return (

View file

@ -1,29 +1,14 @@
// import resetConvo from './resetConvo';
// import { useSelector, useDispatch } from 'react-redux';
// import { setNewConvo } from '~/store/convoSlice';
// import { setMessages } from '~/store/messageSlice';
// import { setSubmitState, setSubmission } from '~/store/submitSlice';
// import { setText } from '~/store/textSlice';
// import { setError } from '~/store/convoSlice';
import { v4 } from 'uuid';
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
import store from '~/store';
const useMessageHandler = () => {
// const dispatch = useDispatch();
// const convo = useSelector((state) => state.convo);
// const { initial } = useSelector((state) => state.models);
// const { messages } = useSelector((state) => state.messages);
// const { model, chatGptLabel, promptPrefix, isSubmitting } = useSelector((state) => state.submit);
// const { latestMessage, error } = convo;
const [currentConversation, setCurrentConversation] = useRecoilState(store.conversation) || {};
const currentConversation = useRecoilValue(store.conversation) || {};
const setSubmission = useSetRecoilState(store.submission);
const isSubmitting = useRecoilValue(store.isSubmitting);
const latestMessage = useRecoilValue(store.latestMessage);
const { error } = currentConversation;
const [messages, setMessages] = useRecoilState(store.messages);
@ -36,16 +21,53 @@ const useMessageHandler = () => {
}
// determine the model to be used
const { model = null, chatGptLabel = null, promptPrefix = null } = currentConversation;
const { endpoint } = currentConversation;
let endpointOption = {};
let responseSender = '';
if (endpoint === 'azureOpenAI' || endpoint === 'openAI') {
endpointOption = {
endpoint,
model: currentConversation?.model || 'gpt-3.5-turbo',
chatGptLabel: currentConversation?.chatGptLabel || null,
promptPrefix: currentConversation?.promptPrefix || null,
temperature: currentConversation?.temperature || 0.8,
top_p: currentConversation?.top_p || 1,
presence_penalty: currentConversation?.presence_penalty || 1
};
responseSender = endpointOption.chatGptLabel || 'ChatGPT';
} else if (endpoint === 'bingAI') {
endpointOption = {
endpoint,
jailbreak: currentConversation?.jailbreak || false,
jailbreakConversationId: currentConversation?.jailbreakConversationId || null,
conversationSignature: currentConversation?.conversationSignature || null,
clientId: currentConversation?.clientId || null,
invocationId: currentConversation?.invocationId || null,
toneStyle: currentConversation?.toneStyle || 'fast',
suggestions: currentConversation?.suggestions || []
};
responseSender = endpointOption.jailbreak ? 'Sydney' : 'BingAI';
} else if (endpoint === 'chatGPTBrowser') {
endpointOption = {
endpoint,
model: currentConversation?.model || 'text-davinci-002-render-sha'
};
responseSender = 'ChatGPT';
} else if (endpoint === null) {
console.error('No endpoint available');
return;
} else {
console.error(`Unknown endpoint ${endpoint}`);
return;
}
let currentMessages = messages;
// construct the query message
// this is not a real messageId, it is used as placeholder before real messageId returned
text = text.trim();
const fakeMessageId = v4();
// const isCustomModel = model === 'chatgptCustom' || !initial[model];
// const sender = model === 'chatgptCustom' ? chatGptLabel : model;
parentMessageId = parentMessageId || latestMessage?.messageId || '00000000-0000-0000-0000-000000000000';
let currentMessages = messages;
conversationId = conversationId || currentConversation?.conversationId;
if (conversationId == 'search') {
console.error('cannot send any message under search view!');
@ -68,7 +90,7 @@ const useMessageHandler = () => {
// construct the placeholder response message
const initialResponse = {
sender: chatGptLabel || model,
sender: responseSender,
text: '<span className="result-streaming">█</span>',
parentMessageId: isRegenerate ? messageId : fakeMessageId,
messageId: (isRegenerate ? messageId : fakeMessageId) + '_',
@ -79,16 +101,11 @@ const useMessageHandler = () => {
const submission = {
conversation: {
...currentConversation,
conversationId,
model,
chatGptLabel,
promptPrefix
conversationId
},
endpointOption,
message: {
...currentMsg,
model,
chatGptLabel,
promptPrefix,
overrideParentMessageId: isRegenerate ? messageId : null
},
messages: currentMessages,