diff --git a/fuel/app/classes/materia/widget/question/generator.php b/fuel/app/classes/materia/widget/question/generator.php index a50e49a39..dfe8982f2 100644 --- a/fuel/app/classes/materia/widget/question/generator.php +++ b/fuel/app/classes/materia/widget/question/generator.php @@ -17,7 +17,7 @@ protected static function get_client() { if (empty(\Config::get('materia.ai_generation.provider'))) { - \Log::error('GENERATION ERROR: Question generation configs missing.'); + \Log::error('GENERATION ERROR: Question generation provider config missing.'); return null; } @@ -29,7 +29,7 @@ protected static function get_client() if (empty($api_key) || empty($endpoint) || empty($api_version)) { - \Log::error('GENERATION ERROR: Question generation configs missing.'); + \Log::error('GENERATION ERROR: Azure OpenAI question generation configs missing.'); return null; } @@ -53,7 +53,7 @@ protected static function get_client() if (empty($api_key)) { - \Log::error('Question generation configs missing.'); + \Log::error('OpenAI Platform question generation configs missing.'); return null; } @@ -84,13 +84,12 @@ public static function is_enabled() * @param string $prompt The prompt for the query. * @return object The result of the query. */ - public static function query($prompt) + public static function query($prompt, $format='json') { $client = static::get_client(); if (empty($client)) return Msg::failure('Failed to initialize generation client.'); $params = [ - 'response_format' => (object) ['type' => 'json_object'], 'messages' => [ ['role' => 'user', 'content' => $prompt] ], @@ -101,10 +100,8 @@ public static function query($prompt) 'top_p' => 1, // 0 to 1 ]; - if ( ! empty(\Config::get('materia.ai_generation.model'))) - { - $params['model'] = \Config::get('materia.ai_generation.model'); - } + if ( ! empty(\Config::get('materia.ai_generation.model'))) $params['model'] = \Config::get('materia.ai_generation.model'); + if ($format == 'json') $params['response_format'] = (object) ['type' => 'json_object']; return $client->chat()->create($params); } @@ -222,7 +219,7 @@ static public function generate_qset($inst, $widget, $topic, $include_images, $n // send the prompt to to the generative AI provider try { - $result = self::query($text); + $result = self::query($text, 'json'); // received the qset - decode the json string from the result $question_set = json_decode($result->choices[0]->message->content); diff --git a/fuel/app/config/materia.php b/fuel/app/config/materia.php index 3362043e6..30cce8412 100644 --- a/fuel/app/config/materia.php +++ b/fuel/app/config/materia.php @@ -129,7 +129,7 @@ 'allow_images' => filter_var($_ENV['GENERATION_ALLOW_IMAGES'] ?? false, FILTER_VALIDATE_BOOLEAN), 'provider' => $_ENV['GENERATION_API_PROVIDER'] ?? '', 'endpoint' => $_ENV['GENERATION_API_ENDPOINT'] ?? '', - 'api_key' => filter_var($_ENV['GENERATION_API_KEY'] ?? false, FILTER_VALIDATE_BOOLEAN), + 'api_key' => $_ENV['GENERATION_API_KEY'] ?? '', 'api_version' => $_ENV['GENERATION_API_VERSION'] ?? '', 'model' => $_ENV['GENERATION_API_MODEL'] ?? '', 'log_stats' => filter_var($_ENV['GENERATION_LOG_STATS'] ?? false, FILTER_VALIDATE_BOOLEAN)