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