Skip to content

Commit

Permalink
Merge pull request #67 from gouguoyin/patch-1
Browse files Browse the repository at this point in the history
Add  setBaseURL and setHeader methods
  • Loading branch information
orhanerday authored Mar 8, 2023
2 parents 7042af8 + 92b693c commit 9050c0e
Showing 1 changed file with 73 additions and 46 deletions.
119 changes: 73 additions & 46 deletions src/OpenAi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
];

Expand All @@ -31,7 +31,6 @@ public function __construct($OPENAI_API_KEY)
}

/**
*
* @return array
* Remove this method from your code before deploying
*/
Expand All @@ -41,7 +40,6 @@ public function getCURLInfo()
}

/**
*
* @return bool|string
*/
public function listModels()
Expand All @@ -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');
Expand All @@ -73,23 +71,23 @@ 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);

return $this->sendRequest($url, 'POST', $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.'
);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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.'
);
Expand All @@ -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);
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -431,26 +429,32 @@ public function embeddings($opts)
}

/**
* @param int $timeout
* @param int $timeout
*/
public function setTimeout(int $timeout)
{
$this->timeout = $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)
{
Expand All @@ -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)
{
Expand All @@ -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 = [])
Expand All @@ -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;
}

Expand All @@ -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)
{
Expand Down

0 comments on commit 9050c0e

Please sign in to comment.