Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions lib/Service/OpenAiAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public function getModels(string $userId): array {
$this->modelResponseCache = $response;
$this->logger->debug('Actually getting OpenAI models with a network request');
}
if (!isset($response['data'])) {
if (!is_array($response) || !isset($response['data'])) {
$this->modelResponseCache = null;
$this->logger->warning('Error retrieving models: ' . json_encode($response));
throw new Exception($this->l10n->t('Unknown models error'), Http::STATUS_INTERNAL_SERVER_ERROR);
}
Expand Down Expand Up @@ -313,7 +314,7 @@ public function createCompletion(

$response = $this->request($userId, 'completions', $params, 'POST');

if (!isset($response['choices'])) {
if (!is_array($response) || !isset($response['choices'])) {
$this->logger->warning('Text generation error: ' . json_encode($response));
throw new Exception($this->l10n->t('Unknown text generation error'), Http::STATUS_INTERNAL_SERVER_ERROR);
}
Expand Down Expand Up @@ -410,7 +411,7 @@ public function createChatCompletion(

$response = $this->request($userId, 'chat/completions', $params, 'POST');

if (!isset($response['choices'])) {
if (!is_array($response) || !isset($response['choices'])) {
$this->logger->warning('Text generation error: ' . json_encode($response));
throw new Exception($this->l10n->t('Unknown text generation error'), Http::STATUS_INTERNAL_SERVER_ERROR);
}
Expand Down Expand Up @@ -522,9 +523,9 @@ public function transcribe(

$response = $this->request($userId, $endpoint, $params, 'POST', $contentType);

if (!isset($response['text'])) {
if (!is_array($response) || !isset($response['text'])) {
$this->logger->warning('Audio transcription error: ' . json_encode($response));
throw new Exception($this->l10n->t('Unknown audio trancription error'), Http::STATUS_INTERNAL_SERVER_ERROR);
throw new Exception($this->l10n->t('Unknown audio transcription error'), Http::STATUS_INTERNAL_SERVER_ERROR);
}

// Extract audio duration from response and store it as quota usage:
Expand Down Expand Up @@ -566,7 +567,7 @@ public function requestImageCreation(

$apiResponse = $this->request($userId, 'images/generations', $params, 'POST');

if (!isset($apiResponse['data']) || !is_array($apiResponse['data'])) {
if (!is_array($apiResponse) || !isset($apiResponse['data']) || !is_array($apiResponse['data'])) {
$this->logger->warning('OpenAI image generation error', ['api_response' => $apiResponse]);
throw new Exception($this->l10n->t('Unknown image generation error'), Http::STATUS_INTERNAL_SERVER_ERROR);

Expand Down Expand Up @@ -659,10 +660,10 @@ public function updateExpImgProcessingTime(int $runtime): void {
* @param array $params Query parameters (key/val pairs)
* @param string $method HTTP query method
* @param string|null $contentType
* @return array decoded request result or error
* @return array|resource decoded request result or error in array, or raw response body
* @throws Exception
*/
public function request(?string $userId, string $endPoint, array $params = [], string $method = 'GET', ?string $contentType = null): array {
public function request(?string $userId, string $endPoint, array $params = [], string $method = 'GET', ?string $contentType = null): mixed {
try {
$serviceUrl = $this->openAiSettingsService->getServiceUrl();
if ($serviceUrl === '') {
Expand All @@ -671,7 +672,11 @@ public function request(?string $userId, string $endPoint, array $params = [], s

$timeout = $this->openAiSettingsService->getRequestTimeout();

$url = $serviceUrl . '/v1/' . $endPoint;
if (str_starts_with($endPoint, 'http://') || str_starts_with($endPoint, 'https://')) {
$url = $endPoint;
} else {
$url = $serviceUrl . '/v1/' . $endPoint;
}
$options = [
'timeout' => $timeout,
'headers' => [
Expand Down Expand Up @@ -755,9 +760,12 @@ public function request(?string $userId, string $endPoint, array $params = [], s

if ($respCode >= 400) {
return ['error' => $this->l10n->t('Bad credentials')];
} else {
}
$this->logger->warning('the booddydyyy', ['body' => $body, 'respCode' => $respCode, 'is_resource' => is_resource($body), 'is_string' => is_string($body), 'is_array' => is_array($body)]);
if (!is_resource($body)) {
return json_decode($body, true) ?: [];
}
return $body;
} catch (ClientException|ServerException $e) {
$responseBody = $e->getResponse()->getBody();
$parsedResponseBody = json_decode($responseBody, true);
Expand Down
10 changes: 6 additions & 4 deletions lib/TaskProcessing/TextToImageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,14 @@ public function process(?string $userId, array $input, callable $reportProgress)
$this->logger->warning('OpenAI/LocalAI\'s text to image generation failed: no image returned');
throw new RuntimeException('OpenAI/LocalAI\'s text to image generation failed: no image returned');
}
$client = $this->clientService->newClient();
$requestOptions = $this->openAiAPIService->getImageRequestOptions($userId);
$output = ['images' => []];
foreach ($urls as $url) {
$imageResponse = $client->get($url, $requestOptions);
$output['images'][] = $imageResponse->getBody();
$imageResponse = $this->openAiAPIService->request($userId, $url);
// if (!is_resource($imageResponse)) {
// $this->logger->warning('JSON returned in an image request to OpenAI/LocalAI', ['response' => $imageResponse]);
// throw new RuntimeException('JSON returned in an image request to OpenAI/LocalAI');
// }
$output['images'][] = $imageResponse;
}
$endTime = time();
$this->openAiAPIService->updateExpImgProcessingTime($endTime - $startTime);
Expand Down
Loading