openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
OpenAI/src/Custom/Audio/AudioTranscription.Serialization.cs
44lines · modecode
| 1 | using System; |
| 2 | using System.ClientModel; |
| 3 | using System.ClientModel.Primitives; |
| 4 | using System.Collections.Generic; |
| 5 | using System.Diagnostics.CodeAnalysis; |
| 6 | using System.Text.Json; |
| 7 | |
| 8 | namespace OpenAI.Audio; |
| 9 | |
| 10 | public partial class AudioTranscription |
| 11 | { |
| 12 | internal static AudioTranscription FromResponse(PipelineResponse response) |
| 13 | { |
| 14 | // Customization: handle plain text responses (SRT/VTT formats) |
| 15 | if (response?.Headers?.TryGetValue("Content-Type", out string contentType) == true && |
| 16 | contentType.StartsWith("text/plain", StringComparison.Ordinal)) |
| 17 | { |
| 18 | return new AudioTranscription( |
| 19 | language: null, |
| 20 | duration: null, |
| 21 | text: response.Content?.ToString(), |
| 22 | words: new ChangeTrackingList<TranscribedWord>(), |
| 23 | segments: new ChangeTrackingList<TranscribedSegment>(), |
| 24 | usage: null, |
| 25 | transcriptionTokenLogProbabilities: new ChangeTrackingList<AudioTokenLogProbabilityDetails>(), |
| 26 | additionalBinaryDataProperties: new Dictionary<string, BinaryData>()); |
| 27 | } |
| 28 | |
| 29 | using var document = JsonDocument.Parse(response.Content); |
| 30 | return DeserializeAudioTranscription(document.RootElement, null); |
| 31 | } |
| 32 | |
| 33 | // CUSTOM: The explicit operator is not auto-generated because the response type |
| 34 | // has been customized in the specification (DotNetCombinedJsonTranscriptionResponse). |
| 35 | // Auto-generation only occurs for root output types that directly match the operation's |
| 36 | // response schema without customization. |
| 37 | [Experimental("OPENAI001")] |
| 38 | public static explicit operator AudioTranscription(ClientResult result) |
| 39 | { |
| 40 | using PipelineResponse response = result.GetRawResponse(); |
| 41 | using JsonDocument document = JsonDocument.Parse(response.Content); |
| 42 | return DeserializeAudioTranscription(document.RootElement, ModelSerializationExtensions.WireOptions); |
| 43 | } |
| 44 | } |
| 45 | |