openai / openai-dotnet

The official .NET library for the OpenAI API
https://www.nuget.org/packages/OpenAI
MIT License
1.13k stars 113 forks source link

Trimming/NativeAOT support #20

Open HavenDV opened 3 months ago

HavenDV commented 3 months ago

According this article: https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/prepare-libraries-for-trimming?pivots=dotnet-8-0

Now there are such warnings:

1>InternalListHelpers.cs(34,9): Warning IL2091 Trim analysis: OpenAI.InternalListHelpers.GetPageFromProtocol<TInstance,UInternalList>(ClientResult): 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in 'System.ClientModel.Primitives.ModelReaderWriter.Read<T>(BinaryData, ModelReaderWriterOptions)'. The generic parameter 'UInternalList' of 'OpenAI.InternalListHelpers.GetPageFromProtocol<TInstance,UInternalList>(ClientResult)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
1>System.Memory.Data.dll: Warning IL2104 : Assembly 'System.Memory.Data' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries

If I use the preview version of System.Memory.Data, the following warnings appear here

1>ModerationClient.cs(77,9): Warning IL2026 Trim analysis: OpenAI.Moderations.ModerationClient.<ClassifyTextInputAsync>d__4.MoveNext(): Using member 'System.BinaryData.FromObjectAsJson<T>(T, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed.
1>ModerationClient.cs(110,9): Warning IL2026 Trim analysis: OpenAI.Moderations.ModerationClient.<ClassifyTextInputsAsync>d__6.MoveNext(): Using member 'System.BinaryData.FromObjectAsJson<T>(T, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed.
1>EmbeddingClient.cs(84,9): Warning IL2026 Trim analysis: OpenAI.Embeddings.EmbeddingClient.<GenerateEmbeddingAsync>d__4.MoveNext(): Using member 'System.BinaryData.FromObjectAsJson<T>(T, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed.
1>EmbeddingClient.cs(120,9): Warning IL2026 Trim analysis: OpenAI.Embeddings.EmbeddingClient.<GenerateEmbeddingsAsync>d__6.MoveNext(): Using member 'System.BinaryData.FromObjectAsJson<T>(T, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed.
1>EmbeddingClient.cs(157,9): Warning IL2026 Trim analysis: OpenAI.Embeddings.EmbeddingClient.<GenerateEmbeddingsAsync>d__8.MoveNext(): Using member 'System.BinaryData.FromObjectAsJson<T>(T, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed.
1>EmbeddingClient.cs(102,9): Warning IL2026 Trim analysis: OpenAI.Embeddings.EmbeddingClient.GenerateEmbedding(String, EmbeddingGenerationOptions): Using member 'System.BinaryData.FromObjectAsJson<T>(T, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed.
1>EmbeddingClient.cs(139,9): Warning IL2026 Trim analysis: OpenAI.Embeddings.EmbeddingClient.GenerateEmbeddings(IEnumerable<String>, EmbeddingGenerationOptions): Using member 'System.BinaryData.FromObjectAsJson<T>(T, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed.
1>EmbeddingClient.cs(175,9): Warning IL2026 Trim analysis: OpenAI.Embeddings.EmbeddingClient.GenerateEmbeddings(IEnumerable<IEnumerable<Int32>>, EmbeddingGenerationOptions): Using member 'System.BinaryData.FromObjectAsJson<T>(T, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed.
1>ModerationClient.cs(93,9): Warning IL2026 Trim analysis: OpenAI.Moderations.ModerationClient.ClassifyTextInput(String): Using member 'System.BinaryData.FromObjectAsJson<T>(T, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed.
1>ModerationClient.cs(126,9): Warning IL2026 Trim analysis: OpenAI.Moderations.ModerationClient.ClassifyTextInputs(IEnumerable<String>): Using member 'System.BinaryData.FromObjectAsJson<T>(T, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed.
1>OpenAIModelFactory.cs(33,9): Warning IL2026 Trim analysis: OpenAI.OpenAIModelFactory.Embedding(ReadOnlyMemory<Single>, Int32): Using member 'System.BinaryData.FromObjectAsJson<T>(T, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed.
trrwilson commented 3 months ago

@HavenDV, thanks for the prompt discovery of this and even getting a comprehensive change prepared! Trimming support is a topic under investigation with the System.ClientModel team (where the result collections originate from) and we'll follow up ASAP based on that conversation.