Skip to content

Commit

Permalink
MBS-9374: Rework handling of predefined models
Browse files Browse the repository at this point in the history
  • Loading branch information
PhMemmel committed Sep 18, 2024
1 parent 0429ffd commit 586b6b4
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 2 deletions.
2 changes: 2 additions & 0 deletions classes/base_instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ final public function store_formdata(stdClass $data): void {
$this->set_connector($data->connector);
$this->set_tenant($data->tenant);
if (empty($data->model)) {
// This is only a fallback. If the connector does not support the selection of a model,
// it is supposed to overwrite this default value in the extend_store_formdata function.
$data->model = self::PRECONFIGURED_MODEL;
}
$this->set_model($data->model);
Expand Down
18 changes: 18 additions & 0 deletions classes/local/aitool_option_azure.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace local_ai_manager\local;

use local_ai_manager\base_connector;
use stdClass;

/**
Expand Down Expand Up @@ -111,4 +112,21 @@ public static function validate_azure_options(array $data): array {
}
return $errors;
}

/**
* Define the model name in case we are using azure.
*
* When using azure we cannot select a model, because it is preconfigured in the azure resource.
* This function defines the string to use as model for logging etc.
*
* @param ?string $connectorname The name of the connector, will be included into the model name
* @return string the string defining the name of the model
* @throws \coding_exception if the $connectorname is null or empty
*/
public static function get_azure_model_name(?string $connectorname): string {
if (empty($connectorname)) {
throw new \coding_exception('Azure model name cannot be empty or null');
}
return $connectorname . '_preconfigured_azure';
}
}
1 change: 1 addition & 0 deletions classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ public function log_request(string $prompttext, prompt_response $promptcompletio
$data = new stdClass();
$data->userid = $USER->id;
$data->value = $promptcompletion->get_usage()->value;
$data->connector = $this->connector->get_instance()->get_connector();
if ($this->connector->has_customvalue1()) {
$data->customvalue1 = $promptcompletion->get_usage()->customvalue1;
}
Expand Down
3 changes: 2 additions & 1 deletion db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="local/ai_manager/db" VERSION="20240809" COMMENT="XMLDB file for Moodle local/ai_manager"
<XMLDB PATH="local/ai_manager/db" VERSION="20240918" COMMENT="XMLDB file for Moodle local/ai_manager"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
Expand Down Expand Up @@ -49,6 +49,7 @@
<FIELD NAME="customvalue1" TYPE="number" LENGTH="20" NOTNULL="false" SEQUENCE="false" DECIMALS="3"/>
<FIELD NAME="customvalue2" TYPE="number" LENGTH="20" NOTNULL="false" SEQUENCE="false" DECIMALS="3"/>
<FIELD NAME="purpose" TYPE="char" LENGTH="50" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="connector" TYPE="char" LENGTH="50" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="model" TYPE="char" LENGTH="50" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="modelinfo" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="duration" TYPE="number" LENGTH="20" NOTNULL="false" SEQUENCE="false" DECIMALS="3"/>
Expand Down
48 changes: 48 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,53 @@ function xmldb_local_ai_manager_upgrade($oldversion) {
// Ai_manager savepoint reached.
upgrade_plugin_savepoint(true, 2024080900, 'local', 'ai_manager');
}

if ($oldversion < 2024091800) {
$table = new xmldb_table('local_ai_manager_request_log');
$field = new xmldb_field('connector', XMLDB_TYPE_CHAR, '50', null, null, null, null, 'purpose');

// Conditionally launch add field connector.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Migrate existing records.
$rs = $DB->get_recordset('local_ai_manager_request_log');
foreach ($rs as $record) {
if ($record->model === 'preconfigured') {
if ($record->purpose === 'tts') {
$record->model = 'openaitts_preconfigured_azure';
} else if ($record->purpose === 'imggen') {
$record->model = 'dalle_preconfigured_azure';
} else {
$record->model = 'chatgpt_preconfigured_azure';
}
}
if ($record->purpose === 'tts') {
if (str_contains($record->model, 'preconfigured') || str_contains($record->model, 'tts-1')) {
$record->connector = 'openaitts';
} else {
$record->connector = 'googlesynthesize';
}
} else if ($record->purpose === 'imggen') {
$record->connector = 'dalle';
} else {
// We have a text based language model.
if (str_starts_with($record->model, 'gemini-')) {
$record->connector = 'gemini';
} else if (str_starts_with($record->model, 'gpt-')) {
$record->connector = 'chatgpt';
} else {
$record->connector = 'ollama';
}
}
$DB->update_record('local_ai_manager_request_log', $record);
}

$rs = $DB->get_recordset('local_ai_manager_request_log');

$rs->close();
upgrade_plugin_savepoint(true, 2024091800, 'local', 'ai_manager');
}
return true;
}
3 changes: 3 additions & 0 deletions tools/chatgpt/classes/instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ protected function extend_store_formdata(stdClass $data): void {
$endpoint = 'https://' . $resourcename .
'.openai.azure.com/openai/deployments/'
. $deploymentid . '/chat/completions?api-version=' . $apiversion;
// We have an empty model because the model is preconfigured if we're using azure.
// So we overwrite the default "preconfigured" value by a better model name.
$this->set_model(aitool_option_azure::get_azure_model_name($this->get_connector()));
} else {
$endpoint = 'https://api.openai.com/v1/chat/completions';
}
Expand Down
3 changes: 3 additions & 0 deletions tools/dalle/classes/instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ protected function extend_store_formdata(stdClass $data): void {
$endpoint = 'https://' . $resourcename .
'.openai.azure.com/openai/deployments/'
. $deploymentid . '/images/generations?api-version=' . $apiversion;
// We have an empty model because the model is preconfigured if we're using azure.
// So we overwrite the default "preconfigured" value by a better model name.
$this->set_model(aitool_option_azure::get_azure_model_name($this->get_connector()));
} else {
$endpoint = 'https://api.openai.com/v1/images/generations';
}
Expand Down
3 changes: 3 additions & 0 deletions tools/openaitts/classes/instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ protected function extend_store_formdata(stdClass $data): void {
$endpoint = 'https://' . $resourcename .
'.openai.azure.com/openai/deployments/'
. $deploymentid . '/audio/speech?api-version=' . $apiversion;
// We have an empty model because the model is preconfigured if we're using azure.
// So we overwrite the default "preconfigured" value by a better model name.
$this->set_model(aitool_option_azure::get_azure_model_name($this->get_connector()));
} else {
$endpoint = 'https://api.openai.com/v1/audio/speech';
}
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
defined('MOODLE_INTERNAL') || die();

$plugin->version = 2024082600;
$plugin->version = 2024091800;
$plugin->requires = 2024042200;
$plugin->release = '0.0.1';
$plugin->component = 'local_ai_manager';
Expand Down

0 comments on commit 586b6b4

Please sign in to comment.