Skip to content

Commit

Permalink
Add support for optional organization and project identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Oct 19, 2024
1 parent a014b62 commit a7eca24
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ OpenAI APIs for XP ChangeLog

## ?.?.? / ????-??-??

* Added support for optional organization and project identifiers, see
https://platform.openai.com/docs/api-reference/authentication
(@thekid)
* Merged PR #4: Implement Azure AI endpoints, which differ in the way
they pass the API key and that they need an API version.
(@thekid)
Expand Down
11 changes: 10 additions & 1 deletion src/main/php/com/openai/rest/OpenAIEndpoint.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/**
* OpenAI REST API endpoint
*
* @see https://platform.openai.com/docs/api-reference/authentication
* @test com.openai.unittest.OpenAIEndpointTest
*/
class OpenAIEndpoint extends ApiEndpoint {
Expand All @@ -13,9 +14,17 @@ class OpenAIEndpoint extends ApiEndpoint {
* Creates a new OpenAI endpoint
*
* @param string|util.URI|webservices.rest.Endpoint
* @param ?string $organization
* @param ?string $project
*/
public function __construct($arg) {
public function __construct($arg, $organization= null, $project= null) {
parent::__construct($arg instanceof Endpoint ? $arg : new Endpoint($arg));

// Pass optional organization and project IDs
$headers= [];
$organization && $headers['OpenAI-Organization']= $organization;
$project && $headers['OpenAI-Project']= $project;
$headers && $this->endpoint->with($headers);
}

/** Returns an API */
Expand Down
21 changes: 20 additions & 1 deletion src/test/php/com/openai/unittest/OpenAIEndpointTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ public function can_create() {

#[Test]
public function authorization_header_set() {
Assert::equals('Bearer sk-test', $this->fixture(self::URI)->headers()['Authorization']);
Assert::equals(
'Bearer sk-test',
$this->fixture(self::URI)->headers()['Authorization']
);
}

#[Test]
public function optional_organization_header() {
Assert::equals(
'org-test',
$this->fixture(self::URI, 'org-test')->headers()['OpenAI-Organization']
);
}

#[Test]
public function optional_project_header() {
Assert::equals(
'prj-test',
$this->fixture(self::URI, 'org-test', 'prj-test')->headers()['OpenAI-Project']
);
}
}

0 comments on commit a7eca24

Please sign in to comment.