Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add setApiKey method #157

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
30 changes: 24 additions & 6 deletions src/OpenAi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
*/
Expand All @@ -953,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
Expand All @@ -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;
Expand Down
26 changes: 26 additions & 0 deletions tests/OpenAiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');