Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 35 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| OpenAI | |
0.00% |
0 / 35 |
|
0.00% |
0 / 10 |
132 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setVersion | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setPrefix | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| post | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
2 | |||
| getEngineList | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| retrieveEngine | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setEngineId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| postCompletions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| search | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Services\FlyMsgAI; |
| 4 | |
| 5 | use GuzzleHttp\Client; |
| 6 | use GuzzleHttp\Exception\GuzzleException; |
| 7 | use GuzzleHttp\RequestOptions; |
| 8 | use Psr\Http\Message\ResponseInterface; |
| 9 | |
| 10 | class OpenAI |
| 11 | { |
| 12 | private $secretKey; |
| 13 | |
| 14 | private $baseUri = 'https://api.openai.com'; |
| 15 | |
| 16 | private $version = 'v1'; |
| 17 | |
| 18 | private $prefix = 'engines'; |
| 19 | |
| 20 | private $engineId = 'gpt-3.5-turbo'; |
| 21 | |
| 22 | const MODELS = [ |
| 23 | 'gpt-3.5-turbo', |
| 24 | 'gpt-3.5-turbo-16k', |
| 25 | 'davinci-002', |
| 26 | 'text-davinci-003', |
| 27 | // Add more new ones here |
| 28 | ]; |
| 29 | |
| 30 | /** |
| 31 | * OpenAI constructor. |
| 32 | */ |
| 33 | public function __construct(string $secretKey) |
| 34 | { |
| 35 | $this->secretKey = $secretKey; |
| 36 | $this->baseUri = $this->baseUri.'/'.$this->version; |
| 37 | } |
| 38 | |
| 39 | public function setVersion(string $version): void |
| 40 | { |
| 41 | $this->version = $version; |
| 42 | } |
| 43 | |
| 44 | public function setPrefix(string $prefix): void |
| 45 | { |
| 46 | $this->prefix = $prefix; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @throws GuzzleException |
| 51 | */ |
| 52 | private function get(string $endPoint = ''): ResponseInterface |
| 53 | { |
| 54 | $endPoint ? $this->baseUri .= '/'.$this->prefix.'/'.$endPoint : $this->baseUri; |
| 55 | |
| 56 | $client = new Client; |
| 57 | $response = $client->request( |
| 58 | 'GET', |
| 59 | $this->baseUri, |
| 60 | [ |
| 61 | 'headers' => [ |
| 62 | 'Authorization' => 'Bearer '.$this->secretKey, |
| 63 | 'Accept' => 'application/json', |
| 64 | ], |
| 65 | ] |
| 66 | ); |
| 67 | |
| 68 | return $response; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @throws GuzzleException |
| 73 | */ |
| 74 | private function post(array $postParams, string $category): ResponseInterface |
| 75 | { |
| 76 | $client = new Client; |
| 77 | $response = $client->request( |
| 78 | 'POST', |
| 79 | $this->baseUri.'/'.$this->prefix.'/'.$category, |
| 80 | // $this->baseUri . '/' . $this->engineId . '/' . $category, |
| 81 | [ |
| 82 | 'headers' => [ |
| 83 | 'Authorization' => 'Bearer '.$this->secretKey, |
| 84 | 'Accept' => 'application/json', |
| 85 | ], |
| 86 | RequestOptions::JSON => $postParams, |
| 87 | ] |
| 88 | ); |
| 89 | |
| 90 | return $response; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @throws GuzzleException |
| 95 | */ |
| 96 | public function getEngineList(): ResponseInterface |
| 97 | { |
| 98 | return $this->get(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @throws GuzzleException |
| 103 | */ |
| 104 | public function retrieveEngine(): ResponseInterface |
| 105 | { |
| 106 | return $this->get($this->engineId); |
| 107 | } |
| 108 | |
| 109 | public function setEngineId(string $engineId): void |
| 110 | { |
| 111 | $this->engineId = $engineId; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @throws GuzzleException |
| 116 | */ |
| 117 | public function postCompletions(array $postParams): ResponseInterface |
| 118 | { |
| 119 | return $this->post($postParams, 'completions'); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @throws GuzzleException |
| 124 | */ |
| 125 | public function search(array $postParams): ResponseInterface |
| 126 | { |
| 127 | return $this->post($postParams, 'search'); |
| 128 | } |
| 129 | } |