28
28
use OCP \TaskProcessing \ShapeEnumValue ;
29
29
use Psr \Log \LoggerInterface ;
30
30
use RuntimeException ;
31
+ use Throwable ;
32
+ use function json_encode ;
31
33
32
34
/**
33
35
* Service to make requests to OpenAI/LocalAI REST API
@@ -132,7 +134,7 @@ public function getModels(string $userId): array {
132
134
throw $ e ;
133
135
}
134
136
if (isset ($ modelsResponse ['error ' ])) {
135
- $ this ->logger ->warning ('Error retrieving models: ' . \ json_encode ($ modelsResponse ));
137
+ $ this ->logger ->warning ('Error retrieving models: ' . json_encode ($ modelsResponse ));
136
138
$ this ->areCredsValid = false ;
137
139
throw new Exception ($ modelsResponse ['error ' ], Http::STATUS_INTERNAL_SERVER_ERROR );
138
140
}
@@ -142,7 +144,7 @@ public function getModels(string $userId): array {
142
144
}
143
145
144
146
if (!$ this ->isModelListValid ($ modelsResponse ['data ' ])) {
145
- $ this ->logger ->warning ('Invalid models response: ' . \ json_encode ($ modelsResponse ));
147
+ $ this ->logger ->warning ('Invalid models response: ' . json_encode ($ modelsResponse ));
146
148
$ this ->areCredsValid = false ;
147
149
throw new Exception ($ this ->l10n ->t ('Invalid models response received ' ), Http::STATUS_INTERNAL_SERVER_ERROR );
148
150
}
@@ -185,7 +187,7 @@ public function getModelEnumValues(?string $userId): array {
185
187
array_unshift ($ modelEnumValues , new ShapeEnumValue ($ this ->l10n ->t ('Default ' ), 'Default ' ));
186
188
}
187
189
return $ modelEnumValues ;
188
- } catch (\ Throwable $ e ) {
190
+ } catch (Throwable $ e ) {
189
191
// avoid flooding the logs with errors from calls of task processing
190
192
$ this ->logger ->info ('Error getting model enum values ' , ['exception ' => $ e ]);
191
193
return [];
@@ -767,7 +769,7 @@ public function requestSpeechCreation(
767
769
'input ' => $ prompt ,
768
770
'voice ' => $ voice === Application::DEFAULT_MODEL_ID ? Application::DEFAULT_SPEECH_VOICE : $ voice ,
769
771
'model ' => $ model === Application::DEFAULT_MODEL_ID ? Application::DEFAULT_SPEECH_MODEL_ID : $ model ,
770
- 'response_format ' => 'wav ' ,
772
+ 'response_format ' => 'mp3 ' ,
771
773
'speed ' => $ speed ,
772
774
];
773
775
@@ -933,18 +935,16 @@ public function request(?string $userId, string $endPoint, array $params = [], s
933
935
934
936
if ($ respCode >= 400 ) {
935
937
return ['error ' => $ this ->l10n ->t ('Bad credentials ' )];
936
- } else {
937
- if ($ response ->getHeader ('Content-Type ' ) === 'application/json ' ) {
938
- $ parsedBody = json_decode ($ body , true );
939
- if ($ parsedBody === null ) {
940
- $ this ->logger ->warning ('Could not JSON parse the response ' , ['body ' => $ body ]);
941
- return ['error ' => 'Could not JSON parse the response ' ];
942
- }
943
- return $ parsedBody ;
944
- } else {
945
- return ['body ' => $ body ];
938
+ }
939
+ if ($ response ->getHeader ('Content-Type ' ) === 'application/json ' ) {
940
+ $ parsedBody = json_decode ($ body , true );
941
+ if ($ parsedBody === null ) {
942
+ $ this ->logger ->warning ('Could not JSON parse the response ' , ['body ' => $ body ]);
943
+ return ['error ' => 'Could not JSON parse the response ' ];
946
944
}
945
+ return $ parsedBody ;
947
946
}
947
+ return ['body ' => $ body ];
948
948
} catch (ClientException |ServerException $ e ) {
949
949
$ responseBody = $ e ->getResponse ()->getBody ();
950
950
$ parsedResponseBody = json_decode ($ responseBody , true );
0 commit comments