Skip to content

Commit c6bfa2c

Browse files
authored
fix: prism >=v0.77.1 incompatibilities (#27)
1 parent 4295c9c commit c6bfa2c

File tree

10 files changed

+141
-64
lines changed

10 files changed

+141
-64
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"php": "^8.2",
2323
"laravel/framework": "^11.0|^12.0",
2424
"aws/aws-sdk-php": "^3.339",
25-
"prism-php/prism": ">=0.63.0"
25+
"prism-php/prism": ">=0.77.1"
2626
},
2727
"config": {
2828
"allow-plugins": {

src/Bedrock.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@
44

55
use Aws\Credentials\Credentials;
66
use Aws\Signature\SignatureV4;
7-
use Generator;
87
use Illuminate\Http\Client\PendingRequest;
98
use Illuminate\Http\Client\Request;
10-
use Illuminate\Support\Facades\Http;
119
use Prism\Bedrock\Enums\BedrockSchema;
10+
use Prism\Prism\Concerns\InitializesClient;
1211
use Prism\Prism\Contracts\PrismRequest;
13-
use Prism\Prism\Contracts\Provider;
1412
use Prism\Prism\Embeddings\Request as EmbeddingRequest;
1513
use Prism\Prism\Embeddings\Response as EmbeddingsResponse;
1614
use Prism\Prism\Exceptions\PrismException;
15+
use Prism\Prism\Providers\Provider;
1716
use Prism\Prism\Structured\Request as StructuredRequest;
1817
use Prism\Prism\Structured\Response as StructuredResponse;
1918
use Prism\Prism\Text\Request as TextRequest;
2019
use Prism\Prism\Text\Response as TextResponse;
2120

22-
class Bedrock implements Provider
21+
class Bedrock extends Provider
2322
{
23+
use InitializesClient;
24+
2425
const KEY = 'bedrock';
2526

2627
public function __construct(
@@ -94,15 +95,6 @@ public function embeddings(EmbeddingRequest $request): EmbeddingsResponse
9495
return $handler->handle($request);
9596
}
9697

97-
#[\Override]
98-
/**
99-
* @return Generator<Chunk>
100-
*/
101-
public function stream(TextRequest $request): Generator
102-
{
103-
throw new PrismException('Prism Bedrock does not support streaming yet.');
104-
}
105-
10698
public function schema(PrismRequest $request): BedrockSchema
10799
{
108100
$override = $request->providerOptions();
@@ -129,7 +121,8 @@ protected function client(TextRequest|StructuredRequest|EmbeddingRequest $reques
129121
? false
130122
: $request->providerOptions('enableCaching') ?? false;
131123

132-
return Http::acceptJson()
124+
return $this->baseClient()
125+
->acceptJson()
133126
->withHeader('explicitPromptCaching', $enableCaching ? 'enabled' : 'disabled')
134127
->contentType('application/json')
135128
->withOptions($options)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Prism\Bedrock\Schemas\Anthropic\Maps;
6+
7+
use Prism\Prism\Providers\Anthropic\Maps\ImageMapper as AnthropicImageMapper;
8+
9+
class ImageMapper extends AnthropicImageMapper
10+
{
11+
protected function validateMedia(): bool
12+
{
13+
if ($this->media->isUrl()) {
14+
return false;
15+
}
16+
17+
return $this->media->hasRawContent();
18+
}
19+
}

src/Schemas/Anthropic/Maps/MessageMap.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use BackedEnum;
88
use Exception;
9-
use InvalidArgumentException;
109
use Prism\Prism\Contracts\Message;
1110
use Prism\Prism\Exceptions\PrismException;
1211
use Prism\Prism\ValueObjects\Messages\AssistantMessage;
@@ -169,20 +168,9 @@ protected static function mapAssistantMessage(AssistantMessage $message): array
169168
*/
170169
protected static function mapImageParts(array $parts, ?array $cache_control = null): array
171170
{
172-
return array_map(function (Image $image) use ($cache_control): array {
173-
if ($image->isUrl()) {
174-
throw new InvalidArgumentException('URL image type is not supported by Anthropic');
175-
}
176-
177-
return array_filter([
178-
'type' => 'image',
179-
'source' => [
180-
'type' => 'base64',
181-
'media_type' => $image->mimeType,
182-
'data' => $image->image,
183-
],
184-
'cache_control' => $cache_control,
185-
]);
186-
}, $parts);
171+
return array_map(
172+
fn (Image $image): array => (new ImageMapper($image, $cache_control))->toPayload(),
173+
$parts
174+
);
187175
}
188176
}

src/Schemas/Anthropic/Maps/ToolMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static function map(array $tools): array
2323
'description' => $tool->description(),
2424
'input_schema' => [
2525
'type' => 'object',
26-
'properties' => $tool->parameters(),
26+
'properties' => $tool->parametersAsArray(),
2727
'required' => $tool->requiredParameters(),
2828
],
2929
'cache_control' => $cacheType
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Prism\Bedrock\Schemas\Converse\Maps;
4+
5+
use Prism\Bedrock\Enums\Mimes;
6+
use Prism\Prism\Contracts\ProviderMediaMapper;
7+
use Prism\Prism\Enums\Provider;
8+
use Prism\Prism\ValueObjects\Messages\Support\Document;
9+
use Prism\Prism\ValueObjects\Messages\Support\Media;
10+
11+
class DocumentMapper extends ProviderMediaMapper
12+
{
13+
/**
14+
* @param Document $media
15+
* @param array<string, mixed> $cacheControl
16+
*/
17+
public function __construct(
18+
public readonly Media $media,
19+
public ?array $cacheControl = null
20+
) {}
21+
22+
/**
23+
* @return array<string,mixed>
24+
*/
25+
public function toPayload(): array
26+
{
27+
return [
28+
'document' => [
29+
'format' => $this->media->mimeType() ? Mimes::tryFrom($this->media->mimeType())?->toExtension() : null,
30+
'name' => $this->media->documentTitle(),
31+
'source' => ['bytes' => $this->media->base64()],
32+
],
33+
];
34+
}
35+
36+
protected function provider(): string|Provider
37+
{
38+
return 'bedrock';
39+
}
40+
41+
protected function validateMedia(): bool
42+
{
43+
if ($this->media->isUrl()) {
44+
return false;
45+
}
46+
47+
return $this->media->hasRawContent();
48+
}
49+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Prism\Bedrock\Schemas\Converse\Maps;
4+
5+
use Prism\Bedrock\Enums\Mimes;
6+
use Prism\Prism\Contracts\ProviderMediaMapper;
7+
use Prism\Prism\Enums\Provider;
8+
use Prism\Prism\ValueObjects\Messages\Support\Image;
9+
use Prism\Prism\ValueObjects\Messages\Support\Media;
10+
11+
class ImageMapper extends ProviderMediaMapper
12+
{
13+
/**
14+
* @param Image $media
15+
* @param array<string, mixed> $cacheControl
16+
*/
17+
public function __construct(
18+
public readonly Media $media,
19+
public ?array $cacheControl = null,
20+
) {}
21+
22+
/**
23+
* @return array<string,mixed>
24+
*/
25+
public function toPayload(): array
26+
{
27+
return [
28+
'image' => [
29+
'format' => $this->media->mimeType() ? Mimes::tryFrom($this->media->mimeType())?->toExtension() : null,
30+
'source' => ['bytes' => $this->media->base64()],
31+
],
32+
];
33+
}
34+
35+
protected function provider(): string|Provider
36+
{
37+
return 'bedrock';
38+
}
39+
40+
protected function validateMedia(): bool
41+
{
42+
if ($this->media->isUrl()) {
43+
return false;
44+
}
45+
46+
return $this->media->hasRawContent();
47+
}
48+
}

src/Schemas/Converse/Maps/MessageMap.php

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
namespace Prism\Bedrock\Schemas\Converse\Maps;
66

77
use Exception;
8-
use InvalidArgumentException;
9-
use Prism\Bedrock\Enums\Mimes;
108
use Prism\Prism\Contracts\Message;
119
use Prism\Prism\Exceptions\PrismException;
1210
use Prism\Prism\ValueObjects\Messages\AssistantMessage;
@@ -156,18 +154,10 @@ protected static function mapToolCalls(array $parts): array
156154
*/
157155
protected static function mapImageParts(array $parts): array
158156
{
159-
return array_map(function (Image $image): array {
160-
if ($image->isUrl()) {
161-
throw new InvalidArgumentException('URL image type is not supported by Bedrock.');
162-
}
163-
164-
return [
165-
'image' => [
166-
'format' => Mimes::tryFrom($image->mimeType)?->toExtension(), // @phpstan-ignore argument.type
167-
'source' => ['bytes' => $image->image],
168-
],
169-
];
170-
}, $parts);
157+
return array_map(
158+
fn (Image $image): array => (new ImageMapper($image))->toPayload(),
159+
$parts
160+
);
171161
}
172162

173163
/**
@@ -176,18 +166,9 @@ protected static function mapImageParts(array $parts): array
176166
*/
177167
protected static function mapDocumentParts(array $parts): array
178168
{
179-
return array_map(function (Document $document): array {
180-
if ($document->dataFormat === 'content') {
181-
throw new Exception('Content data format is not supported');
182-
}
183-
184-
return [
185-
'document' => [
186-
'format' => Mimes::tryFrom($document->mimeType)?->toExtension(), // @phpstan-ignore argument.type
187-
'name' => $document->documentTitle,
188-
'source' => ['bytes' => $document->dataFormat === 'base64' ? $document->document : base64_encode($document->document)], // @phpstan-ignore argument.type
189-
],
190-
];
191-
}, $parts);
169+
return array_map(
170+
fn (Document $document): array => (new DocumentMapper($document))->toPayload(),
171+
$parts
172+
);
192173
}
193174
}

src/Schemas/Converse/Maps/ToolMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static function map(array $tools): array
2121
'inputSchema' => [
2222
'json' => [
2323
'type' => 'object',
24-
'properties' => $tool->parameters(),
24+
'properties' => $tool->parametersAsArray(),
2525
'required' => $tool->requiredParameters(),
2626
],
2727
],

tests/Schemas/Anthropic/Maps/MessageMapTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Tests\Schemas\Anthropic\Maps;
66

7-
use InvalidArgumentException;
87
use Prism\Bedrock\Schemas\Anthropic\Maps\MessageMap;
8+
use Prism\Prism\Exceptions\PrismException;
99
use Prism\Prism\Providers\Anthropic\Enums\AnthropicCacheType;
1010
use Prism\Prism\ValueObjects\Messages\AssistantMessage;
1111
use Prism\Prism\ValueObjects\Messages\Support\Image;
@@ -61,13 +61,12 @@
6161
});
6262

6363
it('does not maps user messages with images from url', function (): void {
64-
$this->expectException(InvalidArgumentException::class);
6564
MessageMap::map([
6665
new UserMessage('Who are you?', [
6766
Image::fromUrl('https://storage.echolabs.dev/assets/logo.png'),
6867
]),
6968
]);
70-
});
69+
})->throws(PrismException::class);
7170

7271
it('maps assistant message', function (): void {
7372
expect(MessageMap::map([
@@ -191,12 +190,12 @@
191190
],
192191
[
193192
'type' => 'image',
193+
'cache_control' => ['type' => 'ephemeral'],
194194
'source' => [
195195
'type' => 'base64',
196196
'media_type' => 'image/png',
197197
'data' => base64_encode(file_get_contents('tests/Fixtures/test-image.png')),
198198
],
199-
'cache_control' => ['type' => 'ephemeral'],
200199
],
201200
],
202201
]]);

0 commit comments

Comments
 (0)