Skip to content

Commit 22ac4bf

Browse files
committed
[docs] Update AI Provider plugin documentation for Pre-defined Models
See: MDL-82980
1 parent 3152cdf commit 22ac4bf

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed

docs/apis/plugintypes/ai/provider.md

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,189 @@ class action_generate_text_form extends action_settings_form {
285285
...
286286
```
287287

288+
## Predefined models
289+
290+
Pre-defined models allow AI providers to define specific model configurations that can be selected by users.
291+
This provides a better user experience by offering known model options with appropriate default parameters rather than requiring manual configuration.
292+
293+
### Creating Model Classes
294+
295+
To implement pre-defined models, you will need to create model classes in the `[your_plugin]/classes/aimodel` directory and extend the `core_ai\aimodel\base` class.
296+
297+
For example, `aiprovider_openai` plugin defines this:
298+
299+
```php
300+
namespace aiprovider_openai\aimodel;
301+
302+
use core_ai\aimodel\base;
303+
use MoodleQuickForm;
304+
305+
class gpt4o extends base implements openai_base {
306+
#[\Override]
307+
public function get_model_name(): string {
308+
return 'gpt-4o';
309+
}
310+
311+
#[\Override]
312+
public function get_model_display_name(): string {
313+
return 'GPT-4o';
314+
}
315+
316+
// Add other model-specific methods or properties
317+
}
318+
```
319+
320+
### Implementing Per-Model Settings
321+
322+
To add configurable settings for individual models:
323+
324+
1. Override the `has_model_settings()` and `add_model_settings()` methods:
325+
326+
```php
327+
#[\Override]
328+
public function has_model_settings(): bool {
329+
return true;
330+
}
331+
332+
#[\Override]
333+
public function add_model_settings(MoodleQuickForm $mform): void {
334+
$mform->addElement(
335+
'text',
336+
'top_p',
337+
get_string('settings_top_p', 'aiprovider_openai'),
338+
);
339+
$mform->setType('top_p', PARAM_FLOAT);
340+
$mform->addHelpButton('top_p', 'settings_top_p', 'aiprovider_openai');
341+
342+
// Add more model settings as needed
343+
$mform->addElement(
344+
'text',
345+
'max_tokens',
346+
get_string('settings_max_tokens', 'aiprovider_openai'),
347+
);
348+
$mform->setType('max_tokens', PARAM_INT);
349+
}
350+
```
351+
352+
2. Create a helper class to manage models. For example, the `aiprovider_openai` plugin defines this:
353+
354+
```php
355+
namespace aiprovider_openai;
356+
357+
class helper {
358+
/**
359+
* Get all model classes.
360+
*
361+
* @return array Array of model classes
362+
*/
363+
public static function get_model_classes(): array {
364+
$models = [];
365+
$modelclasses = \core_component::get_component_classes_in_namespace('aiprovider_openai', 'aimodel');
366+
foreach ($modelclasses as $class => $path) {
367+
if (!class_exists($class) || !is_a($class, \core_ai\aimodel\base::class, true)) {
368+
throw new \coding_exception("Model class not valid: {$class}");
369+
}
370+
$models[] = $class;
371+
}
372+
return $models;
373+
}
374+
375+
/**
376+
* Get a specific model class instance.
377+
*
378+
* @param string $modelname The model name
379+
* @return \core_ai\aimodel\base|null The model class or null if not found
380+
*/
381+
public static function get_model_class(string $modelname): ?\core_ai\aimodel\base {
382+
foreach (self::get_model_classes() as $modelclass) {
383+
$model = new $modelclass();
384+
if ($model->get_model_name() === $modelname) {
385+
return $model;
386+
}
387+
}
388+
return null;
389+
}
390+
}
391+
```
392+
393+
### Integrating with Action Settings Forms
394+
395+
To add model settings to action forms, you will need to create a hook listener that adds the model settings to the action form.
396+
397+
For example, the `aiprovider_openai` plugin does these:
398+
399+
1. Create a hook listener that adds model settings to the action form:
400+
401+
```php
402+
public static function set_model_form_definition_for_aiprovider_openai(after_ai_action_settings_form_hook $hook): void {
403+
if ($hook->plugin !== 'aiprovider_openai') {
404+
return;
405+
}
406+
407+
$mform = $hook->mform;
408+
if (isset($mform->_elementIndex['modeltemplate'])) {
409+
$model = $mform->getElementValue('modeltemplate');
410+
if (is_array($model)) {
411+
$model = $model[0];
412+
}
413+
414+
// Handle custom model option.
415+
if ($model == 'custom') {
416+
$mform->addElement('header', 'modelsettingsheader', get_string('settings', 'aiprovider_openai'));
417+
$mform->addElement(
418+
'textarea',
419+
'modelextraparams',
420+
get_string('extraparams', 'aiprovider_openai'),
421+
['rows' => 5, 'cols' => 20],
422+
);
423+
$mform->setType('modelextraparams', PARAM_TEXT);
424+
} else {
425+
// Handle pre-defined model settings.
426+
$targetmodel = helper::get_model_class($model);
427+
if ($targetmodel && $targetmodel->has_model_settings()) {
428+
$mform->addElement('header', 'modelsettingsheader', get_string('settings', 'aiprovider_openai'));
429+
$targetmodel->add_model_settings($mform);
430+
}
431+
}
432+
}
433+
}
434+
```
435+
436+
2. Register the hook callback in `db/hooks.php`:
437+
438+
```php
439+
$callbacks = [
440+
[
441+
'hook' => \core_ai\hook\after_ai_action_settings_form_hook::class,
442+
'callback' => \aiprovider_openai\hook_listener::class . '::set_model_form_definition_for_aiprovider_openai',
443+
],
444+
];
445+
```
446+
447+
### Model Selection UI
448+
449+
To create a model selector UI for your action settings form, you will need to add the model selector element to your action settings form.
450+
451+
For example, `aiprovider_openai` plugin defines this:
452+
453+
```php
454+
// Create model selector options
455+
$models = [];
456+
foreach (helper::get_model_classes() as $modelclass) {
457+
$model = new $modelclass();
458+
$models[$model->get_model_name()] = $model->get_model_display_name();
459+
}
460+
$models['custom'] = get_string('custom_model', 'aiprovider_openai');
461+
462+
$mform->addElement(
463+
'select',
464+
'modeltemplate',
465+
get_string('model_template', 'aiprovider_openai'),
466+
$models
467+
);
468+
$mform->setDefault('modeltemplate', 'gpt-4o');
469+
```
470+
288471
## Rate limiting
289472

290473
Provider plugins by default implement rate limiting to prevent abuse of the external AI services.

docs/apis/subsystems/ai/index.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ The AI Subsystem consists of the following (main) components:
6161
- It is the 'controller' that sits between Placement and Provider plugins.
6262
- It allows Placements to pass user requests to Providers and handles all the calls to the Providers, including prioritisation and logging.
6363
- It allows Providers to respond to action requests.
64+
- **Pre-defined Models**
65+
- These are pre-defined models that are used by Providers to do the Actions.
66+
- They are not part of the AI subsystem, but they are used by Providers.
6467

6568
### Placements
6669

@@ -199,6 +202,12 @@ Each Action will need to define its own database schema and stored data that is
199202

200203
The naming convention for Action database tables is `ai_action_<action_name>`. For example: `ai_action_generate_image`, `ai_action_translate_text`.
201204

205+
#### Pre-defined Models
206+
207+
We can define a set of models that are available for the Actions of the Providers.
208+
See the [Pre-defined Models](/apis/plugintypes/ai/provider.md#predefined-models) documentation for more information
209+
on developing Pre-defined Models for Provider plugins.
210+
202211
#### Responses
203212

204213
When a Provider processes an Action it **must** return a response object.

0 commit comments

Comments
 (0)