update schema for bing options

This commit is contained in:
Daniel Avila 2023-02-19 21:06:21 -05:00
parent ac2e897709
commit b4e22936ba
5 changed files with 53 additions and 44 deletions

View file

@ -20,7 +20,7 @@ const davinciOptions = {
const askClient = async ({ model, text, progressCallback, convo }) => { const askClient = async ({ model, text, progressCallback, convo }) => {
// const clientOptions = model === 'chatgpt' ? proxyOptions : davinciOptions; // const clientOptions = model === 'chatgpt' ? proxyOptions : davinciOptions;
const ChatGPTClient = (await import('@waylaidwanderer/chatgpt-api')).default; const ChatGPTClient = (await import('@waylaidwanderer/chatgpt-api')).default;
const client = new ChatGPTClient(process.env.CHATGPT_TOKEN, davinciOptions, { const client = new ChatGPTClient(process.env.OPENAI_KEY, davinciOptions, {
store: new KeyvFile({ filename: 'cache.json' }) store: new KeyvFile({ filename: 'cache.json' })
}); });
let options = { let options = {

View file

@ -13,17 +13,21 @@ const convoSchema = mongoose.Schema({
}, },
title: { title: {
type: String, type: String,
default: 'New conversation', default: 'New conversation'
}, },
conversationSignature: { conversationSignature: {
type: String, type: String
}, },
clientId: { clientId: {
type: String, type: String
}, },
invocationId: { invocationId: {
type: String, type: String
}, },
model: {
type: String
},
suggestions: [{ type: String }],
messages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Message' }], messages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Message' }],
created: { created: {
type: Date, type: Date,
@ -35,10 +39,10 @@ const Conversation =
mongoose.models.Conversation || mongoose.model('Conversation', convoSchema); mongoose.models.Conversation || mongoose.model('Conversation', convoSchema);
module.exports = { module.exports = {
saveConvo: async ({ conversationId, parentMessageId, title }) => { saveConvo: async ({ conversationId, title, ...convo }) => {
try { try {
const messages = await getMessages({ conversationId }); const messages = await getMessages({ conversationId });
const update = { parentMessageId, messages }; const update = { ...convo, messages };
if (title) { if (title) {
update.title = title; update.title = title;
} }
@ -55,11 +59,9 @@ module.exports = {
}, },
updateConvo: async ({ conversationId, ...update }) => { updateConvo: async ({ conversationId, ...update }) => {
try { try {
return await Conversation.findOneAndUpdate( return await Conversation.findOneAndUpdate({ conversationId }, update, {
{ conversationId }, new: true
update, }).exec();
{ new: true }
).exec();
} catch (error) { } catch (error) {
console.log(error); console.log(error);
return { message: 'Error updating conversation' }; return { message: 'Error updating conversation' };
@ -67,7 +69,6 @@ module.exports = {
}, },
getConvos: async () => await Conversation.find({}).sort({ created: -1 }).exec(), getConvos: async () => await Conversation.find({}).sort({ created: -1 }).exec(),
deleteConvos: async (filter) => { deleteConvos: async (filter) => {
let deleteCount = await Conversation.deleteMany(filter).exec(); let deleteCount = await Conversation.deleteMany(filter).exec();
deleteCount.messages = await deleteMessages(filter); deleteCount.messages = await deleteMessages(filter);
return deleteCount; return deleteCount;

View file

@ -10,9 +10,19 @@ const messageSchema = mongoose.Schema({
type: String, type: String,
required: true required: true
}, },
conversationSignature: {
type: String,
// required: true
},
clientId: {
type: String,
},
invocationId: {
type: Number,
},
parentMessageId: { parentMessageId: {
type: String, type: String,
required: true // required: true
}, },
sender: { sender: {
type: String, type: String,

View file

@ -17,7 +17,7 @@ const sendMessage = (res, message) => {
}; };
router.post('/bing', async (req, res) => { router.post('/bing', async (req, res) => {
const { model, text, parentMessageId, conversationId } = req.body; const { model, text, conversationSignature, conversationId } = req.body;
if (!text.trim().includes(' ') && text.length < 5) { if (!text.trim().includes(' ') && text.length < 5) {
return handleError(res, 'Prompt empty or too short'); return handleError(res, 'Prompt empty or too short');
} }
@ -25,7 +25,7 @@ router.post('/bing', async (req, res) => {
const userMessageId = crypto.randomUUID(); const userMessageId = crypto.randomUUID();
let userMessage = { id: userMessageId, sender: 'User', text }; let userMessage = { id: userMessageId, sender: 'User', text };
console.log('ask log', { model, ...userMessage, parentMessageId, conversationId }); console.log('ask log', { model, ...userMessage, conversationSignature, conversationId });
res.writeHead(200, { res.writeHead(200, {
Connection: 'keep-alive', Connection: 'keep-alive',
@ -45,7 +45,7 @@ router.post('/bing', async (req, res) => {
let response = await askBing({ let response = await askBing({
text, text,
progressCallback, progressCallback
// convo: { // convo: {
// parentMessageId, // parentMessageId,
// conversationId // conversationId
@ -55,21 +55,11 @@ router.post('/bing', async (req, res) => {
console.log('CLIENT RESPONSE'); console.log('CLIENT RESPONSE');
console.dir(response, { depth: null }); console.dir(response, { depth: null });
// if (!parentMessageId) { userMessage.conversationSignature =
// response.title = await titleConvo(text, response.text); conversationSignature || response.conversationSignature;
// } userMessage.conversationId = conversationId || response.conversationId;
userMessage.invocationId = response.invocationId;
// if (!response.parentMessageId) { await saveMessage(userMessage);
// response.text = response.response;
// response.id = response.messageId;
// response.parentMessageId = response.messageId;
// userMessage.parentMessageId = parentMessageId ? parentMessageId : response.messageId;
// userMessage.conversationId = conversationId
// ? conversationId
// : response.conversationId;
// await saveMessage(userMessage);
// delete response.response;
// }
// if ( // if (
// (response.text.includes('2023') && !response.text.trim().includes(' ')) || // (response.text.includes('2023') && !response.text.trim().includes(' ')) ||
@ -79,15 +69,22 @@ router.post('/bing', async (req, res) => {
// return handleError(res, 'Prompt empty or too short'); // return handleError(res, 'Prompt empty or too short');
// } // }
response.sender = 'Bing'; if (!conversationSignature) {
response.title = await titleConvo(text, response.response);
}
response.text = response.response;
response.id = response.details.messageId;
response.suggestions = response.details.suggestedResponses.map((s) => s.text);
response.sender = model;
response.final = true; response.final = true;
// await saveMessage(response); await saveMessage(response);
// await saveConvo(response); await saveConvo(response);
sendMessage(res, response); sendMessage(res, response);
res.end(); res.end();
} catch (error) { } catch (error) {
console.log(error); console.log(error);
// await deleteMessages({ id: userMessageId }); await deleteMessages({ id: userMessageId });
handleError(res, error.message); handleError(res, error.message);
} }
}); });
@ -143,10 +140,6 @@ router.post('/', async (req, res) => {
console.log('CLIENT RESPONSE', gptResponse); console.log('CLIENT RESPONSE', gptResponse);
if (!parentMessageId) {
gptResponse.title = await titleConvo(text, gptResponse.text);
}
if (!gptResponse.parentMessageId) { if (!gptResponse.parentMessageId) {
gptResponse.text = gptResponse.response; gptResponse.text = gptResponse.response;
gptResponse.id = gptResponse.messageId; gptResponse.id = gptResponse.messageId;
@ -167,6 +160,9 @@ router.post('/', async (req, res) => {
return handleError(res, 'Prompt empty or too short'); return handleError(res, 'Prompt empty or too short');
} }
if (!parentMessageId) {
gptResponse.title = await titleConvo(text, gptResponse.text);
}
gptResponse.sender = model; gptResponse.sender = model;
gptResponse.final = true; gptResponse.final = true;
await saveMessage(gptResponse); await saveMessage(gptResponse);

View file

@ -5,7 +5,9 @@ const initialState = {
title: 'ChatGPT Clone', title: 'ChatGPT Clone',
conversationId: null, conversationId: null,
parentMessageId: null, parentMessageId: null,
// convos: [], conversationSignature: null,
clientId: null,
invocationId: null,
convosLoading: false, convosLoading: false,
}; };