Skip to content

Commit

Permalink
remove beta stuff. static analysis improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
nunodonato committed Jun 27, 2024
1 parent d7e0796 commit f11b561
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
parameters:
level: 5
level: 8
paths:
- src
17 changes: 9 additions & 8 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
}
Expand All @@ -38,6 +35,10 @@ public function setTimeout(int $seconds): self
return $this;
}

/**
* @param string[] $stopSequences
* @return array<string, mixed>
*/
public function messages(
string $model,
Messages $messages,
Expand All @@ -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,
Expand All @@ -82,6 +79,10 @@ public function messages(
return $response->json();
}

/**
* @param string[] $stopSequences
* @return array<string, mixed>
*/
public function completion(
string $model,
string $prompt,
Expand Down
7 changes: 7 additions & 0 deletions src/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ class Messages
const ROLE_USER = 'user';
const ROLE_ASSISTANT = 'assistant';

/** @var array<int, array<mixed, mixed>> */
private array $messages = [];


/**
* @return array<int, array<mixed, mixed>>
*/
public function messages(): array
{
return $this->messages;
Expand Down Expand Up @@ -57,6 +61,9 @@ public function addAssistantTextMessage(string $text): self
return $this->addMessage(self::ROLE_ASSISTANT, $text);
}

/**
* @param string|array<int, array<string, string>> $content
*/
public function addMessage(string $role, string|array $content): self
{
if (!in_array($role, [self::ROLE_USER, self::ROLE_ASSISTANT])) {
Expand Down
8 changes: 8 additions & 0 deletions src/Tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@

class Tools
{
/** @var array<int, array<string, mixed>> */
private array $tools = [];


/** @return array<int, array<string, mixed>> */
public function tools(): array
{
return $this->tools;
}

/**
* @param array<string, mixed> $tool
*/
public function addToolFromArray(array $tool): self
{
$required = ['name', 'description', 'input_schema'];
Expand Down Expand Up @@ -39,6 +44,9 @@ public function addToolFromArray(array $tool): self
return $this;
}

/**
* @param array<int, array<string, mixed>> $tools
*/
public function addToolsFromArray(array $tools): self
{
foreach ($tools as $i => $tool) {
Expand Down
4 changes: 2 additions & 2 deletions tests/AnthropicAPIClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -104,4 +104,4 @@ public function test_setTimeoutSetsCorrectValue()

$this->assertEquals($timeoutSeconds, $options['timeout'], 'The set timeout does not match the expected value');
}
}
}

0 comments on commit f11b561

Please sign in to comment.