Repository navigation
fix(OpenAiAPIService): Throw UserFacingProcessingException if service is not responding properly #380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(OpenAiAPIService): Throw UserFacingProcessingException if service is not responding properly #380
Changes from all commits
7bef1b3
818875a
098210a
9988cef
3937de6
af7a9dc
3f96091
5db2001
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
|
||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe qualifies for
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = [ | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this too perhaps if the above is done |
||
| } | ||
| } | ||
| $history[] = json_encode([ | ||
|
|
@@ -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']; | ||
|
|
||
|
|
@@ -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()); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.