-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathcustomService.js
316 lines (267 loc) · 10.3 KB
/
customService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
const OpenAI = require('openai');
const config = require('../config/config');
const tiktoken = require('tiktoken');
const paperlessService = require('./paperlessService');
const fs = require('fs').promises;
const path = require('path');
class CustomOpenAIService {
constructor() {
this.client = null;
this.tokenizer = null;
}
initialize() {
if (!this.client && config.aiProvider === 'custom') {
this.client = new OpenAI({
baseURL: config.custom.apiUrl,
apiKey: config.custom.apiKey
});
}
}
// Calculate tokens for a given text
async calculateTokens(text) {
if (!this.tokenizer) {
// Use the appropriate model encoding
this.tokenizer = await tiktoken.encoding_for_model(process.env.OPENAI_MODEL || "gpt-4o-mini");
}
return this.tokenizer.encode(text).length;
}
// Calculate tokens for a given text
async calculateTotalPromptTokens(systemPrompt, additionalPrompts = []) {
let totalTokens = 0;
// Count tokens for system prompt
totalTokens += await this.calculateTokens(systemPrompt);
// Count tokens for additional prompts
for (const prompt of additionalPrompts) {
if (prompt) { // Only count if prompt exists
totalTokens += await this.calculateTokens(prompt);
}
}
// Add tokens for message formatting (approximately 4 tokens per message)
const messageCount = 1 + additionalPrompts.filter(p => p).length; // Count system + valid additional prompts
totalTokens += messageCount * 4;
return totalTokens;
}
// Truncate text to fit within token limit
async truncateToTokenLimit(text, maxTokens) {
const tokens = await this.calculateTokens(text);
if (tokens <= maxTokens) return text;
// Simple truncation strategy - could be made more sophisticated
const ratio = maxTokens / tokens;
return text.slice(0, Math.floor(text.length * ratio));
}
async analyzeDocument(content, existingTags = [], existingCorrespondentList = [], id) {
const cachePath = path.join('./public/images', `${id}.png`);
try {
this.initialize();
const now = new Date();
const timestamp = now.toLocaleString('de-DE', { dateStyle: 'short', timeStyle: 'short' });
if (!this.client) {
throw new Error('Custom OpenAI client not initialized');
}
// Handle thumbnail caching
try {
await fs.access(cachePath);
console.log('[DEBUG] Thumbnail already cached');
} catch (err) {
console.log('Thumbnail not cached, fetching from Paperless');
const thumbnailData = await paperlessService.getThumbnailImage(id);
if (!thumbnailData) {
console.warn('Thumbnail nicht gefunden');
}
await fs.mkdir(path.dirname(cachePath), { recursive: true });
await fs.writeFile(cachePath, thumbnailData);
}
// Format existing tags
const existingTagsList = existingTags
.map(tag => tag.name)
.join(', ');
let systemPrompt = '';
let promptTags = '';
const model = config.custom.model;
// Get system prompt and model
if(process.env.USE_EXISTING_DATA === 'yes') {
systemPrompt = `
Prexisting tags: ${existingTagsList}\n\n
Prexisiting correspondent: ${existingCorrespondentList}\n\n
` + process.env.SYSTEM_PROMPT + '\n\n' + config.mustHavePrompt;
promptTags = '';
} else {
systemPrompt = process.env.SYSTEM_PROMPT + '\n\n' + config.mustHavePrompt;
promptTags = '';
}
if (process.env.USE_PROMPT_TAGS === 'yes') {
promptTags = process.env.PROMPT_TAGS;
systemPrompt = `
Take these tags and try to match one or more to the document content.\n\n
` + config.specialPromptPreDefinedTags;
}
// Calculate total prompt tokens including all components
const totalPromptTokens = await this.calculateTotalPromptTokens(
systemPrompt,
process.env.USE_PROMPT_TAGS === 'yes' ? [promptTags] : []
);
// Calculate available tokens
const maxTokens = 128000; // Model's maximum context length
const reservedTokens = totalPromptTokens + 1000; // Reserve for response
const availableTokens = maxTokens - reservedTokens;
// Truncate content if necessary
const truncatedContent = await this.truncateToTokenLimit(content, availableTokens);
// Make API request
const response = await this.client.chat.completions.create({
model: model,
messages: [
{
role: "system",
content: systemPrompt
},
{
role: "user",
content: truncatedContent
}
],
...(model !== 'o3-mini' && { temperature: 0.3 }),
});
// Handle response
if (!response?.choices?.[0]?.message?.content) {
throw new Error('Invalid API response structure');
}
// Log token usage
console.log(`[DEBUG] [${timestamp}] OpenAI request sent`);
console.log(`[DEBUG] [${timestamp}] Total tokens: ${response.usage.total_tokens}`);
const usage = response.usage;
const mappedUsage = {
promptTokens: usage.prompt_tokens,
completionTokens: usage.completion_tokens,
totalTokens: usage.total_tokens
};
let jsonContent = response.choices[0].message.content;
jsonContent = jsonContent.replace(/```json\n?/g, '').replace(/```\n?/g, '').trim();
let parsedResponse;
try {
parsedResponse = JSON.parse(jsonContent);
} catch (error) {
console.error('Failed to parse JSON response:', error);
throw new Error('Invalid JSON response from API');
}
// Validate response structure
if (!parsedResponse || !Array.isArray(parsedResponse.tags) || typeof parsedResponse.correspondent !== 'string') {
throw new Error('Invalid response structure: missing tags array or correspondent string');
}
return {
document: parsedResponse,
metrics: mappedUsage,
truncated: truncatedContent.length < content.length
};
} catch (error) {
console.error('Failed to analyze document:', error);
return {
document: { tags: [], correspondent: null },
metrics: null,
error: error.message
};
}
}
async writePromptToFile(systemPrompt, truncatedContent) {
const filePath = './logs/prompt.txt';
const maxSize = 10 * 1024 * 1024;
try {
const stats = await fs.stat(filePath);
if (stats.size > maxSize) {
await fs.unlink(filePath); // Delete the file if is biger 10MB
}
} catch (error) {
if (error.code !== 'ENOENT') {
console.warn('[WARNING] Error checking file size:', error);
}
}
try {
await fs.appendFile(filePath, systemPrompt + truncatedContent + '\n\n');
} catch (error) {
console.error('[ERROR] Error writing to file:', error);
}
}
async analyzePlayground(content, prompt) {
const musthavePrompt = `
Return the result EXCLUSIVELY as a JSON object. The Tags and Title MUST be in the language that is used in the document.:
{
"title": "xxxxx",
"correspondent": "xxxxxxxx",
"tags": ["Tag1", "Tag2", "Tag3", "Tag4"],
"document_date": "YYYY-MM-DD",
"language": "en/de/es/..."
}`;
try {
this.initialize();
const now = new Date();
const timestamp = now.toLocaleString('de-DE', { dateStyle: 'short', timeStyle: 'short' });
if (!this.client) {
throw new Error('OpenAI client not initialized - missing API key');
}
// Calculate total prompt tokens including musthavePrompt
const totalPromptTokens = await this.calculateTotalPromptTokens(
prompt + musthavePrompt // Combined system prompt
);
// Calculate available tokens
const maxTokens = 128000;
const reservedTokens = totalPromptTokens + 1000; // Reserve for response
const availableTokens = maxTokens - reservedTokens;
// Truncate content if necessary
const truncatedContent = await this.truncateToTokenLimit(content, availableTokens);
// Make API request
const response = await this.client.chat.completions.create({
model: config.custom.model,
messages: [
{
role: "system",
content: prompt + musthavePrompt
},
{
role: "user",
content: truncatedContent
}
],
temperature: 0.3,
});
// Handle response
if (!response?.choices?.[0]?.message?.content) {
throw new Error('Invalid API response structure');
}
// Log token usage
console.log(`[DEBUG] [${timestamp}] OpenAI request sent`);
console.log(`[DEBUG] [${timestamp}] Total tokens: ${response.usage.total_tokens}`);
const usage = response.usage;
const mappedUsage = {
promptTokens: usage.prompt_tokens,
completionTokens: usage.completion_tokens,
totalTokens: usage.total_tokens
};
console.log(mappedUsage);
let jsonContent = response.choices[0].message.content;
jsonContent = jsonContent.replace(/```json\n?/g, '').replace(/```\n?/g, '').trim();
let parsedResponse;
try {
parsedResponse = JSON.parse(jsonContent);
} catch (error) {
console.error('Failed to parse JSON response:', error);
throw new Error('Invalid JSON response from API');
}
// Validate response structure
if (!parsedResponse || !Array.isArray(parsedResponse.tags) || typeof parsedResponse.correspondent !== 'string') {
throw new Error('Invalid response structure: missing tags array or correspondent string');
}
return {
document: parsedResponse,
metrics: mappedUsage,
truncated: truncatedContent.length < content.length
};
} catch (error) {
console.error('Failed to analyze document:', error);
return {
document: { tags: [], correspondent: null },
metrics: null,
error: error.message
};
}
}
}
module.exports = new CustomOpenAIService();