|
| 1 | +using azure_project_generator.models; |
| 2 | +using Microsoft.AspNetCore.Http; |
| 3 | +using Microsoft.AspNetCore.Mvc; |
| 4 | +using Microsoft.Azure.Cosmos; |
| 5 | +using Microsoft.Azure.Functions.Worker; |
| 6 | +using Microsoft.Azure.Functions.Worker.Http; |
| 7 | +using Microsoft.Extensions.Logging; |
| 8 | +using OpenAI.Embeddings; |
| 9 | +using System.ComponentModel; |
| 10 | + |
| 11 | +namespace azure_project_generator |
| 12 | +{ |
| 13 | + public class GenerateProject |
| 14 | + { |
| 15 | + private readonly ILogger<GenerateProject> _logger; |
| 16 | + |
| 17 | + public GenerateProject(ILogger<GenerateProject> logger) |
| 18 | + { |
| 19 | + _logger = logger; |
| 20 | + } |
| 21 | + |
| 22 | + [Function("GenerateProject")] |
| 23 | + public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req, string certificationCode, |
| 24 | + |
| 25 | + [CosmosDBInput(Connection = "CosmosDBConnection")] CosmosClient client) |
| 26 | + { |
| 27 | + _logger.LogInformation("C# HTTP trigger function processed a request."); |
| 28 | + |
| 29 | + var query = new QueryDefinition("SELECT * FROM c WHERE c.certificationCode = @certificationCode") |
| 30 | + .WithParameter("@certificationCode", certificationCode); |
| 31 | + |
| 32 | + var iterator = client.GetContainer("AzureCertDB", "projectpromptvectors") |
| 33 | + .GetItemQueryIterator<CertificationProjectPromptDocument>(query); |
| 34 | + |
| 35 | + |
| 36 | + CertificationProjectPromptDocument certificationProjectPromptDocument = iterator.ReadNextAsync().Result.FirstOrDefault(); |
| 37 | + |
| 38 | + float[] projectPromptVector = certificationProjectPromptDocument.ProjectPromptVector; |
| 39 | + //"SELECT * FROM c WHERE c.certificationCode = {certificationCode}" |
| 40 | + |
| 41 | + |
| 42 | + var queryDef = new QueryDefinition( |
| 43 | + query: $"SELECT c.serviceName, VectorDistance(c.contextVector,@embedding) AS SimilarityScore FROM c ORDER BY VectorDistance(c.contextVector,@embedding)" |
| 44 | + ).WithParameter("@embedding", projectPromptVector); |
| 45 | + |
| 46 | + using FeedIterator<CertificationServiceDocument> resultSetIterator = client.GetContainer("AzureCertDB", "certvectors").GetItemQueryIterator<CertificationServiceDocument>(queryDef); |
| 47 | + |
| 48 | + while (resultSetIterator.HasMoreResults) |
| 49 | + { |
| 50 | + FeedResponse<CertificationServiceDocument> response = resultSetIterator.ReadNextAsync().Result; |
| 51 | + foreach (var item in response) |
| 52 | + { |
| 53 | + _logger.LogInformation(item.ServiceName); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return new OkObjectResult("Welcome to Azure Functions!"); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments