Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cached tokens usage #7

Merged
merged 3 commits into from
Dec 21, 2024
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
98 changes: 51 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
- [Support Us](#support-us)
- [Get Started](#get-started)
- [Usage](#usage)
- [Completions Resource](#completions-resource)
- [Messages Resource](#messages-resource)
- [Completions Resource (Legacy)](#completions-resource-legacy)
- [Meta Information](#meta-information)
- [Troubleshooting](#troubleshooting)
- [Testing](#testing)
Expand Down Expand Up @@ -82,52 +82,6 @@ $client = Anthropic::factory()

## Usage

### `Completions` Resource

#### `create`

Creates a completion for the provided prompt and parameters.

```php
$response = $client->completions()->create([
'model' => 'claude-2.1',
'prompt' => '\n\nHuman: Hello, Claude\n\nAssistant:',
'max_tokens_to_sample' => 100,
'temperature' => 0
]);

$response->type; // 'completion'
$response->id; // 'compl_01EKm5HZ9y6khqaSZjsX44fS'
$response->completion; // ' Hello! Nice to meet you.'
$response->stop_reason; // 'stop_sequence'
$response->model; // 'claude-2.1'
$response->stop; // '\n\nHuman:'
$response->log_id; // 'compl_01EKm5HZ9y6khqaSZjsX44fS'

$response->toArray(); // ['id' => 'compl_01EKm5HZ9y6khqaSZjsX44fS', ...]
```

#### `create streamed`

Creates a streamed completion for the provided prompt and parameters.

```php
$stream = $client->completions()->createStreamed([
'model' => 'claude-2.1',
'prompt' => 'Hi',
'max_tokens_to_sample' => 70,
]);

foreach($stream as $response){
$response->completion;
}
// 1. iteration => 'I'
// 2. iteration => ' am'
// 3. iteration => ' very'
// 4. iteration => ' excited'
// ...
```

### `Messages` Resource

#### `create`
Expand Down Expand Up @@ -157,6 +111,8 @@ foreach ($response->content as $result) {

$response->usage->inputTokens; // 10,
$response->usage->outputTokens; // 19,
$response->usage->cacheCreationInputTokens; // 0,
$response->usage->cacheReadInputTokens; // 0,

$response->toArray(); // ['id' => 'msg_01BSy0WCV7QR2adFBauynAX7', ...]
```
Expand Down Expand Up @@ -211,6 +167,8 @@ $response->content[1]->input['unit']; // 'fahrenheit'

$response->usage->inputTokens; // 448,
$response->usage->outputTokens; // 87,
$response->usage->cacheCreationInputTokens; // 0,
$response->usage->cacheReadInputTokens; // 0,

$response->toArray(); // ['id' => 'msg_01BSy0WCV7QR2adFBauynAX7', ...]
```
Expand Down Expand Up @@ -409,6 +367,52 @@ foreach($stream as $response){
]
```

### `Completions` Resource (Legacy)

#### `create`

Creates a completion for the provided prompt and parameters.

```php
$response = $client->completions()->create([
'model' => 'claude-2.1',
'prompt' => '\n\nHuman: Hello, Claude\n\nAssistant:',
'max_tokens_to_sample' => 100,
'temperature' => 0
]);

$response->type; // 'completion'
$response->id; // 'compl_01EKm5HZ9y6khqaSZjsX44fS'
$response->completion; // ' Hello! Nice to meet you.'
$response->stop_reason; // 'stop_sequence'
$response->model; // 'claude-2.1'
$response->stop; // '\n\nHuman:'
$response->log_id; // 'compl_01EKm5HZ9y6khqaSZjsX44fS'

$response->toArray(); // ['id' => 'compl_01EKm5HZ9y6khqaSZjsX44fS', ...]
```

#### `create streamed`

Creates a streamed completion for the provided prompt and parameters.

```php
$stream = $client->completions()->createStreamed([
'model' => 'claude-2.1',
'prompt' => 'Hi',
'max_tokens_to_sample' => 70,
]);

foreach($stream as $response){
$response->completion;
}
// 1. iteration => 'I'
// 2. iteration => ' am'
// 3. iteration => ' very'
// 4. iteration => ' excited'
// ...
```

## Meta Information

On messages response object you can access the meta information returned by the API via the `meta()` method.
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function create(array $parameters): CreateResponse

$payload = Payload::create('messages', $parameters);

/** @var Response<array{id: string, type: string, role: string, model: string, stop_sequence: string|null, usage: array{input_tokens: int, output_tokens: int}, content: array<int, array{type: string, text?: string|null, id?: string, name?: string, input?: array<string, string>}>, stop_reason: string}> $response */
/** @var Response<array{id: string, type: string, role: string, model: string, stop_sequence: string|null, usage: array{input_tokens: int, output_tokens: int, cache_creation_input_tokens: int, cache_read_input_tokens: int}, content: array<int, array{type: string, text?: string|null, id?: string, name?: string, input?: array<string, string>}>, stop_reason: string}> $response */
$response = $this->transporter->requestObject($payload);

return CreateResponse::from($response->data(), $response->meta());
Expand Down
6 changes: 3 additions & 3 deletions src/Responses/Messages/CreateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
use Anthropic\Testing\Responses\Concerns\Messages\Fakeable;

/**
* @implements ResponseContract<array{id: string, type: string, role: string, model: string, stop_sequence: string|null, usage: array{input_tokens: int, output_tokens: int}, content: array<int, array{type: string, text?: string|null, id?: string|null, name?: string|null, input?: array<string, string>|null}>, stop_reason: string}>
* @implements ResponseContract<array{id: string, type: string, role: string, model: string, stop_sequence: string|null, usage: array{input_tokens: int, output_tokens: int, cache_creation_input_tokens: int, cache_read_input_tokens: int}, content: array<int, array{type: string, text?: string|null, id?: string|null, name?: string|null, input?: array<string, string>|null}>, stop_reason: string}>
*/
final class CreateResponse implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<array{id: string, type: string, role: string, model: string, stop_sequence: string|null, usage: array{input_tokens: int, output_tokens: int}, content: array<int, array{type: string, text?: string|null, id?: string|null, name?: string|null, input?: array<string, string>|null}>, stop_reason: string}>
* @use ArrayAccessible<array{id: string, type: string, role: string, model: string, stop_sequence: string|null, usage: array{input_tokens: int, output_tokens: int, cache_creation_input_tokens: int, cache_read_input_tokens: int}, content: array<int, array{type: string, text?: string|null, id?: string|null, name?: string|null, input?: array<string, string>|null}>, stop_reason: string}>
*/
use ArrayAccessible;

Expand All @@ -42,7 +42,7 @@ private function __construct(
/**
* Acts as static factory, and returns a new Response instance.
*
* @param array{id: string, type: string, role: string, model: string, stop_sequence: string|null, usage: array{input_tokens: int, output_tokens: int}, content: array<int, array{type: string, text?: string|null, id?: string|null, name?: string|null, input?: array<string, string>|null}>, stop_reason: string} $attributes
* @param array{id: string, type: string, role: string, model: string, stop_sequence: string|null, usage: array{input_tokens: int, output_tokens: int, cache_creation_input_tokens: int|null, cache_read_input_tokens: int|null}, content: array<int, array{type: string, text?: string|null, id?: string|null, name?: string|null, input?: array<string, string>|null}>, stop_reason: string} $attributes
*/
public static function from(array $attributes, MetaInformation $meta): self
{
Expand Down
10 changes: 8 additions & 2 deletions src/Responses/Messages/CreateResponseUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,33 @@ final class CreateResponseUsage
private function __construct(
public readonly int $inputTokens,
public readonly int $outputTokens,
public readonly int $cacheCreationInputTokens,
public readonly int $cacheReadInputTokens,
) {}

/**
* @param array{input_tokens: int, output_tokens: int} $attributes
* @param array{input_tokens: int, cache_creation_input_tokens: int|null, cache_read_input_tokens: int|null, output_tokens: int} $attributes
*/
public static function from(array $attributes): self
{
return new self(
$attributes['input_tokens'],
$attributes['output_tokens'],
$attributes['cache_creation_input_tokens'] ?? 0,
$attributes['cache_read_input_tokens'] ?? 0,
);
}

/**
* @return array{input_tokens: int, output_tokens: int}
* @return array{input_tokens: int, output_tokens: int, cache_creation_input_tokens: int, cache_read_input_tokens: int}
*/
public function toArray(): array
{
return [
'input_tokens' => $this->inputTokens,
'output_tokens' => $this->outputTokens,
'cache_creation_input_tokens' => $this->cacheCreationInputTokens,
'cache_read_input_tokens' => $this->cacheReadInputTokens,
];
}
}
26 changes: 26 additions & 0 deletions tests/Helpers/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@ function messagesCompletion(): array
'usage' => [
'input_tokens' => 10,
'output_tokens' => 20,
'cache_creation_input_tokens' => 0,
'cache_read_input_tokens' => 0,
],
'content' => [
[
'type' => 'text',
'text' => "Hello! I'm Claude, an AI assistant. How can I help you today?",
],
],
'stop_reason' => 'end_turn',
];
}

function messagesCompletionWithCache(): array
{
return [
'id' => 'msg_019hiOHAEXQwq1PTeETNEBWe',
'type' => 'message',
'role' => 'assistant',
'model' => 'claude-3-opus-20240229',
'stop_sequence' => null,
'usage' => [
'input_tokens' => 10,
'output_tokens' => 20,
'cache_creation_input_tokens' => 30,
'cache_read_input_tokens' => 40,
],
'content' => [
[
Expand Down
26 changes: 25 additions & 1 deletion tests/Responses/Messages/CreateResponseUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@

expect($result)
->inputTokens->toBe(10)
->outputTokens->toBe(20);
->outputTokens->toBe(20)
->cacheCreationInputTokens->toBe(0)
->cacheReadInputTokens->toBe(0);
});

test('from with cache', function () {
$result = CreateResponseUsage::from(messagesCompletionWithCache()['usage']);

expect($result)
->inputTokens->toBe(10)
->outputTokens->toBe(20)
->cacheCreationInputTokens->toBe(30)
->cacheReadInputTokens->toBe(40);
});

test('to array', function () {
Expand All @@ -16,3 +28,15 @@
expect($result->toArray())
->toBe(messagesCompletion()['usage']);
});

test('to array wit cache', function () {
$result = CreateResponseUsage::from(messagesCompletionWithCache()['usage']);

expect($result->toArray())
->toBe([
'input_tokens' => 10,
'output_tokens' => 20,
'cache_creation_input_tokens' => 30,
'cache_read_input_tokens' => 40,
]);
});
Loading