@@ -13,10 +13,7 @@ namespace azure_project_generator
13
13
public class ProcessFile
14
14
{
15
15
private readonly ILogger < ProcessFile > _logger ;
16
-
17
16
private readonly EmbeddingClient _embeddingClient ;
18
-
19
-
20
17
public ProcessFile ( ILogger < ProcessFile > logger )
21
18
{
22
19
_logger = logger ;
@@ -40,9 +37,10 @@ public ProcessFile(ILogger<ProcessFile> logger)
40
37
41
38
}
42
39
43
-
44
40
[ Function ( nameof ( ProcessFile ) ) ]
45
- public async Task Run ( [ BlobTrigger ( "certdata/{name}" , Connection = "AzureWebJobsStorage" ) ] Stream stream , string name )
41
+ [ CosmosDBOutput ( "%CosmosDb%" , "%CosmosContainerOut%" , Connection = "CosmosDBConnection" ) ]
42
+ public async Task < CertServiceDocument > Run (
43
+ [ BlobTrigger ( "certdata/{name}" , Connection = "AzureWebJobsStorage" ) ] Stream stream , string name )
46
44
{
47
45
string content ;
48
46
try
@@ -53,21 +51,20 @@ public async Task Run([BlobTrigger("certdata/{name}", Connection = "AzureWebJobs
53
51
catch ( IOException ex )
54
52
{
55
53
_logger . LogError ( $ "Error reading blob content: { ex . Message } ") ;
56
- return ;
54
+ return null ;
57
55
}
58
56
59
57
_logger . LogInformation ( $ "C# Blob trigger function Processed blob\n Name: { name } ") ;
60
58
61
59
if ( string . IsNullOrWhiteSpace ( content ) )
62
60
{
63
61
_logger . LogError ( "Blob content is empty or whitespace." ) ;
64
- return ;
62
+ return null ;
65
63
}
66
64
67
65
try
68
66
{
69
67
ValidateJsonContent ( content ) ;
70
-
71
68
}
72
69
catch ( JsonReaderException ex )
73
70
{
@@ -79,37 +76,49 @@ public async Task Run([BlobTrigger("certdata/{name}", Connection = "AzureWebJobs
79
76
}
80
77
81
78
var mappedServiceData = JsonConvert . DeserializeObject < MappedService > ( content ) ;
82
-
83
- string contextSentence =
79
+
80
+ string contextSentence =
84
81
$ "The { mappedServiceData . CertificationCode } { mappedServiceData . CertificationName } certification includes the skill of { mappedServiceData . SkillName } . Within this skill, there is a focus on the topic of { mappedServiceData . TopicName } , particularly through the use of the service { mappedServiceData . ServiceName } .";
85
82
86
- await GenerateEmbeddings ( contextSentence ) ;
87
-
83
+ List < float > contentVector = await GenerateEmbeddings ( contextSentence ) ;
84
+ CertServiceDocument certServiceDocument = new CertServiceDocument ( ) ;
85
+ certServiceDocument . id = Guid . NewGuid ( ) . ToString ( ) ;
86
+ certServiceDocument . CertificationServiceKey = $ "{ mappedServiceData . CertificationCode } -{ mappedServiceData . ServiceName } ";
87
+ certServiceDocument . CertificationCode = mappedServiceData . CertificationCode ;
88
+ certServiceDocument . CertificationName = mappedServiceData . CertificationName ;
89
+ certServiceDocument . SkillName = mappedServiceData . SkillName ;
90
+ certServiceDocument . TopicName = mappedServiceData . TopicName ;
91
+ certServiceDocument . ServiceName = mappedServiceData . ServiceName ;
92
+ certServiceDocument . ContextSentence = contextSentence ;
93
+ certServiceDocument . ContextVector = contentVector . ToArray ( ) ;
88
94
89
- }
95
+ _logger . LogInformation ( "Document created successfully." ) ;
96
+
97
+ return certServiceDocument ;
90
98
91
- private async Task GenerateEmbeddings ( string content )
99
+ }
100
+ private async Task < List < float > > GenerateEmbeddings ( string content )
92
101
{
93
-
94
102
try
95
103
{
96
104
_logger . LogInformation ( "Generating embedding..." ) ;
97
- Embedding embedding = await _embeddingClient . GenerateEmbeddingAsync ( content ) . ConfigureAwait ( false ) ;
105
+ var embeddingResult = await _embeddingClient . GenerateEmbeddingAsync ( content ) . ConfigureAwait ( false ) ;
106
+ List < float > embeddingVector = embeddingResult . Value . Vector . ToArray ( ) . ToList ( ) ;
98
107
_logger . LogInformation ( "Embedding created successfully." ) ;
99
- // save to cosmos
100
-
101
-
108
+ return embeddingVector ;
102
109
}
103
110
catch ( RequestFailedException ex )
104
111
{
105
112
_logger . LogError ( $ "Azure OpenAI API request failed: { ex . Message } ") ;
113
+ throw ; // Re-throw the exception to ensure the caller is aware of the failure
106
114
}
107
115
catch ( Exception ex )
108
116
{
109
117
_logger . LogError ( $ "Error generating embedding: { ex . Message } ") ;
118
+ throw ; // Re-throw the exception to ensure the caller is aware of the failure
110
119
}
111
120
}
112
- private void ValidateJsonContent ( string content )
121
+ private void ValidateJsonContent ( string content )
113
122
{
114
123
var generator = new JSchemaGenerator ( ) ;
115
124
JSchema schema = generator . Generate ( typeof ( MappedService ) ) ;
0 commit comments