| 9 | 
           ariadna | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
           // This file is part of Moodle - http://moodle.org/
  | 
        
        
            | 
            | 
           3 | 
           //
  | 
        
        
            | 
            | 
           4 | 
           // Moodle is free software: you can redistribute it and/or modify
  | 
        
        
            | 
            | 
           5 | 
           // it under the terms of the GNU General Public License as published by
  | 
        
        
            | 
            | 
           6 | 
           // the Free Software Foundation, either version 3 of the License, or
  | 
        
        
            | 
            | 
           7 | 
           // (at your option) any later version.
  | 
        
        
            | 
            | 
           8 | 
           //
  | 
        
        
            | 
            | 
           9 | 
           // Moodle is distributed in the hope that it will be useful,
  | 
        
        
            | 
            | 
           10 | 
           // but WITHOUT ANY WARRANTY; without even the implied warranty of
  | 
        
        
            | 
            | 
           11 | 
           // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  | 
        
        
            | 
            | 
           12 | 
           // GNU General Public License for more details.
  | 
        
        
            | 
            | 
           13 | 
           //
  | 
        
        
            | 
            | 
           14 | 
           // You should have received a copy of the GNU General Public License
  | 
        
        
            | 
            | 
           15 | 
           // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  | 
        
        
            | 
            | 
           16 | 
              | 
        
        
            | 
            | 
           17 | 
           /**
  | 
        
        
            | 
            | 
           18 | 
            * Class providing completions for assistant API
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * @package    block_openai_chat
  | 
        
        
            | 
            | 
           21 | 
            * @copyright  2023 Bryce Yoder <me@bryceyoder.com>
  | 
        
        
            | 
            | 
           22 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           23 | 
           */
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           namespace block_openai_chat\completion;
  | 
        
        
            | 
            | 
           26 | 
              | 
        
        
            | 
            | 
           27 | 
           use block_openai_chat\completion;
  | 
        
        
            | 
            | 
           28 | 
           defined('MOODLE_INTERNAL') || die;
  | 
        
        
            | 
            | 
           29 | 
              | 
        
        
            | 
            | 
           30 | 
           class assistant extends \block_openai_chat\completion {
  | 
        
        
            | 
            | 
           31 | 
              | 
        
        
            | 
            | 
           32 | 
               private $thread_id;
  | 
        
        
            | 
            | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
               public function __construct($model, $message, $history, $block_settings, $thread_id) {
  | 
        
        
            | 
            | 
           35 | 
                   parent::__construct($model, $message, $history, $block_settings);
  | 
        
        
            | 
            | 
           36 | 
              | 
        
        
            | 
            | 
           37 | 
                   // If thread_id is NULL, create a new thread
  | 
        
        
            | 
            | 
           38 | 
                   if (!$thread_id) {
  | 
        
        
            | 
            | 
           39 | 
                       $thread_id = $this->create_thread();
  | 
        
        
            | 
            | 
           40 | 
                   }
  | 
        
        
            | 
            | 
           41 | 
                   $this->thread_id = $thread_id;
  | 
        
        
            | 
            | 
           42 | 
               }
  | 
        
        
            | 
            | 
           43 | 
              | 
        
        
            | 
            | 
           44 | 
               /**
  | 
        
        
            | 
            | 
           45 | 
                * Given everything we know after constructing the parent, create a completion by constructing the prompt and making the api call
  | 
        
        
            | 
            | 
           46 | 
                * @return JSON: The API response from OpenAI
  | 
        
        
            | 
            | 
           47 | 
                */
  | 
        
        
            | 
            | 
           48 | 
               public function create_completion($context) {
  | 
        
        
            | 
            | 
           49 | 
                   $this->add_message_to_thread();
  | 
        
        
            | 
            | 
           50 | 
                   return $this->run();
  | 
        
        
            | 
            | 
           51 | 
               }
  | 
        
        
            | 
            | 
           52 | 
              | 
        
        
            | 
            | 
           53 | 
               private function create_thread() {
  | 
        
        
            | 
            | 
           54 | 
                   $curl = new \curl();
  | 
        
        
            | 
            | 
           55 | 
                   $curl->setopt(array(
  | 
        
        
            | 
            | 
           56 | 
                       'CURLOPT_HTTPHEADER' => array(
  | 
        
        
            | 
            | 
           57 | 
                           'Authorization: Bearer ' . $this->apikey,
  | 
        
        
            | 
            | 
           58 | 
                           'Content-Type: application/json',
  | 
        
        
            | 
            | 
           59 | 
                           'OpenAI-Beta: assistants=v2'
  | 
        
        
            | 
            | 
           60 | 
                       ),
  | 
        
        
            | 
            | 
           61 | 
                   ));
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
                   $response = $curl->post("https://api.openai.com/v1/threads");
  | 
        
        
            | 
            | 
           64 | 
                   $response = json_decode($response);
  | 
        
        
            | 
            | 
           65 | 
              | 
        
        
            | 
            | 
           66 | 
                   return $response->id;
  | 
        
        
            | 
            | 
           67 | 
               }
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
               private function add_message_to_thread() {
  | 
        
        
            | 
            | 
           70 | 
                   $curlbody = [
  | 
        
        
            | 
            | 
           71 | 
                       "role" => "user",
  | 
        
        
            | 
            | 
           72 | 
                       "content" => $this->message
  | 
        
        
            | 
            | 
           73 | 
                   ];
  | 
        
        
            | 
            | 
           74 | 
              | 
        
        
            | 
            | 
           75 | 
                   $curl = new \curl();
  | 
        
        
            | 
            | 
           76 | 
                   $curl->setopt(array(
  | 
        
        
            | 
            | 
           77 | 
                       'CURLOPT_HTTPHEADER' => array(
  | 
        
        
            | 
            | 
           78 | 
                           'Authorization: Bearer ' . $this->apikey,
  | 
        
        
            | 
            | 
           79 | 
                           'Content-Type: application/json',
  | 
        
        
            | 
            | 
           80 | 
                           'OpenAI-Beta: assistants=v2'
  | 
        
        
            | 
            | 
           81 | 
                       ),
  | 
        
        
            | 
            | 
           82 | 
                   ));
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
                   $response = $curl->post(
  | 
        
        
            | 
            | 
           85 | 
                       "https://api.openai.com/v1/threads/" . $this->thread_id ."/messages",
  | 
        
        
            | 
            | 
           86 | 
                       json_encode($curlbody)
  | 
        
        
            | 
            | 
           87 | 
                   );
  | 
        
        
            | 
            | 
           88 | 
                   $response = json_decode($response);
  | 
        
        
            | 
            | 
           89 | 
              | 
        
        
            | 
            | 
           90 | 
                   return $response->id;
  | 
        
        
            | 
            | 
           91 | 
               }
  | 
        
        
            | 
            | 
           92 | 
              | 
        
        
            | 
            | 
           93 | 
               /**
  | 
        
        
            | 
            | 
           94 | 
                * Make the actual API call to OpenAI
  | 
        
        
            | 
            | 
           95 | 
                * @return JSON: The response from OpenAI
  | 
        
        
            | 
            | 
           96 | 
                */
  | 
        
        
            | 
            | 
           97 | 
               private function run() {
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
                   $curlbody = [
  | 
        
        
            | 
            | 
           100 | 
                       "assistant_id" => $this->assistant,
  | 
        
        
            | 
            | 
           101 | 
                   ];
  | 
        
        
            | 
            | 
           102 | 
                   if ($this->instructions) {
  | 
        
        
            | 
            | 
           103 | 
                       $curlbody["instructions"] = $this->instructions;
  | 
        
        
            | 
            | 
           104 | 
                   }
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
            | 
            | 
           106 | 
                   $curl = new \curl();
  | 
        
        
            | 
            | 
           107 | 
                   $curl->setopt(array(
  | 
        
        
            | 
            | 
           108 | 
                       'CURLOPT_HTTPHEADER' => array(
  | 
        
        
            | 
            | 
           109 | 
                           'Authorization: Bearer ' . $this->apikey,
  | 
        
        
            | 
            | 
           110 | 
                           'Content-Type: application/json',
  | 
        
        
            | 
            | 
           111 | 
                           'OpenAI-Beta: assistants=v2'
  | 
        
        
            | 
            | 
           112 | 
                       ),
  | 
        
        
            | 
            | 
           113 | 
                   ));
  | 
        
        
            | 
            | 
           114 | 
              | 
        
        
            | 
            | 
           115 | 
                   $response = $curl->post(
  | 
        
        
            | 
            | 
           116 | 
                       "https://api.openai.com/v1/threads/" . $this->thread_id . "/runs",
  | 
        
        
            | 
            | 
           117 | 
                       json_encode($curlbody)
  | 
        
        
            | 
            | 
           118 | 
                   );
  | 
        
        
            | 
            | 
           119 | 
                   $response = json_decode($response);
  | 
        
        
            | 
            | 
           120 | 
              | 
        
        
            | 
            | 
           121 | 
                   if (isset($response->error)) {
  | 
        
        
            | 
            | 
           122 | 
                       throw new \Exception($response->error->message);
  | 
        
        
            | 
            | 
           123 | 
                   }
  | 
        
        
            | 
            | 
           124 | 
              | 
        
        
            | 
            | 
           125 | 
                   $run_id = $response->id;
  | 
        
        
            | 
            | 
           126 | 
                   $run_completed = false;
  | 
        
        
            | 
            | 
           127 | 
                   while (!$run_completed) {
  | 
        
        
            | 
            | 
           128 | 
                       $run_completed = $this->check_run_status($run_id);
  | 
        
        
            | 
            | 
           129 | 
                       sleep(1);
  | 
        
        
            | 
            | 
           130 | 
                   }
  | 
        
        
            | 
            | 
           131 | 
              | 
        
        
            | 
            | 
           132 | 
                   $curl = new \curl();
  | 
        
        
            | 
            | 
           133 | 
                   $curl->setopt(array(
  | 
        
        
            | 
            | 
           134 | 
                       'CURLOPT_HTTPHEADER' => array(
  | 
        
        
            | 
            | 
           135 | 
                           'Authorization: Bearer ' . $this->apikey,
  | 
        
        
            | 
            | 
           136 | 
                           'Content-Type: application/json',
  | 
        
        
            | 
            | 
           137 | 
                           'OpenAI-Beta: assistants=v2'
  | 
        
        
            | 
            | 
           138 | 
                       ),
  | 
        
        
            | 
            | 
           139 | 
                   ));
  | 
        
        
            | 
            | 
           140 | 
                   $response = $curl->get("https://api.openai.com/v1/threads/" . $this->thread_id . '/messages');
  | 
        
        
            | 
            | 
           141 | 
                   $response = json_decode($response);
  | 
        
        
            | 
            | 
           142 | 
              | 
        
        
            | 
            | 
           143 | 
                   return [
  | 
        
        
            | 
            | 
           144 | 
                       "id" => $response->data[0]->id,
  | 
        
        
            | 
            | 
           145 | 
                       "message" => $response->data[0]->content[0]->text->value,
  | 
        
        
            | 
            | 
           146 | 
                       "thread_id" => $response->data[0]->thread_id
  | 
        
        
            | 
            | 
           147 | 
                   ];
  | 
        
        
            | 
            | 
           148 | 
               }
  | 
        
        
            | 
            | 
           149 | 
              | 
        
        
            | 
            | 
           150 | 
               private function check_run_status($run_id) {
  | 
        
        
            | 
            | 
           151 | 
                   $curl = new \curl();
  | 
        
        
            | 
            | 
           152 | 
                   $curl->setopt(array(
  | 
        
        
            | 
            | 
           153 | 
                       'CURLOPT_HTTPHEADER' => array(
  | 
        
        
            | 
            | 
           154 | 
                           'Authorization: Bearer ' . $this->apikey,
  | 
        
        
            | 
            | 
           155 | 
                           'Content-Type: application/json',
  | 
        
        
            | 
            | 
           156 | 
                           'OpenAI-Beta: assistants=v2'
  | 
        
        
            | 
            | 
           157 | 
                       ),
  | 
        
        
            | 
            | 
           158 | 
                   ));
  | 
        
        
            | 
            | 
           159 | 
              | 
        
        
            | 
            | 
           160 | 
                   $response = $curl->get("https://api.openai.com/v1/threads/" . $this->thread_id . "/runs/" . $run_id);
  | 
        
        
            | 
            | 
           161 | 
                   $response = json_decode($response);
  | 
        
        
            | 
            | 
           162 | 
              | 
        
        
            | 
            | 
           163 | 
                   if ($response->status === 'completed') {
  | 
        
        
            | 
            | 
           164 | 
                       return true;
  | 
        
        
            | 
            | 
           165 | 
                   }
  | 
        
        
            | 
            | 
           166 | 
                   return false;
  | 
        
        
            | 
            | 
           167 | 
               }
  | 
        
        
            | 
            | 
           168 | 
           }
  |