From d7f99034110046a78cf65a8cd06861a237f82f9e Mon Sep 17 00:00:00 2001 From: mervyn <6359152+reply2future@users.noreply.github.com> Date: Mon, 17 Apr 2023 18:03:50 +0800 Subject: [PATCH 1/2] fix the incorrect condition to check stream method --- src/OpenAi.php | 4 ++-- tests/OpenAiTest.php | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/OpenAi.php b/src/OpenAi.php index c71b4d6..ffc7a99 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -86,8 +86,8 @@ public function complete($opts) */ public function completion($opts, $stream = null) { - if ($stream != null && array_key_exists('stream', $opts)) { - if (!$opts['stream']) { + if (array_key_exists('stream', $opts) && $opts['stream']) { + if ($stream == null) { throw new Exception( 'Please provide a stream function. Check https://github.com/orhanerday/open-ai#stream-example for an example.' ); diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index 2e8db54..ead54e6 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -17,6 +17,17 @@ $this->assertStringContainsString('text', $result); })->group('working'); +it('should throw error when set the stream to true and does not set the stream method', function () use ($open_ai) { + expect(fn () => $open_ai->completion([ + 'prompt' => "Hello", + 'temperature' => 0.9, + "max_tokens" => 150, + "frequency_penalty" => 0, + "presence_penalty" => 0.6, + "stream" => true + ]))->toThrow(Exception::class, 'Please provide a stream function. Check https://github.com/orhanerday/open-ai#stream-example for an example.'); +})->group('working'); + it('should handle simple completion using the deprecated endpoint', function () use ($open_ai) { $result = $open_ai->complete([ 'engine' => 'davinci', @@ -107,22 +118,19 @@ $result = $open_ai->cancelFineTune('file-XGinujblHPwGLSztz8cPS8XY'); $this->assertStringContainsString('training_files', $result); -})->group('requires-missing-file'); -; +})->group('requires-missing-file');; it('should handle list fine-tune event', function () use ($open_ai) { $result = $open_ai->listFineTuneEvents('file-XGinujblHPwGLSztz8cPS8XY'); $this->assertStringContainsString('data', $result); -})->group('requires-missing-file'); -; +})->group('requires-missing-file');; it('should handle delete fine-tune model', function () use ($open_ai) { $result = $open_ai->deleteFineTune('curie:ft-acmeco-2021-03-03-21-44-20'); $this->assertStringContainsString('deleted', $result); -})->group('requires-missing-file'); -; +})->group('requires-missing-file');; it('should handle answers', function () use ($open_ai) { $result = $open_ai->answer([ From 746dd8767fffda8f8c2cc2d7d8d34655f4ada303 Mon Sep 17 00:00:00 2001 From: mervyn <6359152+reply2future@users.noreply.github.com> Date: Mon, 17 Apr 2023 18:07:33 +0800 Subject: [PATCH 2/2] refact: reading key from env variable in unit test --- tests/OpenAiTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index ead54e6..7d15198 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -3,7 +3,7 @@ use Orhanerday\OpenAi\OpenAi; -$open_ai = new OpenAi('OPEN-AI-KEY'); +$open_ai = new OpenAi(getenv('OPENAI_API_KEY')); it('should handle simple completion using the new endpoint', function () use ($open_ai) { $result = $open_ai->completion([