openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Audio/AudioTranscriptionOptions.cs
136lines · modecode
| 1 | using OpenAI.Internal; |
| 2 | using System; |
| 3 | using System.Collections.Generic; |
| 4 | using System.IO; |
| 5 | |
| 6 | namespace OpenAI.Audio; |
| 7 | |
| 8 | [CodeGenModel("CreateTranscriptionRequest")] |
| 9 | [CodeGenSuppress("AudioTranscriptionOptions", typeof(BinaryData), typeof(InternalCreateTranscriptionRequestModel))] |
| 10 | public partial class AudioTranscriptionOptions |
| 11 | { |
| 12 | // CUSTOM: Made internal. This value comes from a parameter on the client method. |
| 13 | /// <summary> |
| 14 | /// The audio file object (not file name) to transcribe, in one of these formats: flac, mp3, mp4, |
| 15 | /// mpeg, mpga, m4a, ogg, pcm, wav, or webm. |
| 16 | /// <para> |
| 17 | /// To assign a byte[] to this property use <see cref="BinaryData.FromBytes(byte[])"/>. |
| 18 | /// The byte[] will be serialized to a Base64 encoded string. |
| 19 | /// </para> |
| 20 | /// <para> |
| 21 | /// Examples: |
| 22 | /// <list type="bullet"> |
| 23 | /// <item> |
| 24 | /// <term>BinaryData.FromBytes(new byte[] { 1, 2, 3 })</term> |
| 25 | /// <description>Creates a payload of "AQID".</description> |
| 26 | /// </item> |
| 27 | /// </list> |
| 28 | /// </para> |
| 29 | /// </summary> |
| 30 | internal BinaryData File { get; } |
| 31 | |
| 32 | // CUSTOM: |
| 33 | // - Made internal. The model is specified by the client. |
| 34 | // - Added setter. |
| 35 | /// <summary> |
| 36 | /// ID of the model to use. Only `whisper-1` (which is powered by our open source Whisper V2 model) |
| 37 | /// is currently available. |
| 38 | /// </summary> |
| 39 | internal InternalCreateTranscriptionRequestModel Model { get; set; } |
| 40 | |
| 41 | // CUSTOM: Made internal. The model is specified by the client. |
| 42 | /// <summary> |
| 43 | /// The timestamp granularities to populate for this transcription. `response_format` must be set |
| 44 | /// `verbose_json` to use timestamp granularities. Either or both of these options are supported: |
| 45 | /// `word`, or `segment`. Note: There is no additional latency for segment timestamps, but |
| 46 | /// generating word timestamps incurs additional latency. |
| 47 | /// <para> |
| 48 | /// To assign an object to the element of this property use <see cref="BinaryData.FromObjectAsJson{T}(T, System.Text.Json.JsonSerializerOptions?)"/>. |
| 49 | /// </para> |
| 50 | /// <para> |
| 51 | /// To assign an already formatted json string to this property use <see cref="BinaryData.FromString(string)"/>. |
| 52 | /// </para> |
| 53 | /// <para> |
| 54 | /// Examples: |
| 55 | /// <list type="bullet"> |
| 56 | /// <item> |
| 57 | /// <term>BinaryData.FromObjectAsJson("foo")</term> |
| 58 | /// <description>Creates a payload of "foo".</description> |
| 59 | /// </item> |
| 60 | /// <item> |
| 61 | /// <term>BinaryData.FromString("\"foo\"")</term> |
| 62 | /// <description>Creates a payload of "foo".</description> |
| 63 | /// </item> |
| 64 | /// <item> |
| 65 | /// <term>BinaryData.FromObjectAsJson(new { key = "value" })</term> |
| 66 | /// <description>Creates a payload of { "key": "value" }.</description> |
| 67 | /// </item> |
| 68 | /// <item> |
| 69 | /// <term>BinaryData.FromString("{\"key\": \"value\"}")</term> |
| 70 | /// <description>Creates a payload of { "key": "value" }.</description> |
| 71 | /// </item> |
| 72 | /// </list> |
| 73 | /// </para> |
| 74 | /// </summary> |
| 75 | internal IList<BinaryData> TimestampGranularities { get; } |
| 76 | |
| 77 | // CUSTOM: Made public now that there are no required properties. |
| 78 | /// <summary> Initializes a new instance of <see cref="AudioTranscriptionOptions"/>. </summary> |
| 79 | public AudioTranscriptionOptions() |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | /// <summary> |
| 84 | /// The timestamp granularities to populate for this transcription. |
| 85 | /// </summary> |
| 86 | public AudioTimestampGranularities Granularities { get; init; } |
| 87 | |
| 88 | internal MultipartFormDataBinaryContent ToMultipartContent(Stream audio, string audioFilename) |
| 89 | { |
| 90 | MultipartFormDataBinaryContent content = new(); |
| 91 | |
| 92 | content.Add(audio, "file", audioFilename); |
| 93 | content.Add(Model.ToString(), "model"); |
| 94 | |
| 95 | if (Language is not null) |
| 96 | { |
| 97 | content.Add(Language, "language"); |
| 98 | } |
| 99 | |
| 100 | if (Prompt is not null) |
| 101 | { |
| 102 | content.Add(Prompt, "prompt"); |
| 103 | } |
| 104 | |
| 105 | if (ResponseFormat is not null) |
| 106 | { |
| 107 | string value = ResponseFormat switch |
| 108 | { |
| 109 | AudioTranscriptionFormat.Simple => "json", |
| 110 | AudioTranscriptionFormat.Verbose => "verbose_json", |
| 111 | AudioTranscriptionFormat.Srt => "srt", |
| 112 | AudioTranscriptionFormat.Vtt => "vtt", |
| 113 | _ => throw new ArgumentException(nameof(ResponseFormat)) |
| 114 | }; |
| 115 | |
| 116 | content.Add(value, "response_format"); |
| 117 | } |
| 118 | |
| 119 | if (Temperature is not null) |
| 120 | { |
| 121 | content.Add(Temperature.Value, "temperature"); |
| 122 | } |
| 123 | |
| 124 | if (Granularities.HasFlag(AudioTimestampGranularities.Word)) |
| 125 | { |
| 126 | content.Add("word", "timestamp_granularities[]"); |
| 127 | } |
| 128 | |
| 129 | if (Granularities.HasFlag(AudioTimestampGranularities.Segment)) |
| 130 | { |
| 131 | content.Add("segment", "timestamp_granularities[]"); |
| 132 | } |
| 133 | |
| 134 | return content; |
| 135 | } |
| 136 | } |
| 137 | |