-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
HuggingFace_EmbeddingGeneration.cs
33 lines (25 loc) · 1.3 KB
/
HuggingFace_EmbeddingGeneration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Embeddings;
using xRetry;
#pragma warning disable format // Format item can be simplified
#pragma warning disable CA1861 // Avoid constant arrays as arguments
namespace Memory;
// The following example shows how to use Semantic Kernel with HuggingFace API.
public class HuggingFace_EmbeddingGeneration(ITestOutputHelper output) : BaseTest(output)
{
[RetryFact(typeof(HttpOperationException))]
public async Task RunInferenceApiEmbeddingAsync()
{
Console.WriteLine("\n======= Hugging Face Inference API - Embedding Example ========\n");
Kernel kernel = Kernel.CreateBuilder()
.AddHuggingFaceTextEmbeddingGeneration(
model: TestConfiguration.HuggingFace.EmbeddingModelId,
apiKey: TestConfiguration.HuggingFace.ApiKey)
.Build();
var embeddingGenerator = kernel.GetRequiredService<ITextEmbeddingGenerationService>();
// Generate embeddings for each chunk.
var embeddings = await embeddingGenerator.GenerateEmbeddingsAsync(["John: Hello, how are you?\nRoger: Hey, I'm Roger!"]);
Console.WriteLine($"Generated {embeddings.Count} embeddings for the provided text");
}
}