56
56
*
57
57
* @author Michael Lavelle
58
58
* @author Christian Tzolov
59
+ * @author Thomas Vitale
59
60
* @see OpenAiAudioApi
60
61
* @since 0.8.1
61
62
*/
@@ -65,7 +66,7 @@ public class OpenAiAudioTranscriptionModel implements Model<AudioTranscriptionPr
65
66
66
67
private final OpenAiAudioTranscriptionOptions defaultOptions ;
67
68
68
- public final RetryTemplate retryTemplate ;
69
+ private final RetryTemplate retryTemplate ;
69
70
70
71
private final OpenAiAudioApi audioApi ;
71
72
@@ -80,8 +81,7 @@ public OpenAiAudioTranscriptionModel(OpenAiAudioApi audioApi) {
80
81
.withModel (OpenAiAudioApi .WhisperModel .WHISPER_1 .getValue ())
81
82
.withResponseFormat (OpenAiAudioApi .TranscriptResponseFormat .JSON )
82
83
.withTemperature (0.7f )
83
- .build (),
84
- RetryUtils .DEFAULT_RETRY_TEMPLATE );
84
+ .build ());
85
85
}
86
86
87
87
/**
@@ -119,74 +119,71 @@ public String call(Resource audioResource) {
119
119
}
120
120
121
121
@ Override
122
- public AudioTranscriptionResponse call (AudioTranscriptionPrompt request ) {
122
+ public AudioTranscriptionResponse call (AudioTranscriptionPrompt transcriptionPrompt ) {
123
123
124
- return this . retryTemplate . execute ( ctx -> {
124
+ Resource audioResource = transcriptionPrompt . getInstructions ();
125
125
126
- Resource audioResource = request . getInstructions ( );
126
+ OpenAiAudioApi . TranscriptionRequest request = createRequest ( transcriptionPrompt );
127
127
128
- OpenAiAudioApi . TranscriptionRequest requestBody = createRequestBody (request );
128
+ if (request . responseFormat (). isJsonType ()) {
129
129
130
- if (requestBody .responseFormat ().isJsonType ()) {
130
+ ResponseEntity <StructuredResponse > transcriptionEntity = this .retryTemplate
131
+ .execute (ctx -> this .audioApi .createTranscription (request , StructuredResponse .class ));
131
132
132
- ResponseEntity <StructuredResponse > transcriptionEntity = this .audioApi .createTranscription (requestBody ,
133
- StructuredResponse .class );
133
+ var transcription = transcriptionEntity .getBody ();
134
134
135
- var transcription = transcriptionEntity .getBody ();
136
-
137
- if (transcription == null ) {
138
- logger .warn ("No transcription returned for request: {}" , audioResource );
139
- return new AudioTranscriptionResponse (null );
140
- }
135
+ if (transcription == null ) {
136
+ logger .warn ("No transcription returned for request: {}" , audioResource );
137
+ return new AudioTranscriptionResponse (null );
138
+ }
141
139
142
- AudioTranscription transcript = new AudioTranscription (transcription .text ());
140
+ AudioTranscription transcript = new AudioTranscription (transcription .text ());
143
141
144
- RateLimit rateLimits = OpenAiResponseHeaderExtractor .extractAiResponseHeaders (transcriptionEntity );
142
+ RateLimit rateLimits = OpenAiResponseHeaderExtractor .extractAiResponseHeaders (transcriptionEntity );
145
143
146
- return new AudioTranscriptionResponse (transcript ,
147
- OpenAiAudioTranscriptionResponseMetadata .from (transcriptionEntity .getBody ())
148
- .withRateLimit (rateLimits ));
144
+ return new AudioTranscriptionResponse (transcript ,
145
+ OpenAiAudioTranscriptionResponseMetadata .from (transcriptionEntity .getBody ())
146
+ .withRateLimit (rateLimits ));
149
147
150
- }
151
- else {
148
+ }
149
+ else {
152
150
153
- ResponseEntity <String > transcriptionEntity = this .audioApi . createTranscription ( requestBody ,
154
- String .class );
151
+ ResponseEntity <String > transcriptionEntity = this .retryTemplate
152
+ . execute ( ctx -> this . audioApi . createTranscription ( request , String .class ) );
155
153
156
- var transcription = transcriptionEntity .getBody ();
154
+ var transcription = transcriptionEntity .getBody ();
157
155
158
- if (transcription == null ) {
159
- logger .warn ("No transcription returned for request: {}" , audioResource );
160
- return new AudioTranscriptionResponse (null );
161
- }
156
+ if (transcription == null ) {
157
+ logger .warn ("No transcription returned for request: {}" , audioResource );
158
+ return new AudioTranscriptionResponse (null );
159
+ }
162
160
163
- AudioTranscription transcript = new AudioTranscription (transcription );
161
+ AudioTranscription transcript = new AudioTranscription (transcription );
164
162
165
- RateLimit rateLimits = OpenAiResponseHeaderExtractor .extractAiResponseHeaders (transcriptionEntity );
163
+ RateLimit rateLimits = OpenAiResponseHeaderExtractor .extractAiResponseHeaders (transcriptionEntity );
166
164
167
- return new AudioTranscriptionResponse (transcript ,
168
- OpenAiAudioTranscriptionResponseMetadata .from (transcriptionEntity .getBody ())
169
- .withRateLimit (rateLimits ));
170
- }
171
- });
165
+ return new AudioTranscriptionResponse (transcript ,
166
+ OpenAiAudioTranscriptionResponseMetadata .from (transcriptionEntity .getBody ())
167
+ .withRateLimit (rateLimits ));
168
+ }
172
169
}
173
170
174
- OpenAiAudioApi .TranscriptionRequest createRequestBody (AudioTranscriptionPrompt request ) {
171
+ OpenAiAudioApi .TranscriptionRequest createRequest (AudioTranscriptionPrompt transcriptionPrompt ) {
175
172
176
173
OpenAiAudioTranscriptionOptions options = this .defaultOptions ;
177
174
178
- if (request .getOptions () != null ) {
179
- if (request .getOptions () instanceof OpenAiAudioTranscriptionOptions runtimeOptions ) {
175
+ if (transcriptionPrompt .getOptions () != null ) {
176
+ if (transcriptionPrompt .getOptions () instanceof OpenAiAudioTranscriptionOptions runtimeOptions ) {
180
177
options = this .merge (runtimeOptions , options );
181
178
}
182
179
else {
183
180
throw new IllegalArgumentException ("Prompt options are not of type TranscriptionOptions: "
184
- + request .getOptions ().getClass ().getSimpleName ());
181
+ + transcriptionPrompt .getOptions ().getClass ().getSimpleName ());
185
182
}
186
183
}
187
184
188
185
return OpenAiAudioApi .TranscriptionRequest .builder ()
189
- .withFile (toBytes (request .getInstructions ()))
186
+ .withFile (toBytes (transcriptionPrompt .getInstructions ()))
190
187
.withResponseFormat (options .getResponseFormat ())
191
188
.withPrompt (options .getPrompt ())
192
189
.withTemperature (options .getTemperature ())
0 commit comments