LibreChat/config/translations/embeddings.ts
Danny Avila 95201908e9
📦 fix: npm warnings; chore: bump deprecated packages (#4707)
* chore: bump langchain deps to address vulnerability warnings

* chore: bump community package and install textsplitters package

* fix: update expected result in tokenSplit tests for accuracy

* chore: remove CodeSherpa tools

* chore: remove E2B tools and loadToolSuite

* chore: remove CodeBrew tool and update related references

* chore: remove HumanTool and ChatTool, update tool references

* chore: remove Zapier tool from manifest.json and update SerpAPI

* chore: remove basic tools

* chore: update import path for RecursiveCharacterTextSplitter

* chore: update import path for DynamicStructuredTool

* chore: remove extractionChain.js and update tool filtering logic

* chore: npm audit fix

* chore: bump google packages

* chore: update DALL-E tool to DALL-E-3 and adjust authentication logic

* ci: update message classes

* chore: elliptic npm audit fix

* chore: update CallbackManager import and remove deprecated tool handling logic

* chore: imports order

* chore: remove unused code

---------

Co-authored-by: Max Sanna <max@maxsanna.com>
2024-11-12 18:51:32 -05:00

43 lines
1.5 KiB
TypeScript

import dotenv from 'dotenv';
dotenv.config({
path: './',
});
import { OpenAIEmbeddings } from '@langchain/openai';
import { HNSWLib } from '@langchain/community/vectorstores/hnswlib';
import { RecursiveCharacterTextSplitter } from '@langchain/textsplitters';
import * as fs from 'fs';
import * as path from 'path';
export const storeEmbeddings = async (modulePath: string) => {
try {
const text = fs.readFileSync(modulePath, 'utf8');
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 600 });
const docs = await textSplitter.createDocuments([text]);
const vectorStore = await HNSWLib.fromDocuments(docs, new OpenAIEmbeddings());
const directory = `./config/translations/stores/${path.basename(modulePath)}`;
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory, { recursive: true });
console.log(`Directory created: ${directory}`);
} else {
console.log(`Directory already exists: ${directory}`);
return;
}
await vectorStore.save(directory);
} catch (error) {
console.error('Error storing embeddings');
console.error(error);
}
};
export const loadEmbeddings = async (modulePath: string) => {
try {
const directory = `./config/translations/stores/${path.basename(modulePath)}`;
const loadedVectorStore = await HNSWLib.load(directory, new OpenAIEmbeddings());
return loadedVectorStore;
} catch (error) {
console.error('Error loading embeddings');
console.error(error);
}
};