diff --git a/classes/base_connector.php b/classes/base_connector.php index 37d0547..c91f151 100644 --- a/classes/base_connector.php +++ b/classes/base_connector.php @@ -242,4 +242,15 @@ protected function get_headers(): array { 'Content-Type' => 'application/json;charset=utf-8', ]; } + + /** + * Returns the allowed mimetypes. + * + * This can be overwritten in connector classes that are capable of files being submitted. + * + * @return array an array of allowed mimetypes + */ + public function allowed_mimetypes(): array { + return []; + } } diff --git a/purposes/itt/classes/purpose.php b/purposes/itt/classes/purpose.php index 5f08ba2..79951c4 100644 --- a/purposes/itt/classes/purpose.php +++ b/purposes/itt/classes/purpose.php @@ -17,6 +17,7 @@ namespace aipurpose_itt; use coding_exception; +use local_ai_manager\base_connector; use local_ai_manager\base_purpose; use local_ai_manager\local\connector_factory; use local_ai_manager\local\userinfo; @@ -43,6 +44,23 @@ public function get_additional_purpose_options(): array { return []; } - return ['image' => PARAM_RAW]; + return ['image' => PARAM_RAW, 'allowedmimetypes' => $this->get_allowed_mimetypes()]; + } + + /** + * Returns an array of allowed mimetypes for files being submitted. + * + * @return array array of allowed mimetypes, for example ['image/jpg', 'image/png'] + * @throws coding_exception if the connector does not declare any allowed mimetypes + */ + public function get_allowed_mimetypes(): array { + global $USER; + $userinfo = new userinfo($USER->id); + $factory = \core\di::get(connector_factory::class); + $connector = $factory->get_connector_by_purpose($this->get_plugin_name(), $userinfo->get_role()); + if (!method_exists($connector, 'allowed_mimetypes') || empty($connector->allowed_mimetypes())) { + throw new coding_exception('Connector does not declare allowed mimetypes. Cannot be used for image to text'); + } + return $connector->allowed_mimetypes(); } } diff --git a/tools/chatgpt/classes/connector.php b/tools/chatgpt/classes/connector.php index c2d2144..bededcc 100644 --- a/tools/chatgpt/classes/connector.php +++ b/tools/chatgpt/classes/connector.php @@ -143,4 +143,9 @@ protected function get_headers(): array { } return $headers; } + + #[\Override] + public function allowed_mimetypes(): array { + return ['image/png', 'image/jpeg', 'image/webp', 'image/gif']; + } } diff --git a/tools/gemini/classes/connector.php b/tools/gemini/classes/connector.php index f098641..29e2241 100644 --- a/tools/gemini/classes/connector.php +++ b/tools/gemini/classes/connector.php @@ -111,7 +111,7 @@ public function get_prompt_data(string $prompttext, array $requestoptions): arra [ 'inline_data' => [ 'mime_type' => mime_content_type($requestoptions['image']), - // Gemini API expects the plain base64 encoded string, without the leading data url meta data. + // Gemini API expects the plain base64 encoded string, without the leading data url metadata. 'data' => explode(',', $requestoptions['image'])[1], ] ], @@ -145,4 +145,11 @@ protected function get_headers(): array { } return $headers; } + + #[\Override] + public function allowed_mimetypes(): array { + // We use the inline_data for sending data to the API, so we basically support every format. + // However, we restrict it to some basics. + return ['image/png', 'image/jpeg', 'image/webp', 'image/heic', 'image/heiff', 'application/pdf']; + } } diff --git a/tools/ollama/classes/connector.php b/tools/ollama/classes/connector.php index e8243f9..6239db6 100644 --- a/tools/ollama/classes/connector.php +++ b/tools/ollama/classes/connector.php @@ -107,4 +107,9 @@ public function get_prompt_data(string $prompttext, array $requestoptions): arra ]; return $data; } + + #[\Override] + public function allowed_mimetypes(): array { + return ['image/png', 'image/jpg']; + } }