-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor implementation to unify tools usage for REST & realtime
- Loading branch information
Showing
7 changed files
with
131 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php namespace com\openai\rest; | ||
|
||
use com\openai\Tools; | ||
use com\openai\tools\Functions; | ||
|
||
/** Base class for OpenAI and AzureAI implementations */ | ||
abstract class RestEndpoint extends ApiEndpoint { | ||
protected $endpoint, $rateLimit; | ||
|
||
/** @param webservices.rest.Endpoint */ | ||
public function __construct($endpoint) { | ||
$this->endpoint= $endpoint; | ||
$this->endpoint->marshalling->mapping(Tools::class, function($tools) { | ||
foreach ($tools->selection as $select) { | ||
if ($select instanceof Functions) { | ||
foreach ($select->schema() as $name => $function) { | ||
yield ['type' => 'function', 'function' => [ | ||
'name' => $name, | ||
'description' => $function['description'], | ||
'parameters' => $function['input'], | ||
]]; | ||
} | ||
} else { | ||
yield $select; | ||
} | ||
} | ||
}); | ||
$this->rateLimit= new RateLimit(); | ||
} | ||
|
||
/** Returns rate limit */ | ||
public function rateLimit(): RateLimit { return $this->rateLimit; } | ||
|
||
/** @return [:var] */ | ||
public function headers() { return $this->endpoint->headers(); } | ||
|
||
/** | ||
* Provides a log category for tracing requests | ||
* | ||
* @param ?util.log.LogCategory $cat | ||
*/ | ||
public function setTrace($cat) { | ||
$this->endpoint->setTrace($cat); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,86 @@ | ||
<?php namespace com\openai\unittest; | ||
|
||
use com\openai\Tools; | ||
use com\openai\realtime\RealtimeApi; | ||
use com\openai\rest\OpenAIEndpoint; | ||
use com\openai\tools\Functions; | ||
use test\{Assert, Test}; | ||
use test\{Assert, Test, Values}; | ||
use webservices\rest\TestEndpoint; | ||
|
||
class ToolsTest { | ||
|
||
/** Returns a testing API endpoint */ | ||
private function testingEndpoint(): TestEndpoint { | ||
return new TestEndpoint([ | ||
'POST /echo' => function($call) { | ||
return $call->respond(200, 'OK', ['Content-Type' => 'application/json'], $call->content()); | ||
} | ||
]); | ||
} | ||
|
||
/** Returns functions with a "Hello World!" registration */ | ||
private function functions(): Functions { | ||
return (new Functions())->register('greet', new class() { | ||
public function world($name= 'World') { return "Hello {$name}!"; } | ||
}); | ||
} | ||
|
||
#[Test] | ||
public function can_create() { | ||
new Tools(); | ||
} | ||
|
||
#[Test] | ||
public function code_interpreter() { | ||
Assert::equals( | ||
[['type' => 'code_interpreter']], | ||
(new Tools('code_interpreter'))->selection | ||
); | ||
#[Test, Values([['code_interpreter'], [['type' => 'code_interpreter']]])] | ||
public function code_interpreter($tool) { | ||
Assert::equals([['type' => 'code_interpreter']], (new Tools($tool))->selection); | ||
} | ||
|
||
#[Test] | ||
public function with_custom_functions() { | ||
$functions= (new Functions())->register('greet', new class() { | ||
public function world() { return 'Hello World!'; } | ||
}); | ||
$functions= $this->functions(); | ||
Assert::equals([$functions], (new Tools($functions))->selection); | ||
} | ||
|
||
#[Test] | ||
public function serialized_for_rest_api() { | ||
$functions= $this->functions(); | ||
$endpoint= new OpenAIEndpoint($this->testingEndpoint()); | ||
$result= $endpoint->api('/echo')->invoke(['tools' => new Tools($functions)]); | ||
|
||
Assert::equals( | ||
[[ | ||
['tools' => [[ | ||
'type' => 'function', | ||
'function' => [ | ||
'name' => 'greet_world', | ||
'description' => 'World', | ||
'parameters' => $functions->schema()->current()['input'], | ||
], | ||
]], | ||
(new Tools($functions))->selection | ||
]]], | ||
$result | ||
); | ||
} | ||
|
||
#[Test] | ||
public function serialized_for_realtime_api() { | ||
$functions= $this->functions(); | ||
$api= new RealtimeApi(new TestingSocket([ | ||
'{"type": "session.created"}', | ||
'{"type": "session.update", "session": { | ||
"tools": [{ | ||
"type": "function", | ||
"name": "greet_world", | ||
"description": "World", | ||
"parameters": { | ||
"type": "object", | ||
"properties": { | ||
"name": {"type": "string", "description": "Name"} | ||
}, | ||
"required": [] | ||
} | ||
}] | ||
}}', | ||
])); | ||
$api->connect(); | ||
$api->send(['type' => 'session.update', 'session' => ['tools' => new Tools($functions)]]); | ||
} | ||
} |