|
| 1 | +//ragService.js |
| 2 | + |
| 3 | +// Used to collect send all documents of paperless-ngx with |
| 4 | +// body: { "title": $TITLE, "content": $CONTENT, "metadata": { "tags": $TAGS, "correspondent": $CORRESPONDENT} } |
| 5 | +// to rag endpoint (http://localhost:5000/api/v1/content) and get the response |
| 6 | + |
| 7 | +const paperlessService = require('./paperlessService'); |
| 8 | + |
| 9 | +const axios = require('axios'); |
| 10 | +const FormData = require('form-data'); |
| 11 | +const fs = require('fs'); |
| 12 | +const path = require('path'); |
| 13 | + |
| 14 | +class RAGService { |
| 15 | + constructor() { |
| 16 | + this.apiUrl = "http://localhost:5000/api/v1"; |
| 17 | + this.apiToken = "test-key-123"; |
| 18 | + } |
| 19 | + |
| 20 | + initialize() { |
| 21 | + if (!this.client && this.apiUrl && this.apiToken) { |
| 22 | + this.client = axios.create({ |
| 23 | + baseURL: this.apiUrl, |
| 24 | + headers: { |
| 25 | + 'Authorization': `Token ${this.apiToken}`, |
| 26 | + 'Content-Type': 'application/json' |
| 27 | + } |
| 28 | + }); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + async fetchAllDocumentsWithMetaData() { |
| 33 | + const documents = await paperlessService.getDocumentsForRAGService(); |
| 34 | + return documents; |
| 35 | + } |
| 36 | + |
| 37 | + async sendDocumentsToRAGService() { |
| 38 | + try { |
| 39 | + const documents = await this.fetchAllDocumentsWithMetaData(); |
| 40 | + for (const document of documents) { |
| 41 | + const tags = document.tags.join(","); |
| 42 | + const correspondent = document.correspondent; |
| 43 | + const title = document.title; |
| 44 | + const content = document.content; |
| 45 | + const body = { |
| 46 | + title, |
| 47 | + content, |
| 48 | + metadata: { |
| 49 | + tags, |
| 50 | + correspondent |
| 51 | + } |
| 52 | + } |
| 53 | + this.client.post('/content', body) |
| 54 | + .then(response => { |
| 55 | + console.log(response.data); |
| 56 | + }) |
| 57 | + .catch(error => { |
| 58 | + console.error(error); |
| 59 | + }).finally(() => { |
| 60 | + console.log("Document sent to RAG service"); |
| 61 | + }); |
| 62 | + } |
| 63 | + return true; |
| 64 | + } catch (error) { |
| 65 | + console.error("Error initializing RAG service client", error); |
| 66 | + return false; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +} |
| 74 | + |
| 75 | +module.exports = new RAGService(); |
0 commit comments