diff --git a/src/Custom/Embeddings/EmbeddingClient.cs b/src/Custom/Embeddings/EmbeddingClient.cs index 862f543b6..cd07961a7 100644 --- a/src/Custom/Embeddings/EmbeddingClient.cs +++ b/src/Custom/Embeddings/EmbeddingClient.cs @@ -99,7 +99,7 @@ public virtual async Task> GenerateEmbeddingAsync( Argument.AssertNotNullOrEmpty(input, nameof(input)); options ??= new(); - CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(input), ref options); + CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(input, SourceGenerationContext.Default.String), ref options); using BinaryContent content = options; ClientResult result = await GenerateEmbeddingsAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); @@ -118,7 +118,7 @@ public virtual ClientResult GenerateEmbedding(string input, Emb Argument.AssertNotNullOrEmpty(input, nameof(input)); options ??= new(); - CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(input), ref options); + CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(input, SourceGenerationContext.Default.String), ref options); using BinaryContent content = options; ClientResult result = GenerateEmbeddings(content, cancellationToken.ToRequestOptions()); @@ -137,7 +137,7 @@ public virtual async Task> GenerateEmbed Argument.AssertNotNullOrEmpty(inputs, nameof(inputs)); options ??= new(); - CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(inputs), ref options); + CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(inputs, SourceGenerationContext.Default.IEnumerableString), ref options); using BinaryContent content = options; ClientResult result = await GenerateEmbeddingsAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); @@ -157,7 +157,7 @@ public virtual ClientResult GenerateEmbeddings(IEnume Argument.AssertNotNullOrEmpty(inputs, nameof(inputs)); options ??= new(); - CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(inputs), ref options); + CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(inputs, SourceGenerationContext.Default.IEnumerableString), ref options); using BinaryContent content = options; ClientResult result = GenerateEmbeddings(content, cancellationToken.ToRequestOptions()); @@ -176,7 +176,7 @@ public virtual async Task> GenerateEmbed Argument.AssertNotNullOrEmpty(inputs, nameof(inputs)); options ??= new(); - CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(inputs), ref options); + CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(inputs, SourceGenerationContext.Default.IEnumerableReadOnlyMemoryInt32), ref options); using BinaryContent content = options; ClientResult result = await GenerateEmbeddingsAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); @@ -195,7 +195,7 @@ public virtual ClientResult GenerateEmbeddings(IEnume Argument.AssertNotNullOrEmpty(inputs, nameof(inputs)); options ??= new(); - CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(inputs), ref options); + CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(inputs, SourceGenerationContext.Default.IEnumerableReadOnlyMemoryInt32), ref options); using BinaryContent content = options; ClientResult result = GenerateEmbeddings(content, cancellationToken.ToRequestOptions()); diff --git a/src/Custom/Moderations/ModerationClient.cs b/src/Custom/Moderations/ModerationClient.cs index e66358c99..c28aace72 100644 --- a/src/Custom/Moderations/ModerationClient.cs +++ b/src/Custom/Moderations/ModerationClient.cs @@ -97,7 +97,7 @@ public virtual async Task> ClassifyTextAsync(stri Argument.AssertNotNullOrEmpty(input, nameof(input)); ModerationOptions options = new(); - CreateModerationOptions(BinaryData.FromObjectAsJson(input), ref options); + CreateModerationOptions(BinaryData.FromObjectAsJson(input, SourceGenerationContext.Default.String), ref options); using BinaryContent content = options; ClientResult result = await ClassifyTextAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); @@ -114,7 +114,7 @@ public virtual ClientResult ClassifyText(string input, Cancell Argument.AssertNotNullOrEmpty(input, nameof(input)); ModerationOptions options = new(); - CreateModerationOptions(BinaryData.FromObjectAsJson(input), ref options); + CreateModerationOptions(BinaryData.FromObjectAsJson(input, SourceGenerationContext.Default.String), ref options); using BinaryContent content = options; ClientResult result = ClassifyText(content, cancellationToken.ToRequestOptions()); @@ -131,7 +131,7 @@ public virtual async Task> ClassifyText Argument.AssertNotNullOrEmpty(inputs, nameof(inputs)); ModerationOptions options = new(); - CreateModerationOptions(BinaryData.FromObjectAsJson(inputs), ref options); + CreateModerationOptions(BinaryData.FromObjectAsJson(inputs, SourceGenerationContext.Default.IEnumerableString), ref options); using BinaryContent content = options; ClientResult result = await ClassifyTextAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); @@ -148,7 +148,7 @@ public virtual ClientResult ClassifyText(IEnumerable Argument.AssertNotNullOrEmpty(inputs, nameof(inputs)); ModerationOptions options = new(); - CreateModerationOptions(BinaryData.FromObjectAsJson(inputs), ref options); + CreateModerationOptions(BinaryData.FromObjectAsJson(inputs, SourceGenerationContext.Default.IEnumerableString), ref options); using BinaryContent content = options; ClientResult result = ClassifyText(content, cancellationToken.ToRequestOptions()); diff --git a/src/Custom/OpenAIModelFactory.cs b/src/Custom/OpenAIModelFactory.cs index 18d762f1e..e3aa789b1 100644 --- a/src/Custom/OpenAIModelFactory.cs +++ b/src/Custom/OpenAIModelFactory.cs @@ -30,7 +30,7 @@ public static OpenAIEmbeddingCollection OpenAIEmbeddingCollection(IEnumerable vector = default, int index = default) { // TODO: Vector must be converted to base64-encoded string. - return new OpenAIEmbedding(index, BinaryData.FromObjectAsJson(vector), InternalEmbeddingObject.Embedding, serializedAdditionalRawData: null); + return new OpenAIEmbedding(index, BinaryData.FromObjectAsJson(vector, SourceGenerationContext.Default.ReadOnlyMemorySingle), InternalEmbeddingObject.Embedding, serializedAdditionalRawData: null); } } diff --git a/src/OpenAI.csproj b/src/OpenAI.csproj index dfcea12a8..1626c53d7 100644 --- a/src/OpenAI.csproj +++ b/src/OpenAI.csproj @@ -78,5 +78,14 @@ + + + + true + true + true + false + false + diff --git a/src/SourceGenerationContext.cs b/src/SourceGenerationContext.cs new file mode 100644 index 000000000..7b909b03e --- /dev/null +++ b/src/SourceGenerationContext.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text.Json.Serialization; + +namespace OpenAI; + +[JsonSourceGenerationOptions] +[JsonSerializable(typeof(string))] +[JsonSerializable(typeof(IEnumerable))] +[JsonSerializable(typeof(IEnumerable>))] +[JsonSerializable(typeof(ReadOnlyMemory))] +internal sealed partial class SourceGenerationContext : JsonSerializerContext; \ No newline at end of file