2424
2525import org .springframework .ai .openai .api .common .ApiUtils ;
2626import org .springframework .core .io .ByteArrayResource ;
27+ import org .springframework .http .ResponseEntity ;
2728import org .springframework .util .Assert ;
2829import org .springframework .util .LinkedMultiValueMap ;
2930import org .springframework .util .MultiValueMap ;
@@ -561,18 +562,19 @@ public record Segment(
561562 /**
562563 * Request to generates audio from the input text.
563564 * @param requestBody The request body.
564- * @return The audio file in bytes .
565+ * @return Response entity containing the audio binary .
565566 */
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 );
568569 }
569570
570571 /**
571572 * Transcribes audio into the input language.
572573 * @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.
574576 */
575- public Object createTranscription (TranscriptionRequest requestBody ) {
577+ public ResponseEntity <?> createTranscription (TranscriptionRequest requestBody ) {
576578 return createTranscription (requestBody , requestBody .responseFormat ().getResponseType ());
577579 }
578580
@@ -582,9 +584,9 @@ public Object createTranscription(TranscriptionRequest requestBody) {
582584 * @param <T> The response type.
583585 * @param requestBody The request body.
584586 * @param responseType The response type class.
585- * @return The transcribed text.
587+ * @return Response entity containing the transcribed text in the responseType format .
586588 */
587- public <T > T createTranscription (TranscriptionRequest requestBody , Class <T > responseType ) {
589+ public <T > ResponseEntity < T > createTranscription (TranscriptionRequest requestBody , Class <T > responseType ) {
588590
589591 MultiValueMap <String , Object > multipartBody = new LinkedMultiValueMap <>();
590592 multipartBody .add ("file" , new ByteArrayResource (requestBody .file ()) {
@@ -604,15 +606,20 @@ public String getFilename() {
604606 multipartBody .add ("timestamp_granularities[]" , requestBody .granularityType ().getValue ());
605607 }
606608
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 );
608614 }
609615
610616 /**
611617 * Translates audio into English.
612618 * @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.
614621 */
615- public Object createTranslation (TranslationRequest requestBody ) {
622+ public ResponseEntity <?> createTranslation (TranslationRequest requestBody ) {
616623 return createTranslation (requestBody , requestBody .responseFormat ().getResponseType ());
617624 }
618625
@@ -622,9 +629,9 @@ public Object createTranslation(TranslationRequest requestBody) {
622629 * @param <T> The response type.
623630 * @param requestBody The request body.
624631 * @param responseType The response type class.
625- * @return The transcribed text.
632+ * @return Response entity containing the transcribed text in the responseType format .
626633 */
627- public <T > T createTranslation (TranslationRequest requestBody , Class <T > responseType ) {
634+ public <T > ResponseEntity < T > createTranslation (TranslationRequest requestBody , Class <T > responseType ) {
628635
629636 MultiValueMap <String , Object > multipartBody = new LinkedMultiValueMap <>();
630637 multipartBody .add ("file" , new ByteArrayResource (requestBody .file ()) {
@@ -638,7 +645,11 @@ public String getFilename() {
638645 multipartBody .add ("response_format" , requestBody .responseFormat ().getValue ());
639646 multipartBody .add ("temperature" , requestBody .temperature ());
640647
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 );
642653 }
643654
644655}
0 commit comments