mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 17:00:15 +01:00
feat: api will plugin mongoMeili without a valid connection
This commit is contained in:
parent
94c0fbb525
commit
194051e424
7 changed files with 167 additions and 144 deletions
|
|
@ -49,16 +49,16 @@ const configSchema = mongoose.Schema(
|
||||||
);
|
);
|
||||||
|
|
||||||
// Instance method
|
// Instance method
|
||||||
ConfigSchema.methods.incrementCount = function () {
|
configSchema.methods.incrementCount = function () {
|
||||||
this.startupCounts += 1;
|
this.startupCounts += 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Static methods
|
// Static methods
|
||||||
ConfigSchema.statics.findByTag = async function (tag) {
|
configSchema.statics.findByTag = async function (tag) {
|
||||||
return await this.findOne({ tag });
|
return await this.findOne({ tag });
|
||||||
};
|
};
|
||||||
|
|
||||||
ConfigSchema.statics.updateByTag = async function (tag, update) {
|
configSchema.statics.updateByTag = async function (tag, update) {
|
||||||
return await this.findOneAndUpdate({ tag }, update, { new: true });
|
return await this.findOneAndUpdate({ tag }, update, { new: true });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,70 +1,6 @@
|
||||||
const mongoose = require('mongoose');
|
const { Conversation } = require('./schema/');
|
||||||
const mongoMeili = require('../lib/db/mongoMeili');
|
|
||||||
const { getMessages, deleteMessages } = require('./Message');
|
const { getMessages, deleteMessages } = require('./Message');
|
||||||
|
|
||||||
const convoSchema = mongoose.Schema(
|
|
||||||
{
|
|
||||||
conversationId: {
|
|
||||||
type: String,
|
|
||||||
unique: true,
|
|
||||||
required: true,
|
|
||||||
index: true,
|
|
||||||
meiliIndex: true
|
|
||||||
},
|
|
||||||
parentMessageId: {
|
|
||||||
type: String,
|
|
||||||
required: true
|
|
||||||
},
|
|
||||||
title: {
|
|
||||||
type: String,
|
|
||||||
default: 'New Chat',
|
|
||||||
meiliIndex: true
|
|
||||||
},
|
|
||||||
jailbreakConversationId: {
|
|
||||||
type: String,
|
|
||||||
default: null
|
|
||||||
},
|
|
||||||
conversationSignature: {
|
|
||||||
type: String,
|
|
||||||
default: null
|
|
||||||
},
|
|
||||||
clientId: {
|
|
||||||
type: String
|
|
||||||
},
|
|
||||||
invocationId: {
|
|
||||||
type: String
|
|
||||||
},
|
|
||||||
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' }]
|
|
||||||
},
|
|
||||||
{ timestamps: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
// convoSchema.plugin(mongoMeili, {
|
|
||||||
// host: process.env.MEILI_HOST,
|
|
||||||
// apiKey: process.env.MEILI_KEY,
|
|
||||||
// indexName: 'convos', // Will get created automatically if it doesn't exist already
|
|
||||||
// primaryKey: 'conversationId'
|
|
||||||
// });
|
|
||||||
|
|
||||||
const Conversation =
|
|
||||||
mongoose.models.Conversation || mongoose.model('Conversation', convoSchema);
|
|
||||||
|
|
||||||
const getConvo = async (user, conversationId) => {
|
const getConvo = async (user, conversationId) => {
|
||||||
try {
|
try {
|
||||||
return await Conversation.findOne({ user, conversationId }).exec();
|
return await Conversation.findOne({ user, conversationId }).exec();
|
||||||
|
|
@ -147,7 +83,7 @@ module.exports = {
|
||||||
// will handle a syncing solution soon
|
// will handle a syncing solution soon
|
||||||
const deletedConvoIds = [];
|
const deletedConvoIds = [];
|
||||||
|
|
||||||
convoIds.forEach((convo) =>
|
convoIds.forEach(convo =>
|
||||||
promises.push(
|
promises.push(
|
||||||
Conversation.findOne({
|
Conversation.findOne({
|
||||||
user,
|
user,
|
||||||
|
|
@ -182,7 +118,7 @@ module.exports = {
|
||||||
pageNumber,
|
pageNumber,
|
||||||
pageSize,
|
pageSize,
|
||||||
// will handle a syncing solution soon
|
// will handle a syncing solution soon
|
||||||
filter: new Set(deletedConvoIds),
|
filter: new Set(deletedConvoIds)
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
|
|
|
||||||
|
|
@ -1,71 +1,5 @@
|
||||||
const mongoose = require('mongoose');
|
const { Message } = require('./schema/');
|
||||||
const mongoMeili = require('../lib/db/mongoMeili');
|
|
||||||
|
|
||||||
const messageSchema = mongoose.Schema({
|
|
||||||
messageId: {
|
|
||||||
type: String,
|
|
||||||
unique: true,
|
|
||||||
required: true,
|
|
||||||
index: true,
|
|
||||||
meiliIndex: true
|
|
||||||
},
|
|
||||||
conversationId: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
meiliIndex: true
|
|
||||||
},
|
|
||||||
conversationSignature: {
|
|
||||||
type: String,
|
|
||||||
// required: true
|
|
||||||
},
|
|
||||||
clientId: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
invocationId: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
parentMessageId: {
|
|
||||||
type: String,
|
|
||||||
// required: true
|
|
||||||
},
|
|
||||||
sender: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
meiliIndex: true
|
|
||||||
},
|
|
||||||
text: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
meiliIndex: true
|
|
||||||
},
|
|
||||||
isCreatedByUser: {
|
|
||||||
type: Boolean,
|
|
||||||
required: true,
|
|
||||||
default: false
|
|
||||||
},
|
|
||||||
error: {
|
|
||||||
type: Boolean,
|
|
||||||
default: false
|
|
||||||
},
|
|
||||||
_meiliIndex: {
|
|
||||||
type: Boolean,
|
|
||||||
required: false,
|
|
||||||
select: false,
|
|
||||||
default: false
|
|
||||||
}
|
|
||||||
}, { timestamps: true });
|
|
||||||
|
|
||||||
// messageSchema.plugin(mongoMeili, {
|
|
||||||
// host: process.env.MEILI_HOST,
|
|
||||||
// apiKey: process.env.MEILI_KEY,
|
|
||||||
// indexName: 'messages', // Will get created automatically if it doesn't exist already
|
|
||||||
// primaryKey: 'messageId',
|
|
||||||
// });
|
|
||||||
|
|
||||||
const Message = mongoose.models.Message || mongoose.model('Message', messageSchema);
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
messageSchema,
|
|
||||||
Message,
|
Message,
|
||||||
saveMessage: async ({ messageId, conversationId, parentMessageId, sender, text, isCreatedByUser=false, error }) => {
|
saveMessage: async ({ messageId, conversationId, parentMessageId, sender, text, isCreatedByUser=false, error }) => {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
53
api/models/schema/convoSchema.js
Normal file
53
api/models/schema/convoSchema.js
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
const mongoose = require('mongoose');
|
||||||
|
module.exports = mongoose.Schema(
|
||||||
|
{
|
||||||
|
conversationId: {
|
||||||
|
type: String,
|
||||||
|
unique: true,
|
||||||
|
required: true,
|
||||||
|
index: true,
|
||||||
|
meiliIndex: true
|
||||||
|
},
|
||||||
|
parentMessageId: {
|
||||||
|
type: String,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
title: {
|
||||||
|
type: String,
|
||||||
|
default: 'New Chat',
|
||||||
|
meiliIndex: true
|
||||||
|
},
|
||||||
|
jailbreakConversationId: {
|
||||||
|
type: String,
|
||||||
|
default: null
|
||||||
|
},
|
||||||
|
conversationSignature: {
|
||||||
|
type: String,
|
||||||
|
default: null
|
||||||
|
},
|
||||||
|
clientId: {
|
||||||
|
type: String
|
||||||
|
},
|
||||||
|
invocationId: {
|
||||||
|
type: String
|
||||||
|
},
|
||||||
|
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' }]
|
||||||
|
},
|
||||||
|
{ timestamps: true }
|
||||||
|
);
|
||||||
46
api/models/schema/index.js
Normal file
46
api/models/schema/index.js
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
const mongoose = require('mongoose');
|
||||||
|
const convoSchema = require('./convoSchema');
|
||||||
|
const messageSchema = require('./messageSchema');
|
||||||
|
const { MeiliSearch } = require('meilisearch');
|
||||||
|
const mongoMeili = require('../../lib/db/mongoMeili');
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
const client = new MeiliSearch({
|
||||||
|
host: process.env.MEILI_HOST,
|
||||||
|
apiKey: process.env.MEILI_KEY
|
||||||
|
});
|
||||||
|
|
||||||
|
const { status } = await client.health();
|
||||||
|
console.log(`Meilisearch: ${status}`);
|
||||||
|
const result = status === 'available' && !!process.env.SEARCH;
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
throw new Error('Meilisearch not available');
|
||||||
|
}
|
||||||
|
|
||||||
|
convoSchema.plugin(mongoMeili, {
|
||||||
|
host: process.env.MEILI_HOST,
|
||||||
|
apiKey: process.env.MEILI_KEY,
|
||||||
|
indexName: 'convos', // Will get created automatically if it doesn't exist already
|
||||||
|
primaryKey: 'conversationId'
|
||||||
|
});
|
||||||
|
|
||||||
|
messageSchema.plugin(mongoMeili, {
|
||||||
|
host: process.env.MEILI_HOST,
|
||||||
|
apiKey: process.env.MEILI_KEY,
|
||||||
|
indexName: 'messages',
|
||||||
|
primaryKey: 'messageId'
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.log('Meilisearch error, search will be disabled');
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
const Conversation =
|
||||||
|
mongoose.models.Conversation || mongoose.model('Conversation', convoSchema);
|
||||||
|
const Message = mongoose.models.Message || mongoose.model('Message', messageSchema);
|
||||||
|
|
||||||
|
module.exports = { Conversation, Message };
|
||||||
54
api/models/schema/messageSchema.js
Normal file
54
api/models/schema/messageSchema.js
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
const mongoose = require('mongoose');
|
||||||
|
module.exports = mongoose.Schema({
|
||||||
|
messageId: {
|
||||||
|
type: String,
|
||||||
|
unique: true,
|
||||||
|
required: true,
|
||||||
|
index: true,
|
||||||
|
meiliIndex: true
|
||||||
|
},
|
||||||
|
conversationId: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
meiliIndex: true
|
||||||
|
},
|
||||||
|
conversationSignature: {
|
||||||
|
type: String,
|
||||||
|
// required: true
|
||||||
|
},
|
||||||
|
clientId: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
invocationId: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
parentMessageId: {
|
||||||
|
type: String,
|
||||||
|
// required: true
|
||||||
|
},
|
||||||
|
sender: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
meiliIndex: true
|
||||||
|
},
|
||||||
|
text: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
meiliIndex: true
|
||||||
|
},
|
||||||
|
isCreatedByUser: {
|
||||||
|
type: Boolean,
|
||||||
|
required: true,
|
||||||
|
default: false
|
||||||
|
},
|
||||||
|
error: {
|
||||||
|
type: Boolean,
|
||||||
|
default: false
|
||||||
|
},
|
||||||
|
_meiliIndex: {
|
||||||
|
type: Boolean,
|
||||||
|
required: false,
|
||||||
|
select: false,
|
||||||
|
default: false
|
||||||
|
}
|
||||||
|
}, { timestamps: true });
|
||||||
|
|
@ -54,7 +54,7 @@ router.get('/', async function (req, res) {
|
||||||
const result = await getConvosQueried(user, sortedHits, pageNumber);
|
const result = await getConvosQueried(user, sortedHits, pageNumber);
|
||||||
cache.set(q, result.cache);
|
cache.set(q, result.cache);
|
||||||
delete result.cache;
|
delete result.cache;
|
||||||
result.messages = messages.filter(message => !result.filter.has(message.conversationId));
|
result.messages = messages.filter((message) => !result.filter.has(message.conversationId));
|
||||||
// console.log(result, messages.length);
|
// console.log(result, messages.length);
|
||||||
res.status(200).send(result);
|
res.status(200).send(result);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
@ -81,12 +81,12 @@ router.get('/test', async function (req, res) {
|
||||||
|
|
||||||
router.get('/enable', async function (req, res) {
|
router.get('/enable', async function (req, res) {
|
||||||
let result = false;
|
let result = false;
|
||||||
|
try {
|
||||||
const client = new MeiliSearch({
|
const client = new MeiliSearch({
|
||||||
host: process.env.MEILI_HOST,
|
host: process.env.MEILI_HOST,
|
||||||
apiKey: process.env.MEILI_KEY
|
apiKey: process.env.MEILI_KEY
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
|
||||||
const { status } = await client.health();
|
const { status } = await client.health();
|
||||||
console.log(`Meilisearch: ${status}`);
|
console.log(`Meilisearch: ${status}`);
|
||||||
result = status === 'available' && !!process.env.SEARCH;
|
result = status === 'available' && !!process.env.SEARCH;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue