Skip to content

Commit fdb9262

Browse files
author
Benjamin Michaelis
committed
fix: Embedding retries
1 parent b1a2aa6 commit fdb9262

5 files changed

Lines changed: 19 additions & 21 deletions

File tree

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<PackageVersion Include="NuGet.Protocol" Version="6.12.5" />
5050
<PackageVersion Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.23.0" />
5151
<PackageVersion Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="10.0.2" />
52+
<PackageVersion Include="Microsoft.Extensions.AI.OpenAI" Version="10.0.1-preview.1.25571.5" />
5253
<PackageVersion Include="ModelContextProtocol" Version="0.3.0-preview.4" />
5354
<PackageVersion Include="ModelContextProtocol.AspNetCore" Version="0.3.0-preview.4" />
5455
<PackageVersion Include="Moq" Version="4.20.72" />

EssentialCSharp.Chat.Shared/EssentialCSharp.Chat.Common.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<ItemGroup>
88
<PackageReference Include="Azure.Identity" />
9+
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" />
910
<PackageReference Include="Microsoft.SemanticKernel" />
1011
<PackageReference Include="Microsoft.SemanticKernel.Connectors.PgVector" />
1112
<PackageReference Include="ModelContextProtocol" />

EssentialCSharp.Chat.Shared/Extensions/ServiceCollectionExtensions.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Azure.Core;
33
using Azure.Identity;
44
using EssentialCSharp.Chat.Common.Services;
5+
using Microsoft.Extensions.AI;
56
using Microsoft.Extensions.Configuration;
67
using Microsoft.Extensions.DependencyInjection;
78
using Microsoft.SemanticKernel;
@@ -56,10 +57,12 @@ public static IServiceCollection AddAzureOpenAIServices(
5657
// Add PostgreSQL vector store with managed identity support
5758
services.AddPostgresVectorStoreWithManagedIdentity(postgresConnectionString, credential);
5859

59-
services.AddAzureOpenAIEmbeddingGenerator(
60-
aiOptions.VectorGenerationDeploymentName,
61-
aiOptions.Endpoint,
62-
credential);
60+
services.AddEmbeddingGenerator(sp =>
61+
sp.GetRequiredService<AzureOpenAIClient>()
62+
.GetEmbeddingClient(aiOptions.VectorGenerationDeploymentName)
63+
.AsIEmbeddingGenerator())
64+
.UseLogging()
65+
.UseOpenTelemetry();
6366
#pragma warning restore SKEXP0010
6467

6568
// Register shared AI services
@@ -227,10 +230,12 @@ public static IServiceCollection AddAzureOpenAIServicesWithApiKey(
227230
// Add PostgreSQL vector store using the NpgsqlDataSource from DI
228231
services.AddPostgresVectorStore();
229232

230-
services.AddAzureOpenAIEmbeddingGenerator(
231-
aiOptions.VectorGenerationDeploymentName,
232-
aiOptions.Endpoint,
233-
apiKey);
233+
services.AddEmbeddingGenerator(sp =>
234+
sp.GetRequiredService<AzureOpenAIClient>()
235+
.GetEmbeddingClient(aiOptions.VectorGenerationDeploymentName)
236+
.AsIEmbeddingGenerator())
237+
.UseLogging()
238+
.UseOpenTelemetry();
234239
#pragma warning restore SKEXP0010
235240

236241
// Register shared AI services

EssentialCSharp.Chat.Shared/Services/EmbeddingService.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,16 @@ public async Task GenerateBookContentEmbeddingsAndUploadToVectorStore(IEnumerabl
3636
await collection.EnsureCollectionDeletedAsync(cancellationToken);
3737
await collection.EnsureCollectionExistsAsync(cancellationToken);
3838

39-
ParallelOptions parallelOptions = new()
40-
{
41-
MaxDegreeOfParallelism = 5,
42-
CancellationToken = cancellationToken
43-
};
44-
4539
int uploadedCount = 0;
4640

47-
await Parallel.ForEachAsync(bookContents, parallelOptions, async (chunk, cancellationToken) =>
41+
foreach (var chunk in bookContents)
4842
{
49-
// Generate the text embedding using the new method.
43+
cancellationToken.ThrowIfCancellationRequested();
5044
chunk.TextEmbedding = await GenerateEmbeddingAsync(chunk.ChunkText, cancellationToken);
51-
5245
await collection.UpsertAsync(chunk, cancellationToken);
5346
Console.WriteLine($"Uploaded chunk '{chunk.Id}' to collection '{collectionName}' for file '{chunk.FileName}' with heading '{chunk.Heading}'.");
54-
55-
Interlocked.Increment(ref uploadedCount);
56-
});
47+
uploadedCount++;
48+
}
5749
Console.WriteLine($"Successfully generated embeddings and uploaded {uploadedCount} chunks to collection '{collectionName}'.");
5850
}
5951
}

EssentialCSharp.Web/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Threading.RateLimiting;
2-
using Azure.Identity;
32
using EssentialCSharp.Chat.Common.Extensions;
43
using EssentialCSharp.Web.Areas.Identity.Data;
54
using EssentialCSharp.Web.Areas.Identity.Services.PasswordValidators;

0 commit comments

Comments
 (0)