modules
3 years ago
admin.php
3 years ago
ai.php
3 years ago
answer.php
3 years ago
core.php
3 years ago
init.php
3 years ago
openai.php
3 years ago
query.php
3 years ago
queryimage.php
3 years ago
querytext.php
3 years ago
rest.php
3 years ago
ui.php
3 years ago
openai.php
154 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_OpenAI { |
| 4 | private $core = null; |
| 5 | private $apiKey = null; |
| 6 | |
| 7 | public function __construct( $core ) { |
| 8 | $this->core = $core; |
| 9 | $this->apiKey = $this->core->get_option( 'openai_apikey' ); |
| 10 | } |
| 11 | |
| 12 | public function listFiles() { |
| 13 | return $this->run( 'GET', '/files' ); |
| 14 | } |
| 15 | |
| 16 | function getSuffixForModel($model) { |
| 17 | preg_match( "/:([a-zA-Z\-]{1,40})-([0-9]{4})-([0-9]{2})-([0-9]{2})/", $model, $matches ); |
| 18 | if ( count( $matches ) > 0) { |
| 19 | return $matches[1] . ' (' . $matches[2] . '/' . $matches[3] . '/' . $matches[4] . ')'; |
| 20 | } |
| 21 | return 'N/A'; |
| 22 | } |
| 23 | |
| 24 | public function listFineTunes() { |
| 25 | $finetunes = $this->run( 'GET', '/fine-tunes' ); |
| 26 | $finetunes['data'] = array_map( function( $finetune ) { |
| 27 | $finetune['suffix'] = $this->getSuffixForModel( $finetune['fine_tuned_model'] ); |
| 28 | return $finetune; |
| 29 | }, $finetunes['data']); |
| 30 | |
| 31 | // Get option openai_finetunes_deleted |
| 32 | $deleted_finetunes = $this->core->get_option( 'openai_finetunes_deleted' ); |
| 33 | // Remove all deleted finetunes from the list, make a new array, without using array_filter |
| 34 | |
| 35 | $finetunes['data'] = array_values( array_filter( $finetunes['data'], function( $finetune ) use ( $deleted_finetunes ) { |
| 36 | return !in_array( $finetune['fine_tuned_model'], $deleted_finetunes ); |
| 37 | } ) ); |
| 38 | |
| 39 | $finetunes_option = $this->core->get_option( 'openai_finetunes' ); |
| 40 | $fresh_finetunes_options = array_map( function( $finetune ) use ( $finetunes_option ) { |
| 41 | $entry = []; |
| 42 | $model = $finetune['fine_tuned_model']; |
| 43 | $entry['suffix'] = $finetune['suffix']; |
| 44 | $entry['model'] = $model; |
| 45 | $entry['enabled'] = true; |
| 46 | for ( $i = 0; $i < count( $finetunes_option ); $i++ ) { |
| 47 | if ( $finetunes_option[$i]['model'] === $model ) { |
| 48 | $entry['enabled'] = $finetunes_option[$i]['enabled']; |
| 49 | break; |
| 50 | } |
| 51 | } |
| 52 | return $entry; |
| 53 | }, $finetunes['data']); |
| 54 | $this->core->update_option( 'openai_finetunes', $fresh_finetunes_options ); |
| 55 | return $finetunes; |
| 56 | } |
| 57 | |
| 58 | public function uploadFile( $filename, $data ) { |
| 59 | $result = $this->run( 'POST', '/files', null, [ 'data' => $data, 'filename' => $filename ] ); |
| 60 | return $result; |
| 61 | } |
| 62 | |
| 63 | public function deleteFile( $fileId ) { |
| 64 | return $this->run( 'DELETE', '/files/' . $fileId ); |
| 65 | } |
| 66 | |
| 67 | public function deleteFineTune( $modelId ) { |
| 68 | return $this->run( 'DELETE', '/models/' . $modelId ); |
| 69 | } |
| 70 | |
| 71 | public function downloadFile( $fileId ) { |
| 72 | return $this->run( 'GET', '/files/' . $fileId . '/content', null, null, false ); |
| 73 | } |
| 74 | |
| 75 | public function fineTuneFile( $fileId, $model, $suffix ) { |
| 76 | $result = $this->run( 'POST', '/fine-tunes', [ |
| 77 | 'training_file' => $fileId, |
| 78 | 'model' => $model, |
| 79 | 'suffix' => $suffix |
| 80 | ] ); |
| 81 | return $result; |
| 82 | } |
| 83 | |
| 84 | public function create_body_for_file( $file, $boundary ) { |
| 85 | $fields = array( |
| 86 | 'purpose' => 'fine-tune', |
| 87 | 'file' => $file['filename'] |
| 88 | ); |
| 89 | |
| 90 | $body = ''; |
| 91 | foreach ( $fields as $name => $value ) { |
| 92 | $body .= "--$boundary\r\n"; |
| 93 | $body .= "Content-Disposition: form-data; name=\"$name\""; |
| 94 | if ( $name == 'file' ) { |
| 95 | $body .= "; filename=\"{$value}\"\r\n"; |
| 96 | $body .= "Content-Type: application/json\r\n\r\n"; |
| 97 | $body .= $file['data'] . "\r\n"; |
| 98 | } else { |
| 99 | $body .= "\r\n\r\n$value\r\n"; |
| 100 | } |
| 101 | } |
| 102 | $body .= "--$boundary--\r\n"; |
| 103 | return $body; |
| 104 | } |
| 105 | |
| 106 | public function run( $method, $url, $query = null, $file = null, $json = true ) { |
| 107 | $apiKey = $this->apiKey; |
| 108 | $headers = "Content-Type: application/json\r\n" . "Authorization: Bearer " . $apiKey . "\r\n"; |
| 109 | $body = $query ? json_encode( $query ) : null; |
| 110 | if ( !empty( $file ) ) { |
| 111 | $boundary = wp_generate_password( 24, false ); |
| 112 | $headers = [ |
| 113 | 'Content-Type' => 'multipart/form-data; boundary=' . $boundary, |
| 114 | 'Authorization' => 'Bearer ' . $this->apiKey, |
| 115 | ]; |
| 116 | $body = $this->create_body_for_file( $file, $boundary ); |
| 117 | } |
| 118 | |
| 119 | $url = 'https://api.openai.com/v1' . $url; |
| 120 | $options = [ |
| 121 | "headers" => $headers, |
| 122 | "method" => $method, |
| 123 | "timeout" => 60, |
| 124 | "body" => $body, |
| 125 | "sslverify" => false |
| 126 | ]; |
| 127 | |
| 128 | try { |
| 129 | $response = wp_remote_request( $url, $options ); |
| 130 | if ( is_wp_error( $response ) ) { |
| 131 | throw new Exception( $response->get_error_message() ); |
| 132 | } |
| 133 | $response = wp_remote_retrieve_body( $response ); |
| 134 | $data = $json ? json_decode( $response, true ) : $response; |
| 135 | |
| 136 | // Error handling |
| 137 | if ( isset( $data['error'] ) ) { |
| 138 | $message = $data['error']['message']; |
| 139 | // If the message contains "Incorrect API key provided: THE_KEY.", replace the key by "----". |
| 140 | if ( preg_match( '/API key provided(: .*)\./', $message, $matches ) ) { |
| 141 | $message = str_replace( $matches[1], '', $message ); |
| 142 | } |
| 143 | throw new Exception( $message ); |
| 144 | } |
| 145 | |
| 146 | return $data; |
| 147 | } |
| 148 | catch ( Exception $e ) { |
| 149 | error_log( $e->getMessage() ); |
| 150 | throw new Exception( 'Error while calling OpenAI: ' . $e->getMessage() ); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 |