Skip to content

Commit

Permalink
WIP - Mimetype options
Browse files Browse the repository at this point in the history
  • Loading branch information
PhMemmel committed Sep 30, 2024
1 parent 7927b70 commit e6e404f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
11 changes: 11 additions & 0 deletions classes/base_connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}
}
20 changes: 19 additions & 1 deletion purposes/itt/classes/purpose.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}
5 changes: 5 additions & 0 deletions tools/chatgpt/classes/connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
}
9 changes: 8 additions & 1 deletion tools/gemini/classes/connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
]
],
Expand Down Expand Up @@ -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'];
}
}
5 changes: 5 additions & 0 deletions tools/ollama/classes/connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
}

0 comments on commit e6e404f

Please sign in to comment.