From f11b56129ebebedfa5377e67fcb296653b0f9527 Mon Sep 17 00:00:00 2001 From: Nuno Donato Date: Thu, 27 Jun 2024 18:27:06 +0100 Subject: [PATCH] remove beta stuff. static analysis improvements --- README.md | 2 -- phpstan.neon | 2 +- src/Client.php | 17 +++++++++-------- src/Messages.php | 7 +++++++ src/Tools.php | 8 ++++++++ tests/AnthropicAPIClientTest.php | 4 ++-- 6 files changed, 27 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index bb51b6b..259aeb5 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,6 @@ use NunoDonato\AnthropicAPIPHP\Client; // ... $client = new Client($yourApiKey); -// or, for tool usage -$client = new Client($yourApiKey, useBeta: true); ``` #### Messages API usage diff --git a/phpstan.neon b/phpstan.neon index 2a83bf6..0b6508e 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,4 +1,4 @@ parameters: - level: 5 + level: 8 paths: - src \ No newline at end of file diff --git a/src/Client.php b/src/Client.php index 8184e86..7d88376 100644 --- a/src/Client.php +++ b/src/Client.php @@ -16,7 +16,7 @@ class Client private PendingRequest $pendingRequest; - public function __construct(string $apiKey, string $version = '2023-06-01', private readonly bool $useBeta = false) + public function __construct(string $apiKey, string $version = '2023-06-01') { $this->pendingRequest = new PendingRequest(); @@ -25,9 +25,6 @@ public function __construct(string $apiKey, string $version = '2023-06-01', priv 'anthropic-version' => $version, 'content-type' => 'application/json' ]; - if ($this->useBeta) { - $headers['anthropic-beta'] = 'tools-2024-04-04'; - } $this->pendingRequest->withHeaders($headers); } @@ -38,6 +35,10 @@ public function setTimeout(int $seconds): self return $this; } + /** + * @param string[] $stopSequences + * @return array + */ public function messages( string $model, Messages $messages, @@ -57,10 +58,6 @@ public function messages( 'max_tokens' => $maxTokens, ]; - if ($tools && count($tools->tools()) > 0 && !$this->useBeta) { - throw new \InvalidArgumentException('Tools are only available in the beta version'); - } - $optionalParams = [ 'tools' => ($tools && count($tools->tools())) > 0 ? $tools->tools() : null, 'stop_sequences' => count($stopSequences) > 0 ? $stopSequences : null, @@ -82,6 +79,10 @@ public function messages( return $response->json(); } + /** + * @param string[] $stopSequences + * @return array + */ public function completion( string $model, string $prompt, diff --git a/src/Messages.php b/src/Messages.php index 2abae12..1ed5eba 100644 --- a/src/Messages.php +++ b/src/Messages.php @@ -7,9 +7,13 @@ class Messages const ROLE_USER = 'user'; const ROLE_ASSISTANT = 'assistant'; + /** @var array> */ private array $messages = []; + /** + * @return array> + */ public function messages(): array { return $this->messages; @@ -57,6 +61,9 @@ public function addAssistantTextMessage(string $text): self return $this->addMessage(self::ROLE_ASSISTANT, $text); } + /** + * @param string|array> $content + */ public function addMessage(string $role, string|array $content): self { if (!in_array($role, [self::ROLE_USER, self::ROLE_ASSISTANT])) { diff --git a/src/Tools.php b/src/Tools.php index 4114ea3..c26b8c2 100644 --- a/src/Tools.php +++ b/src/Tools.php @@ -4,14 +4,19 @@ class Tools { + /** @var array> */ private array $tools = []; + /** @return array> */ public function tools(): array { return $this->tools; } + /** + * @param array $tool + */ public function addToolFromArray(array $tool): self { $required = ['name', 'description', 'input_schema']; @@ -39,6 +44,9 @@ public function addToolFromArray(array $tool): self return $this; } + /** + * @param array> $tools + */ public function addToolsFromArray(array $tools): self { foreach ($tools as $i => $tool) { diff --git a/tests/AnthropicAPIClientTest.php b/tests/AnthropicAPIClientTest.php index 606c079..7f9aab2 100644 --- a/tests/AnthropicAPIClientTest.php +++ b/tests/AnthropicAPIClientTest.php @@ -47,7 +47,7 @@ public function test_messages() public function test_tools() { - $client = new Client($this->apiKey, useBeta: true); + $client = new Client($this->apiKey); $weatherTool = [ 'name' => 'weather', 'description' => 'Get the weather in a given location', @@ -104,4 +104,4 @@ public function test_setTimeoutSetsCorrectValue() $this->assertEquals($timeoutSeconds, $options['timeout'], 'The set timeout does not match the expected value'); } -} +} \ No newline at end of file