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

Anthropic prompt caching #104

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions config/prism.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
'anthropic' => [
'api_key' => env('ANTHROPIC_API_KEY', ''),
'version' => env('ANTHROPIC_API_VERSION', '2023-06-01'),
// Seperate each feature with a comma with no spaces.
'beta_features' => ENV('ANTHROPIC_BETA_FEATURES', null),
],
'ollama' => [
'url' => env('OLLAMA_URL', 'http://localhost:11434/v1'),
Expand Down
29 changes: 29 additions & 0 deletions src/Concerns/HasProviderMeta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace EchoLabs\Prism\Concerns;

use EchoLabs\Prism\Enums\Provider;

trait HasProviderMeta
{
/** @var array<string, array<string, mixed>> */
protected $providerMeta = [];

/**
* @param array<string, mixed> $meta
*/
public function withProviderMeta(Provider $provider, array $meta): self
{
$this->providerMeta[$provider->value] = $meta;

return $this;
}

/**
* @return array<string, mixed>> $meta
*/
public function providerMeta(Provider $provider): array
{
return data_get($this->providerMeta, $provider->value, []);
}
}
1 change: 1 addition & 0 deletions src/PrismManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ protected function createAnthropicProvider(array $config): Anthropic
return new Anthropic(
$config['api_key'],
$config['version'],
$config['beta_features']
);
}

Expand Down
6 changes: 4 additions & 2 deletions src/Providers/Anthropic/Anthropic.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Anthropic implements Provider
public function __construct(
public readonly string $apiKey,
public readonly string $apiVersion,
public readonly ?string $betaFeatures = null
) {}

#[\Override]
Expand Down Expand Up @@ -56,10 +57,11 @@ public function embeddings(EmbeddingRequest $request): EmbeddingResponse
*/
protected function client(array $options = [], array $retry = []): PendingRequest
{
return Http::withHeaders([
return Http::withHeaders(array_filter([
'x-api-key' => $this->apiKey,
'anthropic-version' => $this->apiVersion,
])
'anthropic-beta' => $this->betaFeatures,
]))
->withOptions($options)
->retry(...$retry)
->baseUrl('https://api.anthropic.com/v1');
Expand Down
8 changes: 8 additions & 0 deletions src/Providers/Anthropic/Enums/AnthropicCacheType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace EchoLabs\Prism\Providers\Anthropic\Enums;

enum AnthropicCacheType
{
case ethemeral;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see value of keeping the enum here but Imo keeping provider related settings in the prisms code can make maintenance way harder.

@sixlive can think different.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hilariously, having added the enum to avoid spelling mistakes, I misspelt it in the enum...! Now fixed.

Definitely not a huge loss if we don't include the enum. Really just a convenience thing.

}
49 changes: 29 additions & 20 deletions src/Providers/Anthropic/Maps/MessageMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace EchoLabs\Prism\Providers\Anthropic\Maps;

use EchoLabs\Prism\Contracts\Message;
use EchoLabs\Prism\Enums\Provider;
use EchoLabs\Prism\ValueObjects\Messages\AssistantMessage;
use EchoLabs\Prism\ValueObjects\Messages\Support\Image;
use EchoLabs\Prism\ValueObjects\Messages\SystemMessage;
Expand All @@ -14,6 +15,7 @@
use EchoLabs\Prism\ValueObjects\ToolResult;
use Exception;
use InvalidArgumentException;
use UnitEnum;

class MessageMap
{
Expand Down Expand Up @@ -45,10 +47,13 @@ protected static function mapMessage(Message $message): array
*/
protected static function mapSystemMessage(SystemMessage $systemMessage): array
{
return [
$cacheType = data_get($systemMessage->providerMeta(Provider::Anthropic), 'cacheType', null);

return array_filter([
'role' => 'user',
'content' => $systemMessage->content,
];
'cache_control' => $cacheType ? ['type' => $cacheType instanceof UnitEnum ? $cacheType->name : $cacheType] : null,
]);
}

/**
Expand Down Expand Up @@ -86,10 +91,16 @@ protected static function mapUserMessage(UserMessage $message): array
];
}, $message->images());

$cacheType = data_get($message->providerMeta(Provider::Anthropic), 'cacheType', null);

return [
'role' => 'user',
'content' => [
['type' => 'text', 'text' => $message->text()],
array_filter([
'type' => 'text',
'text' => $message->text(),
'cache_control' => $cacheType ? ['type' => $cacheType instanceof UnitEnum ? $cacheType->name : $cacheType] : null,
ChrisB-TL marked this conversation as resolved.
Show resolved Hide resolved
]),
...$imageParts,
],

Expand All @@ -101,32 +112,30 @@ protected static function mapUserMessage(UserMessage $message): array
*/
protected static function mapAssistantMessage(AssistantMessage $message): array
{
if ($message->toolCalls) {
$content = [];
$content = [];

if ($message->content !== '' && $message->content !== '0') {
$content[] = [
'type' => 'text',
'text' => $message->content,
];
}
if ($message->content !== '' && $message->content !== '0') {
$cacheType = data_get($message->providerMeta(Provider::Anthropic), 'cacheType', null);

$toolCalls = array_map(fn (ToolCall $toolCall): array => [
$content[] = array_filter([
'type' => 'text',
'text' => $message->content,
'cache_control' => $cacheType ? ['type' => $cacheType instanceof UnitEnum ? $cacheType->name : $cacheType] : null,
]);
}

$toolCalls = $message->toolCalls
? array_map(fn (ToolCall $toolCall): array => [
'type' => 'tool_use',
'id' => $toolCall->id,
'name' => $toolCall->name,
'input' => $toolCall->arguments(),
], $message->toolCalls);

return [
'role' => 'assistant',
'content' => array_merge($content, $toolCalls),
];
}
], $message->toolCalls)
: [];

return [
'role' => 'assistant',
'content' => $message->content,
'content' => array_merge($content, $toolCalls),
];
}
}
25 changes: 16 additions & 9 deletions src/Providers/Anthropic/Maps/ToolMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace EchoLabs\Prism\Providers\Anthropic\Maps;

use EchoLabs\Prism\Enums\Provider;
use EchoLabs\Prism\Tool as PrismTool;
use UnitEnum;

class ToolMap
{
Expand All @@ -14,14 +16,19 @@ class ToolMap
*/
public static function map(array $tools): array
{
return array_map(fn (PrismTool $tool): array => [
'name' => $tool->name(),
'description' => $tool->description(),
'input_schema' => [
'type' => 'object',
'properties' => $tool->parameters(),
'required' => $tool->requiredParameters(),
],
], $tools);
return array_map(function (PrismTool $tool): array {
$cacheType = data_get($tool->providerMeta(Provider::Anthropic), 'cacheType', null);

return array_filter([
'name' => $tool->name(),
'description' => $tool->description(),
'input_schema' => [
'type' => 'object',
'properties' => $tool->parameters(),
'required' => $tool->requiredParameters(),
],
'cache_control' => $cacheType ? ['type' => $cacheType instanceof UnitEnum ? $cacheType->name : $cacheType] : null,
]);
}, $tools);
}
}
25 changes: 3 additions & 22 deletions src/Tool.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use ArgumentCountError;
use Closure;
use EchoLabs\Prism\Concerns\HasProviderMeta;
use EchoLabs\Prism\Contracts\Schema;
use EchoLabs\Prism\Enums\Provider;
use EchoLabs\Prism\Exceptions\PrismException;
use EchoLabs\Prism\Schema\ArraySchema;
use EchoLabs\Prism\Schema\BooleanSchema;
Expand All @@ -21,6 +21,8 @@

class Tool
{
use HasProviderMeta;

protected string $name = '';

protected string $description;
Expand All @@ -34,9 +36,6 @@ class Tool
/** @var Closure():string|callable():string */
protected $fn;

/** @var array<string, array<string, mixed>> */
protected $providerMeta = [];

public function as(string $name): self
{
$this->name = $name;
Expand Down Expand Up @@ -139,24 +138,6 @@ public function withEnumParameter(
return $this;
}

/**
* @param array<string, mixed> $meta
*/
public function withProviderMeta(Provider $provider, array $meta): self
{
$this->providerMeta[$provider->value] = $meta;

return $this;
}

/**
* @return array<string, mixed>> $meta
*/
public function providerMeta(Provider $provider): array
{
return data_get($this->providerMeta, $provider->value, []);
}

/** @return array<int, string> */
public function requiredParameters(): array
{
Expand Down
3 changes: 3 additions & 0 deletions src/ValueObjects/Messages/AssistantMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

namespace EchoLabs\Prism\ValueObjects\Messages;

use EchoLabs\Prism\Concerns\HasProviderMeta;
use EchoLabs\Prism\Contracts\Message;
use EchoLabs\Prism\ValueObjects\ToolCall;

class AssistantMessage implements Message
{
use HasProviderMeta;

/**
* @param ToolCall[] $toolCalls
*/
Expand Down
5 changes: 4 additions & 1 deletion src/ValueObjects/Messages/SystemMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

namespace EchoLabs\Prism\ValueObjects\Messages;

use EchoLabs\Prism\Concerns\HasProviderMeta;
use EchoLabs\Prism\Contracts\Message;

class SystemMessage implements Message
{
use HasProviderMeta;

public function __construct(
public readonly string $content,
public readonly string $content
) {}
}
3 changes: 3 additions & 0 deletions src/ValueObjects/Messages/UserMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

namespace EchoLabs\Prism\ValueObjects\Messages;

use EchoLabs\Prism\Concerns\HasProviderMeta;
use EchoLabs\Prism\Contracts\Message;
use EchoLabs\Prism\ValueObjects\Messages\Support\Image;
use EchoLabs\Prism\ValueObjects\Messages\Support\Text;

class UserMessage implements Message
{
use HasProviderMeta;

/**
* @param array<int, Text|Image> $additionalContent
*/
Expand Down
34 changes: 34 additions & 0 deletions tests/Providers/Anthropic/AnthropicBetaHeaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use EchoLabs\Prism\Prism;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Tests\Fixtures\FixtureResponse;

beforeEach(function (): void {
config()->set('prism.providers.anthropic.api_key', env('ANTHROPIC_API_KEY', 'sk-1234'));
});

it('does not set the anthropic-beta header if no beta feature is enabled in config', function (): void {
FixtureResponse::fakeResponseSequence('v1/messages', 'anthropic/generate-text-with-a-prompt');

Prism::text()
->using('anthropic', 'claude-3-5-sonnet-20240620')
->withPrompt('Who are you?')
->generate();

Http::assertSent(fn (Request $request) => $request->hasHeader('anthropic-beta') === false);
});

it('sends the anthropic-beta header if a beta feature is enabled in config', function (): void {
config()->set('prism.providers.anthropic.beta_features', 'prompt-caching-2024-07-31');

FixtureResponse::fakeResponseSequence('v1/messages', 'anthropic/generate-text-with-a-prompt');

Prism::text()
->using('anthropic', 'claude-3-5-sonnet-20240620')
->withPrompt('Who are you?')
->generate();

Http::assertSent(fn (Request $request) => $request->hasHeader('anthropic-beta', 'prompt-caching-2024-07-31'));
});
Loading