Skip to content

Commit

Permalink
Improves error messaging in question generator
Browse files Browse the repository at this point in the history
  • Loading branch information
clpetersonucf committed Sep 4, 2024
1 parent 88d194e commit 8445aae
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
17 changes: 13 additions & 4 deletions fuel/app/classes/materia/api/v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -875,10 +875,19 @@ static public function question_set_generate($inst_id, $widget_id, $topic, $incl
if ($num_questions < 1) $num_questions = 8;
if ($num_questions > 32) $num_questions = 32;

return [
...Widget_Question_Generator::generate_qset($inst, $widget, $topic, $include_images, $num_questions, $build_off_existing),
'title' => $topic
];
$query = Widget_Question_Generator::generate_qset($inst, $widget, $topic, $include_images, $num_questions, $build_off_existing);
if ( ! $query instanceof Msg && is_array($query))
{
return [
...$query,
'title' => $topic
];
}
else
{
\Log::error(print_r($query, true));
return $query;
}
}

/**
Expand Down
10 changes: 5 additions & 5 deletions fuel/app/classes/materia/widget/question/generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ public static function query($prompt)
*/
static public function generate_qset($inst, $widget, $topic, $include_images, $num_questions, $existing)
{
if ( ! self::is_enabled()) return new Msg(Msg::ERROR, 'Question generation is not enabled.');
if ( ! self::is_enabled()) return Msg::failure('Question generation is not enabled.');

// 'allow images' environment variable overrides whatever the api request sends
if ( empty(\Config::get('materia.ai_generation.allow_images'))) $include_images = false;

$demo = Widget_Instance_Manager::get($widget->meta_data['demo']);
if ( ! $demo) throw new \HttpNotFoundException;
if ( ! $demo) return Msg::not_found();

if ($inst) $instance_name = $inst->name;
$widget_name = $widget->name;
Expand All @@ -151,7 +151,7 @@ static public function generate_qset($inst, $widget, $topic, $include_images, $n
{
if ( ! $inst) return Msg::invalid_input('Requires a previously saved instance to build from.');
$qset = Api_V1::question_set_get($inst->id);
if ( ! $qset) return new Msg(Msg::ERROR, 'No existing question set found');
if ( ! $qset) return Msg::failure('No existing question set found.');
$qset_version = $qset->version;

$qset_text = json_encode($qset->data);
Expand Down Expand Up @@ -186,7 +186,7 @@ static public function generate_qset($inst, $widget, $topic, $include_images, $n
// get the demo.json from the demo instance
$demo_qset = Api_V1::question_set_get($widget->meta_data['demo']);
$qset_version = $demo_qset->version;
if ( ! $demo_qset) throw new \HttpNotFoundException;
if ( ! $demo_qset) return Msg::not_found('Could not locate demo question set for widget engine.');
$qset_text = json_encode($demo_qset->data);

// non-image prompt
Expand Down Expand Up @@ -250,7 +250,7 @@ static public function generate_qset($inst, $widget, $topic, $include_images, $n
.'Number of questions asked to generate: '.$num_questions.PHP_EOL
.'Error: '.$e->getMessage().PHP_EOL);

return new Msg(Msg::ERROR, 'Error generating question set');
return Msg::failure('Error generating question set.');
}

if ($include_images) $question_set = static::generate_images($question_set, $existing);
Expand Down

0 comments on commit 8445aae

Please sign in to comment.