Files
microsoft--semantic-kernel/dotnet/samples/Concepts/Memory/Ollama_EmbeddingGeneration.cs
T
wehub-resource-sync b957a53def
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:21:23 +08:00

33 lines
1.2 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Extensions.AI;
using Microsoft.SemanticKernel;
using xRetry;
namespace Memory;
// The following example shows how to use Semantic Kernel with Ollama API.
public class Ollama_EmbeddingGeneration(ITestOutputHelper output) : BaseTest(output)
{
[RetryFact(typeof(HttpOperationException))]
public async Task RunEmbeddingAsync()
{
Assert.NotNull(TestConfiguration.Ollama.EmbeddingModelId);
Console.WriteLine("\n======= Ollama - Embedding Example ========\n");
Kernel kernel = Kernel.CreateBuilder()
.AddOllamaEmbeddingGenerator(
endpoint: new Uri(TestConfiguration.Ollama.Endpoint),
modelId: TestConfiguration.Ollama.EmbeddingModelId)
.Build();
var embeddingGenerator = kernel.GetRequiredService<IEmbeddingGenerator<string, Embedding<float>>>();
// Generate embeddings for each chunk.
var embeddings = await embeddingGenerator.GenerateAsync(["John: Hello, how are you?\nRoger: Hey, I'm Roger!"]);
Console.WriteLine($"Generated {embeddings.Count} embeddings for the provided text");
}
}