Skip to content
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

Make a number of improvements to the OpenAI serialization helpers. #5799

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -3,6 +3,7 @@

using System;
using System.ClientModel.Primitives;
using System.Text.Json;

namespace Microsoft.Extensions.AI;

Expand All @@ -23,6 +24,12 @@ public static TModel Deserialize<TModel>(BinaryData data)
return JsonModelDeserializationWitness<TModel>.Value.Create(data, ModelReaderWriterOptions.Json);
}

public static TModel Deserialize<TModel>(ref Utf8JsonReader reader)
where TModel : IJsonModel<TModel>, new()
{
return JsonModelDeserializationWitness<TModel>.Value.Create(ref reader, ModelReaderWriterOptions.Json);
}

private sealed class JsonModelDeserializationWitness<TModel>
where TModel : IJsonModel<TModel>, new()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text.Json;
using System.Text.Json.Serialization;
using OpenAI.Chat;

#pragma warning disable CA1034 // Nested types should not be visible

namespace Microsoft.Extensions.AI;

/// <summary>
/// Represents an OpenAI chat completion request deserialized as Microsoft.Extension.AI models.
/// </summary>
[JsonConverter(typeof(Converter))]
public sealed class OpenAIChatCompletionRequest
{
/// <summary>
Expand All @@ -29,4 +37,33 @@ public sealed class OpenAIChatCompletionRequest
/// Gets the model id requested by the chat completion.
/// </summary>
public string? ModelId { get; init; }

/// <summary>
/// Converts an OpenAIChatCompletionRequest object to and from JSON.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class Converter : JsonConverter<OpenAIChatCompletionRequest>
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// Reads and converts the JSON to type OpenAIChatCompletionRequest.
/// </summary>
/// <param name="reader">The reader.</param>
/// <param name="typeToConvert">The type to convert.</param>
/// <param name="options">The serializer options.</param>
/// <returns>The converted OpenAIChatCompletionRequest object.</returns>
public override OpenAIChatCompletionRequest? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
ChatCompletionOptions chatCompletionOptions = JsonModelHelpers.Deserialize<ChatCompletionOptions>(ref reader);
return OpenAIModelMappers.FromOpenAIChatCompletionRequest(chatCompletionOptions);
}

