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

Add OpenAI serialization helper methods. #5697

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

eiriktsarpalis
Copy link
Member

@eiriktsarpalis eiriktsarpalis commented Nov 26, 2024

Very early draft implementation of helper methods for serializing M.E.AI.Abstractions using the OpenAI wire format. Contains three separate commits that should be reviewed separately:

  1. a0a6696 adds a System.Net.ServerSentEvents polyfill.
  2. 339d3ad refactors the chat client so that model mapping is pulled into a standalone class.
  3. ac2a89f provides the initial skeleton implementation of the serialization methods.
Microsoft Reviewers: Open in CodeFlow

/// <param name="options">The <see cref="JsonSerializerOptions"/> governing deserialization of function call content.</param>
/// <param name="cancellationToken">A token used to cancel the operation.</param>
/// <returns>The deserialized list of chat messages and chat options.</returns>
public static async Task<(IList<ChatMessage> Messages, ChatOptions? Options)> DeserializeFromOpenAIAsync(
Copy link
Member

@stephentoub stephentoub Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this about deserializing a request, deserializing a non-streaming response, or deserializing a streaming response? Presumably a request, but we'll want it to be clear in the naming.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's for deserializing requests. API names are largely placeholders, will refine.

options ??= AIJsonUtilities.DefaultOptions;

OpenAI.Chat.ChatCompletion openAiChatCompletion = OpenAIModelMappers.ToOpenAIChatCompletion(chatCompletion);
BinaryData binaryData = JsonModelHelpers.Serialize(openAiChatCompletion);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't serialize directly to the stream?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The OpenAI models use the IJsonModel<T> pattern which doesn't support async streaming serialization.

@dotnet-comment-bot
Copy link
Collaborator

‼️ Found issues ‼️

Project Coverage Type Expected Actual
Microsoft.Extensions.AI.OpenAI Line 66 34.95 🔻
Microsoft.Extensions.AI.OpenAI Branch 66 26.69 🔻

🎉 Good job! The coverage increased 🎉
Update MinCodeCoverage in the project files.

Project Expected Actual
Microsoft.Extensions.Diagnostics.Probes 70 76
Microsoft.Extensions.Caching.Hybrid 75 86

Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=881220&view=codecoverage-tab

@dotnet-comment-bot
Copy link
Collaborator

‼️ Found issues ‼️

Project Coverage Type Expected Actual
Microsoft.Extensions.AI.OpenAI Line 66 33.18 🔻
Microsoft.Extensions.AI.OpenAI Branch 66 25.54 🔻

🎉 Good job! The coverage increased 🎉
Update MinCodeCoverage in the project files.

Project Expected Actual
Microsoft.Extensions.Diagnostics.Probes 70 76
Microsoft.Extensions.Caching.Hybrid 75 86

Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=881767&view=codecoverage-tab

@dotnet-comment-bot
Copy link
Collaborator

‼️ Found issues ‼️

Project Coverage Type Expected Actual
Microsoft.Extensions.AI.OpenAI Line 66 33.15 🔻
Microsoft.Extensions.AI.OpenAI Branch 66 25.96 🔻

🎉 Good job! The coverage increased 🎉
Update MinCodeCoverage in the project files.

Project Expected Actual
Microsoft.Extensions.Diagnostics.Probes 70 76
Microsoft.Extensions.Caching.Hybrid 75 78
Microsoft.Extensions.AI.AzureAIInference 83 91
Microsoft.Extensions.AI.Abstractions 83 84
Microsoft.Extensions.AI 83 84

Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=886081&view=codecoverage-tab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants