Skip to content

Commit

Permalink
Fix deserialization of run steps when using file search and mitigate …
Browse files Browse the repository at this point in the history
….NET runtime issue when serializing `ChatResponseFormat` (#206)

* Fixed the deserialization of run steps when using file search. The internal representation of File Search as an `IReadOnlyDictionary<string, string>` is incorrect and causing issues during deserialization after the latest service. This fix changes this internal representation to `IReadOnlyDictionary<string, BinaryData>` instead.
* Mitigated a .NET runtime issue that prevented `ChatResponseFormat` from serializing correct on targets including Unity.
* Renamed `SpeechGenerationOptions`'s `Speed` property to `SpeedRatio`.
* Changed `GeneratedSpeechFormat` from an enum to an extensible enum.
  • Loading branch information
joseharriaga authored Sep 3, 2024
1 parent 78f3f96 commit cc9169a
Show file tree
Hide file tree
Showing 54 changed files with 647 additions and 387 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
- Updated fine-tuning pagination methods `GetJobs`, `GetEvents`, and `GetJobCheckpoints` to return `IEnumerable<ClientResult>` instead of `ClientResult`. (commit_hash)
- Updated the batching pagination method `GetBatches` to return `IEnumerable<ClientResult>` instead of `ClientResult`. (commit_hash)
- Changed `GeneratedSpeechVoice` from an enum to an "extensible enum". (commit_hash)
- Changed `GeneratedSpeechFormat` from an enum to an "extensible enum". (commit_hash)
- Renamed `SpeechGenerationOptions`'s `Speed` property to `SpeedRatio`. (commit_hash)

### Bugs Fixed

- Corrected an internal deserialization issue that caused recent updates to Assistants `file_search` to fail when streaming a run. Strongly typed support for `ranking_options` is not included but will arrive soon. (commit_hash)
- Mitigated a .NET runtime issue that prevented `ChatResponseFormat` from serializing correct on targets including Unity. (commit_hash)

### Other Changes

- Reverted the removal of the version path parameter "v1" from the default endpoint URL. (commit_hash)
Expand Down
49 changes: 31 additions & 18 deletions api/OpenAI.netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public class CodeInterpreterToolDefinition : ToolDefinition, IJsonModel<CodeInte
CodeInterpreterToolDefinition IPersistableModel<CodeInterpreterToolDefinition>.Create(BinaryData data, ModelReaderWriterOptions options);
string IPersistableModel<CodeInterpreterToolDefinition>.GetFormatFromOptions(ModelReaderWriterOptions options);
BinaryData IPersistableModel<CodeInterpreterToolDefinition>.Write(ModelReaderWriterOptions options);
protected override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
protected internal override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
}
public class CodeInterpreterToolResources : IJsonModel<CodeInterpreterToolResources>, IPersistableModel<CodeInterpreterToolResources> {
public IList<string> FileIds { get; set; }
Expand All @@ -359,7 +359,7 @@ public class FileSearchToolDefinition : ToolDefinition, IJsonModel<FileSearchToo
FileSearchToolDefinition IPersistableModel<FileSearchToolDefinition>.Create(BinaryData data, ModelReaderWriterOptions options);
string IPersistableModel<FileSearchToolDefinition>.GetFormatFromOptions(ModelReaderWriterOptions options);
BinaryData IPersistableModel<FileSearchToolDefinition>.Write(ModelReaderWriterOptions options);
protected override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
protected internal override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
}
public class FileSearchToolResources : IJsonModel<FileSearchToolResources>, IPersistableModel<FileSearchToolResources> {
public IList<VectorStoreCreationHelper> NewVectorStores { get; }
Expand All @@ -382,7 +382,7 @@ public class FunctionToolDefinition : ToolDefinition, IJsonModel<FunctionToolDef
FunctionToolDefinition IPersistableModel<FunctionToolDefinition>.Create(BinaryData data, ModelReaderWriterOptions options);
string IPersistableModel<FunctionToolDefinition>.GetFormatFromOptions(ModelReaderWriterOptions options);
BinaryData IPersistableModel<FunctionToolDefinition>.Write(ModelReaderWriterOptions options);
protected override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
protected internal override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
}
public class MessageCollectionOptions {
public string AfterId { get; set; }
Expand Down Expand Up @@ -955,7 +955,7 @@ public abstract class ToolDefinition : IJsonModel<ToolDefinition>, IPersistableM
ToolDefinition IPersistableModel<ToolDefinition>.Create(BinaryData data, ModelReaderWriterOptions options);
string IPersistableModel<ToolDefinition>.GetFormatFromOptions(ModelReaderWriterOptions options);
BinaryData IPersistableModel<ToolDefinition>.Write(ModelReaderWriterOptions options);
protected abstract void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
protected internal abstract void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
}
public class ToolOutput : IJsonModel<ToolOutput>, IPersistableModel<ToolOutput> {
public ToolOutput();
Expand Down Expand Up @@ -1084,13 +1084,25 @@ public class AudioTranslationOptions : IJsonModel<AudioTranslationOptions>, IPer
string IPersistableModel<AudioTranslationOptions>.GetFormatFromOptions(ModelReaderWriterOptions options);
BinaryData IPersistableModel<AudioTranslationOptions>.Write(ModelReaderWriterOptions options);
}
public enum GeneratedSpeechFormat {
Mp3 = 0,
Opus = 1,
Aac = 2,
Flac = 3,
Wav = 4,
Pcm = 5
public readonly partial struct GeneratedSpeechFormat : IEquatable<GeneratedSpeechFormat> {
private readonly object _dummy;
private readonly int _dummyPrimitive;
public GeneratedSpeechFormat(string value);
public static GeneratedSpeechFormat Aac { get; }
public static GeneratedSpeechFormat Flac { get; }
public static GeneratedSpeechFormat Mp3 { get; }
public static GeneratedSpeechFormat Opus { get; }
public static GeneratedSpeechFormat Pcm { get; }
public static GeneratedSpeechFormat Wav { get; }
public readonly bool Equals(GeneratedSpeechFormat other);
[EditorBrowsable(EditorBrowsableState.Never)]
public override readonly bool Equals(object obj);
[EditorBrowsable(EditorBrowsableState.Never)]
public override readonly int GetHashCode();
public static bool operator ==(GeneratedSpeechFormat left, GeneratedSpeechFormat right);
public static implicit operator GeneratedSpeechFormat(string value);
public static bool operator !=(GeneratedSpeechFormat left, GeneratedSpeechFormat right);
public override readonly string ToString();
}
public readonly partial struct GeneratedSpeechVoice : IEquatable<GeneratedSpeechVoice> {
private readonly object _dummy;
Expand Down Expand Up @@ -1120,7 +1132,7 @@ public static class OpenAIAudioModelFactory {
}
public class SpeechGenerationOptions : IJsonModel<SpeechGenerationOptions>, IPersistableModel<SpeechGenerationOptions> {
public GeneratedSpeechFormat? ResponseFormat { get; set; }
public float? Speed { get; set; }
public float? SpeedRatio { get; set; }
SpeechGenerationOptions IJsonModel<SpeechGenerationOptions>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options);
void IJsonModel<SpeechGenerationOptions>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options);
SpeechGenerationOptions IPersistableModel<SpeechGenerationOptions>.Create(BinaryData data, ModelReaderWriterOptions options);
Expand Down Expand Up @@ -1203,7 +1215,7 @@ public class AssistantChatMessage : ChatMessage, IJsonModel<AssistantChatMessage
AssistantChatMessage IPersistableModel<AssistantChatMessage>.Create(BinaryData data, ModelReaderWriterOptions options);
string IPersistableModel<AssistantChatMessage>.GetFormatFromOptions(ModelReaderWriterOptions options);
BinaryData IPersistableModel<AssistantChatMessage>.Write(ModelReaderWriterOptions options);
protected override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
protected internal override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
}
public class ChatClient {
protected ChatClient();
Expand Down Expand Up @@ -1335,7 +1347,7 @@ public abstract class ChatMessage : IJsonModel<ChatMessage>, IPersistableModel<C
ChatMessage IPersistableModel<ChatMessage>.Create(BinaryData data, ModelReaderWriterOptions options);
string IPersistableModel<ChatMessage>.GetFormatFromOptions(ModelReaderWriterOptions options);
BinaryData IPersistableModel<ChatMessage>.Write(ModelReaderWriterOptions options);
protected abstract void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
protected internal abstract void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
}
public class ChatMessageContentPart : IJsonModel<ChatMessageContentPart>, IPersistableModel<ChatMessageContentPart> {
public BinaryData ImageBytes { get; }
Expand Down Expand Up @@ -1403,6 +1415,7 @@ public abstract class ChatResponseFormat : IEquatable<ChatResponseFormat>, IJson
[EditorBrowsable(EditorBrowsableState.Never)]
bool IEquatable<ChatResponseFormat>.Equals(ChatResponseFormat other);
public override string ToString();
protected internal abstract void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
}
public class ChatTokenLogProbabilityInfo : IJsonModel<ChatTokenLogProbabilityInfo>, IPersistableModel<ChatTokenLogProbabilityInfo> {
public float LogProbability { get; }
Expand Down Expand Up @@ -1510,7 +1523,7 @@ public class FunctionChatMessage : ChatMessage, IJsonModel<FunctionChatMessage>,
FunctionChatMessage IPersistableModel<FunctionChatMessage>.Create(BinaryData data, ModelReaderWriterOptions options);
string IPersistableModel<FunctionChatMessage>.GetFormatFromOptions(ModelReaderWriterOptions options);
BinaryData IPersistableModel<FunctionChatMessage>.Write(ModelReaderWriterOptions options);
protected override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
protected internal override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
}
public readonly partial struct ImageChatMessageContentPartDetail : IEquatable<ImageChatMessageContentPartDetail> {
private readonly object _dummy;
Expand Down Expand Up @@ -1589,7 +1602,7 @@ public class SystemChatMessage : ChatMessage, IJsonModel<SystemChatMessage>, IPe
SystemChatMessage IPersistableModel<SystemChatMessage>.Create(BinaryData data, ModelReaderWriterOptions options);
string IPersistableModel<SystemChatMessage>.GetFormatFromOptions(ModelReaderWriterOptions options);
BinaryData IPersistableModel<SystemChatMessage>.Write(ModelReaderWriterOptions options);
protected override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
protected internal override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
}
public class ToolChatMessage : ChatMessage, IJsonModel<ToolChatMessage>, IPersistableModel<ToolChatMessage> {
public ToolChatMessage(string toolCallId, params ChatMessageContentPart[] contentParts);
Expand All @@ -1601,7 +1614,7 @@ public class ToolChatMessage : ChatMessage, IJsonModel<ToolChatMessage>, IPersis
ToolChatMessage IPersistableModel<ToolChatMessage>.Create(BinaryData data, ModelReaderWriterOptions options);
string IPersistableModel<ToolChatMessage>.GetFormatFromOptions(ModelReaderWriterOptions options);
BinaryData IPersistableModel<ToolChatMessage>.Write(ModelReaderWriterOptions options);
protected override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
protected internal override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
}
public class UserChatMessage : ChatMessage, IJsonModel<UserChatMessage>, IPersistableModel<UserChatMessage> {
public UserChatMessage(params ChatMessageContentPart[] content);
Expand All @@ -1613,7 +1626,7 @@ public class UserChatMessage : ChatMessage, IJsonModel<UserChatMessage>, IPersis
UserChatMessage IPersistableModel<UserChatMessage>.Create(BinaryData data, ModelReaderWriterOptions options);
string IPersistableModel<UserChatMessage>.GetFormatFromOptions(ModelReaderWriterOptions options);
BinaryData IPersistableModel<UserChatMessage>.Write(ModelReaderWriterOptions options);
protected override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
protected internal override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
}
}
namespace OpenAI.Embeddings {
Expand Down
6 changes: 3 additions & 3 deletions examples/CombinationExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void AlpacaArtAssessor()
GeneratedSpeechVoice.Fable,
new SpeechGenerationOptions()
{
Speed = 0.9f,
SpeedRatio = 0.9f,
ResponseFormat = GeneratedSpeechFormat.Opus,
});
FileInfo ttsFileInfo = new($"{chatCompletion.Id}.opus");
Expand Down Expand Up @@ -89,7 +89,7 @@ public async Task CuriousCreatureCreator()
GeneratedSpeechVoice.Onyx,
new SpeechGenerationOptions()
{
Speed = 1.1f,
SpeedRatio = 1.1f,
ResponseFormat = GeneratedSpeechFormat.Opus,
});
_ = Task.Run(async () =>
Expand Down Expand Up @@ -136,7 +136,7 @@ public async Task CuriousCreatureCreator()
GeneratedSpeechVoice.Fable,
new SpeechGenerationOptions()
{
Speed = 0.9f,
SpeedRatio = 0.9f,
ResponseFormat = GeneratedSpeechFormat.Opus,
});
FileInfo criticAudioFileInfo = new($"{criticalAppraisalResult.Value.Id}-appraisal.opus");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void IJsonModel<CodeInterpreterToolDefinition>.Write(Utf8JsonWriter writer, Mode
internal static void SerializeCodeInterpreterToolDefinition(CodeInterpreterToolDefinition instance, Utf8JsonWriter writer, ModelReaderWriterOptions options)
=> instance.WriteCore(writer, options);

protected override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
protected internal override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
writer.WriteStartObject();
writer.WritePropertyName("type"u8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void IJsonModel<FileSearchToolDefinition>.Write(Utf8JsonWriter writer, ModelRead
internal static void SerializeFileSearchToolDefinition(FileSearchToolDefinition instance, Utf8JsonWriter writer, ModelReaderWriterOptions options)
=> instance.WriteCore(writer, options);

protected override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
protected internal override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
writer.WriteStartObject();
writer.WritePropertyName("type"u8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void IJsonModel<FunctionToolDefinition>.Write(Utf8JsonWriter writer, ModelReader
internal static void SerializeFunctionToolDefinition(FunctionToolDefinition instance, Utf8JsonWriter writer, ModelReaderWriterOptions options)
=> instance.WriteCore(writer, options);

protected override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
protected internal override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
writer.WriteStartObject();
writer.WritePropertyName("function"u8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void IJsonModel<ToolDefinition>.Write(Utf8JsonWriter writer, ModelReaderWriterOp
internal static void SerializeUnknownAssistantToolDefinition(UnknownAssistantToolDefinition instance, Utf8JsonWriter writer, ModelReaderWriterOptions options)
=> instance.WriteCore(writer, options);

protected override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
protected internal override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
writer.WriteStartObject();
writer.WritePropertyName("type"u8);
Expand Down
2 changes: 1 addition & 1 deletion src/Custom/Assistants/ToolDefinition.Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ void IJsonModel<ToolDefinition>.Write(Utf8JsonWriter writer, ModelReaderWriterOp
internal static void WriteCore(ToolDefinition instance, Utf8JsonWriter writer, ModelReaderWriterOptions options)
=> instance.WriteCore(writer, options);

protected abstract void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
protected internal abstract void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
}
29 changes: 2 additions & 27 deletions src/Custom/Audio/GeneratedSpeechFormat.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,7 @@
namespace OpenAI.Audio;

/// <summary>
/// Represents an audio data format available as either input or output into an audio operation.
/// </summary>
/// <summary> The audio format in which to generate the speech. </summary>
[CodeGenModel("CreateSpeechRequestResponseFormat")]
public enum GeneratedSpeechFormat
public readonly partial struct GeneratedSpeechFormat
{
/// <summary> MP3. /// </summary>
[CodeGenMember("Mp3")]
Mp3,

/// <summary> Opus. /// </summary>
[CodeGenMember("Opus")]
Opus,

/// <summary> AAC (advanced audio coding). /// </summary>
[CodeGenMember("Aac")]
Aac,

/// <summary> FLAC (free lossless audio codec). /// </summary>
[CodeGenMember("Flac")]
Flac,

/// <summary> WAV. /// </summary>
[CodeGenMember("Wav")]
Wav,

/// <summary> PCM (pulse-code modulation). /// </summary>
[CodeGenMember("Pcm")]
Pcm,
}
22 changes: 12 additions & 10 deletions src/Custom/Audio/SpeechGenerationOptions.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
namespace OpenAI.Audio;

/// <summary>
/// A representation of additional options available to control the behavior of a text-to-speech audio generation
/// operation.
/// </summary>
/// <summary> The options to configure text-to-speech audio generation. </summary>
[CodeGenModel("CreateSpeechRequest")]
[CodeGenSuppress("SpeechGenerationOptions", typeof(InternalCreateSpeechRequestModel), typeof(string), typeof(GeneratedSpeechVoice))]
public partial class SpeechGenerationOptions
{
// CUSTOM:
// - Made internal. The model is specified by the client.
// - Added setter.
/// <summary> One of the available [TTS models](/docs/models/tts): `tts-1` or `tts-1-hd`. </summary>
[CodeGenMember("Model")]
internal InternalCreateSpeechRequestModel Model { get; set; }

// CUSTOM:
// - Made internal. This value comes from a parameter on the client method.
// - Added setter.
/// <summary> The text to generate audio for. The maximum length is 4096 characters. </summary>
[CodeGenMember("Input")]
internal string Input { get; set; }

// CUSTOM:
// - Made internal. This value comes from a parameter on the client method.
// - Added setter.
/// <summary>
/// The voice to use when generating the audio. Supported voices are `alloy`, `echo`, `fable`,
/// `onyx`, `nova`, and `shimmer`. Previews of the voices are available in the
/// [Text to speech guide](/docs/guides/text-to-speech/voice-options).
/// </summary>
[CodeGenMember("Voice")]
internal GeneratedSpeechVoice Voice { get; set; }

// CUSTOM: Made public now that there are no required properties.
/// <summary> Initializes a new instance of <see cref="SpeechGenerationOptions"/>. </summary>
public SpeechGenerationOptions()
{
}

// CUSTOM: Renamed.
/// <summary>
/// The speed of the generated audio expressed as a ratio between 0.5 and 2.0. The default is 1.0.
/// </summary>
[CodeGenMember("Speed")]

public float? SpeedRatio { get; set; }
}
4 changes: 1 addition & 3 deletions src/Custom/Chat/AssistantChatMessage.Serialization.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.ClientModel.Primitives;
using System.Collections;
using System.Collections.Generic;
using System.Text.Json;

namespace OpenAI.Chat;
Expand All @@ -14,7 +12,7 @@ void IJsonModel<AssistantChatMessage>.Write(Utf8JsonWriter writer, ModelReaderWr
internal static void SerializeAssistantChatMessage(AssistantChatMessage instance, Utf8JsonWriter writer, ModelReaderWriterOptions options)
=> instance.WriteCore(writer, options);

protected override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
protected internal override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
writer.WriteStartObject();
writer.WritePropertyName("role"u8);
Expand Down
Loading

0 comments on commit cc9169a

Please sign in to comment.