From 92b693c3acddf1d8e6f49a0b1daf4068a2fdcc7e Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Wed, 8 Mar 2023 14:44:37 +0800 Subject: [PATCH] Add setBaseURL and setHeader methods Add setBaseURL and setHeader methods, setCustomURL method will be replaced by setBaseUrl and be deprecated in the future --- src/OpenAi.php | 119 ++++++++++++++++++++++++++++++------------------- 1 file changed, 73 insertions(+), 46 deletions(-) diff --git a/src/OpenAi.php b/src/OpenAi.php index 3eac621..5b06b3c 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -20,7 +20,7 @@ class OpenAi public function __construct($OPENAI_API_KEY) { $this->contentTypes = [ - "application/json" => "Content-Type: application/json", + "application/json" => "Content-Type: application/json", "multipart/form-data" => "Content-Type: multipart/form-data", ]; @@ -31,7 +31,6 @@ public function __construct($OPENAI_API_KEY) } /** - * * @return array * Remove this method from your code before deploying */ @@ -41,7 +40,6 @@ public function getCURLInfo() } /** - * * @return bool|string */ public function listModels() @@ -59,7 +57,7 @@ public function listModels() public function retrieveModel($model) { $model = "/$model"; - $url = Url::fineTuneModel() . $model; + $url = Url::fineTuneModel().$model; $this->baseUrl($url); return $this->sendRequest($url, 'GET'); @@ -73,7 +71,7 @@ public function retrieveModel($model) public function complete($opts) { $engine = $opts['engine'] ?? $this->engine; - $url = Url::completionURL($engine); + $url = Url::completionURL($engine); unset($opts['engine']); $this->baseUrl($url); @@ -81,15 +79,15 @@ public function complete($opts) } /** - * @param $opts - * @param null $stream + * @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']) { + if (!$opts['stream']) { throw new Exception( 'Please provide a stream function. Check https://github.com/orhanerday/open-ai#stream-example for an example.' ); @@ -99,7 +97,7 @@ public function completion($opts, $stream = null) } $opts['model'] = $opts['model'] ?? $this->model; - $url = Url::completionsURL(); + $url = Url::completionsURL(); $this->baseUrl($url); return $this->sendRequest($url, 'POST', $opts); @@ -123,7 +121,7 @@ public function createEdit($opts) */ public function image($opts) { - $url = Url::imageUrl() . "/generations"; + $url = Url::imageUrl()."/generations"; $this->baseUrl($url); return $this->sendRequest($url, 'POST', $opts); @@ -135,7 +133,7 @@ public function image($opts) */ public function imageEdit($opts) { - $url = Url::imageUrl() . "/edits"; + $url = Url::imageUrl()."/edits"; $this->baseUrl($url); return $this->sendRequest($url, 'POST', $opts); @@ -147,7 +145,7 @@ public function imageEdit($opts) */ public function createImageVariation($opts) { - $url = Url::imageUrl() . "/variations"; + $url = Url::imageUrl()."/variations"; $this->baseUrl($url); return $this->sendRequest($url, 'POST', $opts); @@ -161,7 +159,7 @@ public function createImageVariation($opts) public function search($opts) { $engine = $opts['engine'] ?? $this->engine; - $url = Url::searchURL($engine); + $url = Url::searchURL($engine); unset($opts['engine']); $this->baseUrl($url); @@ -207,15 +205,15 @@ public function moderation($opts) } /** - * @param $opts - * @param null $stream + * @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']) { + if (!$opts['stream']) { throw new Exception( 'Please provide a stream function. Check https://github.com/orhanerday/open-ai#stream-example for an example.' ); @@ -225,7 +223,7 @@ public function chat($opts, $stream = null) } $opts['model'] = $opts['model'] ?? $this->chatModel; - $url = Url::chatUrl(); + $url = Url::chatUrl(); $this->baseUrl($url); return $this->sendRequest($url, 'POST', $opts); @@ -285,7 +283,7 @@ public function listFiles() public function retrieveFile($file_id) { $file_id = "/$file_id"; - $url = Url::filesUrl() . $file_id; + $url = Url::filesUrl().$file_id; $this->baseUrl($url); return $this->sendRequest($url, 'GET'); @@ -298,7 +296,7 @@ public function retrieveFile($file_id) public function retrieveFileContent($file_id) { $file_id = "/$file_id/content"; - $url = Url::filesUrl() . $file_id; + $url = Url::filesUrl().$file_id; $this->baseUrl($url); return $this->sendRequest($url, 'GET'); @@ -311,7 +309,7 @@ public function retrieveFileContent($file_id) public function deleteFile($file_id) { $file_id = "/$file_id"; - $url = Url::filesUrl() . $file_id; + $url = Url::filesUrl().$file_id; $this->baseUrl($url); return $this->sendRequest($url, 'DELETE'); @@ -347,7 +345,7 @@ public function listFineTunes() public function retrieveFineTune($fine_tune_id) { $fine_tune_id = "/$fine_tune_id"; - $url = Url::fineTuneUrl() . $fine_tune_id; + $url = Url::fineTuneUrl().$fine_tune_id; $this->baseUrl($url); return $this->sendRequest($url, 'GET'); @@ -360,7 +358,7 @@ public function retrieveFineTune($fine_tune_id) public function cancelFineTune($fine_tune_id) { $fine_tune_id = "/$fine_tune_id/cancel"; - $url = Url::fineTuneUrl() . $fine_tune_id; + $url = Url::fineTuneUrl().$fine_tune_id; $this->baseUrl($url); return $this->sendRequest($url, 'POST'); @@ -373,7 +371,7 @@ public function cancelFineTune($fine_tune_id) public function listFineTuneEvents($fine_tune_id) { $fine_tune_id = "/$fine_tune_id/events"; - $url = Url::fineTuneUrl() . $fine_tune_id; + $url = Url::fineTuneUrl().$fine_tune_id; $this->baseUrl($url); return $this->sendRequest($url, 'GET'); @@ -386,7 +384,7 @@ public function listFineTuneEvents($fine_tune_id) public function deleteFineTune($fine_tune_id) { $fine_tune_id = "/$fine_tune_id"; - $url = Url::fineTuneModel() . $fine_tune_id; + $url = Url::fineTuneModel().$fine_tune_id; $this->baseUrl($url); return $this->sendRequest($url, 'DELETE'); @@ -431,7 +429,7 @@ public function embeddings($opts) } /** - * @param int $timeout + * @param int $timeout */ public function setTimeout(int $timeout) { @@ -439,18 +437,24 @@ public function setTimeout(int $timeout) } /** - * @param string $proxy + * @param string $proxy */ public function setProxy(string $proxy) { if ($proxy && strpos($proxy, '://') === false) { - $proxy = 'http://' . $proxy; + $proxy = 'https://'.$proxy; } $this->proxy = $proxy; } /** - * @param string $customUrl + * @param string $customUrl + * @deprecated + */ + + /** + * @param string $customUrl + * @return void */ public function setCustomURL(string $customUrl) { @@ -460,7 +464,30 @@ public function setCustomURL(string $customUrl) } /** - * @param string $org + * @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) { + [$key, $value] = $header; + $this->headers[$key] = $value; + } + } + + /** + * @param string $org */ public function setORG(string $org) { @@ -470,9 +497,9 @@ public function setORG(string $org) } /** - * @param string $url - * @param string $method - * @param array $opts + * @param string $url + * @param string $method + * @param array $opts * @return bool|string */ private function sendRequest(string $url, string $method, array $opts = []) @@ -481,28 +508,28 @@ private function sendRequest(string $url, string $method, array $opts = []) if (array_key_exists('file', $opts) || array_key_exists('image', $opts)) { $this->headers[0] = $this->contentTypes["multipart/form-data"]; - $post_fields = $opts; + $post_fields = $opts; } else { $this->headers[0] = $this->contentTypes["application/json"]; } $curl_info = [ - CURLOPT_URL => $url, + CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, - CURLOPT_ENCODING => '', - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => $this->timeout, + 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, + 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)) { + if (!empty($this->proxy)) { $curl_info[CURLOPT_PROXY] = $this->proxy; } @@ -513,18 +540,18 @@ private function sendRequest(string $url, string $method, array $opts = []) $curl = curl_init(); curl_setopt_array($curl, $curl_info); - $response = curl_exec($curl); + $response = curl_exec($curl); - $info = curl_getinfo($curl); + $info = curl_getinfo($curl); $this->curlInfo = $info; - + curl_close($curl); return $response; } /** - * @param string $url + * @param string $url */ private function baseUrl(string &$url) {