├── LICENSE ├── README.md ├── includes └── Openai.php └── tests ├── request-test.php └── search-test.php /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Luis Paulo Dobreira 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Easy OpenAI GPT-3 API Library for PHP 2 | [![License](https://img.shields.io/github/license/mashape/apistatus.svg)](https://opensource.org/licenses/MIT) 3 | 4 | A simple and easy to use PHP wrapper for the OpenAI GPT-3 API. 5 | 6 | Example usage: 7 | 8 | ```php 9 | request("ada", "This is a test", 5); 17 | 18 | ?> 19 | ``` 20 | 21 | ## Requirements 22 | All the examples require your OpenAI api token 23 | ```php 24 | $secret_key = 'Bearer ******YOUR-KEY-HERE********'; 25 | ``` 26 | 27 | ## License 28 | 29 | OpenAI GPT-3 API Library is released under the 30 | [MIT License](http://www.opensource.org/licenses/MIT). 31 | -------------------------------------------------------------------------------- /includes/Openai.php: -------------------------------------------------------------------------------- 1 | $prompt, 11 | "max_tokens" => $max_tokens, 12 | "temperature" => 0.7, 13 | "top_p" => 1, 14 | "presence_penalty" => 0.75, 15 | "frequency_penalty"=> 0.75, 16 | "best_of"=> 1, 17 | "stream" => false, 18 | ]; 19 | 20 | $postfields = json_encode($request_body); 21 | $curl = curl_init(); 22 | curl_setopt_array($curl, [ 23 | CURLOPT_URL => "https://api.openai.com/v1/engines/" . $engine . "/completions", 24 | CURLOPT_RETURNTRANSFER => true, 25 | CURLOPT_FOLLOWLOCATION => true, 26 | CURLOPT_ENCODING => "", 27 | CURLOPT_MAXREDIRS => 10, 28 | CURLOPT_TIMEOUT => 30, 29 | CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 30 | CURLOPT_CUSTOMREQUEST => "POST", 31 | CURLOPT_POSTFIELDS => $postfields, 32 | CURLOPT_HTTPHEADER => [ 33 | 'Content-Type: application/json', 34 | 'Authorization: ' . $this->secret_key() 35 | ], 36 | ]); 37 | 38 | $response = curl_exec($curl); 39 | $err = curl_error($curl); 40 | 41 | curl_close($curl); 42 | 43 | if ($err) { 44 | echo "Error #:" . $err; 45 | } else { 46 | echo $response; 47 | } 48 | 49 | } 50 | 51 | public function search($engine, $documents, $query){ 52 | 53 | $request_body = [ 54 | "max_tokens" => 10, 55 | "temperature" => 0.7, 56 | "top_p" => 1, 57 | "presence_penalty" => 0.75, 58 | "frequency_penalty"=> 0.75, 59 | "documents" => $documents, 60 | "query" => $query 61 | ]; 62 | 63 | $postfields = json_encode($request_body); 64 | $curl = curl_init(); 65 | curl_setopt_array($curl, [ 66 | CURLOPT_URL => "https://api.openai.com/v1/engines/" . $engine . "/search", 67 | CURLOPT_RETURNTRANSFER => true, 68 | CURLOPT_FOLLOWLOCATION => true, 69 | CURLOPT_ENCODING => "", 70 | CURLOPT_MAXREDIRS => 10, 71 | CURLOPT_TIMEOUT => 30, 72 | CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 73 | CURLOPT_CUSTOMREQUEST => "POST", 74 | CURLOPT_POSTFIELDS => $postfields, 75 | CURLOPT_HTTPHEADER => [ 76 | 'Content-Type: application/json', 77 | 'Authorization: ' . $this->secret_key() 78 | ], 79 | ]); 80 | 81 | $response = curl_exec($curl); 82 | $err = curl_error($curl); 83 | 84 | curl_close($curl); 85 | 86 | if ($err) { 87 | echo "Error #:" . $err; 88 | } else { 89 | echo $response; 90 | } 91 | 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /tests/request-test.php: -------------------------------------------------------------------------------- 1 | request("ada", "This is a test", 5); 8 | 9 | ?> 10 | -------------------------------------------------------------------------------- /tests/search-test.php: -------------------------------------------------------------------------------- 1 | search("ada", $documents, $query); 11 | 12 | ?> 13 | --------------------------------------------------------------------------------