/// <summary>
/// Writes the specified value as JSON.
/// </summary>
/// <param name="writer">The writer.</param>
/// <param name="value">The value to write.</param>
/// <param name="options">The serializer options.</param>
public override void Write(Utf8JsonWriter writer, OpenAIChatCompletionRequest value, JsonSerializerOptions options) =>
throw new NotSupportedException("Request body serialization is not supported.");
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public static OpenAI.Chat.ChatCompletion ToOpenAIChatCompletion(ChatCompletion c

return OpenAIChatModelFactory.ChatCompletion(
id: chatCompletion.CompletionId ?? CreateCompletionId(),
model: chatCompletion.ModelId,
createdAt: chatCompletion.CreatedAt ?? default,
model: chatCompletion.ModelId ?? string.Empty,
createdAt: chatCompletion.CreatedAt ?? DateTimeOffset.UtcNow,
role: ToOpenAIChatRole(chatCompletion.Message.Role).Value,
finishReason: ToOpenAIFinishReason(chatCompletion.FinishReason),
content: new(ToOpenAIChatContent(chatCompletion.Message.Contents)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ internal static partial class OpenAIModelMappers

yield return OpenAIChatModelFactory.StreamingChatCompletionUpdate(
completionId: chatCompletionUpdate.CompletionId ?? CreateCompletionId(),
model: chatCompletionUpdate.ModelId,
createdAt: chatCompletionUpdate.CreatedAt ?? default,
model: chatCompletionUpdate.ModelId ?? string.Empty,
createdAt: chatCompletionUpdate.CreatedAt ?? DateTimeOffset.UtcNow,
role: ToOpenAIChatRole(chatCompletionUpdate.Role),
finishReason: ToOpenAIFinishReason(chatCompletionUpdate.FinishReason),
finishReason: chatCompletionUpdate.FinishReason is null ? null : ToOpenAIFinishReason(chatCompletionUpdate.FinishReason),
contentUpdate: [.. ToOpenAIChatContent(chatCompletionUpdate.Contents)],
toolCallUpdates: toolCallUpdates,
refusalUpdate: chatCompletionUpdate.AdditionalProperties.GetValueOrDefault<string>(nameof(OpenAI.Chat.StreamingChatCompletionUpdate.RefusalUpdate)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,38 @@ public static async Task RequestDeserialization_SimpleMessage_Stream()
Assert.Null(textContent.AdditionalProperties);
}

[Fact]
public static void RequestDeserialization_SimpleMessage_JsonSerializer()
{
const string RequestJson = """
{"messages":[{"role":"user","content":"hello"}],"model":"gpt-4o-mini","max_completion_tokens":20,"stream":true,"stream_options":{"include_usage":true},"temperature":0.5}
""";

OpenAIChatCompletionRequest? request = JsonSerializer.Deserialize<OpenAIChatCompletionRequest>(RequestJson);

Assert.NotNull(request);
Assert.True(request.Stream);
Assert.Equal("gpt-4o-mini", request.ModelId);

Assert.NotNull(request.Options);
Assert.Equal("gpt-4o-mini", request.Options.ModelId);
Assert.Equal(0.5f, request.Options.Temperature);
Assert.Equal(20, request.Options.MaxOutputTokens);
Assert.Null(request.Options.TopK);
Assert.Null(request.Options.TopP);
Assert.Null(request.Options.StopSequences);
Assert.Null(request.Options.AdditionalProperties);
Assert.Null(request.Options.Tools);

ChatMessage message = Assert.Single(request.Messages);
Assert.Equal(ChatRole.User, message.Role);
AIContent content = Assert.Single(message.Contents);
TextContent textContent = Assert.IsType<TextContent>(content);
Assert.Equal("hello", textContent.Text);
Assert.Null(textContent.RawRepresentation);
Assert.Null(textContent.AdditionalProperties);
}

[Fact]
public static async Task RequestDeserialization_MultipleMessages()
{
Expand Down Expand Up @@ -614,13 +646,13 @@ static async IAsyncEnumerable<StreamingChatCompletionUpdate> CreateStreamingComp
string result = Encoding.UTF8.GetString(stream.ToArray());

AssertSseEqual("""
data: {"id":"chatcmpl-ADymNiWWeqCJqHNFXiI1QtRcLuXcl","choices":[{"delta":{"content":"Streaming update 0","tool_calls":[],"role":"assistant"},"logprobs":{"content":[],"refusal":[]},"finish_reason":"stop","index":0}],"created":1727888631,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_f85bea6784","object":"chat.completion.chunk"}
data: {"id":"chatcmpl-ADymNiWWeqCJqHNFXiI1QtRcLuXcl","choices":[{"delta":{"content":"Streaming update 0","tool_calls":[],"role":"assistant"},"logprobs":{"content":[],"refusal":[]},"index":0}],"created":1727888631,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_f85bea6784","object":"chat.completion.chunk"}

data: {"id":"chatcmpl-ADymNiWWeqCJqHNFXiI1QtRcLuXcl","choices":[{"delta":{"content":"Streaming update 1","tool_calls":[],"role":"assistant"},"logprobs":{"content":[],"refusal":[]},"finish_reason":"stop","index":0}],"created":1727888631,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_f85bea6784","object":"chat.completion.chunk"}
data: {"id":"chatcmpl-ADymNiWWeqCJqHNFXiI1QtRcLuXcl","choices":[{"delta":{"content":"Streaming update 1","tool_calls":[],"role":"assistant"},"logprobs":{"content":[],"refusal":[]},"index":0}],"created":1727888631,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_f85bea6784","object":"chat.completion.chunk"}

data: {"id":"chatcmpl-ADymNiWWeqCJqHNFXiI1QtRcLuXcl","choices":[{"delta":{"content":"Streaming update 2","tool_calls":[{"index":0,"id":"callId","type":"function","function":{"name":"MyCoolFunc","arguments":"{\r\n \u0022arg1\u0022: 42,\r\n \u0022arg2\u0022: \u0022str\u0022\r\n}"}}],"role":"assistant"},"logprobs":{"content":[],"refusal":[]},"finish_reason":"stop","index":0}],"created":1727888631,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_f85bea6784","object":"chat.completion.chunk"}
data: {"id":"chatcmpl-ADymNiWWeqCJqHNFXiI1QtRcLuXcl","choices":[{"delta":{"content":"Streaming update 2","tool_calls":[{"index":0,"id":"callId","type":"function","function":{"name":"MyCoolFunc","arguments":"{\r\n \u0022arg1\u0022: 42,\r\n \u0022arg2\u0022: \u0022str\u0022\r\n}"}}],"role":"assistant"},"logprobs":{"content":[],"refusal":[]},"index":0}],"created":1727888631,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_f85bea6784","object":"chat.completion.chunk"}

data: {"id":"chatcmpl-ADymNiWWeqCJqHNFXiI1QtRcLuXcl","choices":[{"delta":{"content":"Streaming update 3","tool_calls":[],"role":"assistant"},"logprobs":{"content":[],"refusal":[]},"finish_reason":"stop","index":0}],"created":1727888631,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_f85bea6784","object":"chat.completion.chunk"}
data: {"id":"chatcmpl-ADymNiWWeqCJqHNFXiI1QtRcLuXcl","choices":[{"delta":{"content":"Streaming update 3","tool_calls":[],"role":"assistant"},"logprobs":{"content":[],"refusal":[]},"index":0}],"created":1727888631,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_f85bea6784","object":"chat.completion.chunk"}

data: {"id":"chatcmpl-ADymNiWWeqCJqHNFXiI1QtRcLuXcl","choices":[{"delta":{"content":"Streaming update 4","tool_calls":[],"role":"assistant"},"logprobs":{"content":[],"refusal":[]},"finish_reason":"stop","index":0}],"created":1727888631,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_f85bea6784","object":"chat.completion.chunk","usage":{"completion_tokens":9,"prompt_tokens":8,"total_tokens":17,"completion_tokens_details":{"audio_tokens":2,"reasoning_tokens":90},"prompt_tokens_details":{"audio_tokens":1,"cached_tokens":13}}}

Expand Down