OpenAi.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <?php
  2. namespace openai;
  3. use Exception;
  4. class OpenAi
  5. {
  6. private $engine = "davinci";
  7. private $model = "text-davinci-002";
  8. private $chatModel = "gpt-3.5-turbo";
  9. private $headers;
  10. private $contentTypes;
  11. private $timeout = 0;
  12. private $stream_method;
  13. private $customUrl = "";
  14. private $proxy = "";
  15. private $curlInfo = [];
  16. public function __construct($OPENAI_API_KEY)
  17. {
  18. $this->contentTypes = [
  19. "application/json" => "Content-Type: application/json",
  20. "multipart/form-data" => "Content-Type: multipart/form-data",
  21. ];
  22. $this->headers = [
  23. $this->contentTypes["application/json"],
  24. "Authorization: Bearer $OPENAI_API_KEY",
  25. ];
  26. }
  27. /**
  28. * @return array
  29. * Remove this method from your code before deploying
  30. */
  31. public function getCURLInfo()
  32. {
  33. return $this->curlInfo;
  34. }
  35. /**
  36. * @return bool|string
  37. */
  38. public function listModels()
  39. {
  40. $url = Url::fineTuneModel();
  41. $this->baseUrl($url);
  42. return $this->sendRequest($url, 'GET');
  43. }
  44. /**
  45. * @param $model
  46. * @return bool|string
  47. */
  48. public function retrieveModel($model)
  49. {
  50. $model = "/$model";
  51. $url = Url::fineTuneModel().$model;
  52. $this->baseUrl($url);
  53. return $this->sendRequest($url, 'GET');
  54. }
  55. /**
  56. * @param $opts
  57. * @return bool|string
  58. * @deprecated
  59. */
  60. public function complete($opts)
  61. {
  62. $engine = $opts['engine'] ?? $this->engine;
  63. $url = Url::completionURL($engine);
  64. unset($opts['engine']);
  65. $this->baseUrl($url);
  66. return $this->sendRequest($url, 'POST', $opts);
  67. }
  68. /**
  69. * @param $opts
  70. * @param null $stream
  71. * @return bool|string
  72. * @throws Exception
  73. */
  74. public function completion($opts, $stream = null)
  75. {
  76. if ($stream != null && array_key_exists('stream', $opts)) {
  77. if (!$opts['stream']) {
  78. throw new Exception(
  79. 'Please provide a stream function. Check https://github.com/orhanerday/open-ai#stream-example for an example.'
  80. );
  81. }
  82. $this->stream_method = $stream;
  83. }
  84. $opts['model'] = $opts['model'] ?? $this->model;
  85. $url = Url::completionsURL();
  86. $this->baseUrl($url);
  87. return $this->sendRequest($url, 'POST', $opts);
  88. }
  89. /**
  90. * @param $opts
  91. * @return bool|string
  92. */
  93. public function createEdit($opts)
  94. {
  95. $url = Url::editsUrl();
  96. $this->baseUrl($url);
  97. return $this->sendRequest($url, 'POST', $opts);
  98. }
  99. /**
  100. * @param $opts
  101. * @return bool|string
  102. */
  103. public function image($opts)
  104. {
  105. $url = Url::imageUrl()."/generations";
  106. $this->baseUrl($url);
  107. return $this->sendRequest($url, 'POST', $opts);
  108. }
  109. /**
  110. * @param $opts
  111. * @return bool|string
  112. */
  113. public function imageEdit($opts)
  114. {
  115. $url = Url::imageUrl()."/edits";
  116. $this->baseUrl($url);
  117. return $this->sendRequest($url, 'POST', $opts);
  118. }
  119. /**
  120. * @param $opts
  121. * @return bool|string
  122. */
  123. public function createImageVariation($opts)
  124. {
  125. $url = Url::imageUrl()."/variations";
  126. $this->baseUrl($url);
  127. return $this->sendRequest($url, 'POST', $opts);
  128. }
  129. /**
  130. * @param $opts
  131. * @return bool|string
  132. * @deprecated
  133. */
  134. public function search($opts)
  135. {
  136. $engine = $opts['engine'] ?? $this->engine;
  137. $url = Url::searchURL($engine);
  138. unset($opts['engine']);
  139. $this->baseUrl($url);
  140. return $this->sendRequest($url, 'POST', $opts);
  141. }
  142. /**
  143. * @param $opts
  144. * @return bool|string
  145. * @deprecated
  146. */
  147. public function answer($opts)
  148. {
  149. $url = Url::answersUrl();
  150. $this->baseUrl($url);
  151. return $this->sendRequest($url, 'POST', $opts);
  152. }
  153. /**
  154. * @param $opts
  155. * @return bool|string
  156. * @deprecated
  157. */
  158. public function classification($opts)
  159. {
  160. $url = Url::classificationsUrl();
  161. $this->baseUrl($url);
  162. return $this->sendRequest($url, 'POST', $opts);
  163. }
  164. /**
  165. * @param $opts
  166. * @return bool|string
  167. */
  168. public function moderation($opts)
  169. {
  170. $url = Url::moderationUrl();
  171. $this->baseUrl($url);
  172. return $this->sendRequest($url, 'POST', $opts);
  173. }
  174. /**
  175. * @param $opts
  176. * @param null $stream
  177. * @return bool|string
  178. * @throws Exception
  179. */
  180. public function chat($opts, $stream = null)
  181. {
  182. if ($stream != null && array_key_exists('stream', $opts)) {
  183. if (!$opts['stream']) {
  184. throw new Exception(
  185. 'Please provide a stream function. Check https://github.com/orhanerday/open-ai#stream-example for an example.'
  186. );
  187. }
  188. $this->stream_method = $stream;
  189. }
  190. $opts['model'] = $opts['model'] ?? $this->chatModel;
  191. $url = Url::chatUrl();
  192. $this->baseUrl($url);
  193. return $this->sendRequest($url, 'POST', $opts);
  194. }
  195. /**
  196. * @param $opts
  197. * @return bool|string
  198. */
  199. public function transcribe($opts)
  200. {
  201. $url = Url::transcriptionsUrl();
  202. $this->baseUrl($url);
  203. return $this->sendRequest($url, 'POST', $opts);
  204. }
  205. /**
  206. * @param $opts
  207. * @return bool|string
  208. */
  209. public function translate($opts)
  210. {
  211. $url = Url::translationsUrl();
  212. $this->baseUrl($url);
  213. return $this->sendRequest($url, 'POST', $opts);
  214. }
  215. /**
  216. * @param $opts
  217. * @return bool|string
  218. */
  219. public function uploadFile($opts)
  220. {
  221. $url = Url::filesUrl();
  222. $this->baseUrl($url);
  223. return $this->sendRequest($url, 'POST', $opts);
  224. }
  225. /**
  226. * @return bool|string
  227. */
  228. public function listFiles()
  229. {
  230. $url = Url::filesUrl();
  231. $this->baseUrl($url);
  232. return $this->sendRequest($url, 'GET');
  233. }
  234. /**
  235. * @param $file_id
  236. * @return bool|string
  237. */
  238. public function retrieveFile($file_id)
  239. {
  240. $file_id = "/$file_id";
  241. $url = Url::filesUrl().$file_id;
  242. $this->baseUrl($url);
  243. return $this->sendRequest($url, 'GET');
  244. }
  245. /**
  246. * @param $file_id
  247. * @return bool|string
  248. */
  249. public function retrieveFileContent($file_id)
  250. {
  251. $file_id = "/$file_id/content";
  252. $url = Url::filesUrl().$file_id;
  253. $this->baseUrl($url);
  254. return $this->sendRequest($url, 'GET');
  255. }
  256. /**
  257. * @param $file_id
  258. * @return bool|string
  259. */
  260. public function deleteFile($file_id)
  261. {
  262. $file_id = "/$file_id";
  263. $url = Url::filesUrl().$file_id;
  264. $this->baseUrl($url);
  265. return $this->sendRequest($url, 'DELETE');
  266. }
  267. /**
  268. * @param $opts
  269. * @return bool|string
  270. */
  271. public function createFineTune($opts)
  272. {
  273. $url = Url::fineTuneUrl();
  274. $this->baseUrl($url);
  275. return $this->sendRequest($url, 'POST', $opts);
  276. }
  277. /**
  278. * @return bool|string
  279. */
  280. public function listFineTunes()
  281. {
  282. $url = Url::fineTuneUrl();
  283. $this->baseUrl($url);
  284. return $this->sendRequest($url, 'GET');
  285. }
  286. /**
  287. * @param $fine_tune_id
  288. * @return bool|string
  289. */
  290. public function retrieveFineTune($fine_tune_id)
  291. {
  292. $fine_tune_id = "/$fine_tune_id";
  293. $url = Url::fineTuneUrl().$fine_tune_id;
  294. $this->baseUrl($url);
  295. return $this->sendRequest($url, 'GET');
  296. }
  297. /**
  298. * @param $fine_tune_id
  299. * @return bool|string
  300. */
  301. public function cancelFineTune($fine_tune_id)
  302. {
  303. $fine_tune_id = "/$fine_tune_id/cancel";
  304. $url = Url::fineTuneUrl().$fine_tune_id;
  305. $this->baseUrl($url);
  306. return $this->sendRequest($url, 'POST');
  307. }
  308. /**
  309. * @param $fine_tune_id
  310. * @return bool|string
  311. */
  312. public function listFineTuneEvents($fine_tune_id)
  313. {
  314. $fine_tune_id = "/$fine_tune_id/events";
  315. $url = Url::fineTuneUrl().$fine_tune_id;
  316. $this->baseUrl($url);
  317. return $this->sendRequest($url, 'GET');
  318. }
  319. /**
  320. * @param $fine_tune_id
  321. * @return bool|string
  322. */
  323. public function deleteFineTune($fine_tune_id)
  324. {
  325. $fine_tune_id = "/$fine_tune_id";
  326. $url = Url::fineTuneModel().$fine_tune_id;
  327. $this->baseUrl($url);
  328. return $this->sendRequest($url, 'DELETE');
  329. }
  330. /**
  331. * @param
  332. * @return bool|string
  333. * @deprecated
  334. */
  335. public function engines()
  336. {
  337. $url = Url::enginesUrl();
  338. $this->baseUrl($url);
  339. return $this->sendRequest($url, 'GET');
  340. }
  341. /**
  342. * @param $engine
  343. * @return bool|string
  344. * @deprecated
  345. */
  346. public function engine($engine)
  347. {
  348. $url = Url::engineUrl($engine);
  349. $this->baseUrl($url);
  350. return $this->sendRequest($url, 'GET');
  351. }
  352. /**
  353. * @param $opts
  354. * @return bool|string
  355. */
  356. public function embeddings($opts)
  357. {
  358. $url = Url::embeddings();
  359. $this->baseUrl($url);
  360. return $this->sendRequest($url, 'POST', $opts);
  361. }
  362. /**
  363. * @param int $timeout
  364. */
  365. public function setTimeout(int $timeout)
  366. {
  367. $this->timeout = $timeout;
  368. }
  369. /**
  370. * @param string $proxy
  371. */
  372. public function setProxy(string $proxy)
  373. {
  374. if ($proxy && strpos($proxy, '://') === false) {
  375. $proxy = 'https://'.$proxy;
  376. }
  377. $this->proxy = $proxy;
  378. }
  379. /**
  380. * @param string $customUrl
  381. * @deprecated
  382. */
  383. /**
  384. * @param string $customUrl
  385. * @return void
  386. */
  387. public function setCustomURL(string $customUrl)
  388. {
  389. if ($customUrl != "") {
  390. $this->customUrl = $customUrl;
  391. }
  392. }
  393. /**
  394. * @param string $customUrl
  395. * @return void
  396. */
  397. public function setBaseURL(string $customUrl)
  398. {
  399. if ($customUrl != '') {
  400. $this->customUrl = $customUrl;
  401. }
  402. }
  403. /**
  404. * @param array $header
  405. * @return void
  406. */
  407. public function setHeader(array $header)
  408. {
  409. if ($header) {
  410. foreach ($header as $key => $value) {
  411. $this->headers[$key] = $value;
  412. }
  413. }
  414. }
  415. /**
  416. * @param string $org
  417. */
  418. public function setORG(string $org)
  419. {
  420. if ($org != "") {
  421. $this->headers[] = "OpenAI-Organization: $org";
  422. }
  423. }
  424. /**
  425. * @param string $url
  426. * @param string $method
  427. * @param array $opts
  428. * @return bool|string
  429. */
  430. private function sendRequest(string $url, string $method, array $opts = [])
  431. {
  432. $post_fields = json_encode($opts);
  433. if (array_key_exists('file', $opts) || array_key_exists('image', $opts)) {
  434. $this->headers[0] = $this->contentTypes["multipart/form-data"];
  435. $post_fields = $opts;
  436. } else {
  437. $this->headers[0] = $this->contentTypes["application/json"];
  438. }
  439. $curl_info = [
  440. CURLOPT_URL => $url,
  441. CURLOPT_RETURNTRANSFER => true,
  442. CURLOPT_ENCODING => '',
  443. CURLOPT_MAXREDIRS => 10,
  444. CURLOPT_TIMEOUT => $this->timeout,
  445. CURLOPT_FOLLOWLOCATION => true,
  446. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  447. CURLOPT_CUSTOMREQUEST => $method,
  448. CURLOPT_POSTFIELDS => $post_fields,
  449. CURLOPT_HTTPHEADER => $this->headers,
  450. ];
  451. if ($opts == []) {
  452. unset($curl_info[CURLOPT_POSTFIELDS]);
  453. }
  454. if (!empty($this->proxy)) {
  455. $curl_info[CURLOPT_PROXY] = $this->proxy;
  456. }
  457. if (array_key_exists('stream', $opts) && $opts['stream']) {
  458. $curl_info[CURLOPT_WRITEFUNCTION] = $this->stream_method;
  459. }
  460. $curl = curl_init();
  461. curl_setopt_array($curl, $curl_info);
  462. $response = curl_exec($curl);
  463. $info = curl_getinfo($curl);
  464. $this->curlInfo = $info;
  465. curl_close($curl);
  466. return $response;
  467. }
  468. /**
  469. * @param string $url
  470. */
  471. private function baseUrl(string &$url)
  472. {
  473. if ($this->customUrl != "") {
  474. $url = str_replace(Url::ORIGIN, $this->customUrl, $url);
  475. }
  476. }
  477. }