public static function chat(string $systemChat, string $userChat, string $responseType = 'text')
{
$parameters = [
'model' => 'gpt-4o-mini',
'messages' => [
['role' => 'system', 'content' => $systemChat],
['role' => 'user', 'content' => $userChat],
],
'temperature' => 0.002,
];
if ($responseType == 'json') {
// Tell OpenAI to return the response as a JSON object
$parameters['response_format'] = ['type' => 'json_object'];
}
$client = self::getClient();
$result = $client->chat()->create($parameters);
if ($responseType == 'json') {
return json_decode($result->choices[0]->message->content, true);
}
return $result->choices[0]->message->content;
}