From 20a6ae49e5c4d70c6beb243651728052ec53a6f0 Mon Sep 17 00:00:00 2001 From: Corey Peterson Date: Wed, 18 Sep 2024 10:09:10 -0400 Subject: [PATCH] Comments & code cleanup --- fuel/app/classes/materia/api/v1.php | 8 ++++++++ fuel/app/classes/materia/widget/question/generator.php | 7 ++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/fuel/app/classes/materia/api/v1.php b/fuel/app/classes/materia/api/v1.php index d9e95b95d..bc8d2538f 100644 --- a/fuel/app/classes/materia/api/v1.php +++ b/fuel/app/classes/materia/api/v1.php @@ -894,11 +894,19 @@ static public function question_set_generate($inst_id, $widget_id, $topic, $incl } } + /** + * Endpoint to facilitate AI text generation for widgets + * + * @param string $prompt The prompt to generate. + * @return array An array to be passed back to the widget containing the response string + */ static public function widget_prompt_generate($prompt) { + // verify eligibility if ( ! Widget_Question_Generator::is_enabled()) return Msg::failure(); if (\Service_User::verify_session() !== true) return Msg::no_login(); + // prompt generation & response handling $result = Widget_Question_Generator::generate_from_prompt($prompt); if ( ! $result instanceof Msg && is_string($result)) { diff --git a/fuel/app/classes/materia/widget/question/generator.php b/fuel/app/classes/materia/widget/question/generator.php index 41b5f9801..16cdfd193 100644 --- a/fuel/app/classes/materia/widget/question/generator.php +++ b/fuel/app/classes/materia/widget/question/generator.php @@ -106,10 +106,15 @@ public static function query($prompt, $format='json') return $client->chat()->create($params); } + /** + * Generates a text response based on the provided prompt. + * + * @param string $prompt The prompt to send to the LLM. + * @return string The generated response. + */ static public function generate_from_prompt($prompt) { if ( ! self::is_enabled()) return Msg::failure('Question generation is not enabled.'); - if (empty($prompt) || strlen($prompt) > 10000) return Msg::invalid_input('Prompt text length invalid.'); try