Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions dotnet/samples/Concepts/Embeddings/VoyageAI_TextEmbedding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft. All rights reserved.

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.VoyageAI;

namespace Embeddings;

/// <summary>
/// Example demonstrating VoyageAI text embeddings with Semantic Kernel.
/// </summary>
public class VoyageAI_TextEmbedding(ITestOutputHelper output) : BaseTest(output)
{
[Fact]
public async Task GenerateTextEmbeddingsAsync()
{
// Get API key from environment
var apiKey = Environment.GetEnvironmentVariable("VOYAGE_AI_API_KEY")
?? throw new InvalidOperationException("Please set the VOYAGE_AI_API_KEY environment variable");

// Create embedding service
var embeddingService = new VoyageAITextEmbeddingGenerationService(
modelId: "voyage-3-large",
apiKey: apiKey
);

// Generate embeddings
var texts = new List<string>
{
"Semantic Kernel is an SDK for integrating AI models",
"VoyageAI provides high-quality embeddings",
"C# is a modern, object-oriented programming language"
};

Console.WriteLine("Generating embeddings for:");
for (int i = 0; i < texts.Count; i++)
{
Console.WriteLine($"{i + 1}. {texts[i]}");
}

var embeddings = await embeddingService.GenerateEmbeddingsAsync(texts);

Console.WriteLine($"\nGenerated {embeddings.Count} embeddings");
Console.WriteLine($"Embedding dimension: {embeddings[0].Length}");
Console.WriteLine($"First embedding (first 5 values): [{string.Join(", ", embeddings[0].Span[..5].ToArray())}]");
}
}
57 changes: 57 additions & 0 deletions dotnet/samples/Concepts/Reranking/VoyageAI_Reranking.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) Microsoft. All rights reserved.

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.VoyageAI;

namespace Reranking;

/// <summary>
/// Example demonstrating VoyageAI reranking with Semantic Kernel.
/// </summary>
public class VoyageAI_Reranking(ITestOutputHelper output) : BaseTest(output)
{
[Fact]
public async Task RerankDocumentsAsync()
{
// Get API key from environment
var apiKey = Environment.GetEnvironmentVariable("VOYAGE_AI_API_KEY")
?? throw new InvalidOperationException("Please set the VOYAGE_AI_API_KEY environment variable");

// Create reranking service
var rerankingService = new VoyageAITextRerankingService(
modelId: "rerank-2.5",
apiKey: apiKey
);

// Define query and documents
var query = "What are the key features of Semantic Kernel?";

var documents = new List<string>
{
"Semantic Kernel is an open-source SDK that lets you easily build agents that can call your existing code.",
"The capital of France is Paris, a beautiful city known for its art and culture.",
"Python is a high-level, interpreted programming language with dynamic typing.",
"Semantic Kernel provides enterprise-ready AI orchestration with model flexibility and plugin ecosystem.",
"Machine learning models require large amounts of data for training."
};

Console.WriteLine($"Query: {query}\n");
Console.WriteLine("Documents to rerank:");
for (int i = 0; i < documents.Count; i++)
{
Console.WriteLine($"{i + 1}. {documents[i]}");
}

// Rerank documents
var results = await rerankingService.RerankAsync(query, documents);

Console.WriteLine("\nReranked results (sorted by relevance):");
for (int i = 0; i < results.Count; i++)
{
var result = results[i];
Console.WriteLine($"\n{i + 1}. Score: {result.RelevanceScore:F4}");
Console.WriteLine($" Original Index: {result.Index}");
Console.WriteLine($" Text: {result.Text}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<RootNamespace>Microsoft.SemanticKernel.Connectors.VoyageAI.UnitTests</RootNamespace>
<AssemblyName>Microsoft.SemanticKernel.Connectors.VoyageAI.UnitTests</AssemblyName>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<Nullable>enable</Nullable>
<NoWarn>$(NoWarn);SKEXP0001</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="Moq" />
<PackageReference Include="FluentAssertions" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Connectors.VoyageAI\Connectors.VoyageAI.csproj" />
</ItemGroup>

</Project>
Loading
Loading