|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Diagnostics; |
| 5 | +using CleanArchitecture.Blazor.Application.Features.Documents.Caching; |
| 6 | +using CleanArchitecture.Blazor.Domain.Common.Enums; |
| 7 | +using Microsoft.Extensions.AI; |
| 8 | +using Microsoft.Extensions.Configuration; |
| 9 | +using OpenAI; |
| 10 | +using OpenAI.Chat; |
| 11 | + |
| 12 | +namespace CleanArchitecture.Blazor.Infrastructure.Services.OpenAI; |
| 13 | + |
| 14 | +public class DocumentOcrJob : IDocumentOcrJob |
| 15 | +{ |
| 16 | + private const int MaxContentLength = 4000; |
| 17 | + private const string SystemPrompt = "You are an advanced visual analysis AI. Analyze and describe images based on visual content, providing structured output in Markdown format."; |
| 18 | + private const string UserPrompt = "Analyze the following image and provide a comprehensive, briefly description in Markdown format."; |
| 19 | + |
| 20 | + private readonly IApplicationDbContext _db; |
| 21 | + private readonly IConfiguration _config; |
| 22 | + private readonly ILogger<DocumentOcrJob> _logger; |
| 23 | + private readonly IApplicationHubWrapper _hubNotification; |
| 24 | + |
| 25 | + public DocumentOcrJob( |
| 26 | + IApplicationHubWrapper hubNotification, |
| 27 | + IApplicationDbContext db, |
| 28 | + IConfiguration config, |
| 29 | + ILogger<DocumentOcrJob> logger) |
| 30 | + { |
| 31 | + _hubNotification = hubNotification; |
| 32 | + _db = db; |
| 33 | + _config = config; |
| 34 | + _logger = logger; |
| 35 | + } |
| 36 | + |
| 37 | + public void Do(int id) |
| 38 | + { |
| 39 | + ProcessDocumentAsync(id, CancellationToken.None).Wait(); |
| 40 | + } |
| 41 | + |
| 42 | + public async Task Recognition(int id, CancellationToken cancellationToken) |
| 43 | + { |
| 44 | + await ProcessDocumentAsync(id, cancellationToken); |
| 45 | + } |
| 46 | + |
| 47 | + private async Task ProcessDocumentAsync(int id, CancellationToken cancellationToken) |
| 48 | + { |
| 49 | + var stopwatch = Stopwatch.StartNew(); |
| 50 | + |
| 51 | + try |
| 52 | + { |
| 53 | + |
| 54 | + |
| 55 | + var document = await _db.Documents.FindAsync(new object[] { id }, cancellationToken); |
| 56 | + if (document is null) |
| 57 | + { |
| 58 | + _logger.LogWarning("Document not found. DocumentId: {DocumentId}", id); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + await _hubNotification.JobStarted(id, document.Title!); |
| 63 | + InvalidateDocumentCache(); |
| 64 | + |
| 65 | + if (string.IsNullOrWhiteSpace(document.URL)) |
| 66 | + { |
| 67 | + await UpdateDocumentWithError(_db, document, "Document URL is missing or invalid.", cancellationToken); |
| 68 | + _logger.LogWarning("Invalid document URL. DocumentId: {DocumentId}", id); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + var analysisResult = await AnalyzeDocumentImageAsync(document.URL, cancellationToken); |
| 73 | + |
| 74 | + await UpdateDocumentWithResult(_db, document, analysisResult, cancellationToken); |
| 75 | + await _hubNotification.JobCompleted(id, document.Title!); |
| 76 | + InvalidateDocumentCache(); |
| 77 | + |
| 78 | + stopwatch.Stop(); |
| 79 | + _logger.LogInformation( |
| 80 | + "Document visual analysis completed. DocumentId: {DocumentId}, Title: {Title}, Duration: {Duration}ms", |
| 81 | + id, document.Title, stopwatch.ElapsedMilliseconds); |
| 82 | + } |
| 83 | + catch (Exception ex) |
| 84 | + { |
| 85 | + stopwatch.Stop(); |
| 86 | + await _hubNotification.JobCompleted(id, $"Analysis failed: {ex.Message}"); |
| 87 | + _logger.LogError(ex, |
| 88 | + "Document visual analysis failed. DocumentId: {DocumentId}, Duration: {Duration}ms", |
| 89 | + id, stopwatch.ElapsedMilliseconds); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private async Task<string> AnalyzeDocumentImageAsync(string imageUrl, CancellationToken cancellationToken) |
| 94 | + { |
| 95 | + try |
| 96 | + { |
| 97 | + var apiKey = _config["AISettings:OpenAIApiKey"]; |
| 98 | + var model = _config["AISettings:OpenAIModel"]; |
| 99 | + var client = new OpenAIClient(apiKey); |
| 100 | + var chatClient = client.GetChatClient(model); |
| 101 | + var agent = chatClient.AsAIAgent(instructions: SystemPrompt); |
| 102 | + |
| 103 | + var message = new Microsoft.Extensions.AI.ChatMessage(ChatRole.User, [ |
| 104 | + new TextContent(UserPrompt), |
| 105 | + new UriContent(imageUrl, "image/png") |
| 106 | + ]); |
| 107 | + |
| 108 | + var response = await agent.RunAsync(message, cancellationToken: cancellationToken); |
| 109 | + var result = response.Text ?? string.Empty; |
| 110 | + |
| 111 | + return result.Length > MaxContentLength |
| 112 | + ? result[..MaxContentLength] |
| 113 | + : result; |
| 114 | + } |
| 115 | + catch (Exception ex) |
| 116 | + { |
| 117 | + _logger.LogError(ex, "AI vision analysis failed. ImageUrl: {ImageUrl}", imageUrl); |
| 118 | + return $"[Analysis Error] {ex.Message}"; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private async Task UpdateDocumentWithResult( |
| 123 | + IApplicationDbContext dbContext, |
| 124 | + Document document, |
| 125 | + string content, |
| 126 | + CancellationToken cancellationToken) |
| 127 | + { |
| 128 | + document.Status = JobStatus.Done; |
| 129 | + document.Description = "Visual analysis completed successfully."; |
| 130 | + document.Content = content; |
| 131 | + |
| 132 | + await dbContext.SaveChangesAsync(cancellationToken); |
| 133 | + } |
| 134 | + |
| 135 | + private async Task UpdateDocumentWithError( |
| 136 | + IApplicationDbContext dbContext, |
| 137 | + Document document, |
| 138 | + string errorMessage, |
| 139 | + CancellationToken cancellationToken) |
| 140 | + { |
| 141 | + document.Status = JobStatus.Pending; |
| 142 | + document.Description = $"Analysis failed: {errorMessage}"; |
| 143 | + document.Content = string.Empty; |
| 144 | + |
| 145 | + await dbContext.SaveChangesAsync(cancellationToken); |
| 146 | + await _hubNotification.JobCompleted(document.Id, errorMessage); |
| 147 | + } |
| 148 | + |
| 149 | + private static void InvalidateDocumentCache() |
| 150 | + { |
| 151 | + DocumentCacheKey.Refresh(); |
| 152 | + } |
| 153 | +} |
| 154 | + |
0 commit comments