<?php
require __DIR__ . '/vendor/autoload.php';


use Orhanerday\OpenAi\OpenAi;


function ValidateRequest()
{
    $token=md5(date('Y-m').'_sl95_');
    if(!$_GET['token'] || $_GET['token']!=$token) die(json_encode(['error'=>'Auth_Error','message'=>'Authorization Failed']));
}

function Ask($msg)
{
    $open_ai = new OpenAi('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');
    $system_msg='You are a helpful assistant.';
    $complete = $open_ai->chat([
       'model' => 'gpt-3.5-turbo',
       'messages' => [
           [
               "role" => "system",
               "content" => $system_msg
           ],
           [
               "role" => "user",
               "content" => $msg
           ]
       ],
       'temperature' => 0.6,
       'max_tokens' => 1000,
       'frequency_penalty' => 0,
       'presence_penalty' => 0,
    ]);
    return $complete;
}

switch($_GET['action']){
    case 'chat':
        break;
    case 'ask':
        ValidateRequest();
        die(Ask($_REQUEST['msg']));
        break;
}
?>
