forked from icereed/paperless-gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_llm.go
More file actions
612 lines (521 loc) · 19 KB
/
app_llm.go
File metadata and controls
612 lines (521 loc) · 19 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"slices"
"strings"
"sync"
"time"
_ "image/jpeg"
"github.com/sirupsen/logrus"
"github.com/tmc/langchaingo/llms"
)
// getSuggestedCorrespondent generates a suggested correspondent for a document using the LLM
func (app *App) getSuggestedCorrespondent(ctx context.Context, content string, suggestedTitle string, availableCorrespondents []string, correspondentBlackList []string) (string, error) {
likelyLanguage := getLikelyLanguage()
templateMutex.RLock()
defer templateMutex.RUnlock()
// Get available tokens for content
templateData := map[string]interface{}{
"Language": likelyLanguage,
"AvailableCorrespondents": availableCorrespondents,
"BlackList": correspondentBlackList,
"Title": suggestedTitle,
}
availableTokens, err := getAvailableTokensForContent(correspondentTemplate, templateData)
if err != nil {
return "", fmt.Errorf("error calculating available tokens: %v", err)
}
// Truncate content if needed
truncatedContent, err := truncateContentByTokens(content, availableTokens)
if err != nil {
return "", fmt.Errorf("error truncating content: %v", err)
}
// Execute template with truncated content
var promptBuffer bytes.Buffer
templateData["Content"] = truncatedContent
err = correspondentTemplate.Execute(&promptBuffer, templateData)
if err != nil {
return "", fmt.Errorf("error executing correspondent template: %v", err)
}
prompt := promptBuffer.String()
log.Debugf("Correspondent suggestion prompt: %s", prompt)
completion, err := app.LLM.GenerateContent(ctx, []llms.MessageContent{
{
Parts: []llms.ContentPart{
llms.TextContent{
Text: prompt,
},
},
Role: llms.ChatMessageTypeHuman,
},
})
if err != nil {
return "", fmt.Errorf("error getting response from LLM: %v", err)
}
response := stripReasoning(strings.TrimSpace(completion.Choices[0].Content))
return response, nil
}
// getSuggestedTags generates suggested tags for a document using the LLM
func (app *App) getSuggestedTags(
ctx context.Context,
content string,
suggestedTitle string,
availableTags []string,
originalTags []string,
logger *logrus.Entry) ([]string, error) {
likelyLanguage := getLikelyLanguage()
templateMutex.RLock()
defer templateMutex.RUnlock()
// Remove all paperless-gpt related tags from available tags
availableTags = removeTagFromList(availableTags, manualTag)
availableTags = removeTagFromList(availableTags, autoTag)
availableTags = removeTagFromList(availableTags, autoOcrTag)
// Get available tokens for content
templateData := map[string]interface{}{
"Language": likelyLanguage,
"AvailableTags": availableTags,
"OriginalTags": originalTags,
"Title": suggestedTitle,
}
availableTokens, err := getAvailableTokensForContent(tagTemplate, templateData)
if err != nil {
logger.Errorf("Error calculating available tokens: %v", err)
return nil, fmt.Errorf("error calculating available tokens: %v", err)
}
// Truncate content if needed
truncatedContent, err := truncateContentByTokens(content, availableTokens)
if err != nil {
logger.Errorf("Error truncating content: %v", err)
return nil, fmt.Errorf("error truncating content: %v", err)
}
// Execute template with truncated content
var promptBuffer bytes.Buffer
templateData["Content"] = truncatedContent
err = tagTemplate.Execute(&promptBuffer, templateData)
if err != nil {
logger.Errorf("Error executing tag template: %v", err)
return nil, fmt.Errorf("error executing tag template: %v", err)
}
prompt := promptBuffer.String()
logger.Debugf("Tag suggestion prompt: %s", prompt)
completion, err := app.LLM.GenerateContent(ctx, []llms.MessageContent{
{
Parts: []llms.ContentPart{
llms.TextContent{
Text: prompt,
},
},
Role: llms.ChatMessageTypeHuman,
},
})
if err != nil {
logger.Errorf("Error getting response from LLM: %v", err)
return nil, fmt.Errorf("error getting response from LLM: %v", err)
}
response := stripReasoning(completion.Choices[0].Content)
suggestedTags := strings.Split(response, ",")
for i, tag := range suggestedTags {
suggestedTags[i] = strings.TrimSpace(tag)
}
// append the original tags to the suggested tags
suggestedTags = append(suggestedTags, originalTags...)
// Remove duplicates
slices.Sort(suggestedTags)
suggestedTags = slices.Compact(suggestedTags)
// Filter out tags that are not in the available tags list
filteredTags := []string{}
for _, tag := range suggestedTags {
for _, availableTag := range availableTags {
if strings.EqualFold(tag, availableTag) {
filteredTags = append(filteredTags, availableTag)
break
}
}
}
return filteredTags, nil
}
// getSuggestedTitle generates a suggested title for a document using the LLM
func (app *App) getSuggestedTitle(ctx context.Context, content string, originalTitle string, logger *logrus.Entry) (string, error) {
likelyLanguage := getLikelyLanguage()
templateMutex.RLock()
defer templateMutex.RUnlock()
// Get available tokens for content
templateData := map[string]interface{}{
"Language": likelyLanguage,
"Content": content,
"Title": originalTitle,
}
availableTokens, err := getAvailableTokensForContent(titleTemplate, templateData)
if err != nil {
logger.Errorf("Error calculating available tokens: %v", err)
return "", fmt.Errorf("error calculating available tokens: %v", err)
}
// Truncate content if needed
truncatedContent, err := truncateContentByTokens(content, availableTokens)
if err != nil {
logger.Errorf("Error truncating content: %v", err)
return "", fmt.Errorf("error truncating content: %v", err)
}
// Execute template with truncated content
var promptBuffer bytes.Buffer
templateData["Content"] = truncatedContent
err = titleTemplate.Execute(&promptBuffer, templateData)
if err != nil {
return "", fmt.Errorf("error executing title template: %v", err)
}
prompt := promptBuffer.String()
logger.Debugf("Title suggestion prompt: %s", prompt)
completion, err := app.LLM.GenerateContent(ctx, []llms.MessageContent{
{
Parts: []llms.ContentPart{
llms.TextContent{
Text: prompt,
},
},
Role: llms.ChatMessageTypeHuman,
},
})
if err != nil {
return "", fmt.Errorf("error getting response from LLM: %v", err)
}
result := stripReasoning(completion.Choices[0].Content)
return strings.TrimSpace(strings.Trim(result, "\"")), nil
}
// getSuggestedCreatedDate generates a suggested createdDate for a document using the LLM
func (app *App) getSuggestedCreatedDate(ctx context.Context, content string, logger *logrus.Entry) (string, error) {
likelyLanguage := getLikelyLanguage()
templateMutex.RLock()
defer templateMutex.RUnlock()
// Get available tokens for content
templateData := map[string]interface{}{
"Language": likelyLanguage,
"Content": content,
"Today": getTodayDate(), // must be in YYYY-MM-DD format
}
availableTokens, err := getAvailableTokensForContent(createdDateTemplate, templateData)
if err != nil {
logger.Errorf("Error calculating available tokens: %v", err)
return "", fmt.Errorf("error calculating available tokens: %v", err)
}
// Truncate content if needed
truncatedContent, err := truncateContentByTokens(content, availableTokens)
if err != nil {
logger.Errorf("Error truncating content: %v", err)
return "", fmt.Errorf("error truncating content: %v", err)
}
// Execute template with truncated content
var promptBuffer bytes.Buffer
templateData["Content"] = truncatedContent
err = createdDateTemplate.Execute(&promptBuffer, templateData)
if err != nil {
return "", fmt.Errorf("error executing createdDate template: %v", err)
}
prompt := promptBuffer.String()
logger.Debugf("CreatedDate suggestion prompt: %s", prompt)
completion, err := app.LLM.GenerateContent(ctx, []llms.MessageContent{
{
Parts: []llms.ContentPart{
llms.TextContent{
Text: prompt,
},
},
Role: llms.ChatMessageTypeHuman,
},
})
if err != nil {
return "", fmt.Errorf("error getting response from LLM: %v", err)
}
result := stripReasoning(completion.Choices[0].Content)
return strings.TrimSpace(strings.Trim(result, "\"")), nil
}
// getSuggestedCustomFields generates suggested custom fields for a document using the LLM
func (app *App) getSuggestedCustomFields(ctx context.Context, doc Document, selectedFieldIDs []int, logger *logrus.Entry) ([]CustomFieldSuggestion, error) {
// Fetch all available custom fields
allCustomFields, err := app.Client.GetCustomFields(ctx)
if err != nil {
return nil, fmt.Errorf("error fetching all custom fields: %v", err)
}
// Filter to get only the selected custom fields
var selectedCustomFields []CustomField
for _, field := range allCustomFields {
for _, selectedID := range selectedFieldIDs {
if field.ID == selectedID {
selectedCustomFields = append(selectedCustomFields, field)
break
}
}
}
if len(selectedCustomFields) == 0 {
return nil, nil // No fields to process
}
// Generate XML for the prompt
var xmlBuilder strings.Builder
xmlBuilder.WriteString("<custom_fields>\n")
for _, field := range selectedCustomFields {
xmlBuilder.WriteString(fmt.Sprintf(" <field name=\"%s\" type=\"%s\"></field>\n", field.Name, field.DataType))
}
xmlBuilder.WriteString("</custom_fields>")
customFieldsXML := xmlBuilder.String()
templateMutex.RLock()
defer templateMutex.RUnlock()
templateData := map[string]interface{}{
"Language": getLikelyLanguage(),
"Title": doc.Title,
"CreatedDate": doc.CreatedDate,
"DocumentType": doc.DocumentTypeName,
"CustomFieldsXML": customFieldsXML,
}
availableTokens, err := getAvailableTokensForContent(customFieldTemplate, templateData)
if err != nil {
return nil, fmt.Errorf("error calculating available tokens for custom fields: %v", err)
}
truncatedContent, err := truncateContentByTokens(doc.Content, availableTokens)
if err != nil {
return nil, fmt.Errorf("error truncating content for custom fields: %v", err)
}
var promptBuffer bytes.Buffer
templateData["Content"] = truncatedContent
err = customFieldTemplate.Execute(&promptBuffer, templateData)
if err != nil {
return nil, fmt.Errorf("error executing custom field template: %v", err)
}
prompt := promptBuffer.String()
logger.Debugf("Custom field suggestion prompt: %s", prompt)
completion, err := app.LLM.GenerateContent(ctx, []llms.MessageContent{
{
Role: llms.ChatMessageTypeHuman,
Parts: []llms.ContentPart{
llms.TextContent{Text: prompt},
},
},
})
if err != nil {
return nil, fmt.Errorf("error getting response from LLM for custom fields: %v", err)
}
response := stripReasoning(completion.Choices[0].Content)
response = stripMarkdown(response)
logger.Debugf("LLM response for custom fields: %s", response)
// Temporary struct to unmarshal LLM response with field name
type LLMCustomFieldResponse struct {
Field string `json:"field"`
Value interface{} `json:"value"`
}
var llmSuggestedFields []LLMCustomFieldResponse
// Handle empty or non-JSON response gracefully
if strings.TrimSpace(response) == "" || !strings.HasPrefix(strings.TrimSpace(response), "[") {
return []CustomFieldSuggestion{}, nil
}
err = json.Unmarshal([]byte(response), &llmSuggestedFields)
if err != nil {
logger.Errorf("Error unmarshalling custom fields JSON from LLM response: %v. Response: %s", err, response)
return []CustomFieldSuggestion{}, nil // Return empty slice on parsing error
}
// Map field names back to IDs
fieldNameIdMap := make(map[string]int)
for _, field := range allCustomFields {
fieldNameIdMap[field.Name] = field.ID
}
var finalSuggestedFields []CustomFieldSuggestion
for _, llmField := range llmSuggestedFields {
if id, ok := fieldNameIdMap[llmField.Field]; ok {
finalSuggestedFields = append(finalSuggestedFields, CustomFieldSuggestion{
ID: id,
Name: llmField.Field,
Value: llmField.Value,
})
} else {
logger.Warnf("LLM returned unknown custom field name '%s', skipping.", llmField.Field)
}
}
return finalSuggestedFields, nil
}
// generateDocumentSuggestions generates suggestions for a set of documents
func (app *App) generateDocumentSuggestions(ctx context.Context, suggestionRequest GenerateSuggestionsRequest, logger *logrus.Entry) ([]DocumentSuggestion, error) {
// Fetch all available tags from paperless-ngx
availableTagsMap, err := app.Client.GetAllTags(ctx)
if err != nil {
return nil, fmt.Errorf("failed to fetch available tags: %v", err)
}
// Prepare a list of tag names
availableTagNames := make([]string, 0, len(availableTagsMap))
for tagName := range availableTagsMap {
if tagName == manualTag {
continue
}
availableTagNames = append(availableTagNames, tagName)
}
// Prepare a list of document correspodents
availableCorrespondentsMap, err := app.Client.GetAllCorrespondents(ctx)
if err != nil {
return nil, fmt.Errorf("failed to fetch available correspondents: %v", err)
}
// Prepare a list of correspondent names
availableCorrespondentNames := make([]string, 0, len(availableCorrespondentsMap))
for correspondentName := range availableCorrespondentsMap {
availableCorrespondentNames = append(availableCorrespondentNames, correspondentName)
}
documents := suggestionRequest.Documents
documentSuggestions := []DocumentSuggestion{}
var wg sync.WaitGroup
var mu sync.Mutex
errorsList := make([]error, 0)
for i := range documents {
wg.Add(1)
go func(doc Document) {
defer wg.Done()
documentID := doc.ID
docLogger := documentLogger(documentID)
startTime := time.Now()
docLogger.Printf("Processing Document ID %d...", documentID)
content := doc.Content
suggestedTitle := doc.Title
var suggestedTags []string
var suggestedCorrespondent string
var suggestedCreatedDate string
var suggestedCustomFields []CustomFieldSuggestion
if suggestionRequest.GenerateTitles {
suggestedTitle, err = app.getSuggestedTitle(ctx, content, suggestedTitle, docLogger)
if err != nil {
mu.Lock()
errorsList = append(errorsList, fmt.Errorf("Document %d: %v", documentID, err))
mu.Unlock()
docLogger.Errorf("Error processing document %d: %v", documentID, err)
return
}
}
if suggestionRequest.GenerateTags {
suggestedTags, err = app.getSuggestedTags(ctx, content, suggestedTitle, availableTagNames, doc.Tags, docLogger)
if err != nil {
mu.Lock()
errorsList = append(errorsList, fmt.Errorf("Document %d: %v", documentID, err))
mu.Unlock()
logger.Errorf("Error generating tags for document %d: %v", documentID, err)
return
}
}
if suggestionRequest.GenerateCorrespondents {
suggestedCorrespondent, err = app.getSuggestedCorrespondent(ctx, content, suggestedTitle, availableCorrespondentNames, correspondentBlackList)
if err != nil {
mu.Lock()
errorsList = append(errorsList, fmt.Errorf("Document %d: %v", documentID, err))
mu.Unlock()
log.Errorf("Error generating correspondents for document %d: %v", documentID, err)
return
}
}
if suggestionRequest.GenerateCreatedDate {
suggestedCreatedDate, err = app.getSuggestedCreatedDate(ctx, content, docLogger)
if err != nil {
mu.Lock()
errorsList = append(errorsList, fmt.Errorf("Document %d: %v", documentID, err))
mu.Unlock()
log.Errorf("Error generating createdDate for document %d: %v", documentID, err)
return
}
}
if suggestionRequest.GenerateCustomFields {
settingsMutex.RLock()
selectedIDs := settings.CustomFieldsSelectedIDs
settingsMutex.RUnlock()
if len(selectedIDs) == 0 {
log.Warnf("Custom field generation is enabled, but no custom fields are selected in the settings. Please select at least one custom field for this feature to work.")
} else {
suggestedCustomFields, err = app.getSuggestedCustomFields(ctx, doc, selectedIDs, docLogger)
if err != nil {
mu.Lock()
errorsList = append(errorsList, fmt.Errorf("Document %d: %v", documentID, err))
mu.Unlock()
log.Errorf("Error generating custom fields for document %d: %v", documentID, err)
return
}
}
}
mu.Lock()
suggestion := DocumentSuggestion{
ID: documentID,
OriginalDocument: doc,
}
settingsMutex.RLock()
suggestion.CustomFieldsWriteMode = settings.CustomFieldsWriteMode
suggestion.CustomFieldsEnable = settings.CustomFieldsEnable
settingsMutex.RUnlock()
// Titles
if suggestionRequest.GenerateTitles {
docLogger.Printf("Suggested title for document %d: %s", documentID, suggestedTitle)
suggestion.SuggestedTitle = suggestedTitle
} else {
suggestion.SuggestedTitle = doc.Title
}
// Tags
if suggestionRequest.GenerateTags {
docLogger.Printf("Suggested tags for document %d: %v", documentID, suggestedTags)
suggestion.SuggestedTags = suggestedTags
} else {
suggestion.SuggestedTags = doc.Tags
}
// Correspondents
if suggestionRequest.GenerateCorrespondents {
log.Printf("Suggested correspondent for document %d: %s", documentID, suggestedCorrespondent)
suggestion.SuggestedCorrespondent = suggestedCorrespondent
} else {
suggestion.SuggestedCorrespondent = ""
}
// CreatedDate
if suggestionRequest.GenerateCreatedDate {
log.Printf("Suggested createdDate for document %d: %s", documentID, suggestedCreatedDate)
suggestion.SuggestedCreatedDate = suggestedCreatedDate
} else {
suggestion.SuggestedCreatedDate = ""
}
// Custom Fields
if suggestionRequest.GenerateCustomFields {
log.Printf("Suggested custom fields for document %d: %v", documentID, suggestedCustomFields)
suggestion.SuggestedCustomFields = suggestedCustomFields
}
// Remove manual tag from the list of suggested tags
suggestion.RemoveTags = []string{manualTag, autoTag}
documentSuggestions = append(documentSuggestions, suggestion)
mu.Unlock()
elapsed := time.Since(startTime)
// Format as HH:MM:SS using UTC zero-time base.
runtime := time.Unix(0, elapsed.Nanoseconds()).UTC()
docLogger.Printf("Document %d processed successfully. Runtime: %s",
documentID, runtime.Format("15:04:05"))
}(documents[i])
}
wg.Wait()
if len(errorsList) > 0 {
return nil, errorsList[0] // Return the first error encountered
}
return documentSuggestions, nil
}
// getTodayDate returns the current date in YYYY-MM-DD format
func getTodayDate() string {
return time.Now().Format("2006-01-02")
}
// stripReasoning removes the reasoning from the content indicated by <think> and </think> tags.
func stripReasoning(content string) string {
// Remove reasoning from the content
reasoningStart := strings.Index(content, "<think>")
if reasoningStart != -1 {
reasoningEnd := strings.Index(content, "</think>")
if reasoningEnd != -1 {
content = content[:reasoningStart] + content[reasoningEnd+len("</think>"):]
}
}
// Trim whitespace
content = strings.TrimSpace(content)
return content
}
// stripMarkdown removes the markdown code block from the content.
func stripMarkdown(content string) string {
// Remove markdown code block
if strings.HasPrefix(content, "```json") {
content = strings.TrimPrefix(content, "```json")
content = strings.TrimSuffix(content, "```")
}
return strings.TrimSpace(content)
}