Skip to content
Merged
12 changes: 6 additions & 6 deletions src/Custom/Embeddings/EmbeddingClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public virtual async Task<ClientResult<OpenAIEmbedding>> 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);
Expand All @@ -118,7 +118,7 @@ public virtual ClientResult<OpenAIEmbedding> 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());
Expand All @@ -137,7 +137,7 @@ public virtual async Task<ClientResult<OpenAIEmbeddingCollection>> 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);
Expand All @@ -157,7 +157,7 @@ public virtual ClientResult<OpenAIEmbeddingCollection> 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());
Expand All @@ -176,7 +176,7 @@ public virtual async Task<ClientResult<OpenAIEmbeddingCollection>> 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);
Expand All @@ -195,7 +195,7 @@ public virtual ClientResult<OpenAIEmbeddingCollection> 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());
Expand Down
8 changes: 4 additions & 4 deletions src/Custom/Moderations/ModerationClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public virtual async Task<ClientResult<ModerationResult>> 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);
Expand All @@ -114,7 +114,7 @@ public virtual ClientResult<ModerationResult> 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());
Expand All @@ -131,7 +131,7 @@ public virtual async Task<ClientResult<ModerationResultCollection>> 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);
Expand All @@ -148,7 +148,7 @@ public virtual ClientResult<ModerationResultCollection> 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());
Expand Down
2 changes: 1 addition & 1 deletion src/Custom/OpenAIModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static OpenAIEmbeddingCollection OpenAIEmbeddingCollection(IEnumerable<Op
public static OpenAIEmbedding OpenAIEmbedding(ReadOnlyMemory<float> 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);
}

}
9 changes: 9 additions & 0 deletions src/OpenAI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,14 @@
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="System.ClientModel" Version="1.2.1" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="6.0.1" />
<PackageReference Include="System.Memory.Data" Version="9.0.0" />
</ItemGroup>

<PropertyGroup Label="Trimmable" Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<IsTrimmable>true</IsTrimmable>
<SuppressTrimAnalysisWarnings>false</SuppressTrimAnalysisWarnings>
<TrimmerSingleWarn>false</TrimmerSingleWarn>
</PropertyGroup>
</Project>
12 changes: 12 additions & 0 deletions src/SourceGenerationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace OpenAI;

[JsonSourceGenerationOptions]
[JsonSerializable(typeof(string))]
[JsonSerializable(typeof(IEnumerable<string>))]
[JsonSerializable(typeof(IEnumerable<ReadOnlyMemory<int>>))]
[JsonSerializable(typeof(ReadOnlyMemory<float>))]
internal sealed partial class SourceGenerationContext : JsonSerializerContext;
Loading