1
- using Microsoft . Azure . Functions . Worker ;
2
- using Microsoft . Extensions . Logging ;
3
- using Newtonsoft . Json ;
4
- using Newtonsoft . Json . Linq ;
5
- using Newtonsoft . Json . Schema ;
6
- using Newtonsoft . Json . Schema . Generation ;
7
- using Newtonsoft . Json . Serialization ;
8
-
9
- namespace azure_project_generator
10
- {
11
- public class ProcessFile
12
- {
13
- private readonly ILogger < ProcessFile > _logger ;
14
-
15
-
16
- public ProcessFile ( ILogger < ProcessFile > logger )
17
- {
18
- _logger = logger ;
19
-
20
- }
21
-
22
-
23
- [ Function ( nameof ( ProcessFile ) ) ] public async Task Run ( [ BlobTrigger ( "certdata/{name}" , Connection = "AzureWebJobsStorage" ) ] Stream stream , string name )
24
- {
25
- string content ;
26
- try
27
- {
28
- using var blobStreamReader = new StreamReader ( stream ) ;
29
- content = await blobStreamReader . ReadToEndAsync ( ) ;
30
- }
31
- catch ( IOException ex )
32
- {
33
- _logger . LogError ( $ "Error reading blob content: { ex . Message } ") ;
34
- return ;
35
- }
36
-
37
- _logger . LogInformation ( $ "C# Blob trigger function Processed blob\n Name: { name } ") ;
38
-
39
- if ( string . IsNullOrWhiteSpace ( content ) )
40
- {
41
- _logger . LogError ( "Blob content is empty or whitespace." ) ;
42
- return ;
43
- }
44
-
45
- try
46
- {
47
- ValidateJsonContent ( content ) ;
48
-
49
-
50
- }
51
- catch ( JsonReaderException ex )
52
- {
53
- _logger . LogError ( $ "JSON parsing error: { ex . Message } ") ;
54
- }
55
- catch ( Exception ex )
56
- {
57
- _logger . LogError ( $ "An unexpected error occurred: { ex . Message } ") ;
58
- }
59
- }
60
- private void ValidateJsonContent ( string content )
1
+ using Azure . AI . OpenAI ;
2
+ using Azure ;
3
+ using Microsoft . Azure . Functions . Worker ;
4
+ using Microsoft . Extensions . Logging ;
5
+ using Newtonsoft . Json ;
6
+ using Newtonsoft . Json . Linq ;
7
+ using Newtonsoft . Json . Schema ;
8
+ using Newtonsoft . Json . Schema . Generation ;
9
+ using Newtonsoft . Json . Serialization ;
10
+ using OpenAI . Chat ;
11
+ using OpenAI . Embeddings ;
12
+
13
+ namespace azure_project_generator
14
+ {
15
+ public class ProcessFile
16
+ {
17
+ private readonly ILogger < ProcessFile > _logger ;
18
+
19
+ private readonly EmbeddingClient _embeddingClient ;
20
+
21
+
22
+ public ProcessFile ( ILogger < ProcessFile > logger )
23
+ {
24
+ _logger = logger ;
25
+ // Initialize and validate environment variables
26
+ string keyFromEnvironment = Environment . GetEnvironmentVariable ( "AZURE_OPENAI_API_KEY" ) ;
27
+ string endpointFromEnvironment = Environment . GetEnvironmentVariable ( "AZURE_OPENAI_API_ENDPOINT" ) ;
28
+ string embeddingsDeployment = Environment . GetEnvironmentVariable ( "EMBEDDINGS_DEPLOYMENT" ) ;
29
+
30
+ if ( string . IsNullOrEmpty ( keyFromEnvironment ) || string . IsNullOrEmpty ( endpointFromEnvironment ) || string . IsNullOrEmpty ( embeddingsDeployment ) )
31
+ {
32
+ _logger . LogError ( "Environment variables for Azure OpenAI API are not set properly." ) ;
33
+ throw new InvalidOperationException ( "Required environment variables are missing." ) ;
34
+ }
35
+
36
+ // Initialize Azure OpenAI client
37
+ AzureOpenAIClient azureClient = new (
38
+ new Uri ( endpointFromEnvironment ) ,
39
+ new AzureKeyCredential ( keyFromEnvironment ) ) ;
40
+
41
+ _embeddingClient = azureClient . GetEmbeddingClient ( embeddingsDeployment ) ;
42
+
43
+ }
44
+
45
+
46
+ [ Function ( nameof ( ProcessFile ) ) ]
47
+ public async Task Run ( [ BlobTrigger ( "certdata/{name}" , Connection = "AzureWebJobsStorage" ) ] Stream stream , string name )
48
+ {
49
+ string content ;
50
+ try
51
+ {
52
+ using var blobStreamReader = new StreamReader ( stream ) ;
53
+ content = await blobStreamReader . ReadToEndAsync ( ) ;
54
+ }
55
+ catch ( IOException ex )
56
+ {
57
+ _logger . LogError ( $ "Error reading blob content: { ex . Message } ") ;
58
+ return ;
59
+ }
60
+
61
+ _logger . LogInformation ( $ "C# Blob trigger function Processed blob\n Name: { name } ") ;
62
+
63
+ if ( string . IsNullOrWhiteSpace ( content ) )
64
+ {
65
+ _logger . LogError ( "Blob content is empty or whitespace." ) ;
66
+ return ;
67
+ }
68
+
69
+ try
70
+ {
71
+ ValidateJsonContent ( content ) ;
72
+
73
+ }
74
+ catch ( JsonReaderException ex )
75
+ {
76
+ _logger . LogError ( $ "JSON parsing error: { ex . Message } ") ;
77
+ }
78
+ catch ( Exception ex )
79
+ {
80
+ _logger . LogError ( $ "An unexpected error occurred: { ex . Message } ") ;
81
+ }
82
+
83
+
84
+
85
+ // TODO generate embeddings
86
+
87
+
88
+ var certificationData = JsonConvert . DeserializeObject < Certification > ( content ) ;
89
+ await GenerateEmbeddings ( certificationData . CertificationName ) ;
90
+
91
+
92
+
93
+
94
+ }
95
+
96
+ private async Task GenerateEmbeddings ( string content )
97
+ {
98
+
99
+ try
100
+ {
101
+ _logger . LogInformation ( "Generating embedding..." ) ;
102
+ Embedding embedding = await _embeddingClient . GenerateEmbeddingAsync ( content ) . ConfigureAwait ( false ) ;
103
+ _logger . LogInformation ( "Embedding created successfully." ) ;
104
+ }
105
+ catch ( RequestFailedException ex )
106
+ {
107
+ _logger . LogError ( $ "Azure OpenAI API request failed: { ex . Message } ") ;
108
+ }
109
+ catch ( Exception ex )
110
+ {
111
+ _logger . LogError ( $ "Error generating embedding: { ex . Message } ") ;
112
+ }
113
+ }
114
+ private void ValidateJsonContent ( string content )
61
115
{
62
116
var generator = new JSchemaGenerator
63
117
{
@@ -80,9 +134,9 @@ private void ValidateJsonContent(string content)
80
134
{
81
135
_logger . LogInformation ( "JSON content is valid against the schema." ) ;
82
136
}
83
- }
84
-
85
-
86
-
87
- }
88
- }
137
+ }
138
+
139
+
140
+
141
+ }
142
+ }
0 commit comments