From 84d8b4125cf048d4ab0b872fcec358c76cdbbf54 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Tue, 30 Dec 2025 13:45:33 +0100 Subject: [PATCH] Allow images inside the document analysis assistance --- .../DocumentAnalysisAssistant.razor.cs | 120 +++++++++++++----- 1 file changed, 87 insertions(+), 33 deletions(-) diff --git a/app/MindWork AI Studio/Assistants/DocumentAnalysis/DocumentAnalysisAssistant.razor.cs b/app/MindWork AI Studio/Assistants/DocumentAnalysis/DocumentAnalysisAssistant.razor.cs index d7ee8987..d0265178 100644 --- a/app/MindWork AI Studio/Assistants/DocumentAnalysis/DocumentAnalysisAssistant.razor.cs +++ b/app/MindWork AI Studio/Assistants/DocumentAnalysis/DocumentAnalysisAssistant.razor.cs @@ -1,3 +1,5 @@ +using System.Text; + using AIStudio.Chat; using AIStudio.Dialogs; using AIStudio.Dialogs.Settings; @@ -34,11 +36,13 @@ public partial class DocumentAnalysisAssistant : AssistantBaseCore - this.loadedDocumentPaths.Count > 1 - ? $"Your task is to analyze {this.loadedDocumentPaths.Count} DOCUMENTS. Different DOCUMENTS are divided by a horizontal rule in markdown formatting followed by the name of the document." - : "Your task is to analyze a single document."; + private string GetDocumentTaskDescription() + { + var numDocuments = this.loadedDocumentPaths.Count(x => x is { Exists: true, IsImage: false }); + var numImages = this.loadedDocumentPaths.Count(x => x is { Exists: true, IsImage: true }); + + return (numDocuments, numImages) switch + { + (0, 1) => "Your task is to analyze a single image file attached as a document.", + (0, > 1) => $"Your task is to analyze {numImages} image file(s) attached as documents.", + + (1, 0) => "Your task is to analyze a single DOCUMENT.", + (1, 1) => "Your task is to analyze a single DOCUMENT and 1 image file attached as a document.", + (1, > 1) => $"Your task is to analyze a single DOCUMENT and {numImages} image file(s) attached as documents.", + + (> 0, 0) => $"Your task is to analyze {numDocuments} DOCUMENTS. Different DOCUMENTS are divided by a horizontal rule in markdown formatting followed by the name of the document.", + (> 0, 1) => $"Your task is to analyze {numDocuments} DOCUMENTS and 1 image file attached as a document. Different DOCUMENTS are divided by a horizontal rule in Markdown formatting followed by the name of the document.", + (> 0, > 0) => $"Your task is to analyze {numDocuments} DOCUMENTS and {numImages} image file(s) attached as documents. Different DOCUMENTS are divided by a horizontal rule in Markdown formatting followed by the name of the document.", + + _ => "Your task is to analyze a single DOCUMENT." + }; + } protected override IReadOnlyList FooterButtons => []; @@ -327,37 +348,68 @@ public partial class DocumentAnalysisAssistant : AssistantBaseCore(); - var count = 1; + var documents = this.loadedDocumentPaths.Where(n => n is { Exists: true, IsImage: false }).ToList(); + var sb = new StringBuilder(); - foreach (var fileAttachment in this.loadedDocumentPaths) + if (documents.Count > 0) { - if (fileAttachment.IsForbidden) - { - this.Logger.LogWarning($"Skipping forbidden file: '{fileAttachment.FilePath}'."); - continue; - } - - var fileContent = await this.RustService.ReadArbitraryFileData(fileAttachment.FilePath, int.MaxValue); + sb.AppendLine(""" + # DOCUMENTS: - documentSections.Add($""" - ## DOCUMENT {count}: - File path: {fileAttachment.FilePath} - Content: - ``` - {fileContent} - ``` - - --- - """); - count++; + """); } - return $""" - # DOCUMENTS: + var numDocuments = 1; + foreach (var document in documents) + { + if (document.IsForbidden) + { + this.Logger.LogWarning($"Skipping forbidden file: '{document.FilePath}'."); + continue; + } - {string.Join("\n", documentSections)} - """; + var fileContent = await this.RustService.ReadArbitraryFileData(document.FilePath, int.MaxValue); + sb.AppendLine($""" + + ## DOCUMENT {numDocuments}: + File path: {document.FilePath} + Content: + ``` + {fileContent} + ``` + + --- + + """); + numDocuments++; + } + + var numImages = this.loadedDocumentPaths.Count(x => x is { IsImage: true, Exists: true }); + if (numImages > 0) + { + if (documents.Count == 0) + { + sb.AppendLine($""" + + There are {numImages} image file(s) attached as documents. + Please consider them as documents as well and use them to + answer accordingly. + + """); + } + else + { + sb.AppendLine($""" + + Additionally, there are {numImages} image file(s) attached. + Please consider them as documents as well and use them to + answer accordingly. + + """); + } + } + + return sb.ToString(); } private async Task Analyze() @@ -370,7 +422,9 @@ public partial class DocumentAnalysisAssistant : AssistantBaseCore n is { Exists: true, IsImage: true }).ToList()); await this.AddAIResponseAsync(userRequest); }