Skip to content

Commit

Permalink
Fix to previous fixes. Adds format param to generator query method.
Browse files Browse the repository at this point in the history
  • Loading branch information
clpetersonucf committed Sep 17, 2024
1 parent a3e6b3c commit 0fa59af
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
17 changes: 7 additions & 10 deletions fuel/app/classes/materia/widget/question/generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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]
],
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion fuel/app/config/materia.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0fa59af

Please sign in to comment.