openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.6.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/Audio/GenerateSpeechTests.cs

86lines · modecode

1using Microsoft.ClientModel.TestFramework;
2using NUnit.Framework;
3using OpenAI.Audio;
4using OpenAI.Tests.Utility;
5using System;
6using System.Text;
7using System.Threading.Tasks;
8using static OpenAI.Tests.TestHelpers;
9
10namespace OpenAI.Tests.Audio;
11
12[Category("Audio")]
13public partial class GenerateSpeechTests : OpenAIRecordedTestBase
14{
15 public GenerateSpeechTests(bool isAsync) : base(isAsync)
16 {
17 }
18
19 [RecordedTest]
20 public async Task BasicTextToSpeechWorks()
21 {
22 AudioClient client = GetProxiedOpenAIClient<AudioClient>(TestScenario.Audio_TTS);
23
24 BinaryData audio = await client.GenerateSpeechAsync("Hello, world! This is a test.", GeneratedSpeechVoice.Shimmer);
25
26 Assert.That(audio, Is.Not.Null);
27 await ValidateGeneratedAudio(audio, "hello");
28 }
29
30 [RecordedTest]
31 [TestCase(null)]
32 [TestCase("mp3")]
33 [TestCase("opus")]
34 [TestCase("aac")]
35 [TestCase("flac")]
36 [TestCase("wav")]
37 [TestCase("pcm")]
38 public async Task OutputFormatWorks(string responseFormat)
39 {
40 AudioClient client = GetProxiedOpenAIClient<AudioClient>(TestScenario.Audio_TTS);
41
42 SpeechGenerationOptions options = new();
43
44 if (!string.IsNullOrEmpty(responseFormat))
45 {
46 options.ResponseFormat = responseFormat switch
47 {
48 "mp3" => GeneratedSpeechFormat.Mp3,
49 "opus" => GeneratedSpeechFormat.Opus,
50 "aac" => GeneratedSpeechFormat.Aac,
51 "flac" => GeneratedSpeechFormat.Flac,
52 "wav" => GeneratedSpeechFormat.Wav,
53 "pcm" => GeneratedSpeechFormat.Pcm,
54 _ => throw new ArgumentException("Invalid response format")
55 };
56 }
57
58 BinaryData audio = await client.GenerateSpeechAsync("Hello, world!", GeneratedSpeechVoice.Alloy, options);
59
60 Assert.That(audio, Is.Not.Null);
61
62 byte[] audioBytes = audio.ToArray();
63 byte[] expectedFileHeader = responseFormat switch
64 {
65 "opus" => Encoding.ASCII.GetBytes("OggS"),
66 "flac" => Encoding.ASCII.GetBytes("fLaC"),
67 "wav" => Encoding.ASCII.GetBytes("RIFF"),
68 _ => []
69 };
70
71 Assert.That(audioBytes.Length, Is.GreaterThanOrEqualTo(expectedFileHeader.Length));
72
73 for (int i = 0; i < expectedFileHeader.Length; i++)
74 {
75 Assert.That(audioBytes[i], Is.EqualTo(expectedFileHeader[i]), $"File header differs on byte {i}. Expected: {expectedFileHeader[i]}. Actual: {audioBytes[i]}.");
76 }
77 }
78
79 private async Task ValidateGeneratedAudio(BinaryData audio, string expectedSubstring)
80 {
81 AudioClient client = GetProxiedOpenAIClient<AudioClient>(TestScenario.Audio_Whisper);
82 AudioTranscription transcription = await client.TranscribeAudioAsync(audio.ToStream(), "hello_world.wav");
83
84 Assert.That(transcription.Text.ToLowerInvariant(), Contains.Substring(expectedSubstring));
85 }
86}
87