1
1
package com .vansisto .aiimagebot .services .openai ;
2
2
3
3
import com .vansisto .aiimagebot .exceptions .ImageGenerationRequestException ;
4
+ import com .vansisto .aiimagebot .exceptions .JsonRequestFormingException ;
4
5
import com .vansisto .aiimagebot .exceptions .TranscriptionRequestException ;
6
+ import com .vansisto .aiimagebot .exceptions .TranslationRequestException ;
5
7
import com .vansisto .aiimagebot .services .openai .requests .ImageGenerationRequestUtil ;
6
8
import com .vansisto .aiimagebot .services .openai .requests .TranscriptionRequestUtil ;
9
+ import com .vansisto .aiimagebot .services .openai .requests .TranslationRequestUtil ;
7
10
import com .vansisto .aiimagebot .services .settings .UserSetting ;
8
11
import lombok .extern .slf4j .Slf4j ;
9
12
import okhttp3 .OkHttpClient ;
10
13
import okhttp3 .Request ;
11
14
import okhttp3 .Response ;
15
+ import org .springframework .boot .configurationprocessor .json .JSONException ;
16
+ import org .springframework .boot .configurationprocessor .json .JSONObject ;
12
17
13
18
import java .io .File ;
14
19
import java .io .IOException ;
@@ -27,21 +32,40 @@ private RequestsExecutor(){}
27
32
28
33
public static String sendTranscriptionRequest (String token , File file ) {
29
34
Request request = TranscriptionRequestUtil .createRequest (token , file );
30
- log .info ("TranscriptionRequestUtil request sent..." );
35
+ log .info ("Transcription request sent..." );
31
36
try (Response response = client .newCall (request ).execute ()) {
32
- return response . isSuccessful () ? response . body (). string () : "Fail: " + response . message ( );
37
+ return getResponseResult ( response );
33
38
} catch (IOException e ) {
34
39
throw new TranscriptionRequestException ();
35
40
}
36
41
}
37
42
38
43
public static String generateImage (String messageText , UserSetting setting ) {
39
44
Request request = ImageGenerationRequestUtil .createRequest (setting .getOpenAiApiKey (), messageText , setting .getNumberOfPictures (), setting .getSize ());
40
- log .info ("Image generating request sent..." );
41
45
try (Response response = client .newCall (request ).execute ()) {
42
- return response .isSuccessful () ? response .body ().string () : "Fail: " + response .message ();
46
+ String responseJson = getResponseResult (response );
47
+ JSONObject jsonResponse = new JSONObject (responseJson );
48
+ return jsonResponse .getJSONArray ("data" ).toString ().replace ("\\ /" , "/" );
43
49
} catch (IOException e ) {
44
50
throw new ImageGenerationRequestException ();
51
+ } catch (JSONException e ) {
52
+ throw new JsonRequestFormingException ();
45
53
}
46
54
}
55
+
56
+ public static String translate (String messageText , UserSetting setting ) {
57
+ Request request = TranslationRequestUtil .createRequest (setting .getOpenAiApiKey (), messageText );
58
+ log .info ("Translation request sent..." );
59
+ try (Response response = client .newCall (request ).execute ()) {
60
+ return getResponseResult (response );
61
+ } catch (IOException e ) {
62
+ throw new TranslationRequestException ();
63
+ }
64
+ }
65
+
66
+ private static String getResponseResult (Response response ) throws IOException {
67
+ return response .isSuccessful ()
68
+ ? response .body ().string ().trim ()
69
+ : "Fail (" + response .code () + "): " + response .message ();
70
+ }
47
71
}
0 commit comments