From dd825dc6d44598be47ba17d225c907607e6b7520 Mon Sep 17 00:00:00 2001 From: Wentao Lyu <35-wentao.lyu@users.noreply.git.stereye.tech> Date: Fri, 31 Mar 2023 00:20:18 +0800 Subject: [PATCH] feat: new endpoint-style structure in server side --- api/lib/db/migrateDb.js | 70 +++++++++++++++++++++++++++---- api/models/schema/convoSchema.js | 72 +++++++++++++++++++++++--------- 2 files changed, 114 insertions(+), 28 deletions(-) diff --git a/api/lib/db/migrateDb.js b/api/lib/db/migrateDb.js index 315f7094bf..24808acb4d 100644 --- a/api/lib/db/migrateDb.js +++ b/api/lib/db/migrateDb.js @@ -1,13 +1,14 @@ const mongoose = require('mongoose'); -const { Conversation, } = require('../../models/Conversation'); -const { getMessages, } = require('../../models/'); +const { Conversation } = require('../../models/Conversation'); +const { getMessages } = require('../../models/'); -async function migrateDb() { +const migrateToStrictFollowParentMessageIdChain = async () => { try { - const conversations = await Conversation.find({ model: null }).exec(); + const conversations = await Conversation.find({ endpoint: null, model: null }).exec(); - if (!conversations || conversations.length === 0) - return { message: '[Migrate] No conversations to migrate' }; + if (!conversations || conversations.length === 0) return { noNeed: true }; + + console.log('Migration: To strict follow the parentMessageId chain.'); for (let convo of conversations) { const messages = await getMessages({ @@ -36,7 +37,7 @@ async function migrateDb() { if (message.sender.toLowerCase() === 'user') { message.isCreatedByUser = true; } - + promises.push(message.save()); }); await Promise.all(promises); @@ -57,7 +58,58 @@ async function migrateDb() { console.log(error); return { message: '[Migrate] Error migrating conversations' }; } +}; + +const migrateToSupportBetterCustomization = async () => { + try { + const conversations = await Conversation.find({ endpoint: null }).exec(); + + if (!conversations || conversations.length === 0) return { noNeed: true }; + + console.log('Migration: To support better customization.'); + + const promises = []; + for (let convo of conversations) { + const originalModel = convo?.model; + + if (originalModel === 'chatgpt') { + convo.endpoint = 'openAI'; + convo.model = 'gpt-3.5-turbo'; + } else if (originalModel === 'chatgptCustom') { + convo.endpoint = 'openAI'; + convo.model = 'gpt-3.5-turbo'; + } else if (originalModel === 'bingai') { + convo.endpoint = 'bingAI'; + convo.model = null; + convo.jailbreak = false; + } else if (originalModel === 'sydney') { + convo.endpoint = 'bingAI'; + convo.model = null; + convo.jailbreak = true; + } else if (originalModel === 'chatgptBrowser') { + convo.endpoint = 'chatGPTBrowser'; + convo.model = 'text-davinci-002-render-sha'; + convo.jailbreak = true; + } + + promises.push(convo.save()); + } + + await Promise.all(promises); + } catch (error) { + console.log(error); + return { message: '[Migrate] Error migrating conversations' }; + } +}; + +async function migrateDb() { + let ret = []; + ret[0] = await migrateToStrictFollowParentMessageIdChain(); + ret[1] = await migrateToSupportBetterCustomization(); + + const isMigrated = !!ret.find(element => !element?.noNeed); + + if (!isMigrated) console.log('[Migrate] Nothing to migrate'); } - -module.exports = migrateDb; \ No newline at end of file +module.exports = migrateDb; diff --git a/api/models/schema/convoSchema.js b/api/models/schema/convoSchema.js index 79e621ef8e..47f0d6df90 100644 --- a/api/models/schema/convoSchema.js +++ b/api/models/schema/convoSchema.js @@ -18,6 +18,54 @@ const convoSchema = mongoose.Schema( default: 'New Chat', meiliIndex: true }, + user: { + type: String, + default: null + }, + messages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Message' }], + // endpoint: [azureOpenAI, openAI, bingAI, chatGPTBrowser] + endpoint: { + type: String, + default: null, + required: true + }, + // for azureOpenAI, openAI, chatGPTBrowser only + model: { + type: String, + default: null, + required: false + }, + // for azureOpenAI, openAI only + chatGptLabel: { + type: String, + default: null, + required: false + }, + promptPrefix: { + type: String, + default: null, + required: false + }, + temperature: { + type: Number, + default: 0.8, + required: false + }, + top_p: { + type: Number, + default: 1, + required: false + }, + presence_penalty: { + type: Number, + default: 1, + required: false + }, + // for bingai only + jailbreak: { + type: Boolean, + default: false + }, jailbreakConversationId: { type: String, default: null @@ -27,32 +75,18 @@ const convoSchema = mongoose.Schema( default: null }, clientId: { - type: String + type: String, + default: null }, invocationId: { - type: String + type: String, + default: null }, toneStyle: { type: String, default: null }, - chatGptLabel: { - type: String, - default: null - }, - promptPrefix: { - type: String, - default: null - }, - model: { - type: String, - required: true - }, - user: { - type: String - }, - suggestions: [{ type: String }], - messages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Message' }] + suggestions: [{ type: String }] }, { timestamps: true } );