Skip to content

Commit

Permalink
openaiapiproviderr
Browse files Browse the repository at this point in the history
  • Loading branch information
mhughes2k committed Mar 22, 2024
1 parent 525d840 commit 2617ff3
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 45 deletions.
41 changes: 0 additions & 41 deletions ai/classes/form/aiprovider.php

This file was deleted.

68 changes: 68 additions & 0 deletions ai/classes/form/openaiapiprovider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace core_ai\form;

class openaiapiprovider extends \core\form\persistent{
/** @var string $persistentclass */
protected static $persistentclass = 'core_ai\\aiprovider';

public function __construct($action = null, $customdata = null, $method = 'post', $target = '', $attributes = null,
$editable = true, array $ajaxformdata = null){
if (array_key_exists('type', $customdata)) {
$this->type = $customdata['type'];
}

parent::__construct($action, $customdata, $method, $target, $attributes, $editable, $ajaxformdata);
}
public function definition() {
global $PAGE, $OUTPUT;

$mform = $this->_form;
$provider = $this->get_persistent();
$mform->addElement('html','intro', 'hello');

// Name.
$mform->addElement('text', 'name', get_string('providername', 'ai'));
$mform->addRule('name', null, 'required', null, 'client');
$mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$mform->addHelpButton('name', 'providername', 'ai');

// Client Secret.
$mform->addElement('text','baseurl', get_string('baseurl', 'ai'));
$mform->setType('baseurl', PARAM_URL);
$mform->addElement('text', 'apikey', get_string('apikey', 'ai'));
$mform->addRule('apikey', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$mform->addHelpButton('apikey', 'apikey', 'ai');

$mform->addElement('header', 'features', get_string('features', 'ai'));

$mform->addElement('advcheckbox', 'allowchat', get_string('allowchat', 'ai'));
$mform->addHelpButton('allowchat', 'allowchat', 'ai');
$mform->addElement('text','completions', get_string('completionspath', 'ai'));
$mform->addElement('text','completionmodel', get_string('completionmodel', 'ai'));
$mform->disabledIf('completions', 'allowchat', 'notchecked');
$mform->disabledIf('completionmodel', 'allowchat', 'notchecked');

$mform->addElement('advcheckbox', 'allowembeddings', get_string('allowembeddings', 'ai'));
$mform->addHelpButton('allowembeddings', 'allowembeddings', 'ai');
$mform->addElement('text','embeddings', get_string('embeddingspath', 'ai'));
$mform->addElement('text','embeddingmodel', get_string('embeddingmodel', 'ai'));
$mform->disabledIf('embeddings', 'allowembeddings', 'notchecked');
$mform->disabledIf('embeddingmodel', 'allowembeddings', 'notchecked');

$mform->addElement('header','constraints', get_string('constraints', 'ai'));
$mform->addElement('checkbox', 'systemwide', get_string('allowsystemwide', 'ai'));
$displaylist = \core_course_category::make_categories_list('moodle/course:changecategory');
// if (!isset($displaylist[$course->category])) {
// //always keep current
// $displaylist[$course->category] = core_course_category::get($course->category, MUST_EXIST, true)
// ->get_formatted_name();
// }
$mform->addElement('autocomplete', 'contextid', get_string('coursecategory'), $displaylist);
// $mform->addRule('contextid', null, 'required', null, 'client');
$mform->addHelpButton('contextid', 'coursecategory');
$mform->disabledIf('contextid', 'systemwide', 'checked');

$this->add_action_buttons(true, get_string('savechanges', 'ai'));
}
}
8 changes: 5 additions & 3 deletions ai/classes/output/index_page.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,21 @@ public function providers_table($providers) {
return \html_writer::table($table);
}

private $providertypes = ['OpenAI API'];
private $providertypes = [
'openaiapi' => 'OpenAI API'
];
protected function template_buttons() {
global $CFG;
$buttons = [];
foreach($this->providertypes as $type) {
foreach($this->providertypes as $type => $name) {
$addurl = new \moodle_url($CFG->wwwroot . '/ai/index.php',
[
'action' => api::ACTION_EDIT_PROVIDER,
'type' => $type,
'pid' => null
]
);
$buttontext = get_string('newprovider', 'ai', $type);
$buttontext = get_string('newprovider', 'ai', $name);
$buttons[] = [$addurl, $buttontext];
}
return $buttons;
Expand Down
2 changes: 1 addition & 1 deletion ai/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
} else {
// Create new
}
$mform = new \core_ai\form\aiprovider(null, [
$mform = new \core_ai\form\openaiapiprovider(null, [
'persistent' => $provider,
'type' => required_param('type', PARAM_RAW)
]);
Expand Down
28 changes: 28 additions & 0 deletions lang/en/ai.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,31 @@
$string['embedding_help'] = 'Embedding allows the AI to generate vector representations of text.';
$string['aiproviderfeatures'] = '';
$string['aiproviderfeatures_desc'] = 'This plugin needs the following AI features';

// providers
$string['newprovider'] = '{$a} Based Provider';

// Provider instance form
$string['providername'] = "Name";
$string['providername_help'] = "Name";
$string['baseurl'] = 'Base URL';
$string['baseurl_help'] = 'Base URL';
$string['apikey'] = 'API Key';
$string['apikey_help'] = 'API KEY ';

$string['features'] = 'Features';
$string['allowchat'] = 'Allow Chat';
$string['allowchat_help'] = 'Allow this provider to provide chat completion.';
$string['completionspath'] = 'Completions path';
$string['completionspath_help'] = 'Completions path';
$string['completionmodel'] = 'Completion Model';
$string['completionmodel_help'] = 'Completion Model';
$string['allowembeddings'] = 'Allow Emebddings';
$string['allowembeddings_help'] = 'Allow this provider to be used to create embeddings.';
$string['embeddingspath'] = 'Embeddings path';
$string['embeddingspath_help'] = 'Embeddings path';
$string['embeddingmodel'] = 'Embedding Model';
$string['embeddingmodel_help'] = 'Embedding Model';

$string['constraints'] = 'Constraints';
$string['savechanges'] = 'Save changes';

0 comments on commit 2617ff3

Please sign in to comment.