Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix stream check condition #85

Merged
merged 2 commits into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/OpenAi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
);
Expand Down
22 changes: 15 additions & 7 deletions tests/OpenAiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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',
Expand Down Expand Up @@ -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([
Expand Down