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
127 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 | public function listFineTunes() { |
| 17 | return $this->run( 'GET', '/fine-tunes' ); |
| 18 | } |
| 19 | |
| 20 | public function uploadFile( $filename, $data ) { |
| 21 | $result = $this->run( 'POST', '/files', null, [ 'data' => $data, 'filename' => $filename ] ); |
| 22 | return $result; |
| 23 | } |
| 24 | |
| 25 | public function deleteFile( $fileId ) { |
| 26 | return $this->run( 'DELETE', '/files/' . $fileId ); |
| 27 | } |
| 28 | |
| 29 | public function downloadFile( $fileId ) { |
| 30 | return $this->run( 'GET', '/files/' . $fileId . '/content', null, null, false ); |
| 31 | } |
| 32 | |
| 33 | public function fineTuneFile( $fileId, $model, $suffix ) { |
| 34 | $result = $this->run( 'POST', '/fine-tunes', [ |
| 35 | 'training_file' => $fileId, |
| 36 | 'model' => $model, |
| 37 | 'suffix' => $suffix |
| 38 | ] ); |
| 39 | return $result; |
| 40 | } |
| 41 | |
| 42 | public function create_body_for_file( $file, $boundary ) { |
| 43 | $fields = array( |
| 44 | 'purpose' => 'fine-tune', |
| 45 | 'file' => $file['filename'] |
| 46 | ); |
| 47 | |
| 48 | $body = ''; |
| 49 | foreach ( $fields as $name => $value ) { |
| 50 | $body .= "--$boundary\r\n"; |
| 51 | $body .= "Content-Disposition: form-data; name=\"$name\""; |
| 52 | if ( $name == 'file' ) { |
| 53 | $body .= "; filename=\"{$value}\"\r\n"; |
| 54 | $body .= "Content-Type: application/json\r\n\r\n"; |
| 55 | $body .= $file['data'] . "\r\n"; |
| 56 | } else { |
| 57 | $body .= "\r\n\r\n$value\r\n"; |
| 58 | } |
| 59 | } |
| 60 | $body .= "--$boundary--\r\n"; |
| 61 | |
| 62 | |
| 63 | // $body = ''; |
| 64 | // $body .= '--' . $boundary; |
| 65 | // $body .= "\r\n"; |
| 66 | // // '"; filename="' . $file['filename'] . '"' |
| 67 | // $body .= 'Content-Disposition: form-data; name="photo_upload_file_name"; filename="' . $_FILES['resume']['name'] . '"' . "\r\n"; |
| 68 | // //$body .= 'Content-Disposition: form-data; baba="yo"; file="' . $file['filename'] . '"; purpose="fine-tune"' . "\r\n"; |
| 69 | // //$body .= 'Content-Type: ' . $format . '\r\n'; // |
| 70 | // //$body .= 'Content-Transfer-Encoding: binary' . "\r\n"; |
| 71 | // $body .= "\r\n"; |
| 72 | // $body .= $file['data']; |
| 73 | // $body .= "\r\n"; |
| 74 | // $body .= '--' . $boundary . '--'; |
| 75 | // $body .= "\r\n\r\n"; |
| 76 | return $body; |
| 77 | } |
| 78 | |
| 79 | public function run( $method, $url, $query = null, $file = null, $json = true ) { |
| 80 | $apiKey = $this->apiKey; |
| 81 | $headers = "Content-Type: application/json\r\n" . "Authorization: Bearer " . $apiKey . "\r\n"; |
| 82 | $body = $query ? json_encode( $query ) : null; |
| 83 | if ( !empty( $file ) ) { |
| 84 | $boundary = wp_generate_password( 24, false ); |
| 85 | $headers = [ |
| 86 | 'Content-Type' => 'multipart/form-data; boundary=' . $boundary, |
| 87 | 'Authorization' => 'Bearer ' . $this->apiKey, |
| 88 | ]; |
| 89 | $body = $this->create_body_for_file( $file, $boundary ); |
| 90 | } |
| 91 | |
| 92 | $url = 'https://api.openai.com/v1' . $url; |
| 93 | $options = [ |
| 94 | "headers" => $headers, |
| 95 | "method" => $method, |
| 96 | "timeout" => 60, |
| 97 | "body" => $body, |
| 98 | "sslverify" => false |
| 99 | ]; |
| 100 | |
| 101 | try { |
| 102 | $response = wp_remote_request( $url, $options ); |
| 103 | if ( is_wp_error( $response ) ) { |
| 104 | throw new Exception( $response->get_error_message() ); |
| 105 | } |
| 106 | $response = wp_remote_retrieve_body( $response ); |
| 107 | $data = $json ? json_decode( $response, true ) : $response; |
| 108 | |
| 109 | // Error handling |
| 110 | if ( isset( $data['error'] ) ) { |
| 111 | $message = $data['error']['message']; |
| 112 | // If the message contains "Incorrect API key provided: THE_KEY.", replace the key by "----". |
| 113 | if ( preg_match( '/API key provided(: .*)\./', $message, $matches ) ) { |
| 114 | $message = str_replace( $matches[1], '', $message ); |
| 115 | } |
| 116 | throw new Exception( $message ); |
| 117 | } |
| 118 | |
| 119 | return $data; |
| 120 | } |
| 121 | catch ( Exception $e ) { |
| 122 | error_log( $e->getMessage() ); |
| 123 | throw new Exception( 'Error while calling OpenAI: ' . $e->getMessage() ); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 |