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