From 8e3c81b207ebfc948006b6bc9bfeb794c4e10d08 Mon Sep 17 00:00:00 2001 From: badasswp Date: Thu, 6 Feb 2025 00:36:40 +0100 Subject: [PATCH 1/3] feat: add setApiKey method --- src/OpenAi.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/OpenAi.php b/src/OpenAi.php index 87bfcbe..40df526 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -18,7 +18,7 @@ class OpenAi private string $proxy = ""; private array $curlInfo = []; - public function __construct($OPENAI_API_KEY) + public function __construct($OPENAI_API_KEY = '') { $this->contentTypes = [ "application/json" => "Content-Type: application/json", @@ -939,7 +939,19 @@ public function setORG(string $org) $this->headers[] = "OpenAI-Organization: $org"; } } - + + /** + * @param string $token + */ + public function setApiKey(string $token) + { + if ($token != "") { + $this->headers[1] = "Authorization: Bearer $token"; + } + + return $this; + } + /** * @param string $org */ @@ -968,6 +980,12 @@ private function sendRequest(string $url, string $method, array $opts = []) { $post_fields = json_encode($opts); + if ($this->headers[1] === 'Authorization: Bearer ') { + throw new Exception( + 'Please provide an API key.' + ); + } + if (array_key_exists('file', $opts) || array_key_exists('image', $opts)) { $this->headers[0] = $this->contentTypes["multipart/form-data"]; $post_fields = $opts; From e039583c7fb89ab242800bb3a834267086a5b5c7 Mon Sep 17 00:00:00 2001 From: badasswp Date: Thu, 6 Feb 2025 00:37:44 +0100 Subject: [PATCH 2/3] fix: linting issues --- src/OpenAi.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/OpenAi.php b/src/OpenAi.php index 40df526..a0cb7f5 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -720,7 +720,7 @@ public function createRun($threadId, $data, $stream = null) $this->stream_method = $stream; } - + $this->addAssistantsBetaHeader(); $url = Url::threadsUrl() . '/' . $threadId . '/runs'; $this->baseUrl($url); @@ -791,7 +791,7 @@ public function submitToolOutputs($threadId, $runId, $outputs, $stream = null) $this->stream_method = $stream; } - + $this->addAssistantsBetaHeader(); $url = Url::threadsUrl() . '/' . $threadId . '/runs/' . $runId . '/submit_tool_outputs'; $this->baseUrl($url); @@ -965,10 +965,10 @@ public function setAssistantsBetaVersion(string $version) /** * @return void */ - private function addAssistantsBetaHeader(){ + private function addAssistantsBetaHeader() + { $this->headers[] = 'OpenAI-Beta: assistants='.$this->assistantsBetaVersion; } - /** * @param string $url From de36a1d9c9a833aa49ffd02ff340ca5a9dc32e34 Mon Sep 17 00:00:00 2001 From: badasswp Date: Thu, 6 Feb 2025 00:40:27 +0100 Subject: [PATCH 3/3] test: specific API key scenarios --- tests/OpenAiTest.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index 4a27b97..f61417d 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -603,3 +603,29 @@ $this->assertStringContainsString('"object": "list"', $steps); $this->assertStringContainsString('data', $steps); })->group('working'); + +$open_ai_with_unset_api_key = new OpenAi(); + +it('should throw error when API key is not set', function () use ($open_ai_with_unset_api_key) { + expect(fn () => $open_ai_with_unset_api_key->completion([ + 'prompt' => "Hello", + 'temperature' => 0.9, + "max_tokens" => 150, + "frequency_penalty" => 0, + "presence_penalty" => 0.6, + ]))->toThrow(Exception::class, 'Please provide an API key.'); +})->group('api-keys'); + +$open_ai_with_set_api_key = (new OpenAi())->setApiKey(getenv('OPENAI_API_KEY')); + +it('should handle simple completion with API key set', function () use ($open_ai_with_set_api_key) { + $result = $open_ai_with_set_api_key->completion([ + 'prompt' => "Hello", + 'temperature' => 0.9, + "max_tokens" => 150, + "frequency_penalty" => 0, + "presence_penalty" => 0.6, + ]); + + $this->assertStringContainsString('text', $result); +})->group('api-keys');