1
+ using Azure ;
2
+ using Azure . Storage . Blobs ;
1
3
using azure_project_generator . models ;
2
4
using azure_project_generator . services ;
3
5
using Microsoft . Azure . Functions . Worker ;
4
6
using Microsoft . Extensions . Logging ;
5
7
using Newtonsoft . Json ;
6
8
using OpenAI . Embeddings ;
7
9
10
+
11
+
8
12
namespace azure_project_generator
9
13
{
10
14
public class ProcessCertServiceFile
@@ -13,16 +17,18 @@ public class ProcessCertServiceFile
13
17
private readonly EmbeddingClient _embeddingClient ;
14
18
private readonly JsonValidationService _jsonValidationService ;
15
19
private readonly ContentGenerationService _contentGenerationService ;
20
+ private readonly BlobServiceClient _blobServiceClient ;
16
21
17
22
public ProcessCertServiceFile ( ILogger < ProcessCertServiceFile > logger ,
18
23
EmbeddingClient embeddingClient ,
19
24
JsonValidationService jsonValidationService ,
20
- ContentGenerationService contentGenerationService )
25
+ ContentGenerationService contentGenerationService , BlobServiceClient blobServiceClient )
21
26
{
22
27
_logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
23
28
_embeddingClient = embeddingClient ?? throw new ArgumentNullException ( nameof ( embeddingClient ) ) ;
24
29
_jsonValidationService = jsonValidationService ?? throw new ArgumentNullException ( nameof ( jsonValidationService ) ) ;
25
30
_contentGenerationService = contentGenerationService ?? throw new ArgumentNullException ( nameof ( contentGenerationService ) ) ;
31
+ _blobServiceClient = blobServiceClient ?? throw new ArgumentNullException ( nameof ( blobServiceClient ) ) ;
26
32
}
27
33
28
34
[ Function ( nameof ( ProcessCertServiceFile ) ) ]
@@ -35,39 +41,64 @@ public async Task<CertificationServiceOutput> Run(
35
41
if ( string . IsNullOrWhiteSpace ( content ) )
36
42
{
37
43
_logger . LogError ( "Blob content is empty or whitespace." ) ;
38
- return new CertificationServiceOutput { Document = null } ;
44
+ return new CertificationServiceOutput { Document = null } ;
39
45
}
40
46
41
47
if ( ! _jsonValidationService . ValidateJsonContent < CertificationService > ( content ) )
42
48
{
49
+ _logger . LogError ( "Invalid JSON content." ) ;
43
50
return new CertificationServiceOutput { Document = null } ;
44
51
}
45
52
46
- var mappedServiceData = JsonConvert . DeserializeObject < CertificationService > ( content ) ;
47
- if ( mappedServiceData == null )
53
+ try
48
54
{
49
- _logger . LogError ( "Failed to deserialize content to MappedService." ) ;
50
- return new CertificationServiceOutput { Document = null } ;
51
- }
55
+ var mappedServiceData = JsonConvert . DeserializeObject < CertificationService > ( content ) ;
56
+ if ( mappedServiceData == null )
57
+ {
58
+ _logger . LogError ( "Failed to deserialize content to CertificationService." ) ;
59
+ return new CertificationServiceOutput { Document = null } ;
60
+ }
52
61
53
- string contextSentence = _contentGenerationService . GenerateCertServiceContextSentence ( mappedServiceData ) ;
54
- float [ ] contentVector = await _contentGenerationService . GenerateEmbeddingsAsync ( contextSentence ) ;
62
+ string contextSentence = _contentGenerationService . GenerateCertServiceContextSentence ( mappedServiceData ) ;
63
+ float [ ] contentVector = await _contentGenerationService . GenerateEmbeddingsAsync ( contextSentence ) ;
55
64
56
- var certServiceDocument = CreateCertServiceDocument ( mappedServiceData , contextSentence , contentVector ) ;
65
+ var certServiceDocument = CreateCertServiceDocument ( mappedServiceData , contextSentence , contentVector ) ;
57
66
58
- _logger . LogInformation ( "Document created successfully." ) ;
59
- _logger . LogInformation ( $ "Archiving blob: { name } ") ;
67
+ _logger . LogInformation ( "Document created successfully." ) ;
68
+ _logger . LogInformation ( $ "Deleting blob: { name } ") ;
60
69
70
+ // delete the blob
71
+ BlobContainerClient blobContainerClient = _blobServiceClient . GetBlobContainerClient ( "certservice" ) ;
72
+ BlobClient blobClient = blobContainerClient . GetBlobClient ( name ) ;
73
+ await blobClient . DeleteAsync ( ) ;
61
74
62
- return new CertificationServiceOutput
75
+ return new CertificationServiceOutput
76
+ {
77
+ Document = certServiceDocument ,
78
+ } ;
79
+ }
80
+ catch ( JsonException jsonEx )
81
+ {
82
+ _logger . LogError ( $ "JSON error occurred: { jsonEx . Message } ") ;
83
+ return new CertificationServiceOutput { Document = null } ;
84
+ }
85
+ catch ( HttpRequestException httpEx )
63
86
{
64
- Document = certServiceDocument ,
65
-
66
- } ;
87
+ _logger . LogError ( $ "HTTP request error occurred: { httpEx . Message } ") ;
88
+ return new CertificationServiceOutput { Document = null } ;
89
+ }
90
+ catch ( RequestFailedException storageEx )
91
+ {
92
+ _logger . LogError ( $ "Azure Storage error occurred: { storageEx . Message } ") ;
93
+ return new CertificationServiceOutput { Document = null } ;
94
+ }
95
+ catch ( Exception ex )
96
+ {
97
+ _logger . LogError ( $ "An unexpected error occurred: { ex . Message } ") ;
98
+ return new CertificationServiceOutput { Document = null } ;
99
+ }
67
100
}
68
101
69
- // Other methods remain unchanged
70
-
71
102
private CertificationServiceDocument CreateCertServiceDocument ( CertificationService data , string contextSentence , float [ ] contentVector ) =>
72
103
new CertificationServiceDocument
73
104
{
0 commit comments