Skip to content

Add GenerateSpeechStreamingAsync() for streaming TTS responses. #426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "8.0.100",
//"version": "8.0.100",
"rollForward": "feature"
}
}
44 changes: 43 additions & 1 deletion src/Custom/Audio/AudioClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Buffers;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -108,9 +111,47 @@ public virtual async Task<ClientResult<BinaryData>> GenerateSpeechAsync(string t

using BinaryContent content = options;
ClientResult result = await GenerateSpeechAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
await result.GetRawResponse().BufferContentAsync(cancellationToken).ConfigureAwait(false);
return ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse());
}

/// <summary> Generates a life-like, spoken audio recording of the input text. </summary>
/// <remarks>
/// The default format of the generated audio is <see cref="GeneratedSpeechFormat.Mp3"/> unless otherwise specified
/// via <see cref="SpeechGenerationOptions.ResponseFormat"/>.
/// </remarks>
/// <param name="text"> The text to generate audio for. </param>
/// <param name="voice"> The voice to use in the generated audio. </param>
/// <param name="options"> The options to configure the audio generation. </param>
/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
/// <exception cref="ArgumentNullException"> <paramref name="text"/> is null. </exception>
/// <returns> Streaming chunks of the generated audio in the specified output format. </returns>
public virtual async IAsyncEnumerable<BinaryData> GenerateSpeechStreamingAsync(string text, GeneratedSpeechVoice voice, SpeechGenerationOptions options = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(text, nameof(text));

options ??= new();
CreateSpeechGenerationOptions(text, voice, ref options);

using BinaryContent content = options;
ClientResult result = await GenerateSpeechAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
var stream = result.GetRawResponse().ContentStream;

var buffer = ArrayPool<byte>.Shared.Rent(1024 * 16);
try
{
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
{
yield return BinaryData.FromBytes(buffer.AsMemory(0, bytesRead));
}
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
}

/// <summary> Generates a life-like, spoken audio recording of the input text. </summary>
/// <remarks>
/// The default format of the generated audio is <see cref="GeneratedSpeechFormat.Mp3"/> unless otherwise specified
Expand All @@ -130,7 +171,8 @@ public virtual ClientResult<BinaryData> GenerateSpeech(string text, GeneratedSpe
CreateSpeechGenerationOptions(text, voice, ref options);

using BinaryContent content = options;
ClientResult result = GenerateSpeech(content, cancellationToken.ToRequestOptions()); ;
ClientResult result = GenerateSpeech(content, cancellationToken.ToRequestOptions());
result.GetRawResponse().BufferContent(cancellationToken);
return ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse());
}

Expand Down
7 changes: 7 additions & 0 deletions src/Custom/Audio/SpeechGenerationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@ public SpeechGenerationOptions()
[CodeGenMember("Speed")]

public float? SpeedRatio { get; set; }

// CUSTOM:
/// <summary>
/// Control the voice of your generated audio with additional instructions. Does not work with tts-1 or tts-1-hd.
/// </summary>
[CodeGenMember("Instructions")]
public string Instructions { get; set; }
}
1 change: 1 addition & 0 deletions src/Generated/AudioClient.RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal virtual PipelineMessage CreateCreateSpeechRequest(BinaryContent content
{
PipelineMessage message = Pipeline.CreateMessage();
message.ResponseClassifier = PipelineMessageClassifier200;
message.BufferResponse = false;
PipelineRequest request = message.Request;
request.Method = "POST";
ClientUriBuilder uri = new ClientUriBuilder();
Expand Down
12 changes: 12 additions & 0 deletions src/Generated/Models/SpeechGenerationOptions.Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWrit
writer.WritePropertyName("speed"u8);
writer.WriteNumberValue(SpeedRatio.Value);
}
if (Optional.IsDefined(Instructions) && _additionalBinaryDataProperties?.ContainsKey("instructions") != true)
{
writer.WritePropertyName("instructions"u8);
writer.WriteStringValue(Instructions);
}
if (_additionalBinaryDataProperties != null)
{
foreach (var item in _additionalBinaryDataProperties)
Expand Down Expand Up @@ -97,6 +102,7 @@ internal static SpeechGenerationOptions DeserializeSpeechGenerationOptions(JsonE
string input = default;
GeneratedSpeechVoice voice = default;
float? speedRatio = default;
string instructions = default;
IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
foreach (var prop in element.EnumerateObject())
{
Expand Down Expand Up @@ -133,6 +139,11 @@ internal static SpeechGenerationOptions DeserializeSpeechGenerationOptions(JsonE
speedRatio = prop.Value.GetSingle();
continue;
}
if (prop.NameEquals("instructions"u8))
{
instructions = prop.Value.GetString();
continue;
}
additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
}
return new SpeechGenerationOptions(
Expand All @@ -141,6 +152,7 @@ internal static SpeechGenerationOptions DeserializeSpeechGenerationOptions(JsonE
input,
voice,
speedRatio,
instructions,
additionalBinaryDataProperties);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Generated/Models/SpeechGenerationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ public partial class SpeechGenerationOptions
{
private protected IDictionary<string, BinaryData> _additionalBinaryDataProperties;

internal SpeechGenerationOptions(GeneratedSpeechFormat? responseFormat, InternalCreateSpeechRequestModel model, string input, GeneratedSpeechVoice voice, float? speedRatio, IDictionary<string, BinaryData> additionalBinaryDataProperties)
internal SpeechGenerationOptions(GeneratedSpeechFormat? responseFormat, InternalCreateSpeechRequestModel model, string input, GeneratedSpeechVoice voice, float? speedRatio, string instructions, IDictionary<string, BinaryData> additionalBinaryDataProperties)
{
ResponseFormat = responseFormat;
Model = model;
Input = input;
Voice = voice;
SpeedRatio = speedRatio;
Instructions = instructions;
_additionalBinaryDataProperties = additionalBinaryDataProperties;
}

Expand Down
2 changes: 1 addition & 1 deletion src/OpenAI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<VersionPrefix>2.2.0</VersionPrefix>
<VersionSuffix>beta.4</VersionSuffix>

<TargetFrameworks>net8.0;net6.0;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net9.0;net8.0;net6.0;netstandard2.0</TargetFrameworks>
<LangVersion>latest</LangVersion>

<!-- Generate an XML documentation file for the project. -->
Expand Down