mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
fix: use new conversation Id
This commit is contained in:
parent
017447b064
commit
3b94a98719
4 changed files with 120 additions and 89 deletions
|
|
@ -35,21 +35,21 @@ router.post('/', async (req, res) => {
|
||||||
let endpointOption = {};
|
let endpointOption = {};
|
||||||
if (req.body?.jailbreak)
|
if (req.body?.jailbreak)
|
||||||
endpointOption = {
|
endpointOption = {
|
||||||
jailbreak: req.body?.jailbreak || false,
|
jailbreak: req.body?.jailbreak ?? false,
|
||||||
jailbreakConversationId: req.body?.jailbreakConversationId || null,
|
jailbreakConversationId: req.body?.jailbreakConversationId ?? null,
|
||||||
systemMessage: req.body?.systemMessage || null,
|
systemMessage: req.body?.systemMessage ?? null,
|
||||||
context: req.body?.context || null,
|
context: req.body?.context ?? null,
|
||||||
toneStyle: req.body?.toneStyle || 'fast'
|
toneStyle: req.body?.toneStyle ?? 'fast'
|
||||||
};
|
};
|
||||||
else
|
else
|
||||||
endpointOption = {
|
endpointOption = {
|
||||||
jailbreak: req.body?.jailbreak || false,
|
jailbreak: req.body?.jailbreak ?? false,
|
||||||
systemMessage: req.body?.systemMessage || null,
|
systemMessage: req.body?.systemMessage ?? null,
|
||||||
context: req.body?.context || null,
|
context: req.body?.context ?? null,
|
||||||
conversationSignature: req.body?.conversationSignature || null,
|
conversationSignature: req.body?.conversationSignature ?? null,
|
||||||
clientId: req.body?.clientId || null,
|
clientId: req.body?.clientId ?? null,
|
||||||
invocationId: req.body?.invocationId || null,
|
invocationId: req.body?.invocationId ?? null,
|
||||||
toneStyle: req.body?.toneStyle || 'fast'
|
toneStyle: req.body?.toneStyle ?? 'fast'
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log('ask log', {
|
console.log('ask log', {
|
||||||
|
|
@ -122,31 +122,23 @@ const ask = async ({
|
||||||
|
|
||||||
console.log('BING RESPONSE', response);
|
console.log('BING RESPONSE', response);
|
||||||
|
|
||||||
|
const newConversationId = endpointOption?.jailbreak
|
||||||
|
? response.jailbreakConversationId
|
||||||
|
: response.conversationId || conversationId;
|
||||||
|
const newUserMassageId = response.parentMessageId || userMessageId;
|
||||||
|
const newResponseMessageId = response.parentMessageId || response.details.requestId || userMessageId;
|
||||||
|
|
||||||
// STEP1 generate response message
|
// STEP1 generate response message
|
||||||
response.text = response.response || response.details.spokenText || '**Bing refused to answer.**';
|
response.text = response.response || response.details.spokenText || '**Bing refused to answer.**';
|
||||||
|
|
||||||
let responseMessage = {
|
let responseMessage = {
|
||||||
|
conversationId: newConversationId,
|
||||||
|
messageId: newResponseMessageId,
|
||||||
|
parentMessageId: overrideParentMessageId || newUserMassageId,
|
||||||
|
sender: endpointOption?.jailbreak ? 'Sydney' : 'BingAI',
|
||||||
text: await handleText(response, true),
|
text: await handleText(response, true),
|
||||||
suggestions:
|
suggestions: response.details.suggestedResponses && response.details.suggestedResponses.map(s => s.text)
|
||||||
response.details.suggestedResponses && response.details.suggestedResponses.map(s => s.text),
|
|
||||||
jailbreak: endpointOption?.jailbreak
|
|
||||||
};
|
};
|
||||||
// // response.text = await handleText(response, true);
|
|
||||||
// response.suggestions =
|
|
||||||
// response.details.suggestedResponses && response.details.suggestedResponses.map(s => s.text);
|
|
||||||
|
|
||||||
if (endpointOption?.jailbreak) {
|
|
||||||
responseMessage.conversationId = response.jailbreakConversationId;
|
|
||||||
responseMessage.messageId = response.messageId || response.details.messageId;
|
|
||||||
responseMessage.parentMessageId = overrideParentMessageId || response.parentMessageId || userMessageId;
|
|
||||||
responseMessage.sender = 'Sydney';
|
|
||||||
} else {
|
|
||||||
responseMessage.conversationId = response.conversationId;
|
|
||||||
responseMessage.messageId = response.messageId || response.details.messageId;
|
|
||||||
responseMessage.parentMessageId =
|
|
||||||
overrideParentMessageId || response.parentMessageId || response.details.requestId || userMessageId;
|
|
||||||
responseMessage.sender = 'BingAI';
|
|
||||||
}
|
|
||||||
|
|
||||||
await saveMessage(responseMessage);
|
await saveMessage(responseMessage);
|
||||||
|
|
||||||
|
|
@ -159,14 +151,22 @@ const ask = async ({
|
||||||
// Attition: the api will also create new conversationId while using invalid userMessage.parentMessageId,
|
// Attition: the api will also create new conversationId while using invalid userMessage.parentMessageId,
|
||||||
// but in this situation, don't change the conversationId, but create new convo.
|
// but in this situation, don't change the conversationId, but create new convo.
|
||||||
|
|
||||||
let conversationUpdate = { conversationId, endpoint: 'bingAI' };
|
let conversationUpdate = { conversationId: newConversationId, endpoint: 'bingAI' };
|
||||||
if (conversationId != responseMessage.conversationId && isNewConversation)
|
if (conversationId != newConversationId)
|
||||||
conversationUpdate = {
|
if (isNewConversation) {
|
||||||
...conversationUpdate,
|
// change the conversationId to new one
|
||||||
conversationId: conversationId,
|
conversationUpdate = {
|
||||||
newConversationId: responseMessage.conversationId || conversationId
|
...conversationUpdate,
|
||||||
};
|
conversationId: conversationId,
|
||||||
conversationId = responseMessage.conversationId || conversationId;
|
newConversationId: newConversationId
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
// create new conversation
|
||||||
|
conversationUpdate = {
|
||||||
|
...conversationUpdate,
|
||||||
|
...endpointOption
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
if (endpointOption?.jailbreak) {
|
if (endpointOption?.jailbreak) {
|
||||||
conversationUpdate.jailbreak = true;
|
conversationUpdate.jailbreak = true;
|
||||||
|
|
@ -179,17 +179,16 @@ const ask = async ({
|
||||||
}
|
}
|
||||||
|
|
||||||
await saveConvo(req?.session?.user?.username, conversationUpdate);
|
await saveConvo(req?.session?.user?.username, conversationUpdate);
|
||||||
|
conversationId = newConversationId;
|
||||||
|
|
||||||
// STEP3 update the user message
|
// STEP3 update the user message
|
||||||
userMessage.conversationId = conversationId;
|
userMessage.conversationId = newConversationId;
|
||||||
userMessage.messageId = responseMessage.parentMessageId;
|
userMessage.messageId = newUserMassageId;
|
||||||
|
|
||||||
// If response has parentMessageId, the fake userMessage.messageId should be updated to the real one.
|
// If response has parentMessageId, the fake userMessage.messageId should be updated to the real one.
|
||||||
if (!overrideParentMessageId) {
|
if (!overrideParentMessageId)
|
||||||
const oldUserMessageId = userMessageId;
|
await saveMessage({ ...userMessage, messageId: userMessageId, newMessageId: newUserMassageId });
|
||||||
await saveMessage({ ...userMessage, messageId: oldUserMessageId, newMessageId: userMessage.messageId });
|
userMessageId = newUserMassageId;
|
||||||
}
|
|
||||||
userMessageId = userMessage.messageId;
|
|
||||||
|
|
||||||
sendMessage(res, {
|
sendMessage(res, {
|
||||||
title: await getConvoTitle(req?.session?.user?.username, conversationId),
|
title: await getConvoTitle(req?.session?.user?.username, conversationId),
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ router.post('/', async (req, res) => {
|
||||||
|
|
||||||
// build endpoint option
|
// build endpoint option
|
||||||
const endpointOption = {
|
const endpointOption = {
|
||||||
model: req.body?.model || 'text-davinci-002-render-sha'
|
model: req.body?.model ?? 'text-davinci-002-render-sha'
|
||||||
};
|
};
|
||||||
|
|
||||||
const availableModels = getChatGPTBrowserModels();
|
const availableModels = getChatGPTBrowserModels();
|
||||||
|
|
@ -106,13 +106,17 @@ const ask = async ({
|
||||||
|
|
||||||
console.log('CLIENT RESPONSE', response);
|
console.log('CLIENT RESPONSE', response);
|
||||||
|
|
||||||
|
const newConversationId = response.conversationId || conversationId;
|
||||||
|
const newUserMassageId = response.parentMessageId || userMessageId;
|
||||||
|
const newResponseMessageId = response.messageId;
|
||||||
|
|
||||||
// STEP1 generate response message
|
// STEP1 generate response message
|
||||||
response.text = response.response || '**ChatGPT refused to answer.**';
|
response.text = response.response || '**ChatGPT refused to answer.**';
|
||||||
|
|
||||||
let responseMessage = {
|
let responseMessage = {
|
||||||
conversationId: response.conversationId,
|
conversationId: newConversationId,
|
||||||
messageId: response.messageId,
|
messageId: newResponseMessageId,
|
||||||
parentMessageId: overrideParentMessageId || response.parentMessageId || userMessageId,
|
parentMessageId: overrideParentMessageId || newUserMassageId,
|
||||||
text: await handleText(response),
|
text: await handleText(response),
|
||||||
sender: endpointOption?.chatGptLabel || 'ChatGPT'
|
sender: endpointOption?.chatGptLabel || 'ChatGPT'
|
||||||
};
|
};
|
||||||
|
|
@ -122,27 +126,34 @@ const ask = async ({
|
||||||
// STEP2 update the conversation
|
// STEP2 update the conversation
|
||||||
|
|
||||||
// First update conversationId if needed
|
// First update conversationId if needed
|
||||||
let conversationUpdate = { conversationId, endpoint: 'chatGPTBrowser' };
|
let conversationUpdate = { conversationId: newConversationId, endpoint: 'chatGPTBrowser' };
|
||||||
if (conversationId != responseMessage.conversationId && isNewConversation)
|
if (conversationId != newConversationId)
|
||||||
conversationUpdate = {
|
if (isNewConversation) {
|
||||||
...conversationUpdate,
|
// change the conversationId to new one
|
||||||
conversationId: conversationId,
|
conversationUpdate = {
|
||||||
newConversationId: responseMessage.conversationId || conversationId
|
...conversationUpdate,
|
||||||
};
|
conversationId: conversationId,
|
||||||
conversationId = responseMessage.conversationId || conversationId;
|
newConversationId: newConversationId
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
// create new conversation
|
||||||
|
conversationUpdate = {
|
||||||
|
...conversationUpdate,
|
||||||
|
...endpointOption
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
await saveConvo(req?.session?.user?.username, conversationUpdate);
|
await saveConvo(req?.session?.user?.username, conversationUpdate);
|
||||||
|
conversationId = newConversationId;
|
||||||
|
|
||||||
// STEP3 update the user message
|
// STEP3 update the user message
|
||||||
userMessage.conversationId = conversationId;
|
userMessage.conversationId = newConversationId;
|
||||||
userMessage.messageId = responseMessage.parentMessageId;
|
userMessage.messageId = newUserMassageId;
|
||||||
|
|
||||||
// If response has parentMessageId, the fake userMessage.messageId should be updated to the real one.
|
// If response has parentMessageId, the fake userMessage.messageId should be updated to the real one.
|
||||||
if (!overrideParentMessageId) {
|
if (!overrideParentMessageId)
|
||||||
const oldUserMessageId = userMessageId;
|
await saveMessage({ ...userMessage, messageId: userMessageId, newMessageId: newUserMassageId });
|
||||||
await saveMessage({ ...userMessage, messageId: oldUserMessageId, newMessageId: userMessage.messageId });
|
userMessageId = newUserMassageId;
|
||||||
}
|
|
||||||
userMessageId = userMessage.messageId;
|
|
||||||
|
|
||||||
sendMessage(res, {
|
sendMessage(res, {
|
||||||
title: await getConvoTitle(req?.session?.user?.username, conversationId),
|
title: await getConvoTitle(req?.session?.user?.username, conversationId),
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ router.post('/', async (req, res) => {
|
||||||
|
|
||||||
// build user message
|
// build user message
|
||||||
const conversationId = oldConversationId || crypto.randomUUID();
|
const conversationId = oldConversationId || crypto.randomUUID();
|
||||||
|
const isNewConversation = !oldConversationId;
|
||||||
const userMessageId = crypto.randomUUID();
|
const userMessageId = crypto.randomUUID();
|
||||||
const userParentMessageId = parentMessageId || '00000000-0000-0000-0000-000000000000';
|
const userParentMessageId = parentMessageId || '00000000-0000-0000-0000-000000000000';
|
||||||
const userMessage = {
|
const userMessage = {
|
||||||
|
|
@ -32,13 +33,13 @@ router.post('/', async (req, res) => {
|
||||||
|
|
||||||
// build endpoint option
|
// build endpoint option
|
||||||
const endpointOption = {
|
const endpointOption = {
|
||||||
model: req.body?.model || 'gpt-3.5-turbo',
|
model: req.body?.model ?? 'gpt-3.5-turbo',
|
||||||
chatGptLabel: req.body?.chatGptLabel || null,
|
chatGptLabel: req.body?.chatGptLabel ?? null,
|
||||||
promptPrefix: req.body?.promptPrefix || null,
|
promptPrefix: req.body?.promptPrefix ?? null,
|
||||||
temperature: req.body?.temperature || 1,
|
temperature: req.body?.temperature ?? 1,
|
||||||
top_p: req.body?.top_p || 1,
|
top_p: req.body?.top_p ?? 1,
|
||||||
presence_penalty: req.body?.presence_penalty || 0,
|
presence_penalty: req.body?.presence_penalty ?? 0,
|
||||||
frequency_penalty: req.body?.frequency_penalty || 0
|
frequency_penalty: req.body?.frequency_penalty ?? 0
|
||||||
};
|
};
|
||||||
|
|
||||||
const availableModels = getOpenAIModels();
|
const availableModels = getOpenAIModels();
|
||||||
|
|
@ -63,6 +64,7 @@ router.post('/', async (req, res) => {
|
||||||
|
|
||||||
// eslint-disable-next-line no-use-before-define
|
// eslint-disable-next-line no-use-before-define
|
||||||
return await ask({
|
return await ask({
|
||||||
|
isNewConversation,
|
||||||
userMessage,
|
userMessage,
|
||||||
endpointOption,
|
endpointOption,
|
||||||
conversationId,
|
conversationId,
|
||||||
|
|
@ -74,6 +76,7 @@ router.post('/', async (req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
const ask = async ({
|
const ask = async ({
|
||||||
|
isNewConversation,
|
||||||
userMessage,
|
userMessage,
|
||||||
endpointOption,
|
endpointOption,
|
||||||
conversationId,
|
conversationId,
|
||||||
|
|
@ -84,8 +87,6 @@ const ask = async ({
|
||||||
}) => {
|
}) => {
|
||||||
let { text, parentMessageId: userParentMessageId, messageId: userMessageId } = userMessage;
|
let { text, parentMessageId: userParentMessageId, messageId: userMessageId } = userMessage;
|
||||||
|
|
||||||
const client = askClient;
|
|
||||||
|
|
||||||
res.writeHead(200, {
|
res.writeHead(200, {
|
||||||
Connection: 'keep-alive',
|
Connection: 'keep-alive',
|
||||||
'Content-Type': 'text/event-stream',
|
'Content-Type': 'text/event-stream',
|
||||||
|
|
@ -100,7 +101,7 @@ const ask = async ({
|
||||||
const progressCallback = createOnProgress();
|
const progressCallback = createOnProgress();
|
||||||
const abortController = new AbortController();
|
const abortController = new AbortController();
|
||||||
res.on('close', () => abortController.abort());
|
res.on('close', () => abortController.abort());
|
||||||
let response = await client({
|
let response = await askClient({
|
||||||
text,
|
text,
|
||||||
parentMessageId: userParentMessageId,
|
parentMessageId: userParentMessageId,
|
||||||
conversationId,
|
conversationId,
|
||||||
|
|
@ -115,13 +116,17 @@ const ask = async ({
|
||||||
|
|
||||||
console.log('CLIENT RESPONSE', response);
|
console.log('CLIENT RESPONSE', response);
|
||||||
|
|
||||||
|
const newConversationId = response.conversationId || conversationId;
|
||||||
|
const newUserMassageId = response.parentMessageId || userMessageId;
|
||||||
|
const newResponseMessageId = response.messageId;
|
||||||
|
|
||||||
// STEP1 generate response message
|
// STEP1 generate response message
|
||||||
response.text = response.response || '**ChatGPT refused to answer.**';
|
response.text = response.response || '**ChatGPT refused to answer.**';
|
||||||
|
|
||||||
let responseMessage = {
|
let responseMessage = {
|
||||||
conversationId: response.conversationId,
|
conversationId: newConversationId,
|
||||||
messageId: response.messageId,
|
messageId: newResponseMessageId,
|
||||||
parentMessageId: overrideParentMessageId || userMessageId,
|
parentMessageId: overrideParentMessageId || newUserMassageId,
|
||||||
text: await handleText(response),
|
text: await handleText(response),
|
||||||
sender: endpointOption?.chatGptLabel || 'ChatGPT'
|
sender: endpointOption?.chatGptLabel || 'ChatGPT'
|
||||||
};
|
};
|
||||||
|
|
@ -129,21 +134,34 @@ const ask = async ({
|
||||||
await saveMessage(responseMessage);
|
await saveMessage(responseMessage);
|
||||||
|
|
||||||
// STEP2 update the conversation
|
// STEP2 update the conversation
|
||||||
conversationId = responseMessage.conversationId || conversationId;
|
let conversationUpdate = { conversationId: newConversationId, endpoint: 'openAI' };
|
||||||
// it seems openAI will not change the conversationId.
|
if (conversationId != newConversationId)
|
||||||
// let conversationUpdate = { conversationId, endpoint: 'openAI' };
|
if (isNewConversation) {
|
||||||
// await saveConvo(req?.session?.user?.username, conversationUpdate);
|
// change the conversationId to new one
|
||||||
|
conversationUpdate = {
|
||||||
|
...conversationUpdate,
|
||||||
|
conversationId: conversationId,
|
||||||
|
newConversationId: newConversationId
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
// create new conversation
|
||||||
|
conversationUpdate = {
|
||||||
|
...conversationUpdate,
|
||||||
|
...endpointOption
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
await saveConvo(req?.session?.user?.username, conversationUpdate);
|
||||||
|
conversationId = newConversationId;
|
||||||
|
|
||||||
// STEP3 update the user message
|
// STEP3 update the user message
|
||||||
userMessage.conversationId = conversationId;
|
userMessage.conversationId = newConversationId;
|
||||||
userMessage.messageId = responseMessage.parentMessageId;
|
userMessage.messageId = newUserMassageId;
|
||||||
|
|
||||||
// If response has parentMessageId, the fake userMessage.messageId should be updated to the real one.
|
// If response has parentMessageId, the fake userMessage.messageId should be updated to the real one.
|
||||||
if (!overrideParentMessageId) {
|
if (!overrideParentMessageId)
|
||||||
const oldUserMessageId = userMessageId;
|
await saveMessage({ ...userMessage, messageId: userMessageId, newMessageId: newUserMassageId });
|
||||||
await saveMessage({ ...userMessage, messageId: oldUserMessageId, newMessageId: userMessage.messageId });
|
userMessageId = newUserMassageId;
|
||||||
}
|
|
||||||
userMessageId = userMessage.messageId;
|
|
||||||
|
|
||||||
sendMessage(res, {
|
sendMessage(res, {
|
||||||
title: await getConvoTitle(req?.session?.user?.username, conversationId),
|
title: await getConvoTitle(req?.session?.user?.username, conversationId),
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import endpoints from './endpoints';
|
||||||
import { atom, selector, useSetRecoilState, useResetRecoilState, useRecoilCallback } from 'recoil';
|
import { atom, selector, useSetRecoilState, useResetRecoilState, useRecoilCallback } from 'recoil';
|
||||||
import buildTree from '~/utils/buildTree';
|
import buildTree from '~/utils/buildTree';
|
||||||
import getDefaultConversation from '~/utils/getDefaultConversation';
|
import getDefaultConversation from '~/utils/getDefaultConversation';
|
||||||
|
import submission from './submission.js';
|
||||||
|
|
||||||
// current conversation, can be null (need to be fetched from server)
|
// current conversation, can be null (need to be fetched from server)
|
||||||
// sample structure
|
// sample structure
|
||||||
|
|
@ -59,6 +60,7 @@ const latestMessage = atom({
|
||||||
const useConversation = () => {
|
const useConversation = () => {
|
||||||
const setConversation = useSetRecoilState(conversation);
|
const setConversation = useSetRecoilState(conversation);
|
||||||
const setMessages = useSetRecoilState(messages);
|
const setMessages = useSetRecoilState(messages);
|
||||||
|
const setSubmission = useSetRecoilState(submission.submission);
|
||||||
const resetLatestMessage = useResetRecoilState(latestMessage);
|
const resetLatestMessage = useResetRecoilState(latestMessage);
|
||||||
|
|
||||||
const switchToConversation = useRecoilCallback(
|
const switchToConversation = useRecoilCallback(
|
||||||
|
|
@ -93,6 +95,7 @@ const useConversation = () => {
|
||||||
|
|
||||||
setConversation(conversation);
|
setConversation(conversation);
|
||||||
setMessages(messages);
|
setMessages(messages);
|
||||||
|
setSubmission({});
|
||||||
resetLatestMessage();
|
resetLatestMessage();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue