Skip to content

.Net: Additional unit tests #6271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public async Task ValidateGenerateEmbeddingsAsync()
var client = new MistralClient("mistral-tiny", this.HttpClient, "key");

// Act
List<string> data = new() { "Hello", "world" };
List<string> data = ["Hello", "world"];
var response = await client.GenerateEmbeddingsAsync(data, default);

// Assert
Expand Down Expand Up @@ -424,13 +424,76 @@ public async Task ValidateGetChatMessageContentsWithAutoFunctionInvocationFilter
Assert.Contains("GetWeather", invokedFunctions);
}

[Theory]
[InlineData("system", "System Content")]
[InlineData("user", "User Content")]
[InlineData("assistant", "Assistant Content")]
public void ValidateToMistralChatMessages(string roleLabel, string content)
{
// Arrange
using var httpClient = new HttpClient();
var client = new MistralClient("mistral-large-latest", httpClient, "key");
var chatMessage = new ChatMessageContent()
{
Role = new AuthorRole(roleLabel),
Content = content,
};

// Act
var messages = client.ToMistralChatMessages(chatMessage, default);

// Assert
Assert.NotNull(messages);
Assert.Single(messages);
}

[Fact]
public void ValidateToMistralChatMessagesWithFunctionCallContent()
{
// Arrange
using var httpClient = new HttpClient();
var client = new MistralClient("mistral-large-latest", httpClient, "key");
var content = new ChatMessageContent()
{
Role = AuthorRole.Assistant,
Items = [new FunctionCallContent("GetWeather"), new FunctionCallContent("GetCurrentTime")],
};

// Act
var messages = client.ToMistralChatMessages(content, default);

// Assert
Assert.NotNull(messages);
Assert.Single(messages);
}

[Fact]
public void ValidateToMistralChatMessagesWithFunctionResultContent()
{
// Arrange
using var httpClient = new HttpClient();
var client = new MistralClient("mistral-large-latest", httpClient, "key");
var content = new ChatMessageContent()
{
Role = AuthorRole.Tool,
Items = [new FunctionResultContent("12°C\nWind: 11 KMPH\nHumidity: 48%\nMostly cloudy"), new FunctionResultContent("15:20:44")],
};

// Act
var messages = client.ToMistralChatMessages(content, default);

// Assert
Assert.NotNull(messages);
Assert.Equal(2, messages.Count);
}

public sealed class WeatherPlugin
{
[KernelFunction]
[Description("Get the current weather in a given location.")]
public string GetWeather(
[Description("The city and department, e.g. Marseille, 13")] string location
) => "12°C\nWind: 11 KMPH\nHumidity: 48%\nMostly cloudy";
) => "12°C\nWind: 11 KMPH\nHumidity: 48%\nMostly cloudy";
}

internal enum TemperatureUnit { Celsius, Fahrenheit }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) Microsoft. All rights reserved.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.MistralAI;
using Microsoft.SemanticKernel.Embeddings;
using Xunit;

namespace SemanticKernel.Connectors.MistralAI.UnitTests;

/// <summary>
/// Unit tests for <see cref="MistralAIServiceCollectionExtensions"/> and <see cref="MistralAIKernelBuilderExtensions"/>.
/// </summary>
public class MistralAIExtensionTests
{
[Fact]
public void AddMistralChatCompletionToServiceCollection()
{
// Arrange
var collection = new ServiceCollection();
collection.AddMistralChatCompletion("model", "apiKey");

// Act
var kernelBuilder = collection.AddKernel();
var kernel = collection.BuildServiceProvider().GetRequiredService<Kernel>();
var service = kernel.GetRequiredService<IChatCompletionService>();

// Assert
Assert.NotNull(service);
Assert.IsType<MistralAIChatCompletionService>(service);
}

[Fact]
public void AddMistralTextEmbeddingGenerationToServiceCollection()
{
// Arrange
var collection = new ServiceCollection();
collection.AddMistralTextEmbeddingGeneration("model", "apiKey");

// Act
var kernelBuilder = collection.AddKernel();
var kernel = collection.BuildServiceProvider().GetRequiredService<Kernel>();
var service = kernel.GetRequiredService<ITextEmbeddingGenerationService>();

// Assert
Assert.NotNull(service);
Assert.IsType<MistralAITextEmbeddingGenerationService>(service);
}

[Fact]
public void AddMistralChatCompletionToKernelBuilder()
{
// Arrange
var collection = new ServiceCollection();
var kernelBuilder = collection.AddKernel();
kernelBuilder.AddMistralChatCompletion("model", "apiKey");

// Act
var kernel = collection.BuildServiceProvider().GetRequiredService<Kernel>();
var service = kernel.GetRequiredService<IChatCompletionService>();

// Assert
Assert.NotNull(service);
Assert.IsType<MistralAIChatCompletionService>(service);
}

[Fact]
public void AddMistralTextEmbeddingGenerationToKernelBuilder()
{
// Arrange
var collection = new ServiceCollection();
var kernelBuilder = collection.AddKernel();
kernelBuilder.AddMistralTextEmbeddingGeneration("model", "apiKey");

// Act
var kernel = collection.BuildServiceProvider().GetRequiredService<Kernel>();
var service = kernel.GetRequiredService<ITextEmbeddingGenerationService>();

// Assert
Assert.NotNull(service);
Assert.IsType<MistralAITextEmbeddingGenerationService>(service);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace SemanticKernel.Connectors.MistralAI.UnitTests;
public class MistralAIPromptExecutionSettingsTests
{
[Fact]
public void FromExecutionSettingsWhenAlreadyMistralShouldReturnSameAsync()
public void FromExecutionSettingsWhenAlreadyMistralShouldReturnSame()
{
// Arrange
var executionSettings = new MistralAIPromptExecutionSettings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ private ChatCompletionRequest CreateChatCompletionRequest(string modelId, bool s
return request;
}

private List<MistralChatMessage> ToMistralChatMessages(ChatMessageContent content, MistralAIToolCallBehavior? toolCallBehavior)
internal List<MistralChatMessage> ToMistralChatMessages(ChatMessageContent content, MistralAIToolCallBehavior? toolCallBehavior)
{
if (content.Role == AuthorRole.Assistant)
{
Expand Down

This file was deleted.

Loading