123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563 |
- <?php
- namespace openai;
- use Exception;
- class OpenAi
- {
- private $engine = "davinci";
- private $model = "text-davinci-002";
- private $chatModel = "gpt-3.5-turbo";
- private $headers;
- private $contentTypes;
- private $timeout = 0;
- private $stream_method;
- private $customUrl = "";
- private $proxy = "";
- private $curlInfo = [];
- public function __construct($OPENAI_API_KEY)
- {
- $this->contentTypes = [
- "application/json" => "Content-Type: application/json",
- "multipart/form-data" => "Content-Type: multipart/form-data",
- ];
- $this->headers = [
- $this->contentTypes["application/json"],
- "Authorization: Bearer $OPENAI_API_KEY",
- ];
- }
- /**
- * @return array
- * Remove this method from your code before deploying
- */
- public function getCURLInfo()
- {
- return $this->curlInfo;
- }
- /**
- * @return bool|string
- */
- public function listModels()
- {
- $url = Url::fineTuneModel();
- $this->baseUrl($url);
- return $this->sendRequest($url, 'GET');
- }
- /**
- * @param $model
- * @return bool|string
- */
- public function retrieveModel($model)
- {
- $model = "/$model";
- $url = Url::fineTuneModel().$model;
- $this->baseUrl($url);
- return $this->sendRequest($url, 'GET');
- }
- /**
- * @param $opts
- * @return bool|string
- * @deprecated
- */
- public function complete($opts)
- {
- $engine = $opts['engine'] ?? $this->engine;
- $url = Url::completionURL($engine);
- unset($opts['engine']);
- $this->baseUrl($url);
- return $this->sendRequest($url, 'POST', $opts);
- }
- /**
- * @param $opts
- * @param null $stream
- * @return bool|string
- * @throws Exception
- */
- public function completion($opts, $stream = null)
- {
- if ($stream != null && array_key_exists('stream', $opts)) {
- if (!$opts['stream']) {
- throw new Exception(
- 'Please provide a stream function. Check https://github.com/orhanerday/open-ai#stream-example for an example.'
- );
- }
- $this->stream_method = $stream;
- }
- $opts['model'] = $opts['model'] ?? $this->model;
- $url = Url::completionsURL();
- $this->baseUrl($url);
- return $this->sendRequest($url, 'POST', $opts);
- }
- /**
- * @param $opts
- * @return bool|string
- */
- public function createEdit($opts)
- {
- $url = Url::editsUrl();
- $this->baseUrl($url);
- return $this->sendRequest($url, 'POST', $opts);
- }
- /**
- * @param $opts
- * @return bool|string
- */
- public function image($opts)
- {
- $url = Url::imageUrl()."/generations";
- $this->baseUrl($url);
- return $this->sendRequest($url, 'POST', $opts);
- }
- /**
- * @param $opts
- * @return bool|string
- */
- public function imageEdit($opts)
- {
- $url = Url::imageUrl()."/edits";
- $this->baseUrl($url);
- return $this->sendRequest($url, 'POST', $opts);
- }
- /**
- * @param $opts
- * @return bool|string
- */
- public function createImageVariation($opts)
- {
- $url = Url::imageUrl()."/variations";
- $this->baseUrl($url);
- return $this->sendRequest($url, 'POST', $opts);
- }
- /**
- * @param $opts
- * @return bool|string
- * @deprecated
- */
- public function search($opts)
- {
- $engine = $opts['engine'] ?? $this->engine;
- $url = Url::searchURL($engine);
- unset($opts['engine']);
- $this->baseUrl($url);
- return $this->sendRequest($url, 'POST', $opts);
- }
- /**
- * @param $opts
- * @return bool|string
- * @deprecated
- */
- public function answer($opts)
- {
- $url = Url::answersUrl();
- $this->baseUrl($url);
- return $this->sendRequest($url, 'POST', $opts);
- }
- /**
- * @param $opts
- * @return bool|string
- * @deprecated
- */
- public function classification($opts)
- {
- $url = Url::classificationsUrl();
- $this->baseUrl($url);
- return $this->sendRequest($url, 'POST', $opts);
- }
- /**
- * @param $opts
- * @return bool|string
- */
- public function moderation($opts)
- {
- $url = Url::moderationUrl();
- $this->baseUrl($url);
- return $this->sendRequest($url, 'POST', $opts);
- }
- /**
- * @param $opts
- * @param null $stream
- * @return bool|string
- * @throws Exception
- */
- public function chat($opts, $stream = null)
- {
- if ($stream != null && array_key_exists('stream', $opts)) {
- if (!$opts['stream']) {
- throw new Exception(
- 'Please provide a stream function. Check https://github.com/orhanerday/open-ai#stream-example for an example.'
- );
- }
- $this->stream_method = $stream;
- }
- $opts['model'] = $opts['model'] ?? $this->chatModel;
- $url = Url::chatUrl();
- $this->baseUrl($url);
- return $this->sendRequest($url, 'POST', $opts);
- }
- /**
- * @param $opts
- * @return bool|string
- */
- public function transcribe($opts)
- {
- $url = Url::transcriptionsUrl();
- $this->baseUrl($url);
- return $this->sendRequest($url, 'POST', $opts);
- }
- /**
- * @param $opts
- * @return bool|string
- */
- public function translate($opts)
- {
- $url = Url::translationsUrl();
- $this->baseUrl($url);
- return $this->sendRequest($url, 'POST', $opts);
- }
- /**
- * @param $opts
- * @return bool|string
- */
- public function uploadFile($opts)
- {
- $url = Url::filesUrl();
- $this->baseUrl($url);
- return $this->sendRequest($url, 'POST', $opts);
- }
- /**
- * @return bool|string
- */
- public function listFiles()
- {
- $url = Url::filesUrl();
- $this->baseUrl($url);
- return $this->sendRequest($url, 'GET');
- }
- /**
- * @param $file_id
- * @return bool|string
- */
- public function retrieveFile($file_id)
- {
- $file_id = "/$file_id";
- $url = Url::filesUrl().$file_id;
- $this->baseUrl($url);
- return $this->sendRequest($url, 'GET');
- }
- /**
- * @param $file_id
- * @return bool|string
- */
- public function retrieveFileContent($file_id)
- {
- $file_id = "/$file_id/content";
- $url = Url::filesUrl().$file_id;
- $this->baseUrl($url);
- return $this->sendRequest($url, 'GET');
- }
- /**
- * @param $file_id
- * @return bool|string
- */
- public function deleteFile($file_id)
- {
- $file_id = "/$file_id";
- $url = Url::filesUrl().$file_id;
- $this->baseUrl($url);
- return $this->sendRequest($url, 'DELETE');
- }
- /**
- * @param $opts
- * @return bool|string
- */
- public function createFineTune($opts)
- {
- $url = Url::fineTuneUrl();
- $this->baseUrl($url);
- return $this->sendRequest($url, 'POST', $opts);
- }
- /**
- * @return bool|string
- */
- public function listFineTunes()
- {
- $url = Url::fineTuneUrl();
- $this->baseUrl($url);
- return $this->sendRequest($url, 'GET');
- }
- /**
- * @param $fine_tune_id
- * @return bool|string
- */
- public function retrieveFineTune($fine_tune_id)
- {
- $fine_tune_id = "/$fine_tune_id";
- $url = Url::fineTuneUrl().$fine_tune_id;
- $this->baseUrl($url);
- return $this->sendRequest($url, 'GET');
- }
- /**
- * @param $fine_tune_id
- * @return bool|string
- */
- public function cancelFineTune($fine_tune_id)
- {
- $fine_tune_id = "/$fine_tune_id/cancel";
- $url = Url::fineTuneUrl().$fine_tune_id;
- $this->baseUrl($url);
- return $this->sendRequest($url, 'POST');
- }
- /**
- * @param $fine_tune_id
- * @return bool|string
- */
- public function listFineTuneEvents($fine_tune_id)
- {
- $fine_tune_id = "/$fine_tune_id/events";
- $url = Url::fineTuneUrl().$fine_tune_id;
- $this->baseUrl($url);
- return $this->sendRequest($url, 'GET');
- }
- /**
- * @param $fine_tune_id
- * @return bool|string
- */
- public function deleteFineTune($fine_tune_id)
- {
- $fine_tune_id = "/$fine_tune_id";
- $url = Url::fineTuneModel().$fine_tune_id;
- $this->baseUrl($url);
- return $this->sendRequest($url, 'DELETE');
- }
- /**
- * @param
- * @return bool|string
- * @deprecated
- */
- public function engines()
- {
- $url = Url::enginesUrl();
- $this->baseUrl($url);
- return $this->sendRequest($url, 'GET');
- }
- /**
- * @param $engine
- * @return bool|string
- * @deprecated
- */
- public function engine($engine)
- {
- $url = Url::engineUrl($engine);
- $this->baseUrl($url);
- return $this->sendRequest($url, 'GET');
- }
- /**
- * @param $opts
- * @return bool|string
- */
- public function embeddings($opts)
- {
- $url = Url::embeddings();
- $this->baseUrl($url);
- return $this->sendRequest($url, 'POST', $opts);
- }
- /**
- * @param int $timeout
- */
- public function setTimeout(int $timeout)
- {
- $this->timeout = $timeout;
- }
- /**
- * @param string $proxy
- */
- public function setProxy(string $proxy)
- {
- if ($proxy && strpos($proxy, '://') === false) {
- $proxy = 'https://'.$proxy;
- }
- $this->proxy = $proxy;
- }
- /**
- * @param string $customUrl
- * @deprecated
- */
- /**
- * @param string $customUrl
- * @return void
- */
- public function setCustomURL(string $customUrl)
- {
- if ($customUrl != "") {
- $this->customUrl = $customUrl;
- }
- }
- /**
- * @param string $customUrl
- * @return void
- */
- public function setBaseURL(string $customUrl)
- {
- if ($customUrl != '') {
- $this->customUrl = $customUrl;
- }
- }
- /**
- * @param array $header
- * @return void
- */
- public function setHeader(array $header)
- {
- if ($header) {
- foreach ($header as $key => $value) {
- $this->headers[$key] = $value;
- }
- }
- }
- /**
- * @param string $org
- */
- public function setORG(string $org)
- {
- if ($org != "") {
- $this->headers[] = "OpenAI-Organization: $org";
- }
- }
- /**
- * @param string $url
- * @param string $method
- * @param array $opts
- * @return bool|string
- */
- private function sendRequest(string $url, string $method, array $opts = [])
- {
- $post_fields = json_encode($opts);
- if (array_key_exists('file', $opts) || array_key_exists('image', $opts)) {
- $this->headers[0] = $this->contentTypes["multipart/form-data"];
- $post_fields = $opts;
- } else {
- $this->headers[0] = $this->contentTypes["application/json"];
- }
- $curl_info = [
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_ENCODING => '',
- CURLOPT_MAXREDIRS => 10,
- CURLOPT_TIMEOUT => $this->timeout,
- CURLOPT_FOLLOWLOCATION => true,
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
- CURLOPT_CUSTOMREQUEST => $method,
- CURLOPT_POSTFIELDS => $post_fields,
- CURLOPT_HTTPHEADER => $this->headers,
- ];
- if ($opts == []) {
- unset($curl_info[CURLOPT_POSTFIELDS]);
- }
- if (!empty($this->proxy)) {
- $curl_info[CURLOPT_PROXY] = $this->proxy;
- }
- if (array_key_exists('stream', $opts) && $opts['stream']) {
- $curl_info[CURLOPT_WRITEFUNCTION] = $this->stream_method;
- }
- $curl = curl_init();
- curl_setopt_array($curl, $curl_info);
- $response = curl_exec($curl);
- $info = curl_getinfo($curl);
- $this->curlInfo = $info;
- curl_close($curl);
- return $response;
- }
- /**
- * @param string $url
- */
- private function baseUrl(string &$url)
- {
- if ($this->customUrl != "") {
- $url = str_replace(Url::ORIGIN, $this->customUrl, $url);
- }
- }
- }
|