From 488f0e1e6c412612fa97e479323e6223f5aa55ba Mon Sep 17 00:00:00 2001 From: Vlad Tatarinov Date: Thu, 16 Nov 2023 12:33:05 +0300 Subject: [PATCH] Changed the visibility and updated the sendRequest method with proxy creds --- src/OpenAi.php | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/src/OpenAi.php b/src/OpenAi.php index ed38d98..07792de 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -6,16 +6,23 @@ class OpenAi { - private string $engine = "davinci"; - private string $model = "text-davinci-002"; - private string $chatModel = "gpt-3.5-turbo"; - private array $headers; - private array $contentTypes; - private int $timeout = 0; - private object $stream_method; - private string $customUrl = ""; - private string $proxy = ""; - private array $curlInfo = []; + protected string $engine = "davinci"; + protected string $model = "text-davinci-002"; + protected string $chatModel = "gpt-3.5-turbo"; + protected array $headers; + protected array $contentTypes; + protected int $timeout = 0; + protected object $stream_method; + protected string $customUrl = ""; + protected string $proxy = ""; + + /** + * format user:pass + * @var string + */ + protected string $proxyAccess = ""; + + protected array $curlInfo = []; public function __construct($OPENAI_API_KEY) { @@ -447,6 +454,15 @@ public function setProxy(string $proxy) $this->proxy = $proxy; } + /** + * @param string $access + * @return void + */ + public function setProxyAccess(string $access) + { + $this->proxyAccess = $access; + } + /** * @param string $customUrl * @deprecated @@ -503,7 +519,7 @@ public function setORG(string $org) * @param array $opts * @return bool|string */ - private function sendRequest(string $url, string $method, array $opts = []) + protected function sendRequest(string $url, string $method, array $opts = []) { $post_fields = json_encode($opts); @@ -534,6 +550,10 @@ private function sendRequest(string $url, string $method, array $opts = []) $curl_info[CURLOPT_PROXY] = $this->proxy; } + if (!empty($this->proxyAccess)) { + $curl_info[CURLOPT_PROXYUSERPWD] = $this->proxyAccess; + } + if (array_key_exists('stream', $opts) && $opts['stream']) { $curl_info[CURLOPT_WRITEFUNCTION] = $this->stream_method; } @@ -549,14 +569,14 @@ private function sendRequest(string $url, string $method, array $opts = []) curl_close($curl); if (!$response) throw new Exception(curl_error($curl)); - + return $response; } /** * @param string $url */ - private function baseUrl(string &$url) + protected function baseUrl(string &$url) { if ($this->customUrl != "") { $url = str_replace(Url::ORIGIN, $this->customUrl, $url);