| const fs = require('fs'); |
| const axios = require('axios'); |
| const FormData = require('form-data'); |
| const { FileSources } = require('librechat-data-provider'); |
| const { logger } = require('~/config'); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const deleteVectors = async (req, file) => { |
| if (!file.embedded || !process.env.RAG_API_URL) { |
| return; |
| } |
| try { |
| const jwtToken = req.headers.authorization.split(' ')[1]; |
| return await axios.delete(`${process.env.RAG_API_URL}/documents`, { |
| headers: { |
| Authorization: `Bearer ${jwtToken}`, |
| 'Content-Type': 'application/json', |
| accept: 'application/json', |
| }, |
| data: [file.file_id], |
| }); |
| } catch (error) { |
| logger.error('Error deleting vectors', error); |
| throw new Error(error.message || 'An error occurred during file deletion.'); |
| } |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async function uploadVectors({ req, file, file_id }) { |
| if (!process.env.RAG_API_URL) { |
| throw new Error('RAG_API_URL not defined'); |
| } |
|
|
| try { |
| const jwtToken = req.headers.authorization.split(' ')[1]; |
| const formData = new FormData(); |
| formData.append('file_id', file_id); |
| formData.append('file', fs.createReadStream(file.path)); |
|
|
| const formHeaders = formData.getHeaders(); |
|
|
| const response = await axios.post(`${process.env.RAG_API_URL}/embed`, formData, { |
| headers: { |
| Authorization: `Bearer ${jwtToken}`, |
| accept: 'application/json', |
| ...formHeaders, |
| }, |
| }); |
|
|
| const responseData = response.data; |
| logger.debug('Response from embedding file', responseData); |
|
|
| if (responseData.known_type === false) { |
| throw new Error(`File embedding failed. The filetype ${file.mimetype} is not supported`); |
| } |
|
|
| if (!responseData.status) { |
| throw new Error('File embedding failed.'); |
| } |
|
|
| return { |
| bytes: file.size, |
| filename: file.originalname, |
| filepath: FileSources.vectordb, |
| embedded: Boolean(responseData.known_type), |
| }; |
| } catch (error) { |
| logger.error('Error embedding file', error); |
| throw new Error(error.message || 'An error occurred during file upload.'); |
| } |
| } |
|
|
| module.exports = { |
| deleteVectors, |
| uploadVectors, |
| }; |
|
|