Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MBS-9476: Implement imagen connector and Vertex AI #27

Merged
merged 10 commits into from
Dec 7, 2024
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Currently supported AI tools:
- OpenAI Dall-E (also via Azure)
- Google Gemini
- Google Synthesize (text to speech)
- Google Imagen 3 (via Vertex AI)
- Ollama

Currently available AI purposes:
Expand Down
11 changes: 11 additions & 0 deletions amd/build/vertexcachestatus.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions amd/build/vertexcachestatus.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

142 changes: 142 additions & 0 deletions amd/src/vertexcachestatus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Module rendering the warning box to inform the users about misleading AI results.
*
* @module local_ai_manager/vertexcachestatus
* @copyright 2024 ISB Bayern
* @author Philipp Memmel
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

import Templates from 'core/templates';
import {call as fetchMany} from 'core/ajax';
import {alert as alertModal, exception as displayException} from 'core/notification';
import {getString} from 'core/str';

/**
* Fetches the current cache status of the specified service account.
*
* @param {string} serviceaccountinfo the stringified JSON with the service account info
*/
const fetchCurrentCacheStatus = (serviceaccountinfo) => fetchMany([{
methodname: 'local_ai_manager_vertex_cache_status',
args: {
serviceaccountinfo
}
}])[0];

/**
* Updates the current cache status.
*
* @param {string} serviceaccountinfo the stringified JSON with the service account info
* @param {boolean} newstatus true if the cache should be enabled, false if it should be disabled
*/
const setCurrentCacheStatus = (serviceaccountinfo, newstatus) => fetchMany([{
methodname: 'local_ai_manager_vertex_cache_status',
args: {
serviceaccountinfo,
newstatus
}
}])[0];

/**
* Controls and renders the Google Vertex AI cache status elements.
*
* @param {string} selector the CSS selector of the status element to operate on
*/
export const init = async(selector) => {
const statusElement = document.querySelector(selector);
const refreshButton = statusElement.querySelector('[data-action="refresh"]');
const enableCachingButton = statusElement.querySelector('[data-action="enablecaching"]');
const disableCachingButton = statusElement.querySelector('[data-action="disablecaching"]');
const serviceaccountinfoTextArea = document.getElementById('id_serviceaccountjson');
let serviceaccountinfo = serviceaccountinfoTextArea.value;
// We want to keep track of the current serviceaccountinfo data, also if the user changes it.
serviceaccountinfoTextArea.addEventListener('input', (event) => {
serviceaccountinfo = event.target.value;
});

refreshButton.addEventListener('click', async(event) => {
event.preventDefault();
await updateCachingStatusDisplay(serviceaccountinfo, statusElement);
});

if (enableCachingButton) {
enableCachingButton.addEventListener('click', async(event) => {
event.preventDefault();
enableCachingButton.disabled = true;
await updateCachingStatus(serviceaccountinfo, statusElement, true);
});
}
if (disableCachingButton) {
disableCachingButton.addEventListener('click', async(event) => {
event.preventDefault();
disableCachingButton.disabled = true;
await updateCachingStatus(serviceaccountinfo, statusElement, false);
});
}
};

/**
* Updates the caching status display.
*
* @param {string} serviceaccountinfo the stringified JSON with the service account info
* @param {string} statusElement the HTML element to operate on
*/
const updateCachingStatusDisplay = async(serviceaccountinfo, statusElement) => {
let queryResult = null;
try {
queryResult = await fetchCurrentCacheStatus(serviceaccountinfo);
} catch (error) {
await displayException(error);
return;
}
if (queryResult.code !== 200) {
const errorTitleString = await getString('vertex_error_cachestatus', 'local_ai_manager');
await alertModal(errorTitleString, queryResult.error);
}
const templateContext = {
cachingEnabled: queryResult.cachingEnabled,
noStatus: false
};

const {html, js} = await Templates.renderForPromise('local_ai_manager/vertexcachestatus', templateContext);
Templates.replaceNode(statusElement, html, js);
};

/**
* Updates the caching status and updates the DOM to reflect the current state.
*
* @param {string} serviceaccountinfo the stringified JSON with the service account info
* @param {string} statusElement the HTML element to operate on
* @param {boolean} newstatus the status to set the caching configuration to (true or false)
*/
const updateCachingStatus = async(serviceaccountinfo, statusElement, newstatus) => {
let queryResult = null;
try {
queryResult = await setCurrentCacheStatus(serviceaccountinfo, newstatus);
} catch (error) {
await displayException(error);
return;
}
if (queryResult.code !== 200) {
const errorTitleString = await getString('vertex_error_cachestatus', 'local_ai_manager');
await alertModal(errorTitleString, queryResult.error);
return;
}
await updateCachingStatusDisplay(serviceaccountinfo, statusElement);
};
10 changes: 6 additions & 4 deletions classes/base_connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ public function get_available_options(): array {
*/
public function make_request(array $data): request_response {
$client = new http_client([
'timeout' => get_config('local_ai_manager', 'requesttimeout'),
'verify' => !empty(get_config('local_ai_manager', 'verifyssl')),
'timeout' => get_config('local_ai_manager', 'requesttimeout'),
'verify' => !empty(get_config('local_ai_manager', 'verifyssl')),
]);

$options['headers'] = $this->get_headers();
Expand All @@ -177,7 +177,8 @@ public function make_request(array $data): request_response {
$return = request_response::create_from_error(
$response->getStatusCode(),
get_string('error_sendingrequestfailed', 'local_ai_manager'),
$response->getBody(),
$response->getBody()->getContents(),
$response->getBody()
);
}
return $return;
Expand Down Expand Up @@ -231,7 +232,8 @@ final protected function create_error_response_from_exception(ClientExceptionInt
if (method_exists($exception, 'getResponse') && !empty($exception->getResponse())) {
$debuginfo .= $exception->getResponse()->getBody()->getContents();
}
return request_response::create_from_error($exception->getCode(), $message, $debuginfo);
return request_response::create_from_error($exception->getCode(), $message, $debuginfo,
$exception->getResponse()->getBody());
}

/**
Expand Down
Loading