2828use OCP \TaskProcessing \ShapeEnumValue ;
2929use Psr \Log \LoggerInterface ;
3030use RuntimeException ;
31+ use Throwable ;
32+ use function json_encode ;
3133
3234/**
3335 * Service to make requests to OpenAI/LocalAI REST API
@@ -132,7 +134,7 @@ public function getModels(string $userId): array {
132134 throw $ e ;
133135 }
134136 if (isset ($ modelsResponse ['error ' ])) {
135- $ this ->logger ->warning ('Error retrieving models: ' . \ json_encode ($ modelsResponse ));
137+ $ this ->logger ->warning ('Error retrieving models: ' . json_encode ($ modelsResponse ));
136138 $ this ->areCredsValid = false ;
137139 throw new Exception ($ modelsResponse ['error ' ], Http::STATUS_INTERNAL_SERVER_ERROR );
138140 }
@@ -142,7 +144,7 @@ public function getModels(string $userId): array {
142144 }
143145
144146 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 ));
146148 $ this ->areCredsValid = false ;
147149 throw new Exception ($ this ->l10n ->t ('Invalid models response received ' ), Http::STATUS_INTERNAL_SERVER_ERROR );
148150 }
@@ -185,7 +187,7 @@ public function getModelEnumValues(?string $userId): array {
185187 array_unshift ($ modelEnumValues , new ShapeEnumValue ($ this ->l10n ->t ('Default ' ), 'Default ' ));
186188 }
187189 return $ modelEnumValues ;
188- } catch (\ Throwable $ e ) {
190+ } catch (Throwable $ e ) {
189191 // avoid flooding the logs with errors from calls of task processing
190192 $ this ->logger ->info ('Error getting model enum values ' , ['exception ' => $ e ]);
191193 return [];
@@ -767,7 +769,7 @@ public function requestSpeechCreation(
767769 'input ' => $ prompt ,
768770 'voice ' => $ voice === Application::DEFAULT_MODEL_ID ? Application::DEFAULT_SPEECH_VOICE : $ voice ,
769771 'model ' => $ model === Application::DEFAULT_MODEL_ID ? Application::DEFAULT_SPEECH_MODEL_ID : $ model ,
770- 'response_format ' => 'wav ' ,
772+ 'response_format ' => 'mp3 ' ,
771773 'speed ' => $ speed ,
772774 ];
773775
@@ -933,18 +935,16 @@ public function request(?string $userId, string $endPoint, array $params = [], s
933935
934936 if ($ respCode >= 400 ) {
935937 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 ' ];
946944 }
945+ return $ parsedBody ;
947946 }
947+ return ['body ' => $ body ];
948948 } catch (ClientException |ServerException $ e ) {
949949 $ responseBody = $ e ->getResponse ()->getBody ();
950950 $ parsedResponseBody = json_decode ($ responseBody , true );
0 commit comments