Skip to content

Commit

Permalink
Merge pull request #21 from learntocloud/removeafterinset
Browse files Browse the repository at this point in the history
Integrate Azure Blob Storage and clean up unused usings
  • Loading branch information
madebygps authored Sep 13, 2024
2 parents e7f8eba + bfd0bac commit 21c5300
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 33 deletions.
67 changes: 49 additions & 18 deletions azure-project-generator/ProcessCertServiceFile.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using Azure;
using Azure.Storage.Blobs;
using azure_project_generator.models;
using azure_project_generator.services;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using OpenAI.Embeddings;



namespace azure_project_generator
{
public class ProcessCertServiceFile
Expand All @@ -13,16 +17,18 @@ public class ProcessCertServiceFile
private readonly EmbeddingClient _embeddingClient;
private readonly JsonValidationService _jsonValidationService;
private readonly ContentGenerationService _contentGenerationService;
private readonly BlobServiceClient _blobServiceClient;

public ProcessCertServiceFile(ILogger<ProcessCertServiceFile> logger,
EmbeddingClient embeddingClient,
JsonValidationService jsonValidationService,
ContentGenerationService contentGenerationService)
ContentGenerationService contentGenerationService, BlobServiceClient blobServiceClient)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_embeddingClient = embeddingClient ?? throw new ArgumentNullException(nameof(embeddingClient));
_jsonValidationService = jsonValidationService ?? throw new ArgumentNullException(nameof(jsonValidationService));
_contentGenerationService = contentGenerationService ?? throw new ArgumentNullException(nameof(contentGenerationService));
_blobServiceClient = blobServiceClient ?? throw new ArgumentNullException(nameof(blobServiceClient));
}

[Function(nameof(ProcessCertServiceFile))]
Expand All @@ -35,39 +41,64 @@ public async Task<CertificationServiceOutput> Run(
if (string.IsNullOrWhiteSpace(content))
{
_logger.LogError("Blob content is empty or whitespace.");
return new CertificationServiceOutput { Document = null};
return new CertificationServiceOutput { Document = null };
}

if (!_jsonValidationService.ValidateJsonContent<CertificationService>(content))
{
_logger.LogError("Invalid JSON content.");
return new CertificationServiceOutput { Document = null };
}

var mappedServiceData = JsonConvert.DeserializeObject<CertificationService>(content);
if (mappedServiceData == null)
try
{
_logger.LogError("Failed to deserialize content to MappedService.");
return new CertificationServiceOutput { Document = null };
}
var mappedServiceData = JsonConvert.DeserializeObject<CertificationService>(content);
if (mappedServiceData == null)
{
_logger.LogError("Failed to deserialize content to CertificationService.");
return new CertificationServiceOutput { Document = null };
}

string contextSentence = _contentGenerationService.GenerateCertServiceContextSentence(mappedServiceData);
float[] contentVector = await _contentGenerationService.GenerateEmbeddingsAsync(contextSentence);
string contextSentence = _contentGenerationService.GenerateCertServiceContextSentence(mappedServiceData);
float[] contentVector = await _contentGenerationService.GenerateEmbeddingsAsync(contextSentence);

var certServiceDocument = CreateCertServiceDocument(mappedServiceData, contextSentence, contentVector);
var certServiceDocument = CreateCertServiceDocument(mappedServiceData, contextSentence, contentVector);

_logger.LogInformation("Document created successfully.");
_logger.LogInformation($"Archiving blob: {name}");
_logger.LogInformation("Document created successfully.");
_logger.LogInformation($"Deleting blob: {name}");

// delete the blob
BlobContainerClient blobContainerClient = _blobServiceClient.GetBlobContainerClient("certservice");
BlobClient blobClient = blobContainerClient.GetBlobClient(name);
await blobClient.DeleteAsync();

return new CertificationServiceOutput
return new CertificationServiceOutput
{
Document = certServiceDocument,
};
}
catch (JsonException jsonEx)
{
_logger.LogError($"JSON error occurred: {jsonEx.Message}");
return new CertificationServiceOutput { Document = null };
}
catch (HttpRequestException httpEx)
{
Document = certServiceDocument,

};
_logger.LogError($"HTTP request error occurred: {httpEx.Message}");
return new CertificationServiceOutput { Document = null };
}
catch (RequestFailedException storageEx)
{
_logger.LogError($"Azure Storage error occurred: {storageEx.Message}");
return new CertificationServiceOutput { Document = null };
}
catch (Exception ex)
{
_logger.LogError($"An unexpected error occurred: {ex.Message}");
return new CertificationServiceOutput { Document = null };
}
}

// Other methods remain unchanged

private CertificationServiceDocument CreateCertServiceDocument(CertificationService data, string contextSentence, float[] contentVector) =>
new CertificationServiceDocument
{
Expand Down
1 change: 1 addition & 0 deletions azure-project-generator/azure-project-generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="2.0.0-beta.3" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.21.2" />
<PackageReference Include="Azure.Storage.Common" Version="12.20.1" />
<PackageReference Include="Azure.Storage.Files.Shares" Version="12.19.1" />
<PackageReference Include="Azure.Storage.Queues" Version="12.19.1" />
<PackageReference Include="JsonSchema.Net" Version="7.2.2" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using Microsoft.Azure.Functions.Worker;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace azure_project_generator.models
{
Expand Down
5 changes: 0 additions & 5 deletions azure-project-generator/models/CertificationServiceOutput.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using Microsoft.Azure.Functions.Worker;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace azure_project_generator.models
{
Expand Down
5 changes: 0 additions & 5 deletions azure-project-generator/models/CloudProjectIdea.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace azure_project_generator.models
{
Expand Down

0 comments on commit 21c5300

Please sign in to comment.