From 8c2d577e60e68ad44c0c01f4555d660422c1af20 Mon Sep 17 00:00:00 2001 From: Wentao Lyu <35-wentao.lyu@users.noreply.git.stereye.tech> Date: Wed, 5 Apr 2023 16:15:46 +0800 Subject: [PATCH] feat: remove customGpts --- api/models/CustomGpt.js | 82 ------------------- api/models/index.js | 6 -- api/server/index.js | 36 ++------ api/server/routes/{ => ask}/askBingAI.js | 4 +- .../routes/{ => ask}/askChatGPTBrowser.js | 4 +- api/server/routes/{ => ask}/askOpenAI.js | 4 +- api/server/routes/{ => ask}/handlers.js | 2 +- api/server/routes/{ask.js => ask/index.js} | 0 api/server/routes/customGpts.js | 67 --------------- api/server/routes/endpoints.js | 17 ++++ api/server/routes/index.js | 6 +- api/server/routes/me.js | 16 ++++ client/src/store/index.js | 4 +- client/src/store/models.js | 80 ------------------ client/src/store/preset.js | 9 +- client/src/store/text.js | 2 +- 16 files changed, 58 insertions(+), 281 deletions(-) delete mode 100644 api/models/CustomGpt.js rename api/server/routes/{ => ask}/askBingAI.js (98%) rename api/server/routes/{ => ask}/askChatGPTBrowser.js (97%) rename api/server/routes/{ => ask}/askOpenAI.js (98%) rename api/server/routes/{ => ask}/handlers.js (97%) rename api/server/routes/{ask.js => ask/index.js} (100%) delete mode 100644 api/server/routes/customGpts.js create mode 100644 api/server/routes/endpoints.js create mode 100644 api/server/routes/me.js delete mode 100644 client/src/store/models.js diff --git a/api/models/CustomGpt.js b/api/models/CustomGpt.js deleted file mode 100644 index af3e09227..000000000 --- a/api/models/CustomGpt.js +++ /dev/null @@ -1,82 +0,0 @@ -const mongoose = require('mongoose'); - -const customGptSchema = mongoose.Schema({ - chatGptLabel: { - type: String, - required: true - }, - promptPrefix: { - type: String - }, - value: { - type: String, - required: true - }, - user: { - type: String - }, -}, { timestamps: true }); - -const CustomGpt = mongoose.models.CustomGpt || mongoose.model('CustomGpt', customGptSchema); - -const createCustomGpt = async ({ chatGptLabel, promptPrefix, value, user }) => { - try { - await CustomGpt.create({ - chatGptLabel, - promptPrefix, - value, - user - }); - return { chatGptLabel, promptPrefix, value }; - } catch (error) { - console.error(error); - return { customGpt: 'Error saving customGpt' }; - } -}; - -module.exports = { - getCustomGpts: async (user, filter) => { - try { - return await CustomGpt.find({ ...filter, user }).exec(); - } catch (error) { - console.error(error); - return { customGpt: 'Error getting customGpts' }; - } - }, - updateCustomGpt: async (user, { value, ...update }) => { - try { - const customGpt = await CustomGpt.findOne({ value, user }).exec(); - - if (!customGpt) { - return await createCustomGpt({ value, ...update, user }); - } else { - return await CustomGpt.findOneAndUpdate({ value, user }, update, { - new: true, - upsert: true - }).exec(); - } - } catch (error) { - console.log(error); - return { message: 'Error updating customGpt' }; - } - }, - updateByLabel: async (user, { prevLabel, ...update }) => { - try { - return await CustomGpt.findOneAndUpdate({ chatGptLabel: prevLabel, user }, update, { - new: true, - upsert: true - }).exec(); - } catch (error) { - console.log(error); - return { message: 'Error updating customGpt' }; - } - }, - deleteCustomGpts: async (user, filter) => { - try { - return await CustomGpt.deleteMany({ ...filter, user }).exec(); - } catch (error) { - console.error(error); - return { customGpt: 'Error deleting customGpts' }; - } - } -}; diff --git a/api/models/index.js b/api/models/index.js index a1694cde3..8ae2eba85 100644 --- a/api/models/index.js +++ b/api/models/index.js @@ -1,5 +1,4 @@ const { getMessages, saveMessage, deleteMessagesSince, deleteMessages } = require('./Message'); -const { getCustomGpts, updateCustomGpt, updateByLabel, deleteCustomGpts } = require('./CustomGpt'); const { getConvoTitle, getConvo, saveConvo, updateConvo } = require('./Conversation'); const { getPreset, getPresets, savePreset, deletePresets } = require('./Preset'); @@ -14,11 +13,6 @@ module.exports = { saveConvo, updateConvo, - getCustomGpts, - updateCustomGpt, - updateByLabel, - deleteCustomGpts, - getPreset, getPresets, savePreset, diff --git a/api/server/index.js b/api/server/index.js index 48823a036..de49b81d1 100644 --- a/api/server/index.js +++ b/api/server/index.js @@ -10,7 +10,6 @@ const errorController = require('./controllers/errorController'); const port = process.env.PORT || 3080; const host = process.env.HOST || 'localhost'; -const userSystemEnabled = process.env.ENABLE_USER_SYSTEM || false; const projectPath = path.join(__dirname, '..', '..', 'client'); (async () => { @@ -34,6 +33,8 @@ const projectPath = path.join(__dirname, '..', '..', 'client'); }) ); + // ROUTES + /* chore: potential redirect error here, can only comment out this block; comment back in if using auth routes i guess */ // app.get('/', routes.authenticatedOrRedirect, function (req, res) { @@ -41,40 +42,21 @@ const projectPath = path.join(__dirname, '..', '..', 'client'); // res.sendFile(path.join(projectPath, 'public', 'index.html')); // }); - app.get('/api/me', function (req, res) { - if (userSystemEnabled) { - const user = req?.session?.user; - - if (user) res.send(JSON.stringify({ username: user?.username, display: user?.display })); - else res.send(JSON.stringify(null)); - } else { - res.send(JSON.stringify({ username: 'anonymous_user', display: 'Anonymous User' })); - } - }); - + // api endpoint app.use('/api/search', routes.authenticatedOr401, routes.search); app.use('/api/ask', routes.authenticatedOr401, routes.ask); app.use('/api/messages', routes.authenticatedOr401, routes.messages); app.use('/api/convos', routes.authenticatedOr401, routes.convos); - app.use('/api/customGpts', routes.authenticatedOr401, routes.customGpts); app.use('/api/presets', routes.authenticatedOr401, routes.presets); app.use('/api/prompts', routes.authenticatedOr401, routes.prompts); app.use('/api/tokenizer', routes.authenticatedOr401, routes.tokenizer); + app.use('/api/endpoints', routes.authenticatedOr401, routes.endpoints); + + // user system app.use('/auth', routes.auth); + app.use('/api/me', routes.me); - app.get('/api/endpoints', function (req, res) { - const azureOpenAI = !!process.env.AZURE_OPENAI_KEY; - const openAI = process.env.OPENAI_KEY - ? { availableModels: ['gpt-4', 'text-davinci-003', 'gpt-3.5-turbo', 'gpt-3.5-turbo-0301'] } - : false; - const bingAI = !!process.env.BING_TOKEN; - const chatGPTBrowser = process.env.OPENAI_KEY - ? { availableModels: ['Default (GPT-3.5)', 'Legacy (GPT-3.5)', 'GPT-4'] } - : false; - - res.send(JSON.stringify({ azureOpenAI, openAI, bingAI, chatGPTBrowser })); - }); - + // static files app.get('/*', routes.authenticatedOrRedirect, function (req, res) { res.sendFile(path.join(projectPath, 'dist', 'index.html')); }); @@ -89,7 +71,7 @@ const projectPath = path.join(__dirname, '..', '..', 'client'); })(); let messageCount = 0; -process.on('uncaughtException', (err) => { +process.on('uncaughtException', err => { if (!err.message.includes('fetch failed')) { console.error('There was an uncaught error:', err.message); } diff --git a/api/server/routes/askBingAI.js b/api/server/routes/ask/askBingAI.js similarity index 98% rename from api/server/routes/askBingAI.js rename to api/server/routes/ask/askBingAI.js index 3f01b69b8..23461e634 100644 --- a/api/server/routes/askBingAI.js +++ b/api/server/routes/ask/askBingAI.js @@ -1,8 +1,8 @@ const express = require('express'); const crypto = require('crypto'); const router = express.Router(); -const { titleConvo, askBing } = require('../../app'); -const { saveMessage, getConvoTitle, saveConvo, getConvo } = require('../../models'); +const { titleConvo, askBing } = require('../../../app'); +const { saveMessage, getConvoTitle, saveConvo, getConvo } = require('../../../models'); const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers'); router.post('/', async (req, res) => { diff --git a/api/server/routes/askChatGPTBrowser.js b/api/server/routes/ask/askChatGPTBrowser.js similarity index 97% rename from api/server/routes/askChatGPTBrowser.js rename to api/server/routes/ask/askChatGPTBrowser.js index b7e6580ee..621f5c67b 100644 --- a/api/server/routes/askChatGPTBrowser.js +++ b/api/server/routes/ask/askChatGPTBrowser.js @@ -1,8 +1,8 @@ const express = require('express'); const crypto = require('crypto'); const router = express.Router(); -const { titleConvo, browserClient } = require('../../app/'); -const { saveMessage, getConvoTitle, saveConvo, updateConvo, getConvo } = require('../../models'); +const { titleConvo, browserClient } = require('../../../app/'); +const { saveMessage, getConvoTitle, saveConvo, updateConvo, getConvo } = require('../../../models'); const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers'); router.post('/', async (req, res) => { diff --git a/api/server/routes/askOpenAI.js b/api/server/routes/ask/askOpenAI.js similarity index 98% rename from api/server/routes/askOpenAI.js rename to api/server/routes/ask/askOpenAI.js index 17169bc9e..f58d529bd 100644 --- a/api/server/routes/askOpenAI.js +++ b/api/server/routes/ask/askOpenAI.js @@ -1,8 +1,8 @@ const express = require('express'); const crypto = require('crypto'); const router = express.Router(); -const { titleConvo, askClient } = require('../../app/'); -const { saveMessage, getConvoTitle, saveConvo, updateConvo, getConvo } = require('../../models'); +const { titleConvo, askClient } = require('../../../app/'); +const { saveMessage, getConvoTitle, saveConvo, updateConvo, getConvo } = require('../../../models'); const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers'); router.post('/', async (req, res) => { diff --git a/api/server/routes/handlers.js b/api/server/routes/ask/handlers.js similarity index 97% rename from api/server/routes/handlers.js rename to api/server/routes/ask/handlers.js index a4cde0ca3..9efcd292c 100644 --- a/api/server/routes/handlers.js +++ b/api/server/routes/ask/handlers.js @@ -3,7 +3,7 @@ const citationRegex = /\[\^\d+?\^]/g; const backtick = /(?█'; -const { getCitations, citeText } = require('../../app/'); +const { getCitations, citeText } = require('../../../app'); const handleError = (res, message) => { res.write(`event: error\ndata: ${JSON.stringify(message)}\n\n`); diff --git a/api/server/routes/ask.js b/api/server/routes/ask/index.js similarity index 100% rename from api/server/routes/ask.js rename to api/server/routes/ask/index.js diff --git a/api/server/routes/customGpts.js b/api/server/routes/customGpts.js deleted file mode 100644 index d430b39ad..000000000 --- a/api/server/routes/customGpts.js +++ /dev/null @@ -1,67 +0,0 @@ -const express = require('express'); -const router = express.Router(); -const { - getCustomGpts, - updateCustomGpt, - updateByLabel, - deleteCustomGpts -} = require('../../models'); - -router.get('/', async (req, res) => { - const models = (await getCustomGpts(req?.session?.user?.username)).map((model) => { - model = model.toObject(); - model._id = model._id.toString(); - return model; - }); - res.status(200).send(models); -}); - -router.post('/delete', async (req, res) => { - const { arg } = req.body; - - try { - await deleteCustomGpts(req?.session?.user?.username, arg); - const models = (await getCustomGpts(req?.session?.user?.username)).map((model) => { - model = model.toObject(); - model._id = model._id.toString(); - return model; - }); - res.status(201).send(models); - // res.status(201).send(dbResponse); - } catch (error) { - console.error(error); - res.status(500).send(error); - } -}); - -// router.post('/create', async (req, res) => { -// const payload = req.body.arg; - -// try { -// const dbResponse = await createCustomGpt(payload); -// res.status(201).send(dbResponse); -// } catch (error) { -// console.error(error); -// res.status(500).send(error); -// } -// }); - -router.post('/', async (req, res) => { - const update = req.body.arg; - - let setter = updateCustomGpt; - - if (update.prevLabel) { - setter = updateByLabel; - } - - try { - const dbResponse = await setter(req?.session?.user?.username, update); - res.status(201).send(dbResponse); - } catch (error) { - console.error(error); - res.status(500).send(error); - } -}); - -module.exports = router; diff --git a/api/server/routes/endpoints.js b/api/server/routes/endpoints.js new file mode 100644 index 000000000..b034d2def --- /dev/null +++ b/api/server/routes/endpoints.js @@ -0,0 +1,17 @@ +const express = require('express'); +const router = express.Router(); + +router.get('/', function (req, res) { + const azureOpenAI = !!process.env.AZURE_OPENAI_KEY; + const openAI = process.env.OPENAI_KEY + ? { availableModels: ['gpt-4', 'text-davinci-003', 'gpt-3.5-turbo', 'gpt-3.5-turbo-0301'] } + : false; + const bingAI = !!process.env.BING_TOKEN; + const chatGPTBrowser = process.env.OPENAI_KEY + ? { availableModels: ['Default (GPT-3.5)', 'Legacy (GPT-3.5)', 'GPT-4'] } + : false; + + res.send(JSON.stringify({ azureOpenAI, openAI, bingAI, chatGPTBrowser })); +}); + +module.exports = router; diff --git a/api/server/routes/index.js b/api/server/routes/index.js index bce6327a0..3be55cd4a 100644 --- a/api/server/routes/index.js +++ b/api/server/routes/index.js @@ -2,10 +2,11 @@ const ask = require('./ask'); const messages = require('./messages'); const convos = require('./convos'); const presets = require('./presets'); -const customGpts = require('./customGpts'); const prompts = require('./prompts'); const search = require('./search'); const tokenizer = require('./tokenizer'); +const me = require('./me'); +const endpoints = require('./endpoints'); const { router: auth, authenticatedOr401, authenticatedOrRedirect } = require('./auth'); module.exports = { @@ -14,10 +15,11 @@ module.exports = { messages, convos, presets, - customGpts, prompts, auth, tokenizer, + me, + endpoints, authenticatedOr401, authenticatedOrRedirect }; diff --git a/api/server/routes/me.js b/api/server/routes/me.js new file mode 100644 index 000000000..3b12b4854 --- /dev/null +++ b/api/server/routes/me.js @@ -0,0 +1,16 @@ +const express = require('express'); +const router = express.Router(); +const userSystemEnabled = !!process.env.ENABLE_USER_SYSTEM || false; + +router.get('/', function (req, res) { + if (userSystemEnabled) { + const user = req?.session?.user; + + if (user) res.send(JSON.stringify({ username: user?.username, display: user?.display })); + else res.send(JSON.stringify(null)); + } else { + res.send(JSON.stringify({ username: 'anonymous_user', display: 'Anonymous User' })); + } +}); + +module.exports = router; diff --git a/client/src/store/index.js b/client/src/store/index.js index f400853ac..948442531 100644 --- a/client/src/store/index.js +++ b/client/src/store/index.js @@ -1,6 +1,5 @@ import conversation from './conversation'; import conversations from './conversations'; -import models from './models'; import endpoints from './endpoints'; import user from './user'; import text from './text'; @@ -11,10 +10,9 @@ import preset from './preset'; export default { ...conversation, ...conversations, - ...models, ...endpoints, ...user, - text, + ...text, ...submission, ...search, ...preset diff --git a/client/src/store/models.js b/client/src/store/models.js deleted file mode 100644 index 5a3621df2..000000000 --- a/client/src/store/models.js +++ /dev/null @@ -1,80 +0,0 @@ -import React from "react"; -import { - RecoilRoot, - atom, - selector, - useRecoilState, - useRecoilValue, -} from "recoil"; - -const customGPTModels = atom({ - key: "customGPTModels", - default: [], -}); - -const models = selector({ - key: "models", - get: ({ get }) => { - return [ - { - _id: "0", - name: "ChatGPT", - value: "chatgpt", - model: "chatgpt", - }, - { - _id: "1", - name: "CustomGPT", - value: "chatgptCustom", - model: "chatgptCustom", - }, - { - _id: "2", - name: "BingAI", - value: "bingai", - model: "bingai", - }, - { - _id: "3", - name: "Sydney", - value: "sydney", - model: "sydney", - }, - { - _id: "4", - name: "ChatGPT", - value: "chatgptBrowser", - model: "chatgptBrowser", - }, - ...get(customGPTModels), - ]; - }, -}); - -const modelsFilter = atom({ - key: "modelsFilter", - default: { - chatgpt: false, - chatgptCustom: false, - bingai: false, - sydney: false, - chatgptBrowser: false, - }, -}); - -const availableModels = selector({ - key: "availableModels", - get: ({ get }) => { - const m = get(models); - const f = get(modelsFilter); - return m.filter(({ model }) => f[model]); - }, -}); -// const modelAvailable - -export default { - customGPTModels, - models, - modelsFilter, - availableModels, -}; diff --git a/client/src/store/preset.js b/client/src/store/preset.js index f2f342684..402d6d698 100644 --- a/client/src/store/preset.js +++ b/client/src/store/preset.js @@ -1,5 +1,4 @@ -import endpoints from './endpoints'; -import { atom, selector, useSetRecoilState, useResetRecoilState, useRecoilCallback } from 'recoil'; +import { atom } from 'recoil'; // preset structure is as same defination as conversation // sample structure @@ -20,11 +19,9 @@ import { atom, selector, useSetRecoilState, useResetRecoilState, useRecoilCallba // frequency_penalty: 0, // // for bingAI only // jailbreak: false, -// jailbreakConversationId: null, -// conversationSignature: null, -// clientId: null, -// invocationId: 1, // toneStyle: null, +// context: null, +// systemMessage: null, // }; // an array of saved presets. diff --git a/client/src/store/text.js b/client/src/store/text.js index d587d65af..7aab98e3e 100644 --- a/client/src/store/text.js +++ b/client/src/store/text.js @@ -5,4 +5,4 @@ const text = atom({ default: '' }); -export default text; +export default { text };