Skip to content

Commit cf4ba77

Browse files
committed
Refactor and update Cosmos DB integration
Removed Certification.cs and SkillsMeasured.cs files. Updated ProcessFile.cs: - Removed EmbeddingClient initialization. - Modified Run method to return CertServiceDocument. - Changed GenerateEmbeddings to return List<float>. - Reformatted ValidateJsonContent method. Added CertServiceDocument class for Cosmos DB structure. Updated azure-project-generator.csproj with CosmosDB package.
1 parent 6a3086d commit cf4ba77

File tree

5 files changed

+63
-54
lines changed

5 files changed

+63
-54
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Text.Json.Serialization;
6+
using System.Threading.Tasks;
7+
8+
namespace azure_project_generator
9+
{
10+
public class CertServiceDocument
11+
{
12+
[JsonPropertyName("id")]
13+
public string id { get; set; } // Unique identifier for the document
14+
15+
[JsonPropertyName("certificationServiceKey")]
16+
public string CertificationServiceKey { get; set; } // Composite key
17+
[JsonPropertyName("certificationCode")]
18+
public string CertificationCode { get; set; } // The certification code
19+
[JsonPropertyName("certificationName")]
20+
public string CertificationName { get; set; } // The certification name
21+
[JsonPropertyName("skillName")]
22+
public string SkillName { get; set; } // The skill associated with this certification
23+
[JsonPropertyName("topicName")]
24+
public string TopicName { get; set; } // The topic within the skill
25+
[JsonPropertyName("serviceName")]
26+
public string ServiceName { get; set; } // The service relevant to this certification and skill
27+
[JsonPropertyName("contextSentence")]
28+
public string ContextSentence { get; set; } // The combined sentence
29+
[JsonPropertyName("contextVector")]
30+
public float[] ContextVector { get; set; } // Example vector embedding generated from the sentence
31+
32+
}
33+
}

azure-project-generator/Certification.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

azure-project-generator/ProcessFile.cs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ namespace azure_project_generator
1313
public class ProcessFile
1414
{
1515
private readonly ILogger<ProcessFile> _logger;
16-
1716
private readonly EmbeddingClient _embeddingClient;
18-
19-
2017
public ProcessFile(ILogger<ProcessFile> logger)
2118
{
2219
_logger = logger;
@@ -40,9 +37,10 @@ public ProcessFile(ILogger<ProcessFile> logger)
4037

4138
}
4239

43-
4440
[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)
4644
{
4745
string content;
4846
try
@@ -53,21 +51,20 @@ public async Task Run([BlobTrigger("certdata/{name}", Connection = "AzureWebJobs
5351
catch (IOException ex)
5452
{
5553
_logger.LogError($"Error reading blob content: {ex.Message}");
56-
return;
54+
return null;
5755
}
5856

5957
_logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {name}");
6058

6159
if (string.IsNullOrWhiteSpace(content))
6260
{
6361
_logger.LogError("Blob content is empty or whitespace.");
64-
return;
62+
return null;
6563
}
6664

6765
try
6866
{
6967
ValidateJsonContent(content);
70-
7168
}
7269
catch (JsonReaderException ex)
7370
{
@@ -79,37 +76,49 @@ public async Task Run([BlobTrigger("certdata/{name}", Connection = "AzureWebJobs
7976
}
8077

8178
var mappedServiceData = JsonConvert.DeserializeObject<MappedService>(content);
82-
83-
string contextSentence =
79+
80+
string contextSentence =
8481
$"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}.";
8582

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();
8894

89-
}
95+
_logger.LogInformation("Document created successfully.");
96+
97+
return certServiceDocument;
9098

91-
private async Task GenerateEmbeddings(string content)
99+
}
100+
private async Task<List<float>> GenerateEmbeddings(string content)
92101
{
93-
94102
try
95103
{
96104
_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();
98107
_logger.LogInformation("Embedding created successfully.");
99-
// save to cosmos
100-
101-
108+
return embeddingVector;
102109
}
103110
catch (RequestFailedException ex)
104111
{
105112
_logger.LogError($"Azure OpenAI API request failed: {ex.Message}");
113+
throw; // Re-throw the exception to ensure the caller is aware of the failure
106114
}
107115
catch (Exception ex)
108116
{
109117
_logger.LogError($"Error generating embedding: {ex.Message}");
118+
throw; // Re-throw the exception to ensure the caller is aware of the failure
110119
}
111120
}
112-
private void ValidateJsonContent(string content)
121+
private void ValidateJsonContent(string content)
113122
{
114123
var generator = new JSchemaGenerator();
115124
JSchema schema = generator.Generate(typeof(MappedService));

azure-project-generator/SkillsMeasured.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

azure-project-generator/azure-project-generator.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<PackageReference Include="Azure.Storage.Files.Shares" Version="12.19.1" />
1818
<PackageReference Include="Azure.Storage.Queues" Version="12.19.1" />
1919
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.23.0" />
20+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.CosmosDB" Version="4.10.0" />
2021
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
2122
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs" Version="6.6.0" />
2223
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.4" />

0 commit comments

Comments
 (0)