Skip to content

Commit e546ce0

Browse files
authored
Update csharp.md
1 parent 3f5d23c commit e546ce0

File tree

1 file changed

+50
-25
lines changed
  • articles/ai-services/speech-service/includes/quickstarts/openai-speech

1 file changed

+50
-25
lines changed

articles/ai-services/speech-service/includes/quickstarts/openai-speech/csharp.md

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Follow these steps to create a new console application.
4141
```
4242
1. Replace the contents of `Program.cs` with the following code.
4343
44-
```csharp
44+
```csharp
4545
using System;
4646
using System.IO;
4747
using System.Threading.Tasks;
@@ -64,42 +64,67 @@ Follow these steps to create a new console application.
6464
// This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
6565
static string speechKey = Environment.GetEnvironmentVariable("SPEECH_KEY");
6666
static string speechRegion = Environment.GetEnvironmentVariable("SPEECH_REGION");
67-
67+
68+
// Sentence end symbols for splitting the response into sentences.
69+
static List<string> sentenceSaperators = new() { ".", "!", "?", ";", "。", "!", "?", ";", "\n" };
70+
71+
private static object consoleLock = new();
72+
6873
// Prompts Azure OpenAI with a request and synthesizes the response.
6974
async static Task AskOpenAI(string prompt)
7075
{
76+
var speechConfig = SpeechConfig.FromSubscription(speechKey, speechRegion);
77+
// The language of the voice that speaks.
78+
speechConfig.SpeechSynthesisVoiceName = "en-US-JennyMultilingualNeural";
79+
var audioOutputConfig = AudioConfig.FromDefaultSpeakerOutput();
80+
using var speechSynthesizer = new SpeechSynthesizer(speechConfig, audioOutputConfig);
81+
speechSynthesizer.Synthesizing += (sender, args) =>
82+
{
83+
lock (consoleLock)
84+
{
85+
Console.ForegroundColor = ConsoleColor.Yellow;
86+
Console.Write($"[Audio]");
87+
Console.ResetColor();
88+
}
89+
};
90+
7191
// Ask Azure OpenAI
7292
OpenAIClient client = new(new Uri(openAIEndpoint), new AzureKeyCredential(openAIKey));
7393
var completionsOptions = new CompletionsOptions()
7494
{
7595
Prompts = { prompt },
7696
MaxTokens = 100,
97+
7798
};
78-
Response<Completions> completionsResponse = client.GetCompletions(engine, completionsOptions);
79-
string text = completionsResponse.Value.Choices[0].Text.Trim();
80-
Console.WriteLine($"Azure OpenAI response: {text}");
81-
82-
var speechConfig = SpeechConfig.FromSubscription(speechKey, speechRegion);
83-
// The language of the voice that speaks.
84-
speechConfig.SpeechSynthesisVoiceName = "en-US-JennyMultilingualNeural";
85-
var audioOutputConfig = AudioConfig.FromDefaultSpeakerOutput();
86-
87-
using (var speechSynthesizer = new SpeechSynthesizer(speechConfig, audioOutputConfig))
99+
var responseStream = await client.GetCompletionsStreamingAsync(engine, completionsOptions);
100+
using var streamingCompletions = responseStream.Value;
101+
StringBuilder gptBuffer = new();
102+
await foreach (var choice in streamingCompletions.GetChoicesStreaming())
88103
{
89-
var speechSynthesisResult = await speechSynthesizer.SpeakTextAsync(text).ConfigureAwait(true);
90-
91-
if (speechSynthesisResult.Reason == ResultReason.SynthesizingAudioCompleted)
104+
await foreach (var message in choice.GetTextStreaming())
92105
{
93-
Console.WriteLine($"Speech synthesized to speaker for text: [{text}]");
94-
}
95-
else if (speechSynthesisResult.Reason == ResultReason.Canceled)
96-
{
97-
var cancellationDetails = SpeechSynthesisCancellationDetails.FromResult(speechSynthesisResult);
98-
Console.WriteLine($"Speech synthesis canceled: {cancellationDetails.Reason}");
99-
100-
if (cancellationDetails.Reason == CancellationReason.Error)
106+
if (string.IsNullOrEmpty(message))
107+
{
108+
continue;
109+
}
110+
111+
lock (consoleLock)
101112
{
102-
Console.WriteLine($"Error details: {cancellationDetails.ErrorDetails}");
113+
Console.ForegroundColor = ConsoleColor.DarkBlue;
114+
Console.Write($"{message}");
115+
Console.ResetColor();
116+
}
117+
118+
gptBuffer.Append(message);
119+
120+
if (sentenceSaperators.Any(message.Contains))
121+
{
122+
var sentence = gptBuffer.ToString().Trim();
123+
if (!string.IsNullOrEmpty(sentence))
124+
{
125+
await speechSynthesizer.SpeakTextAsync(sentence).ConfigureAwait(true);
126+
gptBuffer.Clear();
127+
}
103128
}
104129
}
105130
}
@@ -164,7 +189,7 @@ Follow these steps to create a new console application.
164189
}
165190
}
166191
}
167-
```
192+
```
168193

169194
1. To increase or decrease the number of tokens returned by Azure OpenAI, change the `MaxTokens` property in the `CompletionsOptions` class instance. For more information tokens and cost implications, see [Azure OpenAI tokens](/azure/ai-services/openai/overview#tokens) and [Azure OpenAI pricing](https://azure.microsoft.com/pricing/details/cognitive-services/openai-service/).
170195

0 commit comments

Comments
 (0)