Skip to content

Commit c2b802c

Browse files
committed
Added pre-flight service test for RAG
FIxed #438
1 parent 2814582 commit c2b802c

6 files changed

+143
-4
lines changed

routes/setup.js

+15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const debugService = require('../services/debugService.js');
1111
const configFile = require('../config/config.js');
1212
const ChatService = require('../services/chatService.js');
1313
const documentsService = require('../services/documentsService.js');
14+
const RAGService = require('../services/ragService.js');
1415
const fs = require('fs').promises;
1516
const path = require('path');
1617
const jwt = require('jsonwebtoken');
@@ -4226,4 +4227,18 @@ router.get('/api/processing-status', async (req, res) => {
42264227
}
42274228
});
42284229

4230+
router.get('/api/rag-test', async (req, res) => {
4231+
RAGService.initialize();
4232+
try {
4233+
if(await RAGService.sendDocumentsToRAGService()){
4234+
res.status(200).json({ success: true });
4235+
}else{
4236+
res.status(500).json({ success: false });
4237+
}
4238+
} catch (error) {
4239+
res.status(500).json({ error: 'Failed to fetch processing status' });
4240+
}
4241+
}
4242+
);
4243+
42294244
module.exports = router;

services/customService.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class CustomOpenAIService {
142142
content: truncatedContent
143143
}
144144
],
145-
temperature: 0.3,
145+
...(model !== 'o3-mini' && { temperature: 0.3 }),
146146
});
147147

148148
// Handle response

services/manualService.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class ManualService {
8787
content: content
8888
}
8989
],
90-
temperature: 0.3,
90+
...(model !== 'o3-mini' && { temperature: 0.3 }),
9191
});
9292

9393
let jsonContent = response.choices[0].message.content;
@@ -183,7 +183,7 @@ class ManualService {
183183
content: content
184184
}
185185
],
186-
temperature: 0.3,
186+
...(model !== 'o3-mini' && { temperature: 0.3 }),
187187
});
188188

189189
let jsonContent = response.choices[0].message.content;

services/openaiService.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ class OpenAIService {
302302
content: truncatedContent
303303
}
304304
],
305-
temperature: 0.3,
305+
...(model !== 'o3-mini' && { temperature: 0.3 }),
306306
});
307307

308308
// Handle response

services/paperlessService.js

+49
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,55 @@ class PaperlessService {
761761
}
762762
}
763763

764+
async getDocumentsForRAGService () {
765+
/**
766+
* Get all documents with metadata (title, tags, correspondent, created date and content).
767+
*
768+
* @returns An array of documents with metadata.
769+
* @throws An error if the request fails.
770+
* @note This method is used to get all documents with metadata for further processing
771+
*/
772+
773+
this.initialize();
774+
try {
775+
let response;
776+
let page = 1;
777+
let hasMore = true;
778+
779+
while (hasMore) {
780+
try {
781+
const params = {
782+
params: { fields: 'id,title,tags,correspondent,created,content' },
783+
page,
784+
page_size: 100, // Maximale Seitengröße für effizientes Laden
785+
ordering: 'name' // Optional: Sortierung nach Namen
786+
};
787+
788+
response = await this.client.get('/documents/', { params });
789+
790+
if (!response?.data?.results || !Array.isArray(response.data.results)) {
791+
console.error(`[DEBUG] Invalid API response on page ${page}`);
792+
break;
793+
}
794+
795+
hasMore = response.data.next !== null;
796+
page++;
797+
798+
} catch (error) {
799+
console.error(`[ERROR] fetching documents page ${page}:`, error.message);
800+
if (error.response) {
801+
console.error('[ERROR] Response status:', error.response.status);
802+
}
803+
break;
804+
}
805+
}
806+
return response.data.results;
807+
} catch (error) {
808+
console.error('[ERROR] fetching documents with metadata:', error.message);
809+
return [];
810+
}
811+
}
812+
764813

765814
// Aktualisierte getDocuments Methode
766815
async getDocuments() {

services/ragService.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)