Skip to content

Commit

Permalink
Merge pull request #180 from jmatthiesen/main
Browse files Browse the repository at this point in the history
Added support for structured output when using Microsoft.Extensions.AI
  • Loading branch information
awaescher authored Jan 31, 2025
2 parents cc180cc + 2ebdfe2 commit 8c3b956
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/MicrosoftAi/AbstractionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ internal static class AbstractionMapper
/// <returns>A <see cref="ChatRequest"/> object containing the converted data.</returns>
public static ChatRequest ToOllamaSharpChatRequest(IList<ChatMessage> chatMessages, ChatOptions? options, bool stream, JsonSerializerOptions serializerOptions)
{
object? format = null;

if (options?.ResponseFormat is ChatResponseFormatJson jsonFormat)
format = jsonFormat.Schema.HasValue ? jsonFormat.Schema.Value : Application.Json;

var request = new ChatRequest
{
Format = Equals(options?.ResponseFormat, ChatResponseFormat.Json) ? Application.Json : null,
Format = format,
KeepAlive = null,
Messages = ToOllamaSharpMessages(chatMessages, serializerOptions),
Model = options?.ModelId ?? string.Empty, // will be set OllamaApiClient.SelectedModel if not set
Expand Down
37 changes: 37 additions & 0 deletions test/AbstractionMapperTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Text.Json;
using System.Text.Json.Schema;
using FluentAssertions;
using Microsoft.Extensions.AI;
using NUnit.Framework;
Expand Down Expand Up @@ -565,6 +567,41 @@ public void Maps_Options()
chatRequest.Options.VocabOnly.Should().BeNull();
}

[Test]
public void Maps_JsonWithoutSchema()
{
var chatMessages = new List<Microsoft.Extensions.AI.ChatMessage>();

var options = new Microsoft.Extensions.AI.ChatOptions
{
ResponseFormat = ChatResponseFormat.Json
};

var chatRequest = AbstractionMapper.ToOllamaSharpChatRequest(chatMessages, options, stream: true, JsonSerializerOptions.Default);
chatRequest.Format.Should().Be("json");
}

[Test]
public void Maps_JsonWithSchema()
{
var chatMessages = new List<Microsoft.Extensions.AI.ChatMessage>();
var schemaElement = AIJsonUtilities.CreateJsonSchema(type: typeof(Sword));

var options = new Microsoft.Extensions.AI.ChatOptions
{
ResponseFormat = ChatResponseFormat.ForJsonSchema(schemaElement)
};

var chatRequest = AbstractionMapper.ToOllamaSharpChatRequest(chatMessages, options, stream: true, JsonSerializerOptions.Default);
chatRequest.Format.Should().Be(schemaElement);
}

private class Sword
{
public required string Name { get; set; }
public required int Damage { get; set; }
}

[Test]
public void Maps_Ollama_Options()
{
Expand Down

0 comments on commit 8c3b956

Please sign in to comment.