mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-01-02 00:28:51 +01:00
feat: reorganize api files, add mongoMeili
This commit is contained in:
parent
854f1c3572
commit
9995a159aa
15 changed files with 200 additions and 45 deletions
171
api/lib/mongoMeili.js
Normal file
171
api/lib/mongoMeili.js
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
const { MeiliSearch } = require('meilisearch');
|
||||
const _ = require('lodash');
|
||||
|
||||
const validateOptions = function (options) {
|
||||
const requiredKeys = ['host', 'apiKey', 'indexName'];
|
||||
requiredKeys.forEach(key => {
|
||||
if (!options[key]) throw new Error(`Missing mongoMeili Option: ${key}`);
|
||||
});
|
||||
};
|
||||
|
||||
const createMeiliMongooseModel = function ({ index, indexName, client, attributesToIndex }) {
|
||||
|
||||
console.log('attributesToIndex', attributesToIndex);
|
||||
// MeiliMongooseModel is of type Mongoose.Model
|
||||
class MeiliMongooseModel {
|
||||
|
||||
// Clear Meili index
|
||||
static async clearMeiliIndex() {
|
||||
await index.delete();
|
||||
// await index.deleteAllDocuments();
|
||||
await this.collection.updateMany({ _meiliIndex: true }, { $set: { _meiliIndex: false } });
|
||||
}
|
||||
|
||||
static async resetIndex() {
|
||||
await this.clearMeiliIndex();
|
||||
await client.createIndex(indexName, { primaryKey: attributesToIndex[0] });
|
||||
}
|
||||
// Clear Meili index
|
||||
// Push a mongoDB collection to Meili index
|
||||
static async syncWithMeili() {
|
||||
await this.resetIndex();
|
||||
// const docs = await this.find();
|
||||
const docs = await this.find({ _meiliIndex: { $in: [null, false] } });
|
||||
console.log('docs', docs.length)
|
||||
await Promise.all(docs.map(function(doc) {
|
||||
return doc.addObjectToMeili();
|
||||
}));
|
||||
}
|
||||
|
||||
// Set one or more settings of the meili index
|
||||
static async setMeiliIndexSettings(settings) {
|
||||
return await index.updateSettings(settings);
|
||||
}
|
||||
|
||||
// Search the index
|
||||
static async meiliSearch({ query, params, populate }) {
|
||||
const data = await index.search(query, params);
|
||||
|
||||
// Populate hits with content from mongodb
|
||||
if (populate) {
|
||||
|
||||
// Find objects into mongodb matching `objectID` from Meili search
|
||||
const hitsFromMongoose = await this.find(
|
||||
{
|
||||
_id: { $in: _.map(data.hits, '_id') },
|
||||
},
|
||||
_.reduce( this.schema.obj, function (results, value, key) { return { ...results, [key]: 1 } }, { _id: 1 } )
|
||||
);
|
||||
|
||||
// Add additional data from mongodb into Meili search hits
|
||||
const populatedHits = data.hits.map(function(hit) {
|
||||
const originalHit = _.find(hitsFromMongoose, {
|
||||
_id: hit._id
|
||||
});
|
||||
|
||||
return {
|
||||
...(originalHit ? originalHit.toJSON() : {}),
|
||||
...hit,
|
||||
};
|
||||
});
|
||||
data.hits = populatedHits;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
// Push new document to Meili
|
||||
async addObjectToMeili() {
|
||||
const object = _.pick(this.toJSON(), attributesToIndex);
|
||||
// object.id = object._id.toString();
|
||||
try {
|
||||
// console.log('Adding document to Meili', object);
|
||||
await index.addDocuments([object]);
|
||||
} catch (error) {
|
||||
console.log('Error adding document to Meili');
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
await this.collection.updateMany(
|
||||
{ _id: this._id },
|
||||
{ $set: { _meiliIndex: true } }
|
||||
);
|
||||
}
|
||||
|
||||
// Update an existing document in Meili
|
||||
async updateObjectToMeili() {
|
||||
const object = pick(this.toJSON(), attributesToIndex);
|
||||
await index.updateDocuments([object]);
|
||||
}
|
||||
|
||||
// Delete a document from Meili
|
||||
async deleteObjectFromMeili() {
|
||||
await index.deleteDocument(this._id);
|
||||
}
|
||||
|
||||
// * schema.post('save')
|
||||
postSaveHook() {
|
||||
if (this._meiliIndex) {
|
||||
this.updateObjectToMeili();
|
||||
} else {
|
||||
this.addObjectToMeili();
|
||||
}
|
||||
}
|
||||
|
||||
// * schema.post('update')
|
||||
postUpdateHook() {
|
||||
if (this._meiliIndex) {
|
||||
this.updateObjectToMeili();
|
||||
}
|
||||
}
|
||||
|
||||
// * schema.post('remove')
|
||||
postRemoveHook() {
|
||||
if (this._meiliIndex) {
|
||||
this.deleteObjectFromMeili();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return MeiliMongooseModel;
|
||||
}
|
||||
|
||||
module.exports = function mongoMeili(schema, options) {
|
||||
|
||||
// Vaidate Options for mongoMeili
|
||||
validateOptions(options);
|
||||
|
||||
// Add meiliIndex to schema
|
||||
schema.add({
|
||||
_meiliIndex: {
|
||||
type: Boolean,
|
||||
required: false,
|
||||
select: false,
|
||||
default: false
|
||||
}
|
||||
});
|
||||
|
||||
const { host, apiKey, indexName } = options;
|
||||
|
||||
// Setup MeiliSearch Client
|
||||
const client = new MeiliSearch({ host, apiKey });
|
||||
|
||||
// Asynchronously create the index
|
||||
client.createIndex(indexName, { primaryKey: 'messageId' });
|
||||
|
||||
// Setup the index to search for this schema
|
||||
const index = client.index(indexName);
|
||||
|
||||
const attributesToIndex = [..._.reduce(schema.obj, function (results, value, key) {
|
||||
return value.meiliIndex ? [...results, key] : results;
|
||||
// }, []), '_id'];
|
||||
}, [])];
|
||||
|
||||
schema.loadClass(createMeiliMongooseModel({ index, indexName, client, attributesToIndex }));
|
||||
|
||||
// Register hooks
|
||||
schema.post('save', function (doc) { doc.postSaveHook() });
|
||||
schema.post('update', function (doc) { doc.postUpdateHook() });
|
||||
schema.post('remove', function (doc) { doc.postRemoveHook() });
|
||||
schema.post('findOneAndUpdate', function(doc) { doc.postSaveHook() });
|
||||
};
|
||||
29
api/lib/parse/citeText.js
Normal file
29
api/lib/parse/citeText.js
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
const citationRegex = /\[\^\d+?\^]/g;
|
||||
|
||||
const citeText = (res, noLinks = false) => {
|
||||
let result = res.text || res;
|
||||
const citations = Array.from(new Set(result.match(citationRegex)));
|
||||
if (citations?.length === 0) return result;
|
||||
|
||||
if (noLinks) {
|
||||
citations.forEach((citation) => {
|
||||
const digit = citation.match(/\d+?/g)[0];
|
||||
result = result.replaceAll(citation, `<sup>[${digit}](#) </sup>`);
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
let sources = res.details.sourceAttributions;
|
||||
if (sources?.length === 0) return result;
|
||||
sources = sources.map((source) => source.seeMoreUrl);
|
||||
|
||||
citations.forEach((citation) => {
|
||||
const digit = citation.match(/\d+?/g)[0];
|
||||
result = result.replaceAll(citation, `<sup>[${digit}](${sources[digit - 1]}) </sup>`);
|
||||
});
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
module.exports = citeText;
|
||||
52
api/lib/parse/detectCode.js
Normal file
52
api/lib/parse/detectCode.js
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
const { ModelOperations } = require('@vscode/vscode-languagedetection');
|
||||
const languages = require('./languages.js');
|
||||
const codeRegex = /(```[\s\S]*?```)/g;
|
||||
// const languageMatch = /```(\w+)/;
|
||||
const replaceRegex = /```\w+\n/g;
|
||||
|
||||
const detectCode = async (input) => {
|
||||
try {
|
||||
let text = input;
|
||||
if (!text.match(codeRegex)) {
|
||||
return text;
|
||||
}
|
||||
|
||||
const langMatches = text.match(replaceRegex);
|
||||
|
||||
if (langMatches?.length > 0) {
|
||||
langMatches.forEach(match => {
|
||||
let lang = match.split('```')[1].trim();
|
||||
|
||||
if (languages.has(lang)) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('[detectCode.js] replacing', match, 'with', '```shell');
|
||||
text = text.replace(match, '```shell\n');
|
||||
});
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
const modelOperations = new ModelOperations();
|
||||
const regexSplit = (await import('./regexSplit.mjs')).default;
|
||||
const parts = regexSplit(text, codeRegex);
|
||||
|
||||
const output = parts.map(async (part) => {
|
||||
if (part.match(codeRegex)) {
|
||||
const code = part.slice(3, -3);
|
||||
let lang = (await modelOperations.runModel(code))[0].languageId;
|
||||
return part.replace(/^```/, `\`\`\`${languages.has(lang) ? lang : 'shell'}`);
|
||||
} else {
|
||||
return part;
|
||||
}
|
||||
});
|
||||
|
||||
return (await Promise.all(output)).join('');
|
||||
} catch (e) {
|
||||
console.log('Error in detectCode function\n', e);
|
||||
return input;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = detectCode;
|
||||
13
api/lib/parse/getCitations.js
Normal file
13
api/lib/parse/getCitations.js
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
// const regex = / \[\d+\..*?\]\(.*?\)/g;
|
||||
const regex = / \[.*?]\(.*?\)/g;
|
||||
|
||||
const getCitations = (res) => {
|
||||
const textBlocks = res.details.adaptiveCards[0].body;
|
||||
if (!textBlocks) return '';
|
||||
let links = textBlocks[textBlocks.length - 1]?.text.match(regex);
|
||||
if (links?.length === 0 || !links) return '';
|
||||
links = links.map((link) => link.trim());
|
||||
return links.join('\n');
|
||||
};
|
||||
|
||||
module.exports = getCitations;
|
||||
318
api/lib/parse/languages.js
Normal file
318
api/lib/parse/languages.js
Normal file
|
|
@ -0,0 +1,318 @@
|
|||
const languages = new Set([
|
||||
'adoc',
|
||||
'apacheconf',
|
||||
'arm',
|
||||
'as',
|
||||
'asc',
|
||||
'atom',
|
||||
'bat',
|
||||
'bf',
|
||||
'bind',
|
||||
'c++',
|
||||
'capnp',
|
||||
'cc',
|
||||
'clj',
|
||||
'cls',
|
||||
'cmake.in',
|
||||
'cmd',
|
||||
'coffee',
|
||||
'console',
|
||||
'cr',
|
||||
'craftcms',
|
||||
'crm',
|
||||
'cs',
|
||||
'cson',
|
||||
'cts',
|
||||
'cxx',
|
||||
'dfm',
|
||||
'docker',
|
||||
'dst',
|
||||
'erl',
|
||||
'f90',
|
||||
'f95',
|
||||
'fs',
|
||||
'gawk',
|
||||
'gemspec',
|
||||
'gms',
|
||||
'golang',
|
||||
'gololang',
|
||||
'gss',
|
||||
'gyp',
|
||||
'h',
|
||||
'h++',
|
||||
'hbs',
|
||||
'hh',
|
||||
'hpp',
|
||||
'hs',
|
||||
'html',
|
||||
'html.handlebars',
|
||||
'html.hbs',
|
||||
'https',
|
||||
'hx',
|
||||
'hxx',
|
||||
'hylang',
|
||||
'i7',
|
||||
'iced',
|
||||
'ino',
|
||||
'instances',
|
||||
'irb',
|
||||
'jinja',
|
||||
'js',
|
||||
'jsp',
|
||||
'jsx',
|
||||
'julia-repl',
|
||||
'kdb',
|
||||
'kt',
|
||||
'lassoscript',
|
||||
'ls',
|
||||
'ls',
|
||||
'mak',
|
||||
'make',
|
||||
'mawk',
|
||||
'md',
|
||||
'mipsasm',
|
||||
'mk',
|
||||
'mkd',
|
||||
'mkdown',
|
||||
'ml',
|
||||
'ml',
|
||||
'mm',
|
||||
'mma',
|
||||
'moon',
|
||||
'mts',
|
||||
'nawk',
|
||||
'nc',
|
||||
'nginxconf',
|
||||
'nimrod',
|
||||
'objc',
|
||||
'obj-c',
|
||||
'obj-c++',
|
||||
'objective-c++',
|
||||
'osascript',
|
||||
'pas',
|
||||
'pascal',
|
||||
'patch',
|
||||
'pcmk',
|
||||
'pf.conf',
|
||||
'pl',
|
||||
'plist',
|
||||
'pm',
|
||||
'podspec',
|
||||
'postgres',
|
||||
'postgresql',
|
||||
'pp',
|
||||
'ps',
|
||||
'ps1',
|
||||
'py',
|
||||
'pycon',
|
||||
'rb',
|
||||
're',
|
||||
'rs',
|
||||
'rss',
|
||||
'sas',
|
||||
'scad',
|
||||
'sci',
|
||||
'sh',
|
||||
'st',
|
||||
'stanfuncs',
|
||||
'step',
|
||||
'stp',
|
||||
'styl',
|
||||
'svg',
|
||||
'tao',
|
||||
'text',
|
||||
'thor',
|
||||
'tk',
|
||||
'toml',
|
||||
'ts',
|
||||
'tsx',
|
||||
'txt',
|
||||
'v',
|
||||
'vb',
|
||||
'vbs',
|
||||
'wl',
|
||||
'x++',
|
||||
'xhtml',
|
||||
'xjb',
|
||||
'xls',
|
||||
'xlsx',
|
||||
'xpath',
|
||||
'xq',
|
||||
'xsd',
|
||||
'xsl',
|
||||
'yaml',
|
||||
'zep',
|
||||
'zone',
|
||||
'zsh',
|
||||
'1c',
|
||||
'abnf',
|
||||
'accesslog',
|
||||
'actionscript',
|
||||
'ada',
|
||||
'angelscript',
|
||||
'apache',
|
||||
'applescript',
|
||||
'arcade',
|
||||
'arduino',
|
||||
'armasm',
|
||||
'asciidoc',
|
||||
'aspectj',
|
||||
'autohotkey',
|
||||
'autoit',
|
||||
'avrasm',
|
||||
'awk',
|
||||
'axapta',
|
||||
'bash',
|
||||
'basic',
|
||||
'bnf',
|
||||
'brainfuck',
|
||||
'c',
|
||||
'cal',
|
||||
'capnproto',
|
||||
'clojure',
|
||||
'cmake',
|
||||
'coffeescript',
|
||||
'coq',
|
||||
'cos',
|
||||
'cpp',
|
||||
'crmsh',
|
||||
'crystal',
|
||||
'csharp',
|
||||
'csp',
|
||||
'css',
|
||||
'd',
|
||||
'dart',
|
||||
'diff',
|
||||
'django',
|
||||
'dns',
|
||||
'dockerfile',
|
||||
'dos',
|
||||
'dpr',
|
||||
'dsconfig',
|
||||
'dts',
|
||||
'dust',
|
||||
'ebnf',
|
||||
'elixir',
|
||||
'elm',
|
||||
'erlang',
|
||||
'excel',
|
||||
'fix',
|
||||
'fortran',
|
||||
'fsharp',
|
||||
'gams',
|
||||
'gauss',
|
||||
'gcode',
|
||||
'gherkin',
|
||||
'glsl',
|
||||
'go',
|
||||
'golo',
|
||||
'gradle',
|
||||
'graph',
|
||||
'graphql',
|
||||
'groovy',
|
||||
'haml',
|
||||
'handlebars',
|
||||
'haskell',
|
||||
'haxe',
|
||||
'http',
|
||||
'hy',
|
||||
'inform7',
|
||||
'ini',
|
||||
'irpf90',
|
||||
'java',
|
||||
'javascript',
|
||||
'json',
|
||||
'julia',
|
||||
'k',
|
||||
'kotlin',
|
||||
'lasso',
|
||||
'ldif',
|
||||
'leaf',
|
||||
'less',
|
||||
'lisp',
|
||||
'livecodeserver',
|
||||
'livescript',
|
||||
'lua',
|
||||
'makefile',
|
||||
'markdown',
|
||||
'mathematica',
|
||||
'matlab',
|
||||
'maxima',
|
||||
'mel',
|
||||
'mercury',
|
||||
'mips',
|
||||
'mizar',
|
||||
'mojolicious',
|
||||
'monkey',
|
||||
'moonscript',
|
||||
'n1ql',
|
||||
'nginx',
|
||||
'nim',
|
||||
'nix',
|
||||
'nsis',
|
||||
'objectivec',
|
||||
'ocaml',
|
||||
'openscad',
|
||||
'oxygene',
|
||||
'p21',
|
||||
'parser3',
|
||||
'perl',
|
||||
'pf',
|
||||
'pgsql',
|
||||
'php',
|
||||
'plaintext',
|
||||
'pony',
|
||||
'powershell',
|
||||
'processing',
|
||||
'profile',
|
||||
'prolog',
|
||||
'properties',
|
||||
'protobuf',
|
||||
'puppet',
|
||||
'python',
|
||||
'python-repl',
|
||||
'qml',
|
||||
'r',
|
||||
'reasonml',
|
||||
'rib',
|
||||
'rsl',
|
||||
'ruby',
|
||||
'ruleslanguage',
|
||||
'rust',
|
||||
'SAS',
|
||||
'scala' ,
|
||||
'scheme',
|
||||
'scilab',
|
||||
'scss',
|
||||
'shell',
|
||||
'smali',
|
||||
'smalltalk',
|
||||
'sml',
|
||||
'sql',
|
||||
'stan',
|
||||
'stata',
|
||||
'stylus',
|
||||
'subunit',
|
||||
'swift',
|
||||
'tap',
|
||||
'tcl',
|
||||
'tex',
|
||||
'thrift',
|
||||
'tp',
|
||||
'twig',
|
||||
'typescript',
|
||||
'vala',
|
||||
'vbnet',
|
||||
'vbscript',
|
||||
'verilog',
|
||||
'vhdl',
|
||||
'vim',
|
||||
'x86asm',
|
||||
'xl',
|
||||
'xml',
|
||||
'xquery',
|
||||
'yml',
|
||||
'zephir',
|
||||
]);
|
||||
|
||||
module.exports = languages;
|
||||
46
api/lib/parse/regexSplit.mjs
Normal file
46
api/lib/parse/regexSplit.mjs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
const primaryRegex = /```([^`\n]*?)\n([\s\S]*?)\n```/g;
|
||||
const secondaryRegex = /```([^`\n]*?)\n?([\s\S]*?)\n?```/g;
|
||||
|
||||
const unenclosedCodeTest = (text) => {
|
||||
let workingText = text;
|
||||
// if (workingText.startsWith('<') || (!workingText.startsWith('`') && workingText.match(/```/g)?.length === 1)) {
|
||||
// workingText = `\`\`\`${workingText}`
|
||||
// }
|
||||
|
||||
return workingText.trim();
|
||||
};
|
||||
|
||||
export default function regexSplit(string) {
|
||||
let matches = [...string.matchAll(primaryRegex)];
|
||||
|
||||
if (!matches[0]) {
|
||||
matches = [...string.matchAll(secondaryRegex)];
|
||||
}
|
||||
|
||||
const output = [matches[0].input.slice(0, matches[0].index)];
|
||||
|
||||
// console.log(matches);
|
||||
|
||||
for (let i = 0; i < matches.length; i++) {
|
||||
const [fullMatch, language, code] = matches[i];
|
||||
// const formattedCode = code.replace(/`+/g, '\\`');
|
||||
output.push(`\`\`\`${language}\n${code}\n\`\`\``);
|
||||
if (i < matches.length - 1) {
|
||||
let nextText = string.slice(matches[i].index + fullMatch.length, matches[i + 1].index);
|
||||
nextText = unenclosedCodeTest(nextText);
|
||||
output.push(nextText);
|
||||
} else {
|
||||
const lastMatch = matches[matches.length - 1][0];
|
||||
// console.log(lastMatch);
|
||||
// console.log(matches[0].input.split(lastMatch));
|
||||
let rest = matches[0].input.split(lastMatch)[1]
|
||||
|
||||
if (rest) {
|
||||
rest = unenclosedCodeTest(rest);
|
||||
output.push(rest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue