Skip to content
Merged
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
40 changes: 33 additions & 7 deletions lib/Service/OpenAiAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use DateTime;
use Exception;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Utils;
use OCA\OpenAi\AppInfo\Application;
Expand All @@ -28,6 +29,7 @@
use OCP\IL10N;
use OCP\Lock\LockedException;
use OCP\Notification\IManager as INotificationManager;
use OCP\TaskProcessing\Exception\UserFacingProcessingException;
use OCP\TaskProcessing\ShapeEnumValue;
use Psr\Log\LoggerInterface;
use RuntimeException;
Expand Down Expand Up @@ -1125,7 +1127,7 @@ public function updateExpImgProcessingTime(int $runtime): void {
* @param string|null $serviceType
* @param int $retryCount number of retries that have been attempted so far
* @return array decoded request result or error
* @throws Exception
* @throws Exception|UserFacingProcessingException
*/
public function request(
?string $userId, string $endPoint, array $params = [], string $method = 'GET',
Expand Down Expand Up @@ -1291,17 +1293,41 @@ public function request(
$this->logger->warning('API request error : ' . $e->getMessage(), ['response_body' => $responseBody, 'exception' => $e]);
}
}
throw new Exception(
$this->l10n->t('API request error: ') . (
$e->getResponse()->getStatusCode() === 401
$errorMessage = (
$e->getResponse()->getStatusCode() === 401
? $this->l10n->t('Invalid API Key/Basic Auth: ')
Comment thread
marcelklehr marked this conversation as resolved.
Resolved
: ''
) . (
isset($parsedResponseBody['error']) && isset($parsedResponseBody['error']['message'])
) . (
isset($parsedResponseBody['error']) && isset($parsedResponseBody['error']['message'])
? $parsedResponseBody['error']['message']
: $e->getMessage()
),
);
if ($e->getResponse()->getStatusCode() == 401 && $e->getResponse()->getStatusCode() < 500) {
throw new UserFacingProcessingException(
$this->l10n->t('API request error: ') . $errorMessage,
intval($e->getCode()),
userFacingMessage: $this->l10n->t('%s API error: Invalid API key or invalid Basic Authentication. Contact your system administrator.', [$this->getServiceName()]),
);
}
if ($e->getResponse()->getStatusCode() >= 500) {
throw new UserFacingProcessingException(
$this->l10n->t('API request error: ') . $errorMessage,
intval($e->getCode()),
userFacingMessage: $this->l10n->t('%s API error: AI backend is currently not available. Contact your system administrator.', [$this->getServiceName()]),
);
}
throw new Exception(
$this->l10n->t('API request error: ') . $errorMessage,
intval($e->getCode()),
);
} catch (ConnectException $e) {
if ($logErrors) {
$this->logger->warning('API connection error: ' . $e->getMessage(), ['exception' => $e]);
}
throw new UserFacingProcessingException(
$this->l10n->t('API connection error: ') . $e->getMessage(),
intval($e->getCode()),
userFacingMessage: $this->l10n->t('%s API error: AI backend is currently not reachable. Contact your system administrator.', [$this->getServiceName()]),
);
}
}
Expand Down
27 changes: 15 additions & 12 deletions lib/TaskProcessing/AnalyzeImagesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
use OCP\Files\File;
use OCP\IL10N;
use OCP\TaskProcessing\EShapeType;
use OCP\TaskProcessing\Exception\ProcessingException;
use OCP\TaskProcessing\Exception\UserFacingProcessingException;
use OCP\TaskProcessing\IProvider;
use OCP\TaskProcessing\ISynchronousOptionsAwareProvider;
use OCP\TaskProcessing\ShapeDescriptor;
use OCP\TaskProcessing\SynchronousProviderOptions;
use Psr\Log\LoggerInterface;
use RuntimeException;

class AnalyzeImagesProvider implements IProvider, ISynchronousOptionsAwareProvider {

Expand Down Expand Up @@ -108,32 +109,32 @@ public function process(
$preferStreaming = $options->getPreferStreaming();

if (!$this->openAiAPIService->isUsingOpenAi() && !$this->openAiSettingsService->getChatEndpointEnabled()) {
throw new RuntimeException('Must support chat completion endpoint');
throw new ProcessingException('Must support chat completion endpoint');
}

$history = [];

if (!isset($input['images']) || !is_array($input['images'])) {
throw new RuntimeException('Invalid file list');
throw new ProcessingException('Invalid file list');
}
// Maximum file count for openai is 500. Seems reasonable enough to enforce for all apis though (https://platform.openai.com/docs/guides/images-vision?api-mode=responses&format=url#image-input-requirements)
if (count($input['images']) > 500) {
throw new RuntimeException('Too many files given. Max is 500');
throw new ProcessingException('Too many files given. Max is 500');
Comment on lines -121 to +122

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe qualifies for UserFacingProcessingException

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be a different PR, let's try to not bloat up this one too much

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by all means

}
$fileSize = 0;
foreach ($input['images'] as $image) {
if (!$image instanceof File || !$image->isReadable()) {
throw new RuntimeException('Invalid input file');
throw new ProcessingException('Invalid input file');
}
$fileSize += intval($image->getSize());
// Maximum file size for openai is 50MB. Seems reasonable enough to enforce for all apis though. (https://platform.openai.com/docs/guides/images-vision?api-mode=responses&format=url#image-input-requirements)
if ($fileSize > 50 * 1000 * 1000) {
throw new RuntimeException('Filesize of input files too large. Max is 50MB');
throw new ProcessingException('Filesize of input files too large. Max is 50MB');
Comment on lines -131 to +132

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this too

}
$inputFile = base64_encode(stream_get_contents($image->fopen('rb')));
$fileType = $image->getMimeType();
if (!str_starts_with($fileType, 'image/')) {
throw new RuntimeException('Invalid input file type ' . $fileType);
throw new ProcessingException('Invalid input file type ' . $fileType);
Comment on lines -136 to +137

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not too sure about this one, but could be harmless if the user sees it anyway

}
if ($this->openAiAPIService->isUsingOpenAi()) {
$validFileTypes = [
Expand All @@ -143,7 +144,7 @@ public function process(
'image/webp',
];
if (!in_array($fileType, $validFileTypes)) {
throw new RuntimeException('Invalid input file type for OpenAI ' . $fileType);
throw new ProcessingException('Invalid input file type for OpenAI ' . $fileType);
Comment on lines -146 to +147

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this too perhaps if the above is done

}
}
$history[] = json_encode([
Expand All @@ -160,7 +161,7 @@ public function process(
}

if (!isset($input['input']) || !is_string($input['input'])) {
throw new RuntimeException('Invalid prompt');
throw new ProcessingException('Invalid prompt');
}
$prompt = $input['input'];

Expand Down Expand Up @@ -205,10 +206,12 @@ public function process(
return ['output' => array_pop($completion)];
}

throw new RuntimeException('No result in OpenAI/LocalAI response.');
} catch (\Exception $e) {
throw new ProcessingException('No result in OpenAI/LocalAI response.');
} catch (UserFacingProcessingException $e) {
throw $e;
} catch (\Throwable $e) {
$this->logger->warning('OpenAI/LocalAI\'s image question generation failed with: ' . $e->getMessage(), ['exception' => $e]);
throw new RuntimeException('OpenAI/LocalAI\'s image question generation failed with: ' . $e->getMessage());
throw new ProcessingException('OpenAI/LocalAI\'s image question generation failed with: ' . $e->getMessage());
}
}
}
46 changes: 28 additions & 18 deletions lib/TaskProcessing/AudioToAudioChatProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@

namespace OCA\OpenAi\TaskProcessing;

use Exception;
use OCA\OpenAi\AppInfo\Application;
use OCA\OpenAi\Service\OpenAiAPIService;
use OCA\OpenAi\Service\OpenAiSettingsService;
use OCP\Files\File;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\TaskProcessing\EShapeType;
use OCP\TaskProcessing\Exception\ProcessingException;
use OCP\TaskProcessing\Exception\UserFacingProcessingException;
use OCP\TaskProcessing\ISynchronousProvider;
use OCP\TaskProcessing\ShapeDescriptor;
use OCP\TaskProcessing\ShapeEnumValue;
use OCP\TaskProcessing\TaskTypes\AudioToAudioChat;
use Psr\Log\LoggerInterface;
use RuntimeException;

class AudioToAudioChatProvider implements ISynchronousProvider {

Expand Down Expand Up @@ -164,12 +164,12 @@ public function getOptionalOutputShapeEnumValues(): array {

public function process(?string $userId, array $input, callable $reportProgress): array {
if (!isset($input['input']) || !$input['input'] instanceof File || !$input['input']->isReadable()) {
throw new RuntimeException('Invalid input audio file in the "input" field. A readable file is expected.');
throw new ProcessingException('Invalid input audio file in the "input" field. A readable file is expected.');
}
$inputFile = $input['input'];

if (!isset($input['system_prompt']) || !is_string($input['system_prompt'])) {
throw new RuntimeException('Invalid system_prompt');
throw new ProcessingException('Invalid system_prompt');
}
$systemPrompt = $input['system_prompt'];

Expand All @@ -179,7 +179,7 @@ public function process(?string $userId, array $input, callable $reportProgress)
}

if (!isset($input['history']) || !is_array($input['history'])) {
throw new RuntimeException('Invalid chat history, array expected');
throw new ProcessingException('Invalid chat history, array expected');
}
$history = $input['history'];

Expand Down Expand Up @@ -256,12 +256,14 @@ private function oneStep(
$apiResponse = $this->openAiAPIService->requestSpeechCreation($userId, $textResponse, $ttsModel, $outputVoice, $speed);
if (!isset($apiResponse['body'])) {
$this->logger->warning($serviceName . ' text to speech generation failed: no speech returned');
throw new RuntimeException($serviceName . ' text to speech generation failed: no speech returned');
throw new ProcessingException($serviceName . ' text to speech generation failed: no speech returned');
}
$output = $apiResponse['body'];
} catch (\Exception $e) {
} catch (UserFacingProcessingException $e) {
throw $e;
} catch (\Throwable $e) {
$this->logger->warning($serviceName . ' text to speech generation failed with: ' . $e->getMessage(), ['exception' => $e]);
throw new RuntimeException($serviceName . ' text to speech generation failed with: ' . $e->getMessage());
throw new ProcessingException($serviceName . ' text to speech generation failed with: ' . $e->getMessage());
}
} else {
$output = base64_decode($message['audio']['data']);
Expand All @@ -280,9 +282,11 @@ private function oneStep(
try {
$inputTranscription = $this->openAiAPIService->transcribeFile($userId, $inputFile, false, $sttModel);
$result['input_transcript'] = $inputTranscription;
} catch (Exception $e) {
} catch (UserFacingProcessingException $e) {
throw $e;
} catch (\Throwable $e) {
$this->logger->warning($serviceName . ' audio input transcription failed with: ' . $e->getMessage(), ['exception' => $e]);
throw new RuntimeException($serviceName . ' audio input transcription failed with: ' . $e->getMessage());
throw new ProcessingException($serviceName . ' audio input transcription failed with: ' . $e->getMessage());
}

return $result;
Expand All @@ -295,20 +299,24 @@ private function threeSteps(
// speech to text
try {
$inputTranscription = $this->openAiAPIService->transcribeFile($userId, $inputFile, false, $sttModel);
} catch (Exception $e) {
} catch (UserFacingProcessingException $e) {
throw $e;
} catch (\Throwable $e) {
$this->logger->warning($serviceName . ' transcription failed with: ' . $e->getMessage(), ['exception' => $e]);
throw new RuntimeException($serviceName . ' transcription failed with: ' . $e->getMessage());
throw new ProcessingException($serviceName . ' transcription failed with: ' . $e->getMessage());
}

// free prompt
try {
$completion = $this->openAiAPIService->createChatCompletion($userId, $llmModel, $inputTranscription, $systemPrompt, $history, 1, 1000);
$completion = $completion['messages'];
} catch (Exception $e) {
throw new RuntimeException($serviceName . ' chat completion request failed: ' . $e->getMessage());
} catch (UserFacingProcessingException $e) {
throw $e;
} catch (\Throwable $e) {
throw new ProcessingException($serviceName . ' chat completion request failed: ' . $e->getMessage());
}
if (count($completion) === 0) {
throw new RuntimeException('No completion in ' . $serviceName . ' response.');
throw new ProcessingException('No completion in ' . $serviceName . ' response.');
}
$llmResult = array_pop($completion);

Expand All @@ -318,16 +326,18 @@ private function threeSteps(

if (!isset($apiResponse['body'])) {
$this->logger->warning($serviceName . ' text to speech generation failed: no speech returned');
throw new RuntimeException($serviceName . ' text to speech generation failed: no speech returned');
throw new ProcessingException($serviceName . ' text to speech generation failed: no speech returned');
}
return [
'output' => $apiResponse['body'],
'output_transcript' => $llmResult,
'input_transcript' => $inputTranscription,
];
} catch (\Exception $e) {
} catch (UserFacingProcessingException $e) {
throw $e;
} catch (\Throwable $e) {
$this->logger->warning($serviceName . ' text to speech generation failed with: ' . $e->getMessage(), ['exception' => $e]);
throw new RuntimeException($serviceName . ' text to speech generation failed with: ' . $e->getMessage());
throw new ProcessingException($serviceName . ' text to speech generation failed with: ' . $e->getMessage());
}
}
}
14 changes: 8 additions & 6 deletions lib/TaskProcessing/AudioToTextProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@

namespace OCA\OpenAi\TaskProcessing;

use Exception;
use OCA\OpenAi\AppInfo\Application;
use OCA\OpenAi\Service\OpenAiAPIService;
use OCP\Files\File;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\TaskProcessing\EShapeType;
use OCP\TaskProcessing\Exception\ProcessingException;
use OCP\TaskProcessing\Exception\UserFacingProcessingException;
use OCP\TaskProcessing\ISynchronousProvider;
use OCP\TaskProcessing\ShapeDescriptor;
use OCP\TaskProcessing\ShapeEnumValue;
use OCP\TaskProcessing\TaskTypes\AudioToText;
use Psr\Log\LoggerInterface;
use RuntimeException;

class AudioToTextProvider implements ISynchronousProvider {

Expand Down Expand Up @@ -92,22 +92,24 @@ public function getOptionalOutputShapeEnumValues(): array {

public function process(?string $userId, array $input, callable $reportProgress): array {
if (!isset($input['input']) || !$input['input'] instanceof File || !$input['input']->isReadable()) {
throw new RuntimeException('Invalid input file');
throw new ProcessingException('Invalid input file');
}
$inputFile = $input['input'];
$language = $input['language'] ?? 'default';
if (!is_string($language)) {
throw new RuntimeException('Invalid language');
throw new ProcessingException('Invalid language');
}

$model = $this->appConfig->getValueString(Application::APP_ID, 'default_stt_model_id', Application::DEFAULT_MODEL_ID, lazy: true) ?: Application::DEFAULT_MODEL_ID;

try {
$transcription = $this->openAiAPIService->transcribeFile($userId, $inputFile, false, $model, $language);
return ['output' => $transcription];
} catch (Exception $e) {
} catch (UserFacingProcessingException $e) {
throw $e;
} catch (\Throwable $e) {
$this->logger->warning('OpenAI\'s Whisper transcription failed with: ' . $e->getMessage(), ['exception' => $e]);
throw new RuntimeException('OpenAI\'s Whisper transcription failed with: ' . $e->getMessage());
throw new ProcessingException('OpenAI\'s Whisper transcription failed with: ' . $e->getMessage());
}
}
}
14 changes: 8 additions & 6 deletions lib/TaskProcessing/ChangeToneProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@

namespace OCA\OpenAi\TaskProcessing;

use Exception;
use OCA\OpenAi\AppInfo\Application;
use OCA\OpenAi\Service\ChunkService;
use OCA\OpenAi\Service\OpenAiAPIService;
use OCA\OpenAi\Service\OpenAiSettingsService;
use OCP\IL10N;
use OCP\TaskProcessing\EShapeType;
use OCP\TaskProcessing\Exception\ProcessingException;
use OCP\TaskProcessing\Exception\UserFacingProcessingException;
use OCP\TaskProcessing\IProvider;
use OCP\TaskProcessing\ISynchronousOptionsAwareProvider;
use OCP\TaskProcessing\ShapeDescriptor;
use OCP\TaskProcessing\ShapeEnumValue;
use OCP\TaskProcessing\SynchronousProviderOptions;
use OCP\TaskProcessing\TaskTypes\TextToTextChangeTone;
use RuntimeException;

class ChangeToneProvider implements IProvider, ISynchronousOptionsAwareProvider {

Expand Down Expand Up @@ -122,7 +122,7 @@ public function process(
$startTime = time();

if (!isset($input['input']) || !is_string($input['input'])) {
throw new RuntimeException('Invalid input text');
throw new ProcessingException('Invalid input text');
}
$textInput = $input['input'];
$toneInput = $input['tone'];
Expand Down Expand Up @@ -172,8 +172,10 @@ public function process(
} else {
$completion = $this->openAiAPIService->createCompletion($userId, $prompt, 1, $model, $maxTokens);
}
} catch (Exception $e) {
throw new RuntimeException('OpenAI/LocalAI request failed: ' . $e->getMessage());
} catch (UserFacingProcessingException $e) {
throw $e;
} catch (\Throwable $e) {
throw new ProcessingException('OpenAI/LocalAI request failed: ' . $e->getMessage());
}
$progress += $increase;
$reportProgress($progress);
Expand All @@ -182,7 +184,7 @@ public function process(
continue;
}

throw new RuntimeException('No result in OpenAI/LocalAI response.');
throw new ProcessingException('No result in OpenAI/LocalAI response.');
}
$endTime = time();
$this->openAiAPIService->updateExpTextProcessingTime($endTime - $startTime);
Expand Down
Loading
Loading