Skip to content

Commit ce29264

Browse files
update
1 parent d2bba38 commit ce29264

File tree

8 files changed

+31
-27
lines changed

8 files changed

+31
-27
lines changed

app/backend/Extensions/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal static IServiceCollection AddAzureServices(this IServiceCollection serv
2727
return sp.GetRequiredService<BlobServiceClient>().GetBlobContainerClient(azureStorageContainer);
2828
});
2929

30-
services.AddSingleton<IDocumentSearchService, AzureDocumentSearchService>(sp =>
30+
services.AddSingleton<IDocumentService, AzureDocumentService>(sp =>
3131
{
3232
var config = sp.GetRequiredService<IConfiguration>();
3333
var azureSearchServiceEndpoint = config["AzureSearchServiceEndpoint"];
@@ -39,7 +39,7 @@ internal static IServiceCollection AddAzureServices(this IServiceCollection serv
3939
var searchClient = new SearchClient(
4040
new Uri(azureSearchServiceEndpoint), azureSearchIndex, s_azureCredential);
4141

42-
return new AzureDocumentSearchService(searchClient);
42+
return new AzureDocumentService(searchClient);
4343
});
4444

4545
services.AddSingleton<DocumentAnalysisClient>(sp =>

app/backend/Services/ReadRetrieveReadChatService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ namespace MinimalApi.Services;
44

55
public class ReadRetrieveReadChatService
66
{
7-
private readonly IDocumentSearchService _searchClient;
7+
private readonly IDocumentService _searchClient;
88
private readonly IKernel _kernel;
99
private readonly IConfiguration _configuration;
1010

1111
public ReadRetrieveReadChatService(
12-
IDocumentSearchService searchClient,
12+
IDocumentService searchClient,
1313
OpenAIClient client,
1414
IConfiguration configuration)
1515
{

app/backend/Services/AzureComputerVisionService.cs renamed to app/shared/Shared/Services/AzureComputerVisionService.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
using System.Net.Http.Headers;
44
using System.Text;
5+
using System.Text.Json;
56
using Azure.Core;
67

7-
namespace MinimalApi.Services;
8-
9-
public class AzureComputerVisionService(IHttpClientFactory httpClientFactory, string endPoint, TokenCredential tokenCredential)
8+
public class AzureComputerVisionService(HttpClient client, string endPoint, TokenCredential tokenCredential)
109
{
1110
// add virtual keyword to make it mockable
12-
public virtual async Task<ImageEmbeddingResponse> VectorizeImageAsync(string imagePathOrUrl, CancellationToken ct = default)
11+
public async Task<ImageEmbeddingResponse> VectorizeImageAsync(string imagePathOrUrl, CancellationToken ct = default)
1312
{
1413
var api = $"{endPoint}/computervision/retrieval:vectorizeImage?api-version=2023-02-01-preview&modelVersion=latest";
1514
var token = await tokenCredential.GetTokenAsync(new TokenRequestContext(new[] { "https://cognitiveservices.azure.com/.default" }), ct);
@@ -27,7 +26,6 @@ public virtual async Task<ImageEmbeddingResponse> VectorizeImageAsync(string ima
2726
request.Content.Headers.ContentType = new MediaTypeHeaderValue("image/*");
2827

2928
// send request
30-
using var client = httpClientFactory.CreateClient();
3129
using var response = await client.SendAsync(request, ct);
3230
response.EnsureSuccessStatusCode();
3331

@@ -53,7 +51,6 @@ public virtual async Task<ImageEmbeddingResponse> VectorizeImageAsync(string ima
5351
request.Content = new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, "application/json");
5452

5553
// send request
56-
using var client = httpClientFactory.CreateClient();
5754
using var response = await client.SendAsync(request, ct);
5855
response.EnsureSuccessStatusCode();
5956

app/backend/Services/AzureDocumentSearchService.cs renamed to app/shared/Shared/Services/AzureDocumentService.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using Azure;
22
using Azure.Search.Documents;
3+
using Azure.Search.Documents.Models;
4+
using Shared.Models;
35

4-
namespace MinimalApi.Services;
5-
6-
public interface IDocumentSearchService
6+
public interface IDocumentService
77
{
88
Task<SupportingContentRecord[]> QueryDocumentsAsync(
99
string? query = null,
@@ -12,10 +12,9 @@ Task<SupportingContentRecord[]> QueryDocumentsAsync(
1212
CancellationToken cancellationToken = default);
1313
}
1414

15-
public class AzureDocumentSearchService(SearchClient searchClient) : IDocumentSearchService
15+
public class AzureDocumentService(SearchClient searchClient) : IDocumentService
1616
{
17-
// prepare for mock out
18-
public virtual async Task<SupportingContentRecord[]> QueryDocumentsAsync(
17+
public async Task<SupportingContentRecord[]> QueryDocumentsAsync(
1918
string? query = null,
2019
float[]? embedding = null,
2120
RequestOverrides? overrides = null,

app/shared/Shared/Shared.csproj

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
@@ -7,4 +7,15 @@
77
<LangVersion>preview</LangVersion>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<PackageReference Include="Azure.AI.FormRecognizer" />
12+
<PackageReference Include="Azure.AI.OpenAI" />
13+
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" />
14+
<PackageReference Include="Azure.Identity" />
15+
<PackageReference Include="Azure.Search.Documents" />
16+
<PackageReference Include="Azure.Storage.Blobs" />
17+
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" />
18+
<PackageReference Include="PdfSharpCore" />
19+
</ItemGroup>
20+
1021
</Project>

app/tests/MinimalApi.Tests/AzureComputerVisionServiceTest.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ public class AzureComputerVisionServiceTest
1414
public async Task VectorizeImageTestAsync()
1515
{
1616
var endpoint = Environment.GetEnvironmentVariable("AZURE_COMPUTER_VISION_ENDPOINT") ?? throw new InvalidOperationException();
17-
18-
var httpClientFactory = Substitute.For<IHttpClientFactory>();
19-
httpClientFactory.CreateClient().ReturnsForAnyArgs(x => new HttpClient());
17+
using var httpClient = new HttpClient();
2018
var imageUrl = @"https://learn.microsoft.com/azure/ai-services/computer-vision/media/quickstarts/presentation.png";
2119

22-
var service = new AzureComputerVisionService(httpClientFactory, endpoint, new DefaultAzureCredential());
20+
var service = new AzureComputerVisionService(httpClient, endpoint, new DefaultAzureCredential());
2321
var result = await service.VectorizeImageAsync(imageUrl);
2422

2523
result.modelVersion.Should().NotBeNullOrEmpty();
@@ -51,9 +49,8 @@ public async Task VectorizeImageTestAsync()
5149
public async Task VectorizeTextTestAsync()
5250
{
5351
var endpoint = Environment.GetEnvironmentVariable("AZURE_COMPUTER_VISION_ENDPOINT") ?? throw new InvalidOperationException();
54-
var httpClientFactory = Substitute.For<IHttpClientFactory>();
55-
httpClientFactory.CreateClient().ReturnsForAnyArgs(x => new HttpClient());
56-
var service = new AzureComputerVisionService(httpClientFactory, endpoint, new DefaultAzureCredential());
52+
using var httpClient = new HttpClient();
53+
var service = new AzureComputerVisionService(httpClient, endpoint, new DefaultAzureCredential());
5754
var text = "Hello world";
5855
var result = await service.VectorizeTextAsync(text);
5956

app/tests/MinimalApi.Tests/AzureDocumentSearchServiceTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public async Task QueryDocumentsTestTextOnlyAsync()
2121
var index = Environment.GetEnvironmentVariable("AZURE_SEARCH_INDEX") ?? throw new InvalidOperationException();
2222
var endpoint = Environment.GetEnvironmentVariable("AZURE_SEARCH_SERVICE_ENDPOINT") ?? throw new InvalidOperationException();
2323
var searchClient = new SearchClient(new Uri(endpoint), index, new DefaultAzureCredential());
24-
var service = new AzureDocumentSearchService(searchClient);
24+
var service = new AzureDocumentService(searchClient);
2525

2626
// query only
2727
var option = new RequestOverrides
@@ -49,7 +49,7 @@ public async Task QueryDocumentsTestEmbeddingOnlyAsync()
4949
var embeddingResponse = await openAIClient.GetEmbeddingsAsync(openAiEmbeddingDeployment, new EmbeddingsOptions(query));
5050
var embedding = embeddingResponse.Value.Data.First().Embedding;
5151
var searchClient = new SearchClient(new Uri(searchServceEndpoint), index, new DefaultAzureCredential());
52-
var service = new AzureDocumentSearchService(searchClient);
52+
var service = new AzureDocumentService(searchClient);
5353

5454
// query only
5555
var option = new RequestOverrides

app/tests/MinimalApi.Tests/ReadRetrieveReadChatServiceTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class ReadRetrieveReadChatServiceTest
2121
[EnvironmentVariablesFact("AZURE_OPENAI_ENDPOINT", "AZURE_OPENAI_EMBEDDING_DEPLOYMENT", "AZURE_OPENAI_CHATGPT_DEPLOYMENT")]
2222
public async Task NorthwindHealthQuestionTest_TextOnlyAsync()
2323
{
24-
var documentSearchService = Substitute.For<IDocumentSearchService>();
24+
var documentSearchService = Substitute.For<IDocumentService>();
2525
documentSearchService.QueryDocumentsAsync(Arg.Any<string?>(), Arg.Any<float[]?>(), Arg.Any<RequestOverrides?>(), Arg.Any<CancellationToken>())
2626
.Returns(new SupportingContentRecord[]
2727
{

0 commit comments

Comments
 (0)