Skip to content
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
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use util\cmd\Console;
$ai= new OpenAIEndpoint('https://'.getenv('OPENAI_API_KEY').'@api.openai.com/v1');

Console::writeLine($ai->api('/responses')->invoke([
'model' => 'gpt-4o-mini',
'model' => 'gpt-5.2',
'input' => $prompt,
]));
```
Expand All @@ -37,7 +37,7 @@ use util\cmd\Console;
$ai= new OpenAIEndpoint('https://'.getenv('OPENAI_API_KEY').'@api.openai.com/v1');

$events= $ai->api('/responses')->stream([
'model' => 'gpt-4o-mini',
'model' => 'gpt-5.2',
'input' => $prompt,
]);
foreach ($events as $type => $value) {
Expand Down Expand Up @@ -195,7 +195,7 @@ $functions= (new Functions())->register('weather', new Weather());

$ai= new OpenAIEndpoint('https://'.getenv('OPENAI_API_KEY').'@api.openai.com/v1');
$payload= [
'model' => 'gpt-4o-mini',
'model' => 'gpt-5.2',
'tools' => new Tools($functions),
'input' => [['type' => 'message', 'role' => 'user', 'content' => $content]],
];
Expand Down Expand Up @@ -266,13 +266,17 @@ These endpoints differ slightly in how they are invoked, which is handled by the
use com\openai\rest\AzureAIEndpoint;
use util\cmd\Console;

// Using V1 API
$ai= new AzureAIEndpoint('https://'.getenv('AZUREAI_API_KEY').'@example.openai.azure.com/openai/v1');

// Using API version
$ai= new AzureAIEndpoint(
'https://'.getenv('AZUREAI_API_KEY').'@example.openai.azure.com/openai/deployments/mini',
'2025-03-01-preview'
'https://'.getenv('AZUREAI_API_KEY').'@example.openai.azure.com/openai/deployments/gpt-5.2',
'2025-04-01-preview'
);

Console::writeLine($ai->api('/responses')->invoke([
'model' => 'gpt-4o-mini',
'model' => 'gpt-5.2',
'input' => $prompt,
]));
```
Expand All @@ -286,14 +290,14 @@ use com\openai\rest\{AzureAIEndpoint, Distributed, ByRemainingRequests};
use util\cmd\Console;

$endpoints= [
new AzureAIEndpoint('https://...@r1.openai.azure.com/openai/deployments/mini', '2024-02-01'),
new AzureAIEndpoint('https://...@r2.openai.azure.com/openai/deployments/mini', '2024-02-01'),
new AzureAIEndpoint('https://...@r1.openai.azure.com/openai/v1'),
new AzureAIEndpoint('https://...@r2.openai.azure.com/openai/v1'),
];

$ai= new Distributed($endpoints, new ByRemainingRequests());

Console::writeLine($ai->api('/responses')->invoke([
'model' => 'gpt-4o-mini',
'model' => 'gpt-5.2',
'input' => $prompt,
]));
foreach ($endpoints as $i => $endpoint) {
Expand Down Expand Up @@ -362,7 +366,7 @@ use util\cmd\Console;
$ai= new OpenAIEndpoint('https://'.getenv('OPENAI_API_KEY').'@api.openai.com/v1');

$flow= $ai->api('/chat/completions')->flow([
'model' => 'gpt-4o-mini',
'model' => 'gpt-5.2',
'messages' => [['role' => 'user', 'content' => $prompt]],
]);
foreach ($flow->deltas() as $type => $delta) {
Expand Down
11 changes: 9 additions & 2 deletions src/main/php/com/openai/rest/AzureAIEndpoint.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,21 @@ public function __construct($arg, $version= null) {
}
}

/** @return string */
private function versioned() {
return null === $this->version ? '' : '?api-version='.$this->version;
}

/** Returns an API */
public function api(string $path, array $segments= []): Api {
return new Api(
$this->endpoint->resource(ltrim($path, '/').'?api-version='.$this->version, $segments),
$this->endpoint->resource(ltrim($path, '/').$this->versioned(), $segments),
$this->rateLimit
);
}

/** @return string */
public function toString() { return nameof($this).'(->'.$this->endpoint->base().'?api-version='.$this->version.')'; }
public function toString() {
return nameof($this).'(->'.$this->endpoint->base().$this->versioned().')';
}
}
12 changes: 12 additions & 0 deletions src/main/php/com/openai/rest/RestEndpoint.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ public function __construct($endpoint) {
/** Returns rate limit */
public function rateLimit(): RateLimit { return $this->rateLimit; }

/**
* Adds headers to be sent with every request
*
* @param string|[:string] $arg
* @param ?string $value
* @return self
*/
public function with($arg, $value= null) {
$this->endpoint->with($arg, $value);
return $this;
}

/** @return [:var] */
public function headers() { return $this->endpoint->headers(); }

Expand Down
33 changes: 31 additions & 2 deletions src/test/php/com/openai/unittest/AzureAIEndpointTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,52 @@ public function can_create() {
$this->fixture(self::URI);
}

#[Test]
public function no_version_by_default() {
Assert::null($this->fixture(self::URI)->version);
}

#[Test]
public function version() {
Assert::equals('2024-02-01', $this->fixture(self::URI, '2024-02-01')->version);
Assert::equals('2025-04-01-preview', $this->fixture(self::URI, '2025-04-01-preview')->version);
}

#[Test]
public function version_extracted_from_uri() {
Assert::equals('2024-02-01', $this->fixture(self::URI.'?api-version=2024-02-01')->version);
Assert::equals('2025-04-01-preview', $this->fixture(self::URI.'?api-version=2025-04-01-preview')->version);
}

#[Test]
public function api_key_header_set() {
Assert::equals('1e51...', $this->fixture(self::URI)->headers()['API-Key']);
}

#[Test]
public function additional_header() {
Assert::equals(
'preview',
$this->fixture(self::URI)->with('aoai-evals', 'preview')->headers()['aoai-evals']
);
}

#[Test]
public function additional_headers() {
Assert::equals(
'preview',
$this->fixture(self::URI)->with(['aoai-evals' => 'preview'])->headers()['aoai-evals']
);
}

#[Test]
public function string_representation() {
Assert::equals(
'com.openai.rest.AzureAIEndpoint(->https://test.openai.azure.com/openai/deployments/omni/)',
$this->fixture(self::URI)->toString()
);
}

#[Test]
public function string_representation_with_version() {
Assert::equals(
'com.openai.rest.AzureAIEndpoint(->https://test.openai.azure.com/openai/deployments/omni/?api-version=2024-02-01)',
$this->fixture(self::URI, '2024-02-01')->toString()
Expand Down
Loading