Skip to content

Commit 016eaa1

Browse files
authored
Merge pull request #22 from xp-forge/feature/realtime-parameters
Allow specifying parameters in the RealtimeApi constructor
2 parents 7b7794e + a3bb4f8 commit 016eaa1

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,10 @@ For Azure AI, the setup code is slightly different:
344344
use com\openai\realtime\RealtimeApi;
345345
use util\cmd\Console;
346346

347-
$api= new RealtimeApi('wss://example.openai.azure.com/openai/realtime?'.
348-
'?api-version=2024-10-01-preview'.
349-
'&deployment=gpt-4o-realtime-preview'
350-
);
347+
$api= new RealtimeApi('wss://example.openai.azure.com/openai/realtime', [
348+
'api-version' => '2024-10-01-preview',
349+
'deployment' => 'gpt-4o-realtime-preview',
350+
]);
351351
$session= $api->connect(['api-key' => getenv('AZUREAI_API_KEY')]);
352352
```
353353

src/main/php/com/openai/realtime/RealtimeApi.class.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,25 @@ class RealtimeApi implements Traceable, Value {
2222
private $ws, $marshalling;
2323
private $cat= null;
2424

25-
/** @param string|util.URI|websocket.WebSocket $endpoint */
26-
public function __construct($endpoint) {
27-
$this->ws= $endpoint instanceof WebSocket ? $endpoint : new WebSocket((string)$endpoint);
25+
/**
26+
* Creates a new realtime API instance
27+
*
28+
* @param string|util.URI|websocket.WebSocket $endpoint
29+
* @param [:string] $parameters
30+
*/
31+
public function __construct($endpoint, array $parameters= []) {
32+
if ($endpoint instanceof WebSocket) {
33+
$this->ws= $endpoint;
34+
} else if ($parameters) {
35+
$uri= $endpoint instanceof URI ? $endpoint->using() : (new URI($endpoint))->using();
36+
foreach ($parameters as $name => $value) {
37+
$uri->param($name, $value);
38+
}
39+
$this->ws= new WebSocket($uri->create());
40+
} else {
41+
$this->ws= new WebSocket($endpoint);
42+
}
43+
2844
$this->marshalling= (new Marshalling())->mapping(Tools::class, function($tools) {
2945
foreach ($tools->selection as $select) {
3046
if ($select instanceof Functions) {
@@ -43,6 +59,9 @@ public function __construct($endpoint) {
4359
});
4460
}
4561

62+
/** @return string */
63+
public function path() { return $this->ws->path(); }
64+
4665
/** @return peer.Socket */
4766
public function socket() { return $this->ws->socket(); }
4867

src/test/php/com/openai/unittest/RealtimeApiTest.class.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,38 @@ public function socket_accessor() {
3232
Assert::instance(Socket::class, (new RealtimeApi(self::URI))->socket());
3333
}
3434

35+
#[Test]
36+
public function path_accessor() {
37+
Assert::equals(
38+
'/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01',
39+
(new RealtimeApi(self::URI))->path()
40+
);
41+
}
42+
43+
#[Test]
44+
public function base_and_parameters() {
45+
Assert::equals(
46+
'/v1/realtime?model=gpt-realtime-2026',
47+
(new RealtimeApi('wss://api.openai.com/v1/realtime', ['model' => 'gpt-realtime-2026']))->path()
48+
);
49+
}
50+
51+
#[Test]
52+
public function addding_parameters() {
53+
Assert::equals(
54+
'/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01&api-version=2024-10-01-preview',
55+
(new RealtimeApi(self::URI, ['api-version' => '2024-10-01-preview']))->path()
56+
);
57+
}
58+
59+
#[Test]
60+
public function overwriting_parameters() {
61+
Assert::equals(
62+
'/v1/realtime?model=gpt-realtime-2026',
63+
(new RealtimeApi(self::URI, ['model' => 'gpt-realtime-2026']))->path()
64+
);
65+
}
66+
3567
#[Test]
3668
public function connect() {
3769
$c= new RealtimeApi(new TestingSocket([self::SESSION_CREATED]));

0 commit comments

Comments
 (0)