mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-18 17:30:16 +01:00
Merge pull request #102 from danny-avila/bing-hotfix
bing hotfix, latest api, uses sydney
This commit is contained in:
commit
9358a4fdb5
8 changed files with 59 additions and 32 deletions
|
|
@ -2,7 +2,7 @@ require('dotenv').config();
|
||||||
const { KeyvFile } = require('keyv-file');
|
const { KeyvFile } = require('keyv-file');
|
||||||
|
|
||||||
const askBing = async ({ text, onProgress, convo }) => {
|
const askBing = async ({ text, onProgress, convo }) => {
|
||||||
const { BingAIClient } = (await import('@waylaidwanderer/chatgpt-api'));
|
const { BingAIClient } = (await import('chatgpt-latest'));
|
||||||
|
|
||||||
const bingAIClient = new BingAIClient({
|
const bingAIClient = new BingAIClient({
|
||||||
// "_U" cookie from bing.com
|
// "_U" cookie from bing.com
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ require('dotenv').config();
|
||||||
const { KeyvFile } = require('keyv-file');
|
const { KeyvFile } = require('keyv-file');
|
||||||
|
|
||||||
const askSydney = async ({ text, onProgress, convo }) => {
|
const askSydney = async ({ text, onProgress, convo }) => {
|
||||||
const { BingAIClient } = (await import('@waylaidwanderer/chatgpt-api'));
|
const { BingAIClient } = (await import('chatgpt-latest'));
|
||||||
|
|
||||||
const sydneyClient = new BingAIClient({
|
const sydneyClient = new BingAIClient({
|
||||||
// "_U" cookie from bing.com
|
// "_U" cookie from bing.com
|
||||||
|
|
|
||||||
|
|
@ -83,6 +83,23 @@ module.exports = {
|
||||||
return { message: 'Error saving message' };
|
return { message: 'Error saving message' };
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
saveBingMessage: async ({ messageId, oldMessageId = messageId, conversationId, parentMessageId, sender, text, isCreatedByUser=false, error }) => {
|
||||||
|
try {
|
||||||
|
await Message.findOneAndUpdate({ messageId: oldMessageId }, {
|
||||||
|
messageId,
|
||||||
|
conversationId,
|
||||||
|
parentMessageId,
|
||||||
|
sender,
|
||||||
|
text,
|
||||||
|
isCreatedByUser,
|
||||||
|
error
|
||||||
|
}, { upsert: true, new: true });
|
||||||
|
return { messageId, conversationId, parentMessageId, sender, text, isCreatedByUser };
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return { message: 'Error saving message' };
|
||||||
|
}
|
||||||
|
},
|
||||||
deleteMessagesSince: async ({ messageId, conversationId }) => {
|
deleteMessagesSince: async ({ messageId, conversationId }) => {
|
||||||
try {
|
try {
|
||||||
const message = await Message.findOne({ messageId }).exec()
|
const message = await Message.findOne({ messageId }).exec()
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
const { getMessages, saveMessage, deleteMessagesSince, deleteMessages } = require('./Message');
|
const { getMessages, saveMessage, saveBingMessage, deleteMessagesSince, deleteMessages } = require('./Message');
|
||||||
const { getCustomGpts, updateCustomGpt, updateByLabel, deleteCustomGpts } = require('./CustomGpt');
|
const { getCustomGpts, updateCustomGpt, updateByLabel, deleteCustomGpts } = require('./CustomGpt');
|
||||||
const { getConvoTitle, getConvo, saveConvo } = require('./Conversation');
|
const { getConvoTitle, getConvo, saveConvo } = require('./Conversation');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getMessages,
|
getMessages,
|
||||||
saveMessage,
|
saveMessage,
|
||||||
|
saveBingMessage,
|
||||||
deleteMessagesSince,
|
deleteMessagesSince,
|
||||||
deleteMessages,
|
deleteMessages,
|
||||||
getConvoTitle,
|
getConvoTitle,
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ const express = require('express');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const { titleConvo, askBing } = require('../../app/');
|
const { titleConvo, askBing } = require('../../app/');
|
||||||
const { saveMessage, getConvoTitle, saveConvo } = require('../../models');
|
const { saveBingMessage, getConvoTitle, saveConvo } = require('../../models');
|
||||||
const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers');
|
const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers');
|
||||||
|
|
||||||
router.post('/', async (req, res) => {
|
router.post('/', async (req, res) => {
|
||||||
|
|
@ -21,7 +21,7 @@ router.post('/', async (req, res) => {
|
||||||
const conversationId = oldConversationId || crypto.randomUUID();
|
const conversationId = oldConversationId || crypto.randomUUID();
|
||||||
const isNewConversation = !oldConversationId;
|
const isNewConversation = !oldConversationId;
|
||||||
|
|
||||||
const userMessageId = crypto.randomUUID();
|
const userMessageId = convo.messageId;
|
||||||
const userParentMessageId = parentMessageId || '00000000-0000-0000-0000-000000000000';
|
const userParentMessageId = parentMessageId || '00000000-0000-0000-0000-000000000000';
|
||||||
let userMessage = {
|
let userMessage = {
|
||||||
messageId: userMessageId,
|
messageId: userMessageId,
|
||||||
|
|
@ -34,13 +34,13 @@ router.post('/', async (req, res) => {
|
||||||
|
|
||||||
console.log('ask log', {
|
console.log('ask log', {
|
||||||
model,
|
model,
|
||||||
...userMessage,
|
...convo,
|
||||||
...convo
|
...userMessage
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!overrideParentMessageId) {
|
if (!overrideParentMessageId) {
|
||||||
await saveMessage(userMessage);
|
await saveBingMessage(userMessage);
|
||||||
await saveConvo(req?.session?.user?.username, { ...userMessage, model, ...convo });
|
await saveConvo(req?.session?.user?.username, { model, ...convo, ...userMessage });
|
||||||
}
|
}
|
||||||
|
|
||||||
return await ask({
|
return await ask({
|
||||||
|
|
@ -114,8 +114,9 @@ const ask = async ({
|
||||||
convo.conversationSignature || response.conversationSignature;
|
convo.conversationSignature || response.conversationSignature;
|
||||||
userMessage.conversationId = response.conversationId || conversationId;
|
userMessage.conversationId = response.conversationId || conversationId;
|
||||||
userMessage.invocationId = response.invocationId;
|
userMessage.invocationId = response.invocationId;
|
||||||
|
userMessage.messageId = response.details.requestId || userMessageId;
|
||||||
if (!overrideParentMessageId)
|
if (!overrideParentMessageId)
|
||||||
await saveMessage(userMessage);
|
await saveBingMessage({ oldMessageId: userMessageId, ...userMessage });
|
||||||
|
|
||||||
// Bing API will not use our conversationId at the first time,
|
// Bing API will not use our conversationId at the first time,
|
||||||
// so change the placeholder conversationId to the real one.
|
// so change the placeholder conversationId to the real one.
|
||||||
|
|
@ -140,13 +141,14 @@ const ask = async ({
|
||||||
response.sender = model;
|
response.sender = model;
|
||||||
// response.final = true;
|
// response.final = true;
|
||||||
|
|
||||||
|
response.messageId = response.details.messageId;
|
||||||
// override the parentMessageId, for the regeneration.
|
// override the parentMessageId, for the regeneration.
|
||||||
response.parentMessageId =
|
response.parentMessageId =
|
||||||
overrideParentMessageId || response.parentMessageId || userMessageId;
|
overrideParentMessageId || response.details.requestId || userMessageId;
|
||||||
|
|
||||||
response.text = await handleText(response, true);
|
response.text = await handleText(response, true);
|
||||||
await saveMessage(response);
|
await saveBingMessage(response);
|
||||||
await saveConvo(req?.session?.user?.username, { ...response, model, chatGptLabel: null, promptPrefix: null, ...convo });
|
await saveConvo(req?.session?.user?.username, { model, chatGptLabel: null, promptPrefix: null, ...convo, ...response });
|
||||||
|
|
||||||
sendMessage(res, {
|
sendMessage(res, {
|
||||||
title: await getConvoTitle(req?.session?.user?.username, conversationId),
|
title: await getConvoTitle(req?.session?.user?.username, conversationId),
|
||||||
|
|
@ -178,7 +180,7 @@ const ask = async ({
|
||||||
error: true,
|
error: true,
|
||||||
text: error.message
|
text: error.message
|
||||||
};
|
};
|
||||||
await saveMessage(errorMessage);
|
await saveBingMessage(errorMessage);
|
||||||
handleError(res, errorMessage);
|
handleError(res, errorMessage);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ const express = require('express');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const { titleConvo, askSydney } = require('../../app/');
|
const { titleConvo, askSydney } = require('../../app/');
|
||||||
const { saveMessage, saveConvo, getConvoTitle } = require('../../models');
|
const { saveBingMessage, saveConvo, getConvoTitle } = require('../../models');
|
||||||
const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers');
|
const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers');
|
||||||
|
|
||||||
router.post('/', async (req, res) => {
|
router.post('/', async (req, res) => {
|
||||||
|
|
@ -21,7 +21,7 @@ router.post('/', async (req, res) => {
|
||||||
const conversationId = oldConversationId || crypto.randomUUID();
|
const conversationId = oldConversationId || crypto.randomUUID();
|
||||||
const isNewConversation = !oldConversationId;
|
const isNewConversation = !oldConversationId;
|
||||||
|
|
||||||
const userMessageId = crypto.randomUUID();
|
const userMessageId = convo.messageId;
|
||||||
const userParentMessageId = parentMessageId || '00000000-0000-0000-0000-000000000000';
|
const userParentMessageId = parentMessageId || '00000000-0000-0000-0000-000000000000';
|
||||||
let userMessage = {
|
let userMessage = {
|
||||||
messageId: userMessageId,
|
messageId: userMessageId,
|
||||||
|
|
@ -34,13 +34,13 @@ router.post('/', async (req, res) => {
|
||||||
|
|
||||||
console.log('ask log', {
|
console.log('ask log', {
|
||||||
model,
|
model,
|
||||||
...userMessage,
|
...convo,
|
||||||
...convo
|
...userMessage
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!overrideParentMessageId) {
|
if (!overrideParentMessageId) {
|
||||||
await saveMessage(userMessage);
|
await saveBingMessage(userMessage);
|
||||||
await saveConvo(req?.session?.user?.username, { ...userMessage, model, ...convo });
|
await saveConvo(req?.session?.user?.username, { model, ...convo, ...userMessage });
|
||||||
}
|
}
|
||||||
|
|
||||||
return await ask({
|
return await ask({
|
||||||
|
|
@ -100,9 +100,9 @@ const ask = async ({
|
||||||
parentMessageId: overrideParentMessageId || userMessageId
|
parentMessageId: overrideParentMessageId || userMessageId
|
||||||
}),
|
}),
|
||||||
convo: {
|
convo: {
|
||||||
|
...convo,
|
||||||
parentMessageId: userParentMessageId,
|
parentMessageId: userParentMessageId,
|
||||||
conversationId,
|
conversationId
|
||||||
...convo
|
|
||||||
},
|
},
|
||||||
abortController
|
abortController
|
||||||
});
|
});
|
||||||
|
|
@ -114,9 +114,10 @@ const ask = async ({
|
||||||
convo.conversationSignature || response.conversationSignature;
|
convo.conversationSignature || response.conversationSignature;
|
||||||
userMessage.conversationId = response.conversationId || conversationId;
|
userMessage.conversationId = response.conversationId || conversationId;
|
||||||
userMessage.invocationId = response.invocationId;
|
userMessage.invocationId = response.invocationId;
|
||||||
|
userMessage.messageId = response.parentMessageId || userMessageId;
|
||||||
// Unlike gpt and bing, Sydney will never accept our given userMessage.messageId, it will generate its own one.
|
// Unlike gpt and bing, Sydney will never accept our given userMessage.messageId, it will generate its own one.
|
||||||
if (!overrideParentMessageId)
|
if (!overrideParentMessageId)
|
||||||
await saveMessage(userMessage);
|
await saveBingMessage({ oldMessageId: userMessageId, ...userMessage });
|
||||||
|
|
||||||
// Save sydney response
|
// Save sydney response
|
||||||
// response.id = response.messageId;
|
// response.id = response.messageId;
|
||||||
|
|
@ -140,7 +141,7 @@ const ask = async ({
|
||||||
// Save user message
|
// Save user message
|
||||||
userMessage.conversationId = response.conversationId || conversationId;
|
userMessage.conversationId = response.conversationId || conversationId;
|
||||||
if (!overrideParentMessageId)
|
if (!overrideParentMessageId)
|
||||||
await saveMessage(userMessage);
|
await saveBingMessage(userMessage);
|
||||||
|
|
||||||
// Bing API will not use our conversationId at the first time,
|
// Bing API will not use our conversationId at the first time,
|
||||||
// so change the placeholder conversationId to the real one.
|
// so change the placeholder conversationId to the real one.
|
||||||
|
|
@ -158,8 +159,8 @@ const ask = async ({
|
||||||
|
|
||||||
response.text = await handleText(response, true);
|
response.text = await handleText(response, true);
|
||||||
// Save sydney response & convo, then send
|
// Save sydney response & convo, then send
|
||||||
await saveMessage(response);
|
await saveBingMessage(response);
|
||||||
await saveConvo(req?.session?.user?.username, { ...response, model, chatGptLabel: null, promptPrefix: null, ...convo });
|
await saveConvo(req?.session?.user?.username, { model, chatGptLabel: null, promptPrefix: null, ...convo, ...response });
|
||||||
|
|
||||||
sendMessage(res, {
|
sendMessage(res, {
|
||||||
title: await getConvoTitle(req?.session?.user?.username, conversationId),
|
title: await getConvoTitle(req?.session?.user?.username, conversationId),
|
||||||
|
|
@ -191,7 +192,7 @@ const ask = async ({
|
||||||
error: true,
|
error: true,
|
||||||
text: error.message
|
text: error.message
|
||||||
};
|
};
|
||||||
await saveMessage(errorMessage);
|
await saveBingMessage(errorMessage);
|
||||||
handleError(res, errorMessage);
|
handleError(res, errorMessage);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -160,12 +160,12 @@ export default function TextChat({ messages }) {
|
||||||
} else if (model === 'bingai') {
|
} else if (model === 'bingai') {
|
||||||
console.log('Bing data:', data);
|
console.log('Bing data:', data);
|
||||||
const { title } = data;
|
const { title } = data;
|
||||||
const { conversationSignature, clientId, conversationId, invocationId } =
|
const { conversationSignature, clientId, conversationId, invocationId, parentMessageId } =
|
||||||
responseMessage;
|
responseMessage;
|
||||||
dispatch(
|
dispatch(
|
||||||
setConversation({
|
setConversation({
|
||||||
title,
|
title,
|
||||||
parentMessageId: null,
|
parentMessageId,
|
||||||
conversationSignature,
|
conversationSignature,
|
||||||
clientId,
|
clientId,
|
||||||
conversationId,
|
conversationId,
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,13 @@ const currentSlice = createSlice({
|
||||||
state.refreshConvoHint = state.refreshConvoHint + 1;
|
state.refreshConvoHint = state.refreshConvoHint + 1;
|
||||||
},
|
},
|
||||||
setConversation: (state, action) => {
|
setConversation: (state, action) => {
|
||||||
return { ...state, ...action.payload };
|
// return { ...state, ...action.payload };
|
||||||
|
|
||||||
|
for (const key in action.payload) {
|
||||||
|
if (Object.hasOwnProperty.call(action.payload, key)) {
|
||||||
|
state[key] = action.payload[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
setError: (state, action) => {
|
setError: (state, action) => {
|
||||||
state.error = action.payload;
|
state.error = action.payload;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue