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

class AICore
{
    const API_KEY = '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';
    static function chat($options, $sys_inst, $user_input = null)
    {

        $messages = [
            [
                'role' => 'system',
                'content' =>  [
                    ['text' => join('\n', $sys_inst), 'type' => 'text']
                ]
            ]
        ];

        if (!is_null($user_input)) {
            $messages[] = [
                'role' => 'user',
                'content' => [
                    ['text' => $user_input, 'type' => 'text']
                ]
            ];
        }

        $client = OpenAI::client(AICore::API_KEY);
        $options['messages'] = $messages;
        $response = $client->chat()->create($options);

        $text_output = '';
        foreach ($response->choices as $result)
            $text_output .= $result->message->content;

        return $text_output;
    }
}
