-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d3f6f6a
commit 8c364b1
Showing
10 changed files
with
166 additions
and
100 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Release 0.01 of the Moodle AIText question type May 2024 | ||
Version 2024050300 | ||
Many thanks to Justin Hunt of Poodll fame https://poodll.com/moodle for contributing code | ||
to allw the testing of prompts from within the question editing form. | ||
|
||
Refined the default prompt settings to ensure Ollama/mistral returns a number when grading | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,101 @@ | ||
<?php | ||
/** | ||
* External. | ||
* | ||
* @package qtype_aitext | ||
* @author Justin Hunt - poodll.com | ||
*/ | ||
|
||
|
||
global $CFG; | ||
require_once($CFG->libdir . '/externallib.php'); | ||
require_once($CFG->dirroot . '/question/engine/bank.php'); | ||
|
||
use tool_aiconnect\ai; | ||
|
||
|
||
|
||
/** | ||
* External class. | ||
* | ||
* @package qtype_aitext | ||
* @author Justin Hunt - poodll.com | ||
*/ | ||
class qtype_aitext_external extends external_api | ||
{ | ||
|
||
public static function fetch_ai_grade_parameters() | ||
{ | ||
return new external_function_parameters( | ||
array('response' => new external_value(PARAM_TEXT, 'The students response to question'), | ||
'defaultmark' => new external_value(PARAM_INT, 'The total possible score'), | ||
'prompt' => new external_value(PARAM_TEXT, 'The AI Prompt'), | ||
'marksscheme' => new external_value(PARAM_TEXT, 'The marks scheme') | ||
) | ||
); | ||
|
||
} | ||
|
||
public static function fetch_ai_grade($response,$defaultmark,$prompt,$marksscheme) | ||
{ | ||
//get our AI helper | ||
$ai = new ai\ai(); | ||
|
||
//build an aitext question instance so we can call the same code that the question type uses when it grades | ||
$type = 'aitext'; | ||
\question_bank::load_question_definition_classes($type); | ||
$aiquestion = new qtype_aitext_question(); | ||
$aiquestion->qtype = \question_bank::get_qtype('aitext'); | ||
|
||
//make sure we have the right data for AI to work with | ||
if (!empty($response) && !empty($prompt) && $defaultmark > 0) { | ||
$full_ai_prompt = $aiquestion->build_full_ai_prompt($response, $prompt, $defaultmark, $marksscheme); | ||
$llmresponse = $ai->prompt_completion($full_ai_prompt); | ||
$feedback = $llmresponse['response']['choices'][0]['message']['content']; | ||
$contentobject = $aiquestion->process_feedback($feedback); | ||
}else{ | ||
$contentobject = ["feedback" => "Invalid parameters. Check that you have a sample answer and prompt","marks" => 0]; | ||
} | ||
|
||
//return whatever we have got | ||
return $contentobject; | ||
|
||
} | ||
|
||
public static function fetch_ai_grade_returns() | ||
{ | ||
return new external_single_structure([ | ||
'feedback' => new external_value(PARAM_TEXT, 'text feedback for display to student', VALUE_DEFAULT), | ||
'marks' => new external_value(PARAM_FLOAT, 'AI grader awarded marks for student response', VALUE_DEFAULT), | ||
]); | ||
|
||
} | ||
|
||
} | ||
<?php | ||
// 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/>. | ||
|
||
/** | ||
* External | ||
* | ||
* @package qtype_aitext | ||
* @author Justin Hunt - poodll.com | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
global $CFG; | ||
require_once($CFG->libdir . '/externallib.php'); | ||
require_once($CFG->dirroot . '/question/engine/bank.php'); | ||
|
||
use tool_aiconnect\ai; | ||
|
||
/** | ||
* External class. | ||
* | ||
* @package qtype_aitext | ||
* @author Justin Hunt - poodll.com | ||
*/ | ||
class qtype_aitext_external extends external_api { | ||
/** | ||
* Get the parameters and types | ||
* | ||
* @return void | ||
*/ | ||
public static function fetch_ai_grade_parameters() { | ||
return new external_function_parameters( | ||
array('response' => new external_value(PARAM_TEXT, 'The students response to question'), | ||
'defaultmark' => new external_value(PARAM_INT, 'The total possible score'), | ||
'prompt' => new external_value(PARAM_TEXT, 'The AI Prompt'), | ||
'marksscheme' => new external_value(PARAM_TEXT, 'The marks scheme') | ||
) | ||
); | ||
|
||
} | ||
/** | ||
* Similar to clicking the submit button. | ||
* | ||
* @param array $response | ||
* @param integer $defaultmark | ||
* @param string $prompt | ||
* @param string $marksscheme | ||
* @return void | ||
*/ | ||
public static function fetch_ai_grade($response, $defaultmark, $prompt, $marksscheme) { | ||
// Get our AI helper. | ||
$ai = new ai\ai(); | ||
|
||
// Build an aitext question instance so we can call the same code that the question type uses when it grades. | ||
$type = 'aitext'; | ||
\question_bank::load_question_definition_classes($type); | ||
$aiquestion = new qtype_aitext_question(); | ||
$aiquestion->qtype = \question_bank::get_qtype('aitext'); | ||
|
||
// Make sure we have the right data for AI to work with. | ||
if (!empty($response) && !empty($prompt) && $defaultmark > 0) { | ||
$fullaiprompt = $aiquestion->build_full_ai_prompt($response, $prompt, $defaultmark, $marksscheme); | ||
$llmresponse = $ai->prompt_completion($fullaiprompt); | ||
$feedback = $llmresponse['response']['choices'][0]['message']['content']; | ||
$contentobject = $aiquestion->process_feedback($feedback); | ||
} else { | ||
$contentobject = ["feedback" => "Invalid parameters. Check that you have a sample answer and prompt", "marks" => 0]; | ||
} | ||
|
||
// Return whatever we have got. | ||
return $contentobject; | ||
|
||
} | ||
|
||
/** | ||
* Get the structure for retuning grade feedbak and marks | ||
* | ||
* @return void | ||
*/ | ||
public static function fetch_ai_grade_returns() { | ||
return new external_single_structure([ | ||
'feedback' => new external_value(PARAM_TEXT, 'text feedback for display to student', VALUE_DEFAULT), | ||
'marks' => new external_value(PARAM_FLOAT, 'AI grader awarded marks for student response', VALUE_DEFAULT), | ||
]); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,42 @@ | ||
<?php | ||
// 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/>. | ||
|
||
/** | ||
* Aitext services definition | ||
* | ||
* @package qtype_aitext | ||
* @author Justin Hunt - poodll.com | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
/** | ||
* Services definition. | ||
* External class. | ||
* | ||
* @package qtype_minispeak | ||
* @package qtype_aitext | ||
* @author Justin Hunt - poodll.com | ||
*/ | ||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
$functions = array( | ||
'qtype_aitext_fetch_ai_grade' => array( | ||
'classname' => 'qtype_aitext_external', | ||
'methodname' => 'fetch_ai_grade', | ||
'description' => 'checks a response with the AI grader' , | ||
'capabilities'=> 'mod/quiz:grade', | ||
'capabilities' => 'mod/quiz:grade', | ||
'type' => 'read', | ||
'ajax' => true, | ||
), | ||
); | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.