24
24
25
25
import org .springframework .ai .openai .api .common .ApiUtils ;
26
26
import org .springframework .core .io .ByteArrayResource ;
27
+ import org .springframework .http .ResponseEntity ;
27
28
import org .springframework .util .Assert ;
28
29
import org .springframework .util .LinkedMultiValueMap ;
29
30
import org .springframework .util .MultiValueMap ;
@@ -561,18 +562,19 @@ public record Segment(
561
562
/**
562
563
* Request to generates audio from the input text.
563
564
* @param requestBody The request body.
564
- * @return The audio file in bytes .
565
+ * @return Response entity containing the audio binary .
565
566
*/
566
- public byte [] createSpeech (SpeechRequest requestBody ) {
567
- return this .restClient .post ().uri ("/v1/audio/speech" ).body (requestBody ).retrieve ().body (byte [].class );
567
+ public ResponseEntity < byte []> createSpeech (SpeechRequest requestBody ) {
568
+ return this .restClient .post ().uri ("/v1/audio/speech" ).body (requestBody ).retrieve ().toEntity (byte [].class );
568
569
}
569
570
570
571
/**
571
572
* Transcribes audio into the input language.
572
573
* @param requestBody The request body.
573
- * @return The transcribed text.
574
+ * @return Response entity containing the transcribed text in either json or text
575
+ * format.
574
576
*/
575
- public Object createTranscription (TranscriptionRequest requestBody ) {
577
+ public ResponseEntity <?> createTranscription (TranscriptionRequest requestBody ) {
576
578
return createTranscription (requestBody , requestBody .responseFormat ().getResponseType ());
577
579
}
578
580
@@ -582,9 +584,9 @@ public Object createTranscription(TranscriptionRequest requestBody) {
582
584
* @param <T> The response type.
583
585
* @param requestBody The request body.
584
586
* @param responseType The response type class.
585
- * @return The transcribed text.
587
+ * @return Response entity containing the transcribed text in the responseType format .
586
588
*/
587
- public <T > T createTranscription (TranscriptionRequest requestBody , Class <T > responseType ) {
589
+ public <T > ResponseEntity < T > createTranscription (TranscriptionRequest requestBody , Class <T > responseType ) {
588
590
589
591
MultiValueMap <String , Object > multipartBody = new LinkedMultiValueMap <>();
590
592
multipartBody .add ("file" , new ByteArrayResource (requestBody .file ()) {
@@ -604,15 +606,20 @@ public String getFilename() {
604
606
multipartBody .add ("timestamp_granularities[]" , requestBody .granularityType ().getValue ());
605
607
}
606
608
607
- return this .restClient .post ().uri ("/v1/audio/transcriptions" ).body (multipartBody ).retrieve ().body (responseType );
609
+ return this .restClient .post ()
610
+ .uri ("/v1/audio/transcriptions" )
611
+ .body (multipartBody )
612
+ .retrieve ()
613
+ .toEntity (responseType );
608
614
}
609
615
610
616
/**
611
617
* Translates audio into English.
612
618
* @param requestBody The request body.
613
- * @return The transcribed text.
619
+ * @return Response entity containing the transcribed text in either json or text
620
+ * format.
614
621
*/
615
- public Object createTranslation (TranslationRequest requestBody ) {
622
+ public ResponseEntity <?> createTranslation (TranslationRequest requestBody ) {
616
623
return createTranslation (requestBody , requestBody .responseFormat ().getResponseType ());
617
624
}
618
625
@@ -622,9 +629,9 @@ public Object createTranslation(TranslationRequest requestBody) {
622
629
* @param <T> The response type.
623
630
* @param requestBody The request body.
624
631
* @param responseType The response type class.
625
- * @return The transcribed text.
632
+ * @return Response entity containing the transcribed text in the responseType format .
626
633
*/
627
- public <T > T createTranslation (TranslationRequest requestBody , Class <T > responseType ) {
634
+ public <T > ResponseEntity < T > createTranslation (TranslationRequest requestBody , Class <T > responseType ) {
628
635
629
636
MultiValueMap <String , Object > multipartBody = new LinkedMultiValueMap <>();
630
637
multipartBody .add ("file" , new ByteArrayResource (requestBody .file ()) {
@@ -638,7 +645,11 @@ public String getFilename() {
638
645
multipartBody .add ("response_format" , requestBody .responseFormat ().getValue ());
639
646
multipartBody .add ("temperature" , requestBody .temperature ());
640
647
641
- return this .restClient .post ().uri ("/v1/audio/translations" ).body (multipartBody ).retrieve ().body (responseType );
648
+ return this .restClient .post ()
649
+ .uri ("/v1/audio/translations" )
650
+ .body (multipartBody )
651
+ .retrieve ()
652
+ .toEntity (responseType );
642
653
}
643
654
644
655
}
0 commit comments