|
| 1 | +using azure_project_generator.models; |
| 2 | +using azure_project_generator.services; |
| 3 | +using Microsoft.Azure.Functions.Worker; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using Newtonsoft.Json; |
| 6 | +using OpenAI.Embeddings; |
| 7 | + |
| 8 | +namespace azure_project_generator |
| 9 | +{ |
| 10 | + public class CertificationServiceFileProcessor |
| 11 | + { |
| 12 | + private readonly ILogger<CertificationServiceFileProcessor> _logger; |
| 13 | + private readonly EmbeddingClient _embeddingClient; |
| 14 | + private readonly JsonValidationService _jsonValidationService; |
| 15 | + private readonly ContentGenerationService _contentGenerationService; |
| 16 | + |
| 17 | + public CertificationServiceFileProcessor(ILogger<CertificationServiceFileProcessor> logger, |
| 18 | + EmbeddingClient embeddingClient, |
| 19 | + JsonValidationService jsonValidationService, |
| 20 | + ContentGenerationService contentGenerationService) |
| 21 | + { |
| 22 | + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| 23 | + _embeddingClient = embeddingClient ?? throw new ArgumentNullException(nameof(embeddingClient)); |
| 24 | + _jsonValidationService = jsonValidationService ?? throw new ArgumentNullException(nameof(jsonValidationService)); |
| 25 | + _contentGenerationService = contentGenerationService ?? throw new ArgumentNullException(nameof(contentGenerationService)); |
| 26 | + } |
| 27 | + |
| 28 | + [Function(nameof(CertificationServiceFileProcessor))] |
| 29 | + public async Task<CertificationServiceOutput> Run( |
| 30 | + [BlobTrigger("certservice/{name}", Connection = "AzureWebJobsStorage")] string content, |
| 31 | + string name) |
| 32 | + { |
| 33 | + _logger.LogInformation($"Processing blob: {name}"); |
| 34 | + |
| 35 | + if (string.IsNullOrWhiteSpace(content)) |
| 36 | + { |
| 37 | + _logger.LogError("Blob content is empty or whitespace."); |
| 38 | + return new CertificationServiceOutput { Document = null}; |
| 39 | + } |
| 40 | + |
| 41 | + if (!_jsonValidationService.ValidateJsonContent<CertificationService>(content)) |
| 42 | + { |
| 43 | + return new CertificationServiceOutput { Document = null }; |
| 44 | + } |
| 45 | + |
| 46 | + var mappedServiceData = JsonConvert.DeserializeObject<CertificationService>(content); |
| 47 | + if (mappedServiceData == null) |
| 48 | + { |
| 49 | + _logger.LogError("Failed to deserialize content to MappedService."); |
| 50 | + return new CertificationServiceOutput { Document = null }; |
| 51 | + } |
| 52 | + |
| 53 | + string contextSentence = _contentGenerationService.GenerateCertServiceContextSentence(mappedServiceData); |
| 54 | + float[] contentVector = await _contentGenerationService.GenerateEmbeddingsAsync(contextSentence); |
| 55 | + |
| 56 | + var certServiceDocument = CreateCertServiceDocument(mappedServiceData, contextSentence, contentVector); |
| 57 | + |
| 58 | + _logger.LogInformation("Document created successfully."); |
| 59 | + _logger.LogInformation($"Archiving blob: {name}"); |
| 60 | + |
| 61 | + |
| 62 | + return new CertificationServiceOutput |
| 63 | + { |
| 64 | + Document = certServiceDocument, |
| 65 | + |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + private CertificationServiceDocument CreateCertServiceDocument(CertificationService data, string contextSentence, float[] contentVector) => |
| 71 | + new CertificationServiceDocument |
| 72 | + { |
| 73 | + Id = Guid.NewGuid().ToString(), |
| 74 | + CertificationServiceKey = $"{data.CertificationCode}-{data.ServiceName}", |
| 75 | + CertificationCode = data.CertificationCode, |
| 76 | + CertificationName = data.CertificationName, |
| 77 | + SkillName = data.SkillName, |
| 78 | + TopicName = data.TopicName, |
| 79 | + ServiceName = data.ServiceName, |
| 80 | + ContextSentence = contextSentence, |
| 81 | + ContextVector = contentVector |
| 82 | + }; |
| 83 | + } |
| 84 | +} |
0 commit comments