@@ -108,6 +108,38 @@ func TestAPI(t *testing.T) {
108108 },
109109 )
110110 checks .NoError (t , err , "CreateChatCompletion (with functions) returned error" )
111+
112+ response , err := c .CreateChatCompletion (
113+ ctx ,
114+ openai.ChatCompletionRequest {
115+ Model : openai .GPT4oAudioPreview ,
116+ Messages : []openai.ChatCompletionMessage {
117+ {
118+ Role : openai .ChatMessageRoleUser ,
119+ Content : "hi" ,
120+ },
121+ },
122+ Audio : & openai.AudioOutput {
123+ Voice : openai .AudioVoiceAlloy ,
124+ Format : openai .AudioFormatPCM16 ,
125+ },
126+ Modalities : []openai.Modality {openai .ModalityText , openai .ModalityAudio },
127+ },
128+ )
129+ checks .NoError (t , err , "CreateChatCompletion (with audio) returned error" )
130+ if response .Choices [0 ].Message .Audio == nil {
131+ t .Fatal ("Audio response is nil" )
132+ }
133+ if len (response .Choices [0 ].Message .Audio .Data ) == 0 {
134+ t .Fatal ("Audio response data is empty" )
135+ }
136+ if response .Choices [0 ].Message .Audio .Transcript == "" {
137+ t .Fatal ("Audio response transcript is empty" )
138+ }
139+ if response .Usage .PromptTokens == 0 || response .Usage .CompletionTokens == 0 || response .Usage .TotalTokens == 0 {
140+ t .Fatal ("Usage is zero" )
141+ }
142+ t .Logf ("Usage: %+v" , response .Usage )
111143}
112144
113145func TestCompletionStream (t * testing.T ) {
@@ -145,6 +177,60 @@ func TestCompletionStream(t *testing.T) {
145177 }
146178}
147179
180+ func TestChatCompletionStream (t * testing.T ) {
181+ apiToken := os .Getenv ("OPENAI_TOKEN" )
182+ if apiToken == "" {
183+ t .Skip ("Skipping testing against production OpenAI API. Set OPENAI_TOKEN environment variable to enable it." )
184+ }
185+
186+ c := openai .NewClient (apiToken )
187+ ctx := context .Background ()
188+
189+ stream , err := c .CreateChatCompletionStream (ctx , openai.ChatCompletionRequest {
190+ Model : openai .GPT4oAudioPreview ,
191+ Messages : []openai.ChatCompletionMessage {
192+ {
193+ Role : openai .ChatMessageRoleUser ,
194+ Content : "hi" ,
195+ },
196+ },
197+ Audio : & openai.AudioOutput {
198+ Voice : openai .AudioVoiceAlloy ,
199+ Format : openai .AudioFormatPCM16 ,
200+ },
201+ Modalities : []openai.Modality {openai .ModalityText , openai .ModalityAudio },
202+ StreamOptions : & openai.StreamOptions {
203+ IncludeUsage : true ,
204+ },
205+ })
206+ checks .NoError (t , err , "CreateCompletionStream returned error" )
207+ defer stream .Close ()
208+
209+ var usage * openai.Usage
210+ counter := 0
211+ for {
212+ response , err := stream .Recv ()
213+ if err != nil {
214+ if errors .Is (err , io .EOF ) {
215+ break
216+ }
217+ t .Errorf ("Stream error: %v" , err )
218+ } else {
219+ counter ++
220+ }
221+ if response .Usage != nil {
222+ usage = response .Usage
223+ t .Logf ("Usage: %+v" , usage )
224+ }
225+ }
226+ if counter == 0 {
227+ t .Error ("Stream did not return any responses" )
228+ }
229+ if usage == nil {
230+ t .Error ("Usage is nil" )
231+ }
232+ }
233+
148234func TestAPIError (t * testing.T ) {
149235 apiToken := os .Getenv ("OPENAI_TOKEN" )
150236 if apiToken == "" {
0 commit comments