From beecbfe508c543b3b41522de756be2561f0fac29 Mon Sep 17 00:00:00 2001 From: Marcus Green Date: Sat, 1 Jun 2024 23:24:56 +0100 Subject: [PATCH] add json mode parameter to constructor --- classes/ai/ai.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/classes/ai/ai.php b/classes/ai/ai.php index e5d8e71..9e6db1b 100644 --- a/classes/ai/ai.php +++ b/classes/ai/ai.php @@ -58,16 +58,23 @@ class ai { */ private string $endpoint; + /** + * response_format + * @var string + */ + private string $format; + /** * Initialise default settings * * @param string $model */ - public function __construct($model = null) { + public function __construct(?string $model = null, ?string $format = null) { $this->model = $model ?? trim(explode(',', get_config('tool_aiconnect', 'model'))[0]); $this->openaiapikey = get_config('tool_aiconnect', 'apikey'); $this->temperature = get_config('tool_aiconnect', 'temperature'); $this->endpoint = trim(get_config('tool_aiconnect', 'endpoint')); + $this->format = $format ?? ''; } /** @@ -148,7 +155,7 @@ public function prompt_completion($prompttext) { * @param string $prompttext The prompt text. * @return array The prompt data. */ - private function get_prompt_data($prompttext): array { + private function get_prompt_data(string $prompttext): array { $data = [ 'model' => $this->model, 'temperature' => $this->temperature, @@ -156,6 +163,9 @@ private function get_prompt_data($prompttext): array { ['role' => 'system', 'content' => 'You: ' . $prompttext], ], ]; + if ($this->format == 'json') { + $data['response_format'] = ['type' => 'json_object']; + } return $data; } /**