From 8b6041a93f9e23b993562f760bbafcb2767e9003 Mon Sep 17 00:00:00 2001 From: Ingmar Stein Date: Mon, 27 Jan 2025 09:10:53 +0100 Subject: [PATCH] Make the existing document title available in prompt template. (#158) This allow a prompt to feed the LLM with the title as determined by Paperless to improve its suggestion. This also fixes the case when `getSuggestedTags` or `getSuggestedCorrespondent` are called, but `GenerateTitles` is off (in which case an empty title was used). Fixes #137 --- app_llm.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app_llm.go b/app_llm.go index 0f5fe0c..d1c6310 100644 --- a/app_llm.go +++ b/app_llm.go @@ -191,7 +191,7 @@ func (app *App) doOCRViaLLM(ctx context.Context, jpegBytes []byte, logger *logru } // getSuggestedTitle generates a suggested title for a document using the LLM -func (app *App) getSuggestedTitle(ctx context.Context, content string, logger *logrus.Entry) (string, error) { +func (app *App) getSuggestedTitle(ctx context.Context, content string, suggestedTitle string, logger *logrus.Entry) (string, error) { likelyLanguage := getLikelyLanguage() templateMutex.RLock() @@ -201,6 +201,7 @@ func (app *App) getSuggestedTitle(ctx context.Context, content string, logger *l err := titleTemplate.Execute(&promptBuffer, map[string]interface{}{ "Language": likelyLanguage, "Content": content, + "Title": suggestedTitle, }) if err != nil { return "", fmt.Errorf("error executing title template: %v", err) @@ -276,12 +277,12 @@ func (app *App) generateDocumentSuggestions(ctx context.Context, suggestionReque content = content[:5000] } - var suggestedTitle string + suggestedTitle := doc.Title var suggestedTags []string var suggestedCorrespondent string if suggestionRequest.GenerateTitles { - suggestedTitle, err = app.getSuggestedTitle(ctx, content, docLogger) + suggestedTitle, err = app.getSuggestedTitle(ctx, content, suggestedTitle, docLogger) if err != nil { mu.Lock() errorsList = append(errorsList, fmt.Errorf("Document %d: %v", documentID, err